diff --git a/cmd/argocd/commands/cluster.go b/cmd/argocd/commands/cluster.go index 2493e094553723..c85a4204275195 100644 --- a/cmd/argocd/commands/cluster.go +++ b/cmd/argocd/commands/cluster.go @@ -34,6 +34,10 @@ const ( clusterFieldName = "name" // cluster field is 'namespaces' clusterFieldNamespaces = "namespaces" + // cluster field is 'labels' + clusterFieldLabel = "labels" + // cluster field is 'annotations' + clusterFieldAnnotation = "annotations" // indicates managing all namespaces allNamespaces = "*" ) @@ -220,6 +224,8 @@ func NewClusterSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command var ( clusterOptions cmdutil.ClusterOptions clusterName string + labels []string + annotations []string ) command := &cobra.Command{ Use: "set NAME", @@ -238,17 +244,25 @@ func NewClusterSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command conn, clusterIf := headless.NewClientOrDie(clientOpts, c).NewClusterClientOrDie() defer io.Close(conn) // checks the fields that needs to be updated - updatedFields := checkFieldsToUpdate(clusterOptions) + updatedFields := checkFieldsToUpdate(clusterOptions, labels, annotations) namespaces := clusterOptions.Namespaces // check if all namespaces have to be considered if len(namespaces) == 1 && strings.EqualFold(namespaces[0], allNamespaces) { namespaces[0] = "" } + // parse the labels you're receiving from the label flag + labelsMap, err := label.Parse(labels) + errors.CheckError(err) + // parse the annotations you're receiving from the annotation flag + annotationsMap, err := label.Parse(annotations) + errors.CheckError(err) if updatedFields != nil { clusterUpdateRequest := clusterpkg.ClusterUpdateRequest{ Cluster: &argoappv1.Cluster{ - Name: clusterOptions.Name, - Namespaces: namespaces, + Name: clusterOptions.Name, + Namespaces: namespaces, + Labels: labelsMap, + Annotations: annotationsMap, }, UpdatedFields: updatedFields, Id: &clusterpkg.ClusterID{ @@ -266,11 +280,13 @@ func NewClusterSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command } command.Flags().StringVar(&clusterOptions.Name, "name", "", "Overwrite the cluster name") command.Flags().StringArrayVar(&clusterOptions.Namespaces, "namespace", nil, "List of namespaces which are allowed to manage. Specify '*' to manage all namespaces") + command.Flags().StringArrayVar(&labels, "label", nil, "Set metadata labels (e.g. --label key=value)") + command.Flags().StringArrayVar(&annotations, "annotation", nil, "Set metadata annotations (e.g. --annotation key=value)") return command } // checkFieldsToUpdate returns the fields that needs to be updated -func checkFieldsToUpdate(clusterOptions cmdutil.ClusterOptions) []string { +func checkFieldsToUpdate(clusterOptions cmdutil.ClusterOptions, labels []string, annotations []string) []string { var updatedFields []string if clusterOptions.Name != "" { updatedFields = append(updatedFields, clusterFieldName) @@ -278,6 +294,12 @@ func checkFieldsToUpdate(clusterOptions cmdutil.ClusterOptions) []string { if clusterOptions.Namespaces != nil { updatedFields = append(updatedFields, clusterFieldNamespaces) } + if labels != nil { + updatedFields = append(updatedFields, clusterFieldLabel) + } + if annotations != nil { + updatedFields = append(updatedFields, clusterFieldAnnotation) + } return updatedFields } diff --git a/docs/user-guide/commands/argocd_cluster_set.md b/docs/user-guide/commands/argocd_cluster_set.md index 2dba9c2ad16e85..3f25dda8dac2dc 100644 --- a/docs/user-guide/commands/argocd_cluster_set.md +++ b/docs/user-guide/commands/argocd_cluster_set.md @@ -19,9 +19,11 @@ argocd cluster set NAME [flags] ### Options ``` - -h, --help help for set - --name string Overwrite the cluster name - --namespace stringArray List of namespaces which are allowed to manage. Specify '*' to manage all namespaces + --annotation stringArray Set metadata annotations (e.g. --annotation key=value) + -h, --help help for set + --label stringArray Set metadata labels (e.g. --label key=value) + --name string Overwrite the cluster name + --namespace stringArray List of namespaces which are allowed to manage. Specify '*' to manage all namespaces ``` ### Options inherited from parent commands