Skip to content

Commit 5dddc4a

Browse files
Cr 6174 - pre installation checks (#78)
1 parent 0b8538e commit 5dddc4a

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
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.77
1+
VERSION=v0.0.78
22
OUT_DIR=dist
33
YEAR?=$(shell date +"%Y")
44

cmd/commands/runtime.go

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"github.com/spf13/cobra"
4646
v1 "k8s.io/api/core/v1"
4747
rbacv1 "k8s.io/api/rbac/v1"
48+
kerrors "k8s.io/apimachinery/pkg/api/errors"
4849
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4950
)
5051

@@ -191,15 +192,8 @@ func NewRuntimeInstallCommand() *cobra.Command {
191192
}
192193

193194
func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
194-
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
195-
if err != nil {
196-
return err
197-
}
198-
199-
for _, rt := range runtimes {
200-
if rt.Metadata.Name == opts.RuntimeName {
201-
return fmt.Errorf("failed to create runtime: %s. A runtime by this name already exists", opts.RuntimeName)
202-
}
195+
if err := preInstallationChecks(ctx, opts); err != nil {
196+
return fmt.Errorf("pre installation checks failed: %w", err)
203197
}
204198

205199
rt, err := runtime.Download(opts.Version, opts.RuntimeName)
@@ -290,6 +284,75 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
290284
return nil
291285
}
292286

287+
func preInstallationChecks(ctx context.Context, opts *RuntimeInstallOptions) error {
288+
log.G(ctx).Debug("running pre-installation checks...")
289+
290+
if err := checkRuntimeCollisions(ctx, opts.RuntimeName, opts.KubeFactory); err != nil {
291+
return fmt.Errorf("runtime collision check failed: %w", err)
292+
}
293+
294+
if err := checkExistingRuntimes(ctx, opts.RuntimeName); err != nil {
295+
return fmt.Errorf("existing runtime check failed: %w", err)
296+
}
297+
298+
return nil
299+
}
300+
301+
func checkRuntimeCollisions(ctx context.Context, runtime string, kube kube.Factory) error {
302+
log.G(ctx).Debug("checking for argocd collisions in cluster")
303+
304+
cs, err := kube.KubernetesClientSet()
305+
if err != nil {
306+
return fmt.Errorf("failed to build kubernetes clientset: %w", err)
307+
}
308+
309+
crb, err := cs.RbacV1().ClusterRoleBindings().Get(ctx, store.Get().ArgoCDServerName, metav1.GetOptions{})
310+
if err != nil {
311+
if kerrors.IsNotFound(err) {
312+
return nil // no collision
313+
}
314+
315+
return fmt.Errorf("failed to get cluster-role-binding '%s': %w", store.Get().ArgoCDServerName, err)
316+
}
317+
318+
log.G(ctx).Debug("argocd cluster-role-binding found")
319+
320+
if len(crb.Subjects) == 0 {
321+
return nil // no collision
322+
}
323+
324+
subjNamespace := crb.Subjects[0].Namespace
325+
326+
// check if some argocd is actually using this crb
327+
_, err = cs.AppsV1().Deployments(subjNamespace).Get(ctx, store.Get().ArgoCDServerName, metav1.GetOptions{})
328+
if err != nil {
329+
if kerrors.IsNotFound(err) {
330+
log.G(ctx).Debug("argocd cluster-role-binding subject does not exist, no collision")
331+
332+
return nil // no collision
333+
}
334+
335+
return fmt.Errorf("failed to get deployment '%s': %w", store.Get().ArgoCDServerName, err)
336+
}
337+
338+
return fmt.Errorf("argo-cd is already installed on this cluster in namespace '%s', you need to uninstall it first", subjNamespace)
339+
}
340+
341+
func checkExistingRuntimes(ctx context.Context, runtime string) error {
342+
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
343+
if err != nil {
344+
return fmt.Errorf("failed to list runtimes: %w", err)
345+
}
346+
347+
for _, rt := range runtimes {
348+
if rt.Metadata.Name == runtime {
349+
return fmt.Errorf("runtime '%s' already exists", runtime)
350+
}
351+
}
352+
353+
return nil
354+
}
355+
293356
func intervalCheckIsRuntimePersisted(milliseconds int, ctx context.Context, runtimeName string, wg *sync.WaitGroup) {
294357
interval := time.Duration(milliseconds) * time.Millisecond
295358
ticker := time.NewTicker(interval)

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cf version
2020
### Linux
2121
```bash
2222
# download and extract the binary
23-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.77/cf-linux-amd64.tar.gz | tar zx
23+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.78/cf-linux-amd64.tar.gz | tar zx
2424

2525
# move the binary to your $PATH
2626
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -32,7 +32,7 @@ cf version
3232
### Mac
3333
```bash
3434
# download and extract the binary
35-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.77/cf-darwin-amd64.tar.gz | tar zx
35+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.78/cf-darwin-amd64.tar.gz | tar zx
3636

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

pkg/store/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type Store struct {
5252
CFTokenSecretKey string
5353
ArgoCDTokenSecret string
5454
ArgoCDTokenKey string
55+
ArgoCDServerName string
5556
EventsReporterName string
5657
WorkflowReporterName string
5758
CodefreshSA string
@@ -87,6 +88,7 @@ func init() {
8788
s.CodefreshCM = "codefresh-cm"
8889
s.CFTokenSecretKey = "token"
8990
s.ArgoCDTokenSecret = "argocd-token"
91+
s.ArgoCDServerName = "argocd-server"
9092
s.ArgoCDTokenKey = "token"
9193
s.EventsReporterName = "events-reporter"
9294
s.WorkflowReporterName = "workflow-reporter"

0 commit comments

Comments
 (0)