@@ -29,6 +29,7 @@ import (
29
29
kubeutil "github.com/codefresh-io/cli-v2/pkg/util/kube"
30
30
kustutil "github.com/codefresh-io/cli-v2/pkg/util/kust"
31
31
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
32
+ "github.com/ghodss/yaml"
32
33
33
34
"github.com/Masterminds/semver/v3"
34
35
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
44
45
clusterName string
45
46
kubeContext string
46
47
kubeconfig string
48
+ annotations map [string ]string
49
+ labels map [string ]string
50
+ tag string
47
51
dryRun bool
48
52
kubeFactory kube.Factory
49
53
}
62
66
63
67
var (
64
68
minAddClusterSupportedVersion = semver .MustParse ("0.0.283" )
69
+ minAddClusterLabelsSupportedVersion = semver .MustParse ("0.0.462" )
65
70
66
71
serviceAccountGVK = resid.Gvk {
67
72
Version : "v1" ,
@@ -100,7 +105,9 @@ func NewClusterCommand() *cobra.Command {
100
105
}
101
106
102
107
func newClusterAddCommand () * cobra.Command {
103
- var opts ClusterAddOptions
108
+ var (
109
+ opts ClusterAddOptions
110
+ )
104
111
105
112
cmd := & cobra.Command {
106
113
Use : "add [RUNTIME_NAME]" ,
@@ -122,17 +129,20 @@ func newClusterAddCommand() *cobra.Command {
122
129
return err
123
130
}
124
131
125
- err = setClusterName (cmd .Context (), & opts )
126
-
127
- return err
132
+ return setClusterName (cmd .Context (), & opts )
128
133
},
129
134
RunE : func (cmd * cobra.Command , args []string ) error {
130
135
return runClusterAdd (cmd .Context (), & opts )
131
136
},
132
137
}
133
138
134
139
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)" )
135
142
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" ))
136
146
opts .kubeFactory = kube .AddFlags (cmd .Flags ())
137
147
138
148
return cmd
@@ -153,6 +163,10 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
153
163
return fmt .Errorf ("runtime \" %s\" does not support this command. Minimal required version is %s" , opts .runtimeName , minAddClusterSupportedVersion )
154
164
}
155
165
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
+
156
170
if runtime .IngressHost == nil {
157
171
return fmt .Errorf ("runtime \" %s\" is missing an ingress URL" , opts .runtimeName )
158
172
}
@@ -166,7 +180,7 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
166
180
log .G (ctx ).Info ("Building \" add-cluster\" manifests" )
167
181
168
182
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 () )
170
184
if err != nil {
171
185
return fmt .Errorf ("failed getting add-cluster resources: %w" , err )
172
186
}
@@ -278,11 +292,16 @@ func getSuffixToClusterName(clusters []model.Cluster, name string, tempName stri
278
292
return counter
279
293
}
280
294
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 ) {
282
296
nameSuffix := getClusterResourcesNameSuffix ()
283
297
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 )
286
305
}
287
306
288
307
k := & kusttypes.Kustomization {
@@ -295,7 +314,7 @@ func createAddClusterManifests(ingressUrl, contextName, server, csdpToken, versi
295
314
KvPairSources : kusttypes.KvPairSources {
296
315
LiteralSources : []string {
297
316
fmt .Sprintf ("ingressUrl=" + ingressUrl ),
298
- fmt .Sprintf ("contextName=" + contextName ),
317
+ fmt .Sprintf ("contextName=" + opts . clusterName ),
299
318
fmt .Sprintf ("server=" + server ),
300
319
},
301
320
},
@@ -358,6 +377,34 @@ func createAddClusterManifests(ingressUrl, contextName, server, csdpToken, versi
358
377
},
359
378
},
360
379
}
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
+
361
408
k .FixKustomizationPostUnmarshalling ()
362
409
util .Die (k .FixKustomizationPreMarshalling ())
363
410
@@ -574,3 +621,12 @@ func runCreateArgoRollouts(ctx context.Context, opts *ClusterCreateArgoRolloutsO
574
621
575
622
return nil
576
623
}
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