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 --node-label flag for node create command (#584, @developer-guy, @ejose, @dentrax) #584

Merged
merged 3 commits into from
May 10, 2021

Conversation

developer-guy
Copy link
Contributor

@developer-guy developer-guy commented Apr 30, 2021

Signed-off-by: Batuhan Apaydın [email protected]

This PR should fix #578.

What

This PR is going to provide users to be able to add labels to the Kubernetes nodes.

Why

The k3s tool has a flag called "--node-label", we can use this flag in order to add labels to nodes, k3d has no support for that, so, we can add it.

Implications

… command as a --node-label flag

Signed-off-by: Batuhan Apaydın <[email protected]>
Copy link
Member

@iwilltry42 iwilltry42 left a comment

Choose a reason for hiding this comment

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

Just a minor comment regarding the naming of the new field... apart from that LGTM! :)

@iwilltry42 iwilltry42 added this to the v4.5.0 milestone May 6, 2021
@iwilltry42
Copy link
Member

Thank you for your efforts here! :)

@developer-guy
Copy link
Contributor Author

developer-guy commented May 7, 2021

@ejose19 @iwilltry42, we updated the variable and flag name, FYI.
Screen Shot 2021-05-07 at 22 33 19

@ejose19
Copy link
Contributor

ejose19 commented May 7, 2021

diff --git a/cmd/node/nodeCreate.go b/cmd/node/nodeCreate.go
index b334b479..f817d211 100644
--- a/cmd/node/nodeCreate.go
+++ b/cmd/node/nodeCreate.go
@@ -74,7 +74,7 @@ func NewCmdNodeCreate() *cobra.Command {
 	cmd.Flags().BoolVar(&createNodeOpts.Wait, "wait", false, "Wait for the node(s) to be ready before returning.")
 	cmd.Flags().DurationVar(&createNodeOpts.Timeout, "timeout", 0*time.Second, "Maximum waiting time for '--wait' before canceling/returning.")
 
-    cmd.Flags().StringSliceP("k3s-node-label", "", []string{}, "Specify k3s node labels in format \"foo=bar\", \"foo=bar:NoExecute\"")
+	cmd.Flags().StringSliceP("k3s-node-label", "", []string{}, "Specify k3s node labels in format \"foo=bar\", \"foo=bar:NoExecute\"")
 
 	// done
 	return cmd
@@ -127,17 +127,17 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cl
 		log.Errorf("Provided memory limit value is invalid")
 	}
 
-	labels, err := cmd.Flags().GetStringSlice("node-label")
+	k3sNodeLabelsFlags, err := cmd.Flags().GetStringSlice("k3s-node-label")
 	if err != nil {
-		log.Errorln("No node-label specified")
+		log.Errorln("No k3s-node-label specified")
 		log.Fatalln(err)
 	}
 
-	k3sNodeLabels := make(map[string]string, len(labels))
-	for _, label := range labels {
+	k3sNodeLabels := make(map[string]string, len(k3sNodeLabelsFlags))
+	for _, label := range k3sNodeLabelsFlags {
 		labelSplitted := strings.Split(label, "=")
 		if len(labelSplitted) != 2 {
-			log.Fatalf("unknown label format format: %s, use format \"foo=bar\"", label)
+			log.Fatalf("unknown k3s-node-label format format: %s, use format \"foo=bar\"", label)
 		}
 		k3sNodeLabels[labelSplitted[0]] = labelSplitted[1]
 	}
@@ -153,8 +153,8 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cl
 				k3d.LabelRole: roleStr,
 			},
 			K3sNodeLabels: k3sNodeLabels,
-			Restart:     true,
-			Memory:      memory,
+			Restart:       true,
+			Memory:        memory,
 		}
 		nodes = append(nodes, node)
 	}
diff --git a/pkg/types/types.go b/pkg/types/types.go
index 642038b3..e916a804 100644
--- a/pkg/types/types.go
+++ b/pkg/types/types.go
@@ -330,26 +330,26 @@ type NodeIP struct {
 
 // Node describes a k3d node
 type Node struct {
-	Name        string            `yaml:"name" json:"name,omitempty"`
-	Role        Role              `yaml:"role" json:"role,omitempty"`
-	Image       string            `yaml:"image" json:"image,omitempty"`
-	Volumes     []string          `yaml:"volumes" json:"volumes,omitempty"`
-	Env         []string          `yaml:"env" json:"env,omitempty"`
-	Cmd         []string          // filled automatically based on role
-	Args        []string          `yaml:"extraArgs" json:"extraArgs,omitempty"`
-	Ports       nat.PortMap       `yaml:"portMappings" json:"portMappings,omitempty"`
-	Restart     bool              `yaml:"restart" json:"restart,omitempty"`
-	Created     string            `yaml:"created" json:"created,omitempty"`
-	Labels      map[string]string // filled automatically
+	Name          string            `yaml:"name" json:"name,omitempty"`
+	Role          Role              `yaml:"role" json:"role,omitempty"`
+	Image         string            `yaml:"image" json:"image,omitempty"`
+	Volumes       []string          `yaml:"volumes" json:"volumes,omitempty"`
+	Env           []string          `yaml:"env" json:"env,omitempty"`
+	Cmd           []string          // filled automatically based on role
+	Args          []string          `yaml:"extraArgs" json:"extraArgs,omitempty"`
+	Ports         nat.PortMap       `yaml:"portMappings" json:"portMappings,omitempty"`
+	Restart       bool              `yaml:"restart" json:"restart,omitempty"`
+	Created       string            `yaml:"created" json:"created,omitempty"`
+	Labels        map[string]string // filled automatically
 	K3sNodeLabels map[string]string `yaml:"k3sNodeLabels" json:"k3sNodeLabels,omitempty"`
-	Networks    []string          // filled automatically
-	ExtraHosts  []string          // filled automatically
-	ServerOpts  ServerOpts        `yaml:"serverOpts" json:"serverOpts,omitempty"`
-	AgentOpts   AgentOpts         `yaml:"agentOpts" json:"agentOpts,omitempty"`
-	GPURequest  string            // filled automatically
-	Memory      string            // filled automatically
-	State       NodeState         // filled automatically
-	IP          NodeIP            // filled automatically
+	Networks      []string          // filled automatically
+	ExtraHosts    []string          // filled automatically
+	ServerOpts    ServerOpts        `yaml:"serverOpts" json:"serverOpts,omitempty"`
+	AgentOpts     AgentOpts         `yaml:"agentOpts" json:"agentOpts,omitempty"`
+	GPURequest    string            // filled automatically
+	Memory        string            // filled automatically
+	State         NodeState         // filled automatically
+	IP            NodeIP            // filled automatically
 }
 
 // ServerOpts describes some additional server role specific opts

Here's a diff with some format & naming adjusts, feel free to add it to your PR.

Signed-off-by: Dentrax <[email protected]>
Signed-off-by: Batuhan Apaydın <[email protected]>
@developer-guy
Copy link
Contributor Author

diff --git a/cmd/node/nodeCreate.go b/cmd/node/nodeCreate.go
index b334b479..f817d211 100644
--- a/cmd/node/nodeCreate.go
+++ b/cmd/node/nodeCreate.go
@@ -74,7 +74,7 @@ func NewCmdNodeCreate() *cobra.Command {
 	cmd.Flags().BoolVar(&createNodeOpts.Wait, "wait", false, "Wait for the node(s) to be ready before returning.")
 	cmd.Flags().DurationVar(&createNodeOpts.Timeout, "timeout", 0*time.Second, "Maximum waiting time for '--wait' before canceling/returning.")
 
-    cmd.Flags().StringSliceP("k3s-node-label", "", []string{}, "Specify k3s node labels in format \"foo=bar\", \"foo=bar:NoExecute\"")
+	cmd.Flags().StringSliceP("k3s-node-label", "", []string{}, "Specify k3s node labels in format \"foo=bar\", \"foo=bar:NoExecute\"")
 
 	// done
 	return cmd
@@ -127,17 +127,17 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cl
 		log.Errorf("Provided memory limit value is invalid")
 	}
 
-	labels, err := cmd.Flags().GetStringSlice("node-label")
+	k3sNodeLabelsFlags, err := cmd.Flags().GetStringSlice("k3s-node-label")
 	if err != nil {
-		log.Errorln("No node-label specified")
+		log.Errorln("No k3s-node-label specified")
 		log.Fatalln(err)
 	}
 
-	k3sNodeLabels := make(map[string]string, len(labels))
-	for _, label := range labels {
+	k3sNodeLabels := make(map[string]string, len(k3sNodeLabelsFlags))
+	for _, label := range k3sNodeLabelsFlags {
 		labelSplitted := strings.Split(label, "=")
 		if len(labelSplitted) != 2 {
-			log.Fatalf("unknown label format format: %s, use format \"foo=bar\"", label)
+			log.Fatalf("unknown k3s-node-label format format: %s, use format \"foo=bar\"", label)
 		}
 		k3sNodeLabels[labelSplitted[0]] = labelSplitted[1]
 	}
@@ -153,8 +153,8 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cl
 				k3d.LabelRole: roleStr,
 			},
 			K3sNodeLabels: k3sNodeLabels,
-			Restart:     true,
-			Memory:      memory,
+			Restart:       true,
+			Memory:        memory,
 		}
 		nodes = append(nodes, node)
 	}
diff --git a/pkg/types/types.go b/pkg/types/types.go
index 642038b3..e916a804 100644
--- a/pkg/types/types.go
+++ b/pkg/types/types.go
@@ -330,26 +330,26 @@ type NodeIP struct {
 
 // Node describes a k3d node
 type Node struct {
-	Name        string            `yaml:"name" json:"name,omitempty"`
-	Role        Role              `yaml:"role" json:"role,omitempty"`
-	Image       string            `yaml:"image" json:"image,omitempty"`
-	Volumes     []string          `yaml:"volumes" json:"volumes,omitempty"`
-	Env         []string          `yaml:"env" json:"env,omitempty"`
-	Cmd         []string          // filled automatically based on role
-	Args        []string          `yaml:"extraArgs" json:"extraArgs,omitempty"`
-	Ports       nat.PortMap       `yaml:"portMappings" json:"portMappings,omitempty"`
-	Restart     bool              `yaml:"restart" json:"restart,omitempty"`
-	Created     string            `yaml:"created" json:"created,omitempty"`
-	Labels      map[string]string // filled automatically
+	Name          string            `yaml:"name" json:"name,omitempty"`
+	Role          Role              `yaml:"role" json:"role,omitempty"`
+	Image         string            `yaml:"image" json:"image,omitempty"`
+	Volumes       []string          `yaml:"volumes" json:"volumes,omitempty"`
+	Env           []string          `yaml:"env" json:"env,omitempty"`
+	Cmd           []string          // filled automatically based on role
+	Args          []string          `yaml:"extraArgs" json:"extraArgs,omitempty"`
+	Ports         nat.PortMap       `yaml:"portMappings" json:"portMappings,omitempty"`
+	Restart       bool              `yaml:"restart" json:"restart,omitempty"`
+	Created       string            `yaml:"created" json:"created,omitempty"`
+	Labels        map[string]string // filled automatically
 	K3sNodeLabels map[string]string `yaml:"k3sNodeLabels" json:"k3sNodeLabels,omitempty"`
-	Networks    []string          // filled automatically
-	ExtraHosts  []string          // filled automatically
-	ServerOpts  ServerOpts        `yaml:"serverOpts" json:"serverOpts,omitempty"`
-	AgentOpts   AgentOpts         `yaml:"agentOpts" json:"agentOpts,omitempty"`
-	GPURequest  string            // filled automatically
-	Memory      string            // filled automatically
-	State       NodeState         // filled automatically
-	IP          NodeIP            // filled automatically
+	Networks      []string          // filled automatically
+	ExtraHosts    []string          // filled automatically
+	ServerOpts    ServerOpts        `yaml:"serverOpts" json:"serverOpts,omitempty"`
+	AgentOpts     AgentOpts         `yaml:"agentOpts" json:"agentOpts,omitempty"`
+	GPURequest    string            // filled automatically
+	Memory        string            // filled automatically
+	State         NodeState         // filled automatically
+	IP            NodeIP            // filled automatically
 }
 
 // ServerOpts describes some additional server role specific opts

Here's a diff with some format & naming adjusts, feel free to add it to your PR.

thank you 👍

@iwilltry42
Copy link
Member

Looks good to me so far, thank you!

@iwilltry42 iwilltry42 changed the title created an extraLabels field in order to pass labels to the k3s agent subcommand as a --node-label flag add --node-label flag for node create command (#584, @developer-guy, @ejose, @dentrax) May 10, 2021
@iwilltry42 iwilltry42 merged commit 5fe8a3c into k3d-io:main May 10, 2021
rancherio-gh-m pushed a commit that referenced this pull request May 10, 2021
Author: Batuhan Apaydın <[email protected]>
Date:   Mon May 10 17:58:01 2021 +0300

    add --node-label flag for node create command (#584, @developer-guy, @EJose, @Dentrax)
rancherio-gh-m pushed a commit that referenced this pull request May 14, 2021
Author: iwilltry42 <[email protected]>
Date:   Fri May 14 13:53:47 2021 +0200

    Revert "add --node-label flag for node create command (#584, @developer-guy, @EJose, @Dentrax)"

    This reverts commit 5fe8a3c.
@iwilltry42 iwilltry42 modified the milestones: v4.5.0, v5.0.0 Jun 11, 2021
@iwilltry42 iwilltry42 linked an issue Jul 20, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] node-label not working [FEATURE] Add --labels flags to add a labels to a node
4 participants