Skip to content

Commit e81003b

Browse files
CR-10413 add cluster labels/annotations (#432)
1 parent 1173d5d commit e81003b

File tree

7 files changed

+79
-20
lines changed

7 files changed

+79
-20
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.461
1+
VERSION=v0.0.462
22

33

44
OUT_DIR=dist

cmd/commands/cluster.go

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -44,6 +45,9 @@ type (
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
}
@@ -62,6 +66,7 @@ type (
6266

6367
var (
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

102107
func 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+
}

docs/commands/cli-v2_cluster_add.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ cli-v2 cluster add my-runtime --context my-context
1515
### Options
1616

1717
```
18-
--context string The name of the kubeconfig context to use
19-
--dry-run
20-
-h, --help help for add
21-
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
22-
--name string Name of the cluster. If omitted, will use the context name
23-
-n, --namespace string If present, the namespace scope for this CLI request
18+
--annotations stringToString Set metadata annotations (e.g. --annotation key=value) (default [])
19+
--context string The name of the kubeconfig context to use
20+
--dry-run
21+
-h, --help help for add
22+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
23+
--labels stringToString Set metadata labels (e.g. --label key=value) (default [])
24+
--name string Name of the cluster. If omitted, will use the context name
25+
-n, --namespace string If present, the namespace scope for this CLI request
2426
```
2527

2628
### Options inherited from parent commands

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.461/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.462/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.461/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.462/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/app-proxy/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ kind: Kustomization
33
images:
44
- name: quay.io/codefresh/cap-app-proxy
55
newName: quay.io/codefresh/cap-app-proxy
6-
newTag: 1.1652.1
6+
newTag: 1.1658.0
77
resources:
88
- app-proxy.deploy.yaml
99
- app-proxy.svc.yaml

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.461
8+
version: 0.0.462
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/store/store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
AddClusterDefURL = "https://github.com/codefresh-io/csdp-official/add-cluster/kustomize"
3636
FallbackAddClusterDefURL = "https://github.com/codefresh-io/cli-v2/manifests/add-cluster/kustomize"
3737
)
38+
3839
type Version struct {
3940
Version *semver.Version
4041
BuildDate string

0 commit comments

Comments
 (0)