diff --git a/cli/cmds/cluster_create.go b/cli/cmds/cluster_create.go index 941600c0..bdf28dbd 100644 --- a/cli/cmds/cluster_create.go +++ b/cli/cmds/cluster_create.go @@ -39,6 +39,8 @@ type CreateConfig struct { agentArgs []string serverEnvs []string agentEnvs []string + labels []string + annotations []string persistenceType string storageClassName string storageRequestSize string @@ -186,8 +188,10 @@ func createAction(appCtx *AppContext, config *CreateConfig) func(cmd *cobra.Comm func newCluster(name, namespace string, config *CreateConfig) *v1beta1.Cluster { cluster := &v1beta1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, + Name: name, + Namespace: namespace, + Labels: parseKeyValuePairs(config.labels, "label"), + Annotations: parseKeyValuePairs(config.annotations, "annotation"), }, TypeMeta: metav1.TypeMeta{ Kind: "Cluster", @@ -366,6 +370,27 @@ func caCertSecret(certName, clusterName, clusterNamespace string, cert, key []by } } +func parseKeyValuePairs(pairs []string, pairType string) map[string]string { + resultMap := make(map[string]string) + + for _, p := range pairs { + var k, v string + + keyValue := strings.SplitN(p, "=", 2) + + k = keyValue[0] + if len(keyValue) == 2 { + v = keyValue[1] + } + + resultMap[k] = v + + logrus.Debugf("Adding '%s=%s' %s to Cluster", k, v, pairType) + } + + return resultMap +} + const clusterDetailsTemplate = `Cluster details: Mode: {{ .Mode }} Servers: {{ .Servers }}{{ if .Agents }} diff --git a/cli/cmds/cluster_create_flags.go b/cli/cmds/cluster_create_flags.go index b9b0626e..47d2585b 100644 --- a/cli/cmds/cluster_create_flags.go +++ b/cli/cmds/cluster_create_flags.go @@ -24,6 +24,8 @@ func createFlags(cmd *cobra.Command, cfg *CreateConfig) { cmd.Flags().StringSliceVar(&cfg.agentArgs, "agent-args", []string{}, "agents extra arguments") cmd.Flags().StringSliceVar(&cfg.serverEnvs, "server-envs", []string{}, "servers extra Envs") cmd.Flags().StringSliceVar(&cfg.agentEnvs, "agent-envs", []string{}, "agents extra Envs") + cmd.Flags().StringArrayVar(&cfg.labels, "labels", []string{}, "Labels to add to the cluster object (e.g. key=value)") + cmd.Flags().StringArrayVar(&cfg.annotations, "annotations", []string{}, "Annotations to add to the cluster object (e.g. key=value)") cmd.Flags().StringVar(&cfg.version, "version", "", "k3s version") cmd.Flags().StringVar(&cfg.mode, "mode", "shared", "k3k mode type (shared, virtual)") cmd.Flags().StringVar(&cfg.kubeconfigServerHost, "kubeconfig-server", "", "override the kubeconfig server host") diff --git a/cli/cmds/policy_create.go b/cli/cmds/policy_create.go index 63f4d23c..93a165fa 100644 --- a/cli/cmds/policy_create.go +++ b/cli/cmds/policy_create.go @@ -18,7 +18,9 @@ import ( ) type VirtualClusterPolicyCreateConfig struct { - mode string + mode string + labels []string + annotations []string } func NewPolicyCreateCmd(appCtx *AppContext) *cobra.Command { @@ -41,6 +43,8 @@ func NewPolicyCreateCmd(appCtx *AppContext) *cobra.Command { } cmd.Flags().StringVar(&config.mode, "mode", "shared", "The allowed mode type of the policy") + cmd.Flags().StringArrayVar(&config.labels, "labels", []string{}, "Labels to add to the policy object (e.g. key=value)") + cmd.Flags().StringArrayVar(&config.annotations, "annotations", []string{}, "Annotations to add to the policy object (e.g. key=value)") return cmd } @@ -51,7 +55,7 @@ func policyCreateAction(appCtx *AppContext, config *VirtualClusterPolicyCreateCo client := appCtx.Client policyName := args[0] - _, err := createPolicy(ctx, client, v1beta1.ClusterMode(config.mode), policyName) + _, err := createPolicy(ctx, client, config, policyName) return err } @@ -81,19 +85,21 @@ func createNamespace(ctx context.Context, client client.Client, name, policyName return nil } -func createPolicy(ctx context.Context, client client.Client, mode v1beta1.ClusterMode, policyName string) (*v1beta1.VirtualClusterPolicy, error) { +func createPolicy(ctx context.Context, client client.Client, config *VirtualClusterPolicyCreateConfig, policyName string) (*v1beta1.VirtualClusterPolicy, error) { logrus.Infof("Creating policy [%s]", policyName) policy := &v1beta1.VirtualClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ - Name: policyName, + Name: policyName, + Labels: parseKeyValuePairs(config.labels, "label"), + Annotations: parseKeyValuePairs(config.annotations, "annotation"), }, TypeMeta: metav1.TypeMeta{ Kind: "VirtualClusterPolicy", APIVersion: "k3k.io/v1beta1", }, Spec: v1beta1.VirtualClusterPolicySpec{ - AllowedMode: mode, + AllowedMode: v1beta1.ClusterMode(config.mode), }, } diff --git a/docs/cli/k3kcli_cluster_create.md b/docs/cli/k3kcli_cluster_create.md index 58fbf301..5ba47a12 100644 --- a/docs/cli/k3kcli_cluster_create.md +++ b/docs/cli/k3kcli_cluster_create.md @@ -18,10 +18,12 @@ k3kcli cluster create [command options] NAME --agent-args strings agents extra arguments --agent-envs strings agents extra Envs --agents int number of agents + --annotations stringArray Annotations to add to the cluster object (e.g. key=value) --cluster-cidr string cluster CIDR --custom-certs string The path for custom certificate directory -h, --help help for create --kubeconfig-server string override the kubeconfig server host + --labels stringArray Labels to add to the cluster object (e.g. key=value) --mirror-host-nodes Mirror Host Cluster Nodes --mode string k3k mode type (shared, virtual) (default "shared") -n, --namespace string namespace of the k3k cluster diff --git a/docs/cli/k3kcli_policy_create.md b/docs/cli/k3kcli_policy_create.md index 2458a3f6..584e165e 100644 --- a/docs/cli/k3kcli_policy_create.md +++ b/docs/cli/k3kcli_policy_create.md @@ -15,8 +15,10 @@ k3kcli policy create [command options] NAME ### Options ``` - -h, --help help for create - --mode string The allowed mode type of the policy (default "shared") + --annotations stringArray Annotations to add to the policy object (e.g. key=value) + -h, --help help for create + --labels stringArray Labels to add to the policy object (e.g. key=value) + --mode string The allowed mode type of the policy (default "shared") ``` ### Options inherited from parent commands