Skip to content

Commit 98a40da

Browse files
authored
Cr 12025 add cluster wait (#427)
* initial commit * add-cluster to build dev image * improved script * fixed dev build * fixed script * updated add-cluster script version fixed logs * updated version to 0.0.380 * pr comments
1 parent bec5aa1 commit 98a40da

File tree

10 files changed

+107
-14
lines changed

10 files changed

+107
-14
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.379
1+
VERSION=v0.0.380
22

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

build/add-cluster-script.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ steps:
5858
- name: compare_versions
5959
on:
6060
- success
61+
branch:
62+
only: ["main"]
63+
64+
build_dev_image:
65+
stage: Release
66+
type: build
67+
title: build and push add-cluster dev image
68+
working_directory: manifests/add-cluster
69+
image_name: codefresh/csdp-add-cluster
70+
tag: ${{CF_BRANCH}}
71+
registry: ${{REGISTRY_INTEGRATION_QUAY}}
72+
buildkit: true
73+
when:
74+
steps:
75+
- name: compare_versions
76+
on:
77+
- success
78+
branch:
79+
ignore: ["main"]
6180

6281
publish_chart:
6382
stage: Release

cmd/commands/cluster.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/codefresh-io/cli-v2/pkg/log"
2525
"github.com/codefresh-io/cli-v2/pkg/store"
2626
"github.com/codefresh-io/cli-v2/pkg/util"
27+
kubeutil "github.com/codefresh-io/cli-v2/pkg/util/kube"
2728
kustutil "github.com/codefresh-io/cli-v2/pkg/util/kust"
2829

2930
"github.com/Masterminds/semver/v3"
@@ -139,15 +140,20 @@ func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
139140

140141
manifests, err := kustutil.BuildKustomization(k)
141142
if err != nil {
142-
return fmt.Errorf("failed building kustomization:%w", err)
143+
return fmt.Errorf("failed building kustomization: %w", err)
143144
}
144145

145146
if opts.dryRun {
146147
fmt.Println(string(manifests))
147148
return nil
148149
}
149150

150-
return opts.kubeFactory.Apply(ctx, manifests)
151+
err = opts.kubeFactory.Apply(ctx, manifests)
152+
if err != nil {
153+
return fmt.Errorf("failed applying manifests to cluster: %w", err)
154+
}
155+
156+
return kubeutil.WaitForJob(ctx, opts.kubeFactory, "kube-system", store.Get().AddClusterJobName)
151157
}
152158

153159
func createAddClusterKustomization(ingressUrl, contextName, server, csdpToken, version string) *kusttypes.Kustomization {
@@ -376,7 +382,7 @@ func runCreateArgoRollouts(ctx context.Context, opts *ClusterCreateArgoRolloutsO
376382
err = appProxy.AppProxyClusters().CreateArgoRollouts(ctx, opts.server, opts.namespace)
377383
if err != nil {
378384
return fmt.Errorf("failed to create argo-rollouts on \"%s'\": %w", opts.server, err)
379-
}
385+
}
380386

381387
log.G(ctx).Infof("created argo-rollouts component on \"%s\"", opts.server)
382388

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.379/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.380/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.379/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.380/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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ STATUS_CODE=$(curl -X POST ${INGRESS_URL}/app-proxy/api/clusters \
3636
-H 'Content-Type: application/json' \
3737
-H 'Authorization: '${CSDP_TOKEN}'' \
3838
-d '{ "name": "'${CONTEXT_NAME}'", "kubeConfig": "'${KUBE_CONFIG_B64}'" }' \
39-
-s -o response -w "%{http_code}")
40-
cat response
39+
-skL -o response -w "%{http_code}")
4140
echo "STATUS_CODE: ${STATUS_CODE}"
41+
cat response
42+
echo
43+
44+
if [[ $STATUS_CODE == 000 ]]; then
45+
echo "error sending request to runtime"
46+
exit 1
47+
fi
4248

43-
if [ $STATUS_CODE -ge 300 ]; then
49+
if [[ $STATUS_CODE -ge 300 ]]; then
4450
echo "error creating cluster in runtime"
4551
exit $STATUS_CODE
4652
fi

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.2.0
9+
appVersion: 0.3.0

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.2.0
7+
newTag: 0.3.0
88

99
resources:
1010
- configmap.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.379
8+
version: 0.0.380
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/store/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Version struct {
4545
}
4646

4747
type Store struct {
48+
AddClusterJobName string
4849
ArgoCDServerName string
4950
ArgoCDTokenKey string
5051
ArgoCDTokenSecret string
@@ -151,6 +152,7 @@ func Get() *Store {
151152
}
152153

153154
func init() {
155+
s.AddClusterJobName = "csdp-add-cluster-job"
154156
s.ArgoCDServerName = "argocd-server"
155157
s.ArgoCDTokenKey = "token"
156158
s.ArgoCDTokenSecret = "argocd-token"

pkg/util/kube/kube.go

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,66 @@ func GetClusterSecret(ctx context.Context, kubeFactory kube.Factory, namespace s
216216
return res, nil
217217
}
218218

219+
func WaitForJob(ctx context.Context, f kube.Factory, ns, jobName string) error {
220+
var attempt int32
221+
return f.Wait(ctx, &kube.WaitOptions{
222+
Interval: time.Second * 10,
223+
Timeout: time.Minute * 11, // BackOffLimit of 6 is a total of 630s, or 10m30s
224+
Resources: []kube.Resource{
225+
{
226+
Name: jobName,
227+
Namespace: ns,
228+
WaitFunc: func(ctx context.Context, f kube.Factory, ns, name string) (bool, error) {
229+
cs, err := f.KubernetesClientSet()
230+
if err != nil {
231+
return false, err
232+
}
233+
234+
j, err := cs.BatchV1().Jobs(ns).Get(ctx, name, metav1.GetOptions{})
235+
if err != nil {
236+
return false, err
237+
}
238+
239+
if j.Status.Failed > attempt {
240+
attempt = j.Status.Failed
241+
log.G(ctx).Warnf("Attempt #%d/%d failed:", attempt, *j.Spec.BackoffLimit)
242+
printJobLogs(ctx, cs, j)
243+
} else if j.Status.Succeeded == 1 {
244+
attempt += 1
245+
log.G(ctx).Infof("Attempt #%d/%d succeeded:", attempt, *j.Spec.BackoffLimit)
246+
printJobLogs(ctx, cs, j)
247+
}
248+
249+
for _, cond := range j.Status.Conditions {
250+
if cond.Type == batchv1.JobFailed {
251+
err = fmt.Errorf("add-cluster-job failed after %d attempts", j.Status.Failed)
252+
break
253+
}
254+
}
255+
256+
return j.Status.Succeeded == 1 || j.Status.Failed == *j.Spec.BackoffLimit, err
257+
},
258+
},
259+
},
260+
})
261+
}
262+
263+
func printJobLogs(ctx context.Context, client kubernetes.Interface, job *batchv1.Job) {
264+
p, err := getPodByJob(ctx, client, job)
265+
if err != nil {
266+
log.G(ctx).Errorf("Failed getting pod for job: $s", err.Error())
267+
return
268+
}
269+
270+
logs, err := getPodLogs(ctx, client, p.GetNamespace(), p.GetName())
271+
if err != nil {
272+
log.G(ctx).Errorf("Failed getting logs for pod: $s", err.Error())
273+
return
274+
}
275+
276+
fmt.Printf("=====\n%s\n=====\n\n", logs)
277+
}
278+
219279
func runNetworkTest(ctx context.Context, kubeFactory kube.Factory, urls ...string) error {
220280
const networkTestsTimeout = 120 * time.Second
221281

@@ -426,8 +486,8 @@ func deleteJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Jo
426486
}
427487

428488
func getPodByJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Job) (*v1.Pod, error) {
429-
pods, err := client.CoreV1().Pods(store.Get().DefaultNamespace).List(ctx, metav1.ListOptions{
430-
LabelSelector: "controller-uid=" + job.GetLabels()["controller-uid"],
489+
pods, err := client.CoreV1().Pods(job.GetNamespace()).List(ctx, metav1.ListOptions{
490+
LabelSelector: "controller-uid=" + job.Spec.Selector.MatchLabels["controller-uid"],
431491
})
432492
if err != nil {
433493
return nil, err

0 commit comments

Comments
 (0)