Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclued node flag for load image command #3688

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/cluster/nodeutils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,14 @@ func ReTagImage(n nodes.Node, imageID, imageName string) error {
var out bytes.Buffer
return n.Command("ctr", "--namespace=k8s.io", "images", "tag", "--force", imageID, imageName).SetStdout(&out).Run()
}

// RemoveNode remove a node from a node list.
func RemoveNode(nodeName string, nodes []nodes.Node) []nodes.Node {
for i := 0; i < len(nodes); i++ {
if nodes[i].String() == nodeName {
nodes = append(nodes[:i], nodes[i+1:]...)
i--
}
}
return nodes
}
21 changes: 19 additions & 2 deletions pkg/cmd/kind/load/docker-image/docker-image.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ type (
)

type flagpole struct {
Name string
Nodes []string
Name string
Nodes []string
ExcludeNodes []string
}

// NewCommand returns a new cobra.Command for loading an image into a cluster
Expand Down Expand Up @@ -78,6 +79,12 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
nil,
"comma separated list of nodes to load images into",
)
cmd.Flags().StringSliceVar(
&flags.ExcludeNodes,
"exclude-nodes",
nil,
"comma separated list of exclude nodes not to load images into",
)
return cmd
}

Expand Down Expand Up @@ -129,6 +136,16 @@ func runE(logger log.Logger, flags *flagpole, args []string) error {
}
}

if len(flags.ExcludeNodes) > 0 {
for _, name := range flags.ExcludeNodes {
_, ok := nodesByName[name]
if !ok {
return fmt.Errorf("unknown node: %q", name)
}
candidateNodes = nodeutils.RemoveNode(name, candidateNodes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the end result candidateNodes is processed with too many loops.
instead try using maps or a smarter way to process the excluded nodes.

for example, only add a node to candiateNodes if it's not in the excluded nodes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reply! Will update later.

}
}

// pick only the nodes that don't have the image
selectedNodes := map[string]nodes.Node{}
fns := []func() error{}
Expand Down
25 changes: 21 additions & 4 deletions pkg/cmd/kind/load/image-archive/image-archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import (
)

type flagpole struct {
Name string
Nodes []string
Name string
Nodes []string
ExcludeNodes []string
}

// NewCommand returns a new cobra.Command for loading an image into a cluster
Expand Down Expand Up @@ -75,6 +76,12 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
nil,
"comma separated list of nodes to load images into",
)
cmd.Flags().StringSliceVar(
&flags.ExcludeNodes,
"exclude-nodes",
nil,
"comma separated list of exclude nodes not to load images into",
)
return cmd
}

Expand All @@ -91,14 +98,14 @@ func runE(logger log.Logger, flags *flagpole, args []string) error {
}

for _, imageTarPath := range args {
if err := loadArchiveToNodes(logger, provider, flags.Name, flags.Nodes, imageTarPath); err != nil {
if err := loadArchiveToNodes(logger, provider, flags.Name, flags.Nodes, flags.ExcludeNodes, imageTarPath); err != nil {
return err
}
}
return nil
}

func loadArchiveToNodes(logger log.Logger, provider *cluster.Provider, clusterName string, nodeNames []string, imageArchivePath string) error {
func loadArchiveToNodes(logger log.Logger, provider *cluster.Provider, clusterName string, nodeNames []string, excludeNodeNames []string, imageArchivePath string) error {
// Check if the cluster nodes exist
nodeList, err := provider.ListInternalNodes(clusterName)
if err != nil {
Expand Down Expand Up @@ -130,6 +137,16 @@ func loadArchiveToNodes(logger log.Logger, provider *cluster.Provider, clusterNa
}
}

if len(excludeNodeNames) > 0 {
for _, name := range excludeNodeNames {
_, ok := nodesByName[name]
if !ok {
return fmt.Errorf("unknown node: %q", name)
}
selectedNodes = nodeutils.RemoveNode(name, selectedNodes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same problem here.

}
}

// Load the image on the selected nodes
fns := []func() error{}
for _, selectedNode := range selectedNodes {
Expand Down