Skip to content

Commit 3bff2d0

Browse files
CR-11438 friendly cluster name (#431)
* set cluster name * wip * validate cluster name * wip * name validation * remove lines * small fix * bump version * bump app-proxy * lint * bump appVersion and tag
1 parent d324299 commit 3bff2d0

File tree

8 files changed

+34
-11
lines changed

8 files changed

+34
-11
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.384
1+
VERSION=v0.0.385
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/cluster.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
type (
3838
ClusterAddOptions struct {
3939
runtimeName string
40+
clusterName string
4041
kubeContext string
4142
kubeconfig string
4243
dryRun bool
@@ -97,13 +98,21 @@ func newClusterAddCommand() *cobra.Command {
9798
}
9899

99100
opts.kubeContext, err = ensureKubeContextName(cmd.Flag("context"), cmd.Flag("kubeconfig"))
101+
if err != nil {
102+
return err
103+
}
104+
105+
setClusterName(&opts)
106+
err = validateClusterName(opts.clusterName)
107+
100108
return err
101109
},
102110
RunE: func(cmd *cobra.Command, args []string) error {
103111
return runClusterAdd(cmd.Context(), &opts)
104112
},
105113
}
106114

115+
cmd.Flags().StringVar(&opts.clusterName, "name", "", "Name of the cluster. If omitted, will use the context name")
107116
cmd.Flags().BoolVar(&opts.dryRun, "dry-run", false, "")
108117
opts.kubeFactory = kube.AddFlags(cmd.Flags())
109118

@@ -136,7 +145,7 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
136145
}
137146

138147
csdpToken := cfConfig.GetCurrentContext().Token
139-
k := createAddClusterKustomization(ingressUrl, opts.kubeContext, server, csdpToken, *runtime.RuntimeVersion)
148+
k := createAddClusterKustomization(ingressUrl, opts.clusterName, server, csdpToken, *runtime.RuntimeVersion)
140149

141150
manifests, err := kustutil.BuildKustomization(k)
142151
if err != nil {
@@ -156,6 +165,20 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
156165
return kubeutil.WaitForJob(ctx, opts.kubeFactory, "kube-system", store.Get().AddClusterJobName)
157166
}
158167

168+
func setClusterName(opts *ClusterAddOptions) {
169+
if opts.clusterName != "" {
170+
return
171+
}
172+
opts.clusterName = opts.kubeContext
173+
}
174+
175+
func validateClusterName(name string) error {
176+
if strings.ContainsAny(name, "%`") {
177+
return fmt.Errorf("cluster name '%s' is invalid. '%%' and '`' are not allowed", name)
178+
}
179+
return nil
180+
}
181+
159182
func createAddClusterKustomization(ingressUrl, contextName, server, csdpToken, version string) *kusttypes.Kustomization {
160183
resourceUrl := store.AddClusterDefURL
161184
if strings.HasPrefix(resourceUrl, "http") {

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.384/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.385/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.384/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.385/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/add-cluster/add-cluster.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ BEARER_TOKEN=$(kubectl get secret ${SECRET_NAME} -n ${NAMESPACE} -o jsonpath='{.
2727

2828
# write KUBE_COPNFIG_DATA to local file
2929
CLUSTER_NAME=$(echo ${SERVER} | sed s/'http[s]\?:\/\/'//)
30-
kubectl config set-cluster ${CLUSTER_NAME} --server=${SERVER} --certificate-authority=${CACERT}
30+
kubectl config set-cluster "${CLUSTER_NAME}" --server=${SERVER} --certificate-authority=${CACERT}
3131
kubectl config set-credentials ${SERVICE_ACCOUNT_NAME} --token ${BEARER_TOKEN}
32-
kubectl config set-context ${CONTEXT_NAME} --cluster=${CLUSTER_NAME} --user=${SERVICE_ACCOUNT_NAME}
33-
KUBE_CONFIG_B64=$(kubectl config view --minify --flatten --output json --context=${CONTEXT_NAME} | base64 -w 0)
32+
kubectl config set-context "${CONTEXT_NAME}" --cluster="${CLUSTER_NAME}" --user=${SERVICE_ACCOUNT_NAME}
33+
KUBE_CONFIG_B64=$(kubectl config view --minify --flatten --output json --context="${CONTEXT_NAME}" | base64 -w 0)
3434

3535
STATUS_CODE=$(curl -X POST ${INGRESS_URL}/app-proxy/api/clusters \
3636
-H 'Content-Type: application/json' \

manifests/add-cluster/helm/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ type: application
66

77
version: 0.2.0
88

9-
appVersion: 0.3.0
9+
appVersion: 0.3.1

manifests/add-cluster/kustomize/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace: kube-system
44

55
images:
66
- name: quay.io/codefresh/csdp-add-cluster
7-
newTag: 0.3.0
7+
newTag: 0.3.1
88

99
resources:
1010
- configmap.yaml

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.1338.0
6+
newTag: 1.1341.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.384
8+
version: 0.0.385
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

0 commit comments

Comments
 (0)