Skip to content

Commit

Permalink
Support custom storage class flag during install (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Sucharevich authored Feb 14, 2019
1 parent 6e37e11 commit 660a9ec
Show file tree
Hide file tree
Showing 21 changed files with 601 additions and 309 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "venona",
"version": "0.15.1",
"version": "0.16.0",
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions venonactl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.PHONY: build
build: build-local

build-local:
sh ./hack/build.sh
152 changes: 152 additions & 0 deletions venonactl/cmd/cmdutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package cmd

import (
"fmt"
"os"
"os/user"
"path"
"strings"

"github.com/codefresh-io/go-sdk/pkg/codefresh"
sdkUtils "github.com/codefresh-io/go-sdk/pkg/utils"
"github.com/codefresh-io/venona/venonactl/pkg/certs"
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
"github.com/codefresh-io/venona/venonactl/pkg/store"
"github.com/sirupsen/logrus"
)

var (
version = "dev"
commit = "none"
date = "unknown"
// set to false by default, when running hack/build.sh will change to true
// to prevent version checking during development
localDevFlow = "false"

verbose bool

configPath string
cfAPIHost string
cfAPIToken string
cfContext string

kubeConfigPath string

skipVerionCheck bool
)

func buildBasicStore() {
s := store.GetStore()
s.Version = &store.Version{
Current: &store.CurrentVersion{
Version: version,
Commit: commit,
Date: date,
},
}

s.Image = &store.Image{
Name: "codefresh/venona",
}

s.Mode = store.ModeInCluster

s.ServerCert = &certs.ServerCert{}

s.AppName = store.ApplicationName

if skipVerionCheck || localDevFlow == "true" {
latestVersion := &store.LatestVersion{
Version: store.DefaultVersion,
IsDefault: true,
}
s.Version.Latest = latestVersion
logrus.WithFields(logrus.Fields{
"Default-Version": store.DefaultVersion,
"Image-Tag": s.Version.Current.Version,
}).Debug("Skipping version check")
} else {
latestVersion := &store.LatestVersion{
Version: store.GetLatestVersion(),
IsDefault: false,
}
s.Image.Tag = latestVersion.Version
s.Version.Latest = latestVersion
res, _ := store.IsRunningLatestVersion()
// the local version and the latest version not match
// make sure the command is no venonactl version
if !res {
logrus.WithFields(logrus.Fields{
"Local-Version": s.Version.Current.Version,
"Latest-Version": s.Version.Latest.Version,
}).Info("New version is avaliable, please update")
}
}
}

func extendStoreWithCodefershClient() error {
s := store.GetStore()
if configPath == "" {
configPath = fmt.Sprintf("%s/.cfconfig", os.Getenv("HOME"))
}

if cfAPIHost == "" && cfAPIToken == "" {
context, err := sdkUtils.ReadAuthContext(configPath, cfContext)
if err != nil {
return err
}
cfAPIHost = context.URL
cfAPIToken = context.Token

logrus.WithFields(logrus.Fields{
"Context-Name": context.Name,
"Codefresh-Host": cfAPIHost,
}).Debug("Using codefresh context")
} else {
logrus.Debug("Using creentials from environment variables")
}

client := codefresh.New(&codefresh.ClientOptions{
Auth: codefresh.AuthOptions{
Token: cfAPIToken,
},
Host: cfAPIHost,
})
s.CodefreshAPI = &store.CodefreshAPI{
Host: cfAPIHost,
Token: cfAPIToken,
Client: client,
}

return nil
}

func extendStoreWithKubeClient() {
s := store.GetStore()
if kubeConfigPath == "" {
currentUser, _ := user.Current()
if currentUser != nil {
kubeConfigPath = path.Join(currentUser.HomeDir, ".kube", "config")
logrus.WithFields(logrus.Fields{
"Kube-Config-Path": kubeConfigPath,
}).Debug("Path to kubeconfig not set, using default")
}
}

s.KubernetesAPI = &store.KubernetesAPI{
ConfigPath: kubeConfigPath,
}
}

func prepareLogger() {
if verbose == true {
logrus.SetLevel(logrus.DebugLevel)
}
}

func isUsingDefaultStorageClass(sc string) bool {
if sc == "" {
return true
}
return strings.HasPrefix(sc, runtimectl.DefaultStorageClassNamePrefix)
}
49 changes: 33 additions & 16 deletions venonactl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ type DeletionError struct {
name string
}

var (
var deleteCmdOptions struct {
kube struct {
inCluster bool
context string
}
revertTo string
)
}

// deleteCmd represents the status command
var deleteCmd = &cobra.Command{
Expand All @@ -50,27 +54,27 @@ var deleteCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
s := store.GetStore()
prepareLogger()
buildBasicStore()
extendStoreWithCodefershClient()
extendStoreWithKubeClient()
var errors []DeletionError
contextName := ""
kubeContextFlag := cmd.Flag("kube-context-name")
if kubeContextFlag != nil {
contextName = kubeContextFlag.Value.String()
}
s.KubernetesAPI.InCluster = inCluster
s.KubernetesAPI.InCluster = deleteCmdOptions.kube.inCluster
for _, name := range args {
re, err := s.CodefreshAPI.Client.RuntimeEnvironments().Get(name)
errors = collectError(errors, err, name, "Get Runtime-Environment from Codefresh")

if revertTo != "" {
_, err := s.CodefreshAPI.Client.RuntimeEnvironments().Default(revertTo)
errors = collectError(errors, err, name, fmt.Sprintf("Revert Runtime-Environment to: %s", revertTo))
if deleteCmdOptions.revertTo != "" {
_, err := s.CodefreshAPI.Client.RuntimeEnvironments().Default(deleteCmdOptions.revertTo)
errors = collectError(errors, err, name, fmt.Sprintf("Revert Runtime-Environment to: %s", deleteCmdOptions.revertTo))
}
deleted, err := s.CodefreshAPI.Client.RuntimeEnvironments().Delete(name)
errors = collectError(errors, err, name, "Delete Runtime-Environment from Codefresh")

if deleted {
if contextName == "" {
contextName = re.RuntimeScheduler.Cluster.ClusterProvider.Selector
contextName := re.RuntimeScheduler.Cluster.ClusterProvider.Selector
if contextName != "" {
contextName = deleteCmdOptions.kube.context
}
s.KubernetesAPI.ContextName = contextName
s.KubernetesAPI.Namespace = re.RuntimeScheduler.Cluster.Namespace
Expand All @@ -83,6 +87,18 @@ var deleteCmd = &cobra.Command{
})
continue
}
if isUsingDefaultStorageClass(re.RuntimeScheduler.Pvcs.Dind.StorageClassName) {
err = runtimectl.GetOperator(runtimectl.VolumeProvisionerOperatorType).Delete()
if err != nil {
errors = append(errors, DeletionError{
err: err,
name: name,
operation: "Delete volume provisioner related components",
})
continue
}
}

if re.Metadata.Agent {
err = runtimectl.GetOperator(runtimectl.VenonaOperatorType).Delete()
if err != nil {
Expand All @@ -94,6 +110,7 @@ var deleteCmd = &cobra.Command{
continue
}
}

logrus.Infof("Deleted %s", name)
}

Expand All @@ -113,9 +130,9 @@ var deleteCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(deleteCmd)
deleteCmd.Flags().String("kube-context-name", "", "Set name to overwrite the context name saved in Codefresh")
deleteCmd.Flags().StringVar(&revertTo, "revert-to", "", "Set to the name of the runtime-environment to set as default")
deleteCmd.Flags().BoolVar(&inCluster, "in-cluster", false, "Set flag if venona is been installed from inside a cluster")
deleteCmd.Flags().StringVar(&deleteCmdOptions.kube.context, "kube-context-name", "", "Set name to overwrite the context name saved in Codefresh")
deleteCmd.Flags().StringVar(&deleteCmdOptions.revertTo, "revert-to", "", "Set to the name of the runtime-environment to set as default")
deleteCmd.Flags().BoolVar(&deleteCmdOptions.kube.inCluster, "in-cluster", false, "Set flag if venona is been installed from inside a cluster")
}

func collectError(errors []DeletionError, err error, reName string, op string) []DeletionError {
Expand Down
Loading

0 comments on commit 660a9ec

Please sign in to comment.