Skip to content

Commit

Permalink
Cr 6174 - pre installation checks (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
roi-codefresh authored Aug 25, 2021
1 parent 0b8538e commit 5dddc4a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.77
VERSION=v0.0.78
OUT_DIR=dist
YEAR?=$(shell date +"%Y")

Expand Down
81 changes: 72 additions & 9 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -191,15 +192,8 @@ func NewRuntimeInstallCommand() *cobra.Command {
}

func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
if err != nil {
return err
}

for _, rt := range runtimes {
if rt.Metadata.Name == opts.RuntimeName {
return fmt.Errorf("failed to create runtime: %s. A runtime by this name already exists", opts.RuntimeName)
}
if err := preInstallationChecks(ctx, opts); err != nil {
return fmt.Errorf("pre installation checks failed: %w", err)
}

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

func preInstallationChecks(ctx context.Context, opts *RuntimeInstallOptions) error {
log.G(ctx).Debug("running pre-installation checks...")

if err := checkRuntimeCollisions(ctx, opts.RuntimeName, opts.KubeFactory); err != nil {
return fmt.Errorf("runtime collision check failed: %w", err)
}

if err := checkExistingRuntimes(ctx, opts.RuntimeName); err != nil {
return fmt.Errorf("existing runtime check failed: %w", err)
}

return nil
}

func checkRuntimeCollisions(ctx context.Context, runtime string, kube kube.Factory) error {
log.G(ctx).Debug("checking for argocd collisions in cluster")

cs, err := kube.KubernetesClientSet()
if err != nil {
return fmt.Errorf("failed to build kubernetes clientset: %w", err)
}

crb, err := cs.RbacV1().ClusterRoleBindings().Get(ctx, store.Get().ArgoCDServerName, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
return nil // no collision
}

return fmt.Errorf("failed to get cluster-role-binding '%s': %w", store.Get().ArgoCDServerName, err)
}

log.G(ctx).Debug("argocd cluster-role-binding found")

if len(crb.Subjects) == 0 {
return nil // no collision
}

subjNamespace := crb.Subjects[0].Namespace

// check if some argocd is actually using this crb
_, err = cs.AppsV1().Deployments(subjNamespace).Get(ctx, store.Get().ArgoCDServerName, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
log.G(ctx).Debug("argocd cluster-role-binding subject does not exist, no collision")

return nil // no collision
}

return fmt.Errorf("failed to get deployment '%s': %w", store.Get().ArgoCDServerName, err)
}

return fmt.Errorf("argo-cd is already installed on this cluster in namespace '%s', you need to uninstall it first", subjNamespace)
}

func checkExistingRuntimes(ctx context.Context, runtime string) error {
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
if err != nil {
return fmt.Errorf("failed to list runtimes: %w", err)
}

for _, rt := range runtimes {
if rt.Metadata.Name == runtime {
return fmt.Errorf("runtime '%s' already exists", runtime)
}
}

return nil
}

func intervalCheckIsRuntimePersisted(milliseconds int, ctx context.Context, runtimeName string, wg *sync.WaitGroup) {
interval := time.Duration(milliseconds) * time.Millisecond
ticker := time.NewTicker(interval)
Expand Down
4 changes: 2 additions & 2 deletions docs/releases/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cf version
### Linux
```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.77/cf-linux-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.78/cf-linux-amd64.tar.gz | tar zx

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

# move the binary to your $PATH
mv ./cf-darwin-amd64 /usr/local/bin/cf
Expand Down
2 changes: 2 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Store struct {
CFTokenSecretKey string
ArgoCDTokenSecret string
ArgoCDTokenKey string
ArgoCDServerName string
EventsReporterName string
WorkflowReporterName string
CodefreshSA string
Expand Down Expand Up @@ -87,6 +88,7 @@ func init() {
s.CodefreshCM = "codefresh-cm"
s.CFTokenSecretKey = "token"
s.ArgoCDTokenSecret = "argocd-token"
s.ArgoCDServerName = "argocd-server"
s.ArgoCDTokenKey = "token"
s.EventsReporterName = "events-reporter"
s.WorkflowReporterName = "workflow-reporter"
Expand Down

0 comments on commit 5dddc4a

Please sign in to comment.