@@ -29,6 +29,7 @@ import (
2929 kubeutil "github.com/codefresh-io/cli-v2/pkg/util/kube"
3030 kustutil "github.com/codefresh-io/cli-v2/pkg/util/kust"
3131 "github.com/codefresh-io/go-sdk/pkg/codefresh/model"
32+ "github.com/ghodss/yaml"
3233
3334 "github.com/Masterminds/semver/v3"
3435 "github.com/argoproj-labs/argocd-autopilot/pkg/kube"
4445 clusterName string
4546 kubeContext string
4647 kubeconfig string
48+ annotations map [string ]string
49+ labels map [string ]string
50+ tag string
4751 dryRun bool
4852 kubeFactory kube.Factory
4953 }
6266
6367var (
6468 minAddClusterSupportedVersion = semver .MustParse ("0.0.283" )
69+ minAddClusterLabelsSupportedVersion = semver .MustParse ("0.0.462" )
6570
6671 serviceAccountGVK = resid.Gvk {
6772 Version : "v1" ,
@@ -100,7 +105,9 @@ func NewClusterCommand() *cobra.Command {
100105}
101106
102107func newClusterAddCommand () * cobra.Command {
103- var opts ClusterAddOptions
108+ var (
109+ opts ClusterAddOptions
110+ )
104111
105112 cmd := & cobra.Command {
106113 Use : "add [RUNTIME_NAME]" ,
@@ -122,17 +129,20 @@ func newClusterAddCommand() *cobra.Command {
122129 return err
123130 }
124131
125- err = setClusterName (cmd .Context (), & opts )
126-
127- return err
132+ return setClusterName (cmd .Context (), & opts )
128133 },
129134 RunE : func (cmd * cobra.Command , args []string ) error {
130135 return runClusterAdd (cmd .Context (), & opts )
131136 },
132137 }
133138
134139 cmd .Flags ().StringVar (& opts .clusterName , "name" , "" , "Name of the cluster. If omitted, will use the context name" )
140+ cmd .Flags ().StringToStringVar (& opts .annotations , "annotations" , nil , "Set metadata annotations (e.g. --annotation key=value)" )
141+ cmd .Flags ().StringToStringVar (& opts .labels , "labels" , nil , "Set metadata labels (e.g. --label key=value)" )
135142 cmd .Flags ().BoolVar (& opts .dryRun , "dry-run" , false , "" )
143+ cmd .Flags ().StringVar (& opts .tag , "tag" , "" , "[dev only] - use a specific tag of the csdp-add-cluster image" )
144+
145+ util .Die (cmd .Flags ().MarkHidden ("tag" ))
136146 opts .kubeFactory = kube .AddFlags (cmd .Flags ())
137147
138148 return cmd
@@ -153,6 +163,10 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
153163 return fmt .Errorf ("runtime \" %s\" does not support this command. Minimal required version is %s" , opts .runtimeName , minAddClusterSupportedVersion )
154164 }
155165
166+ if (len (opts .annotations ) > 0 || len (opts .labels ) > 0 ) && version .LessThan (minAddClusterLabelsSupportedVersion ) {
167+ return fmt .Errorf ("runtime \" %s\" does not support adding clusters with annotations or labels. Minimal required version is %s" , opts .runtimeName , minAddClusterLabelsSupportedVersion )
168+ }
169+
156170 if runtime .IngressHost == nil {
157171 return fmt .Errorf ("runtime \" %s\" is missing an ingress URL" , opts .runtimeName )
158172 }
@@ -166,7 +180,7 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
166180 log .G (ctx ).Info ("Building \" add-cluster\" manifests" )
167181
168182 csdpToken := cfConfig .GetCurrentContext ().Token
169- manifests , nameSuffix , err := createAddClusterManifests (ingressUrl , opts . clusterName , server , csdpToken , * runtime . RuntimeVersion )
183+ manifests , nameSuffix , err := createAddClusterManifests (opts , ingressUrl , server , csdpToken , version . String () )
170184 if err != nil {
171185 return fmt .Errorf ("failed getting add-cluster resources: %w" , err )
172186 }
@@ -278,11 +292,16 @@ func getSuffixToClusterName(clusters []model.Cluster, name string, tempName stri
278292 return counter
279293}
280294
281- func createAddClusterManifests (ingressUrl , contextName , server , csdpToken , version string ) ([]byte , string , error ) {
295+ func createAddClusterManifests (opts * ClusterAddOptions , ingressUrl , server , csdpToken , version string ) ([]byte , string , error ) {
282296 nameSuffix := getClusterResourcesNameSuffix ()
283297 resourceUrl := store .AddClusterDefURL
284- if strings .HasPrefix (resourceUrl , "http" ) && ! strings .Contains (resourceUrl , "?ref=" ) {
285- resourceUrl = fmt .Sprintf ("%s?ref=%s" , resourceUrl , version )
298+ if strings .HasPrefix (resourceUrl , "http" ) {
299+ ref := version
300+ if opts .tag != "" {
301+ ref = opts .tag
302+ }
303+
304+ resourceUrl = fmt .Sprintf ("%s?ref=%s" , resourceUrl , ref )
286305 }
287306
288307 k := & kusttypes.Kustomization {
@@ -295,7 +314,7 @@ func createAddClusterManifests(ingressUrl, contextName, server, csdpToken, versi
295314 KvPairSources : kusttypes.KvPairSources {
296315 LiteralSources : []string {
297316 fmt .Sprintf ("ingressUrl=" + ingressUrl ),
298- fmt .Sprintf ("contextName=" + contextName ),
317+ fmt .Sprintf ("contextName=" + opts . clusterName ),
299318 fmt .Sprintf ("server=" + server ),
300319 },
301320 },
@@ -358,6 +377,34 @@ func createAddClusterManifests(ingressUrl, contextName, server, csdpToken, versi
358377 },
359378 },
360379 }
380+
381+ if len (opts .annotations ) > 0 {
382+ annotationsStr , err := mapToYaml (opts .annotations )
383+ if err != nil {
384+ return nil , "" , fmt .Errorf ("failed encoding annotations: %w" , err )
385+ }
386+
387+ k .ConfigMapGenerator [0 ].KvPairSources .LiteralSources = append (k .ConfigMapGenerator [0 ].KvPairSources .LiteralSources , fmt .Sprintf ("annotations=" + annotationsStr ))
388+ }
389+
390+ if len (opts .labels ) > 0 {
391+ labelsStr , err := mapToYaml (opts .labels )
392+ if err != nil {
393+ return nil , "" , fmt .Errorf ("failed encoding labels: %w" , err )
394+ }
395+
396+ k .ConfigMapGenerator [0 ].KvPairSources .LiteralSources = append (k .ConfigMapGenerator [0 ].KvPairSources .LiteralSources , fmt .Sprintf ("labels=" + labelsStr ))
397+ }
398+
399+ if opts .tag != "" {
400+ k .Images = []kusttypes.Image {
401+ {
402+ Name : "quay.io/codefresh/csdp-add-cluster" ,
403+ NewTag : opts .tag ,
404+ },
405+ }
406+ }
407+
361408 k .FixKustomizationPostUnmarshalling ()
362409 util .Die (k .FixKustomizationPreMarshalling ())
363410
@@ -574,3 +621,12 @@ func runCreateArgoRollouts(ctx context.Context, opts *ClusterCreateArgoRolloutsO
574621
575622 return nil
576623}
624+
625+ func mapToYaml (m map [string ]string ) (string , error ) {
626+ data , err := yaml .Marshal (m )
627+ if err != nil {
628+ return "" , err
629+ }
630+
631+ return string (data ), nil
632+ }
0 commit comments