Skip to content

Commit 660a9ec

Browse files
author
Oleg Sucharevich
authored
Support custom storage class flag during install (#28)
1 parent 6e37e11 commit 660a9ec

21 files changed

+601
-309
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "venona",
3-
"version": "0.15.1",
3+
"version": "0.16.0",
44
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
55
"main": "index.js",
66
"scripts": {

venonactl/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.PHONY: build
2+
build: build-local
3+
4+
build-local:
5+
sh ./hack/build.sh

venonactl/cmd/cmdutils.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/user"
7+
"path"
8+
"strings"
9+
10+
"github.com/codefresh-io/go-sdk/pkg/codefresh"
11+
sdkUtils "github.com/codefresh-io/go-sdk/pkg/utils"
12+
"github.com/codefresh-io/venona/venonactl/pkg/certs"
13+
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
14+
"github.com/codefresh-io/venona/venonactl/pkg/store"
15+
"github.com/sirupsen/logrus"
16+
)
17+
18+
var (
19+
version = "dev"
20+
commit = "none"
21+
date = "unknown"
22+
// set to false by default, when running hack/build.sh will change to true
23+
// to prevent version checking during development
24+
localDevFlow = "false"
25+
26+
verbose bool
27+
28+
configPath string
29+
cfAPIHost string
30+
cfAPIToken string
31+
cfContext string
32+
33+
kubeConfigPath string
34+
35+
skipVerionCheck bool
36+
)
37+
38+
func buildBasicStore() {
39+
s := store.GetStore()
40+
s.Version = &store.Version{
41+
Current: &store.CurrentVersion{
42+
Version: version,
43+
Commit: commit,
44+
Date: date,
45+
},
46+
}
47+
48+
s.Image = &store.Image{
49+
Name: "codefresh/venona",
50+
}
51+
52+
s.Mode = store.ModeInCluster
53+
54+
s.ServerCert = &certs.ServerCert{}
55+
56+
s.AppName = store.ApplicationName
57+
58+
if skipVerionCheck || localDevFlow == "true" {
59+
latestVersion := &store.LatestVersion{
60+
Version: store.DefaultVersion,
61+
IsDefault: true,
62+
}
63+
s.Version.Latest = latestVersion
64+
logrus.WithFields(logrus.Fields{
65+
"Default-Version": store.DefaultVersion,
66+
"Image-Tag": s.Version.Current.Version,
67+
}).Debug("Skipping version check")
68+
} else {
69+
latestVersion := &store.LatestVersion{
70+
Version: store.GetLatestVersion(),
71+
IsDefault: false,
72+
}
73+
s.Image.Tag = latestVersion.Version
74+
s.Version.Latest = latestVersion
75+
res, _ := store.IsRunningLatestVersion()
76+
// the local version and the latest version not match
77+
// make sure the command is no venonactl version
78+
if !res {
79+
logrus.WithFields(logrus.Fields{
80+
"Local-Version": s.Version.Current.Version,
81+
"Latest-Version": s.Version.Latest.Version,
82+
}).Info("New version is avaliable, please update")
83+
}
84+
}
85+
}
86+
87+
func extendStoreWithCodefershClient() error {
88+
s := store.GetStore()
89+
if configPath == "" {
90+
configPath = fmt.Sprintf("%s/.cfconfig", os.Getenv("HOME"))
91+
}
92+
93+
if cfAPIHost == "" && cfAPIToken == "" {
94+
context, err := sdkUtils.ReadAuthContext(configPath, cfContext)
95+
if err != nil {
96+
return err
97+
}
98+
cfAPIHost = context.URL
99+
cfAPIToken = context.Token
100+
101+
logrus.WithFields(logrus.Fields{
102+
"Context-Name": context.Name,
103+
"Codefresh-Host": cfAPIHost,
104+
}).Debug("Using codefresh context")
105+
} else {
106+
logrus.Debug("Using creentials from environment variables")
107+
}
108+
109+
client := codefresh.New(&codefresh.ClientOptions{
110+
Auth: codefresh.AuthOptions{
111+
Token: cfAPIToken,
112+
},
113+
Host: cfAPIHost,
114+
})
115+
s.CodefreshAPI = &store.CodefreshAPI{
116+
Host: cfAPIHost,
117+
Token: cfAPIToken,
118+
Client: client,
119+
}
120+
121+
return nil
122+
}
123+
124+
func extendStoreWithKubeClient() {
125+
s := store.GetStore()
126+
if kubeConfigPath == "" {
127+
currentUser, _ := user.Current()
128+
if currentUser != nil {
129+
kubeConfigPath = path.Join(currentUser.HomeDir, ".kube", "config")
130+
logrus.WithFields(logrus.Fields{
131+
"Kube-Config-Path": kubeConfigPath,
132+
}).Debug("Path to kubeconfig not set, using default")
133+
}
134+
}
135+
136+
s.KubernetesAPI = &store.KubernetesAPI{
137+
ConfigPath: kubeConfigPath,
138+
}
139+
}
140+
141+
func prepareLogger() {
142+
if verbose == true {
143+
logrus.SetLevel(logrus.DebugLevel)
144+
}
145+
}
146+
147+
func isUsingDefaultStorageClass(sc string) bool {
148+
if sc == "" {
149+
return true
150+
}
151+
return strings.HasPrefix(sc, runtimectl.DefaultStorageClassNamePrefix)
152+
}

venonactl/cmd/delete.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ type DeletionError struct {
3434
name string
3535
}
3636

37-
var (
37+
var deleteCmdOptions struct {
38+
kube struct {
39+
inCluster bool
40+
context string
41+
}
3842
revertTo string
39-
)
43+
}
4044

4145
// deleteCmd represents the status command
4246
var deleteCmd = &cobra.Command{
@@ -50,27 +54,27 @@ var deleteCmd = &cobra.Command{
5054
},
5155
Run: func(cmd *cobra.Command, args []string) {
5256
s := store.GetStore()
57+
prepareLogger()
58+
buildBasicStore()
59+
extendStoreWithCodefershClient()
60+
extendStoreWithKubeClient()
5361
var errors []DeletionError
54-
contextName := ""
55-
kubeContextFlag := cmd.Flag("kube-context-name")
56-
if kubeContextFlag != nil {
57-
contextName = kubeContextFlag.Value.String()
58-
}
59-
s.KubernetesAPI.InCluster = inCluster
62+
s.KubernetesAPI.InCluster = deleteCmdOptions.kube.inCluster
6063
for _, name := range args {
6164
re, err := s.CodefreshAPI.Client.RuntimeEnvironments().Get(name)
6265
errors = collectError(errors, err, name, "Get Runtime-Environment from Codefresh")
6366

64-
if revertTo != "" {
65-
_, err := s.CodefreshAPI.Client.RuntimeEnvironments().Default(revertTo)
66-
errors = collectError(errors, err, name, fmt.Sprintf("Revert Runtime-Environment to: %s", revertTo))
67+
if deleteCmdOptions.revertTo != "" {
68+
_, err := s.CodefreshAPI.Client.RuntimeEnvironments().Default(deleteCmdOptions.revertTo)
69+
errors = collectError(errors, err, name, fmt.Sprintf("Revert Runtime-Environment to: %s", deleteCmdOptions.revertTo))
6770
}
6871
deleted, err := s.CodefreshAPI.Client.RuntimeEnvironments().Delete(name)
6972
errors = collectError(errors, err, name, "Delete Runtime-Environment from Codefresh")
7073

7174
if deleted {
72-
if contextName == "" {
73-
contextName = re.RuntimeScheduler.Cluster.ClusterProvider.Selector
75+
contextName := re.RuntimeScheduler.Cluster.ClusterProvider.Selector
76+
if contextName != "" {
77+
contextName = deleteCmdOptions.kube.context
7478
}
7579
s.KubernetesAPI.ContextName = contextName
7680
s.KubernetesAPI.Namespace = re.RuntimeScheduler.Cluster.Namespace
@@ -83,6 +87,18 @@ var deleteCmd = &cobra.Command{
8387
})
8488
continue
8589
}
90+
if isUsingDefaultStorageClass(re.RuntimeScheduler.Pvcs.Dind.StorageClassName) {
91+
err = runtimectl.GetOperator(runtimectl.VolumeProvisionerOperatorType).Delete()
92+
if err != nil {
93+
errors = append(errors, DeletionError{
94+
err: err,
95+
name: name,
96+
operation: "Delete volume provisioner related components",
97+
})
98+
continue
99+
}
100+
}
101+
86102
if re.Metadata.Agent {
87103
err = runtimectl.GetOperator(runtimectl.VenonaOperatorType).Delete()
88104
if err != nil {
@@ -94,6 +110,7 @@ var deleteCmd = &cobra.Command{
94110
continue
95111
}
96112
}
113+
97114
logrus.Infof("Deleted %s", name)
98115
}
99116

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

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

121138
func collectError(errors []DeletionError, err error, reName string, op string) []DeletionError {

0 commit comments

Comments
 (0)