Skip to content

Commit 9d2ef10

Browse files
Saas 8261 --values param (#174)
* added --values, --set-value, --set-file to all * added debugging from codefresh-cli * values -tst1 * rewrite cmd-template-values * --values for uninstall and upgrade * debug with cli-1 * README --values * fix debug on windows * wip * rename * add windows 386 * fixed-windows build * fixed-windows build * fixed venona-ci go build old dependencies Co-authored-by: Oren Gurfinkel <[email protected]>
1 parent f606efa commit 9d2ef10

25 files changed

+511
-390
lines changed

venona/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.19
1+
1.4.20

venona/go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ module github.com/codefresh-io/go/venona
33
go 1.14
44

55
require (
6-
github.com/codefresh-io/venona/venonactl v0.0.0-20200628143411-3135d91be7ee // indirect
6+
github.com/evanphx/json-patch v4.5.0+incompatible // indirect
77
github.com/gin-gonic/gin v1.6.3
88
github.com/go-playground/validator/v10 v10.3.0 // indirect
99
github.com/golang/protobuf v1.4.2 // indirect
1010
github.com/inconshreveable/log15 v0.0.0-20200109203555-b30bc20e4fd1
1111
github.com/json-iterator/go v1.1.10 // indirect
12+
github.com/mattn/go-colorable v0.1.6 // indirect
13+
github.com/pkg/errors v0.9.1 // indirect
1214
github.com/spf13/cobra v1.0.0
1315
github.com/spf13/pflag v1.0.5
1416
github.com/spf13/viper v1.7.0
17+
github.com/stretchr/objx v0.2.0 // indirect
1518
github.com/stretchr/testify v1.5.1
19+
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d // indirect
1620
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
1721
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
1822
google.golang.org/protobuf v1.24.0 // indirect
23+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1924
gopkg.in/yaml.v2 v2.3.0
2025
k8s.io/api v0.18.3
2126
k8s.io/apimachinery v0.18.3

venona/go.sum

Lines changed: 0 additions & 327 deletions
Large diffs are not rendered by default.

venonactl/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ rules:
9898
verbs:
9999
- get
100100
```
101+
#### Install using --values <values.yaml>
102+
`codefresh runner init --values <values.yaml> [parameters]`
103+
the values from values.yaml are applied to the templates in [pkg/templates/kubernetes](pkg/templates/kubernetes)
104+
105+
See BuildValues() func in [store.go](pkg/store/store.go) for the format
106+
Example with explaination is in [values-example.yaml](values-example.yaml)
101107

102108
#### Install on GCP
103109
* Make sure your user has `Kubernetes Engine Cluster Admin` role in google console

venonactl/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.19
1+
1.4.20

venonactl/cmd/attach.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,33 @@ var attachRuntimeCmdOptions struct {
4040
context string
4141
}
4242
restartAgent bool
43+
templateValues []string
44+
templateFileValues []string
45+
templateValueFiles []string
4346
}
4447

4548
var attachRuntimeCmd = &cobra.Command{
4649
Use: "attach",
4750
Short: "Attach Codefresh runtime to agent",
4851
Run: func(cmd *cobra.Command, args []string) {
52+
// get valuesMap from --values <values.yaml> --set-value k=v --set-file k=<context-of file>
53+
templateValuesMap, err := templateValuesToMap(
54+
attachRuntimeCmdOptions.templateValueFiles,
55+
attachRuntimeCmdOptions.templateValues,
56+
attachRuntimeCmdOptions.templateFileValues)
57+
if err != nil {
58+
dieOnError(err)
59+
}
60+
// Merge cmd options with template
61+
mergeValueStr(templateValuesMap, "ConfigPath", &kubeConfigPath)
62+
mergeValueStr(templateValuesMap, "ConfigPath", &attachRuntimeCmdOptions.kube.kubePath)
63+
mergeValueStr(templateValuesMap, "CodefreshHost", &cfAPIHost)
64+
mergeValueStr(templateValuesMap, "Token", &cfAPIToken)
65+
66+
mergeValueStr(templateValuesMap, "Namespace", &attachRuntimeCmdOptions.kube.namespace)
67+
mergeValueStr(templateValuesMap, "Context", &attachRuntimeCmdOptions.kube.context)
68+
mergeValueStr(templateValuesMap, "RuntimeEnvironmentName", &attachRuntimeCmdOptions.runtimeEnvironmentName)
69+
mergeValueStr(templateValuesMap, "RuntimeServiceAccount", &attachRuntimeCmdOptions.kube.serviceAccount)
4970

5071
s := store.GetStore()
5172
lgr := createLogger("Attach-runtime", verbose, logFormatter)
@@ -96,8 +117,8 @@ var attachRuntimeCmd = &cobra.Command{
96117

97118
builder.Add(plugins.RuntimeAttachType)
98119

99-
var err error
100120
values := s.BuildValues()
121+
values = mergeMaps(values, templateValuesMap)
101122
spn := createSpinner("Attaching runtime to agent (might take a few seconds)", "")
102123
spn.Start()
103124
for _, p := range builder.Get() {
@@ -129,4 +150,8 @@ func init() {
129150
attachRuntimeCmd.Flags().StringVar(&attachRuntimeCmdOptions.kubeVenona.kubePath, "kube-config-path-agent", viper.GetString("kubeconfig-agent"), "Path to kubeconfig file (default is $HOME/.kube/config) for agent [$KUBECONFIG]")
130151
attachRuntimeCmd.Flags().BoolVar(&attachRuntimeCmdOptions.restartAgent, "restart-agent", viper.GetBool("restart-agent"), "Restart agent after attach operation")
131152

153+
attachRuntimeCmd.Flags().StringArrayVar(&attachRuntimeCmdOptions.templateValues, "set-value", []string{}, "Set values for templates --set-value agentId=12345")
154+
attachRuntimeCmd.Flags().StringArrayVar(&attachRuntimeCmdOptions.templateFileValues, "set-file", []string{}, "Set values for templates from file")
155+
attachRuntimeCmd.Flags().StringArrayVarP(&attachRuntimeCmdOptions.templateValueFiles, "values", "f", []string{}, "specify values in a YAML file")
156+
132157
}

venonactl/cmd/cmdutils.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmd
22

33
import (
4-
"encoding/base64"
4+
55
"fmt"
66
"io/ioutil"
77
"os"
@@ -25,7 +25,9 @@ import (
2525
k8sApi "k8s.io/api/core/v1"
2626
"k8s.io/client-go/tools/clientcmd"
2727

28-
"helm.sh/helm/v3/pkg/strvals"
28+
cliValues "helm.sh/helm/v3/pkg/cli/values"
29+
"helm.sh/helm/v3/pkg/getter"
30+
"github.com/stretchr/objx"
2931
)
3032

3133
var (
@@ -220,38 +222,37 @@ func extendStoreWithAgentAPI(logger logger.Logger, token string, agentID string)
220222
s := store.GetStore()
221223
logger.Debug("Using agent's token", "Token", token)
222224
s.AgentAPI = &store.AgentAPI{
223-
Token: base64.StdEncoding.EncodeToString([]byte(token)),
225+
Token: token,
224226
Id: agentID,
225227
}
226228
}
227229

228230
// Parsing helpers --set-value , --set-file
229231
// by https://github.com/helm/helm/blob/ec1d1a3d3eb672232f896f9d3b3d0797e4f519e3/pkg/cli/values/options.go#L41
230232

231-
// parses --set-value options
232-
func parseSetValues(setValuesOpts []string) (map[string]interface{}, error) {
233-
base := map[string]interface{}{}
234-
for _, value := range setValuesOpts {
235-
if err := strvals.ParseInto(value, base); err != nil {
236-
return nil, fmt.Errorf("Cannot parse option --set-value %s", value)
233+
// templateValuesToMap - processes cmd --values <values-file.yaml> --set-value k=v --set-file v=<context-of-file>
234+
// using helm libraries
235+
func templateValuesToMap(templateValueFiles, templateValues, templateFileValues []string) (map[string]interface{}, error) {
236+
valueOpts := &cliValues.Options{}
237+
if len(templateValueFiles) > 0 {
238+
for _, v := range(templateValueFiles) {
239+
valueOpts.ValueFiles = append(valueOpts.ValueFiles, v)
237240
}
238241
}
239-
return base, nil
240-
}
241-
242-
// parses --set-file options
243-
func parseSetFiles(setFilesOpts []string) (map[string]interface{}, error) {
244-
base := map[string]interface{}{}
245-
for _, value := range setFilesOpts {
246-
reader := func(rs []rune) (interface{}, error) {
247-
bytes, err := ioutil.ReadFile(string(rs))
248-
return string(bytes), err
249-
}
250-
if err := strvals.ParseIntoFile(value, base, reader); err != nil {
251-
return nil, fmt.Errorf("Cannot parse option --set-file %s", value)
242+
243+
if len(templateValues) > 0 {
244+
for _, v := range(templateValues) {
245+
valueOpts.Values = append(valueOpts.Values, v)
246+
}
247+
}
248+
249+
if len(templateFileValues) > 0 {
250+
for _, v := range(templateFileValues) {
251+
valueOpts.FileValues = append(valueOpts.FileValues, v)
252252
}
253253
}
254-
return base, nil
254+
valuesMap, err := valueOpts.MergeValues(getter.Providers{})
255+
return valuesMap, err
255256
}
256257

257258
func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
@@ -272,3 +273,25 @@ func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
272273
}
273274
return out
274275
}
276+
277+
// mergeValueStr - for merging cli parameters with mapped parameters
278+
func mergeValueStr(valuesMap map[string]interface{}, key string, param *string) {
279+
mapX := objx.New(valuesMap)
280+
if param != nil && *param != "" {
281+
mapX.Set(key, *param)
282+
return
283+
}
284+
val := mapX.Get(key).String()
285+
*param = val
286+
}
287+
288+
// mergeValueBool - for merging cli parameters with mapped parameters
289+
func mergeValueBool(valuesMap map[string]interface{}, key string, param *bool) {
290+
mapX := objx.New(valuesMap)
291+
if param != nil ||*param == true {
292+
mapX.Set(key, *param)
293+
return
294+
}
295+
val := mapX.Get(key).Bool()
296+
*param = val
297+
}

venonactl/cmd/install-agent.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"github.com/codefresh-io/venona/venonactl/pkg/store"
2626
"github.com/spf13/cobra"
2727
"github.com/spf13/viper"
28+
// cliValues "helm.sh/helm/v3/pkg/cli/values"
29+
// "helm.sh/helm/v3/pkg/getter"
2830
)
2931

3032
var installAgentCmdOptions struct {
@@ -44,15 +46,47 @@ var installAgentCmdOptions struct {
4446
tolerations string
4547
envVars []string
4648
dockerRegistry string
49+
templateValues []string
50+
templateFileValues []string
51+
templateValueFiles []string
4752
}
4853

4954
var installAgentCmd = &cobra.Command{
5055
Use: "agent",
5156
Short: "Install Codefresh's agent ",
5257
Run: func(cmd *cobra.Command, args []string) {
58+
59+
// get valuesMap from --values <values.yaml> --set-value k=v --set-file k=<context-of file>
60+
templateValuesMap, err := templateValuesToMap(
61+
installAgentCmdOptions.templateValueFiles,
62+
installAgentCmdOptions.templateValues,
63+
installAgentCmdOptions.templateFileValues)
64+
if err != nil {
65+
dieOnError(err)
66+
}
67+
// Merge cmd options with template
68+
mergeValueStr(templateValuesMap, "ConfigPath", &kubeConfigPath)
69+
mergeValueStr(templateValuesMap, "CodefreshHost", &cfAPIHost)
70+
mergeValueStr(templateValuesMap, "Token", &cfAPIToken)
71+
mergeValueStr(templateValuesMap, "Namespace", &installAgentCmdOptions.kube.namespace)
72+
mergeValueStr(templateValuesMap, "Context", &installAgentCmdOptions.kube.context)
73+
mergeValueStr(templateValuesMap, "NodeSelector", &installAgentCmdOptions.kube.nodeSelector)
74+
mergeValueStr(templateValuesMap, "Tolerations", &installAgentCmdOptions.tolerations)
75+
mergeValueStr(templateValuesMap, "DockerRegistry", &installAgentCmdOptions.dockerRegistry)
76+
77+
mergeValueStr(templateValuesMap, "AgentToken", &installAgentCmdOptions.agentToken)
78+
mergeValueStr(templateValuesMap, "AgentId", &installAgentCmdOptions.agentID)
79+
mergeValueStr(templateValuesMap, "Image.Tag", &installAgentCmdOptions.venona.version)
80+
81+
//mergeValueStrArray(&installAgentCmdOptions.envVars, "envVars", nil, "More env vars to be declared \"key=value\"")
82+
83+
mergeValueBool(templateValuesMap, "InCluster", &installAgentCmdOptions.kube.inCluster)
84+
mergeValueBool(templateValuesMap, "kubernetesRunnerType", &installAgentCmdOptions.kubernetesRunnerType)
85+
5386
s := store.GetStore()
5487
lgr := createLogger("Install-agent", verbose, logFormatter)
5588
buildBasicStore(lgr)
89+
5690
extendStoreWithAgentAPI(lgr, installAgentCmdOptions.agentToken, installAgentCmdOptions.agentID)
5791
extendStoreWithKubeClient(lgr)
5892
fillCodefreshAPI(lgr)
@@ -64,6 +98,9 @@ var installAgentCmd = &cobra.Command{
6498
CodefreshHost: cfAPIHost,
6599
}
66100

101+
if installAgentCmdOptions.agentToken == "" {
102+
installAgentCmdOptions.agentToken = cfAPIToken
103+
}
67104
if installAgentCmdOptions.agentToken == "" {
68105
dieOnError(fmt.Errorf("Agent token is required in order to install agent"))
69106
}
@@ -115,7 +152,8 @@ var installAgentCmd = &cobra.Command{
115152
builder.Add(plugins.VenonaPluginType)
116153

117154
values := s.BuildValues()
118-
var err error
155+
values = mergeMaps(values, templateValuesMap)
156+
119157
for _, p := range builder.Get() {
120158
values, err = p.Install(builderInstallOpt, values)
121159
if err != nil {
@@ -144,6 +182,11 @@ func init() {
144182
installAgentCmd.Flags().BoolVar(&installAgentCmdOptions.kube.inCluster, "in-cluster", false, "Set flag if venona is been installed from inside a cluster")
145183
installAgentCmd.Flags().BoolVar(&installAgentCmdOptions.dryRun, "dry-run", false, "Set to true to simulate installation")
146184
installAgentCmd.Flags().BoolVar(&installAgentCmdOptions.kubernetesRunnerType, "kubernetes-runner-type", false, "Set the runner type to kubernetes (alpha feature)")
185+
186+
installAgentCmd.Flags().StringArrayVar(&installAgentCmdOptions.templateValues, "set-value", []string{}, "Set values for templates --set-value agentId=12345")
187+
installAgentCmd.Flags().StringArrayVar(&installAgentCmdOptions.templateFileValues, "set-file", []string{}, "Set values for templates from file")
188+
installAgentCmd.Flags().StringArrayVarP(&installAgentCmdOptions.templateValueFiles, "values", "f", []string{}, "specify values in a YAML file")
189+
147190
}
148191

149192
func fillCodefreshAPI(logger logger.Logger) {

venonactl/cmd/install-monitor.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/codefresh-io/venona/venonactl/pkg/store"
2424
"github.com/spf13/cobra"
2525
"github.com/spf13/viper"
26+
2627
)
2728

2829
var installMonitorAgentCmdOptions struct {
@@ -36,13 +37,36 @@ var installMonitorAgentCmdOptions struct {
3637
codefreshToken string
3738
codefreshHost string
3839
dockerRegistry string
40+
templateValues []string
41+
templateFileValues []string
42+
templateValueFiles []string
3943
}
4044

4145
// installK8sAgentCmd represents the install command
4246
var installMonitorAgentCmd = &cobra.Command{
4347
Use: "monitor",
4448
Short: "Install Codefresh's monitor agent on cluster",
4549
Run: func(cmd *cobra.Command, args []string) {
50+
// get valuesMap from --values <values.yaml> --set-value k=v --set-file k=<context-of file>
51+
templateValuesMap, err := templateValuesToMap(
52+
installMonitorAgentCmdOptions.templateValueFiles,
53+
installMonitorAgentCmdOptions.templateValues,
54+
installMonitorAgentCmdOptions.templateFileValues)
55+
if err != nil {
56+
dieOnError(err)
57+
}
58+
// Merge cmd options with template
59+
mergeValueStr(templateValuesMap, "ConfigPath", &kubeConfigPath)
60+
mergeValueStr(templateValuesMap, "CodefreshHost", &cfAPIHost)
61+
mergeValueStr(templateValuesMap, "CodefreshHost", &installMonitorAgentCmdOptions.codefreshHost)
62+
mergeValueStr(templateValuesMap, "Token", &cfAPIToken)
63+
mergeValueStr(templateValuesMap, "Token", &installMonitorAgentCmdOptions.codefreshToken)
64+
mergeValueStr(templateValuesMap, "Namespace", &installMonitorAgentCmdOptions.kube.namespace)
65+
mergeValueStr(templateValuesMap, "Context", &installMonitorAgentCmdOptions.kube.context)
66+
67+
mergeValueStr(templateValuesMap, "DockerRegistry", &installMonitorAgentCmdOptions.dockerRegistry)
68+
mergeValueStr(templateValuesMap, "ClusterId", &installMonitorAgentCmdOptions.clusterId)
69+
mergeValueBool(templateValuesMap, "helm3", &installMonitorAgentCmdOptions.helm3)
4670

4771
s := store.GetStore()
4872

@@ -88,6 +112,7 @@ var installMonitorAgentCmd = &cobra.Command{
88112
}
89113

90114
values := s.BuildValues()
115+
values = mergeMaps(values, templateValuesMap)
91116

92117
for _, p := range builder.Get() {
93118
_, err := p.Install(builderInstallOpt, values)
@@ -111,5 +136,8 @@ func init() {
111136
installMonitorAgentCmd.Flags().StringVar(&installMonitorAgentCmdOptions.codefreshHost, "codefreshHost", "", "Override codefresh host if you use your own codefresh installation")
112137

113138
installMonitorAgentCmd.Flags().BoolVar(&installMonitorAgentCmdOptions.helm3, "helm3", false, "Set flag if cluster use helm3")
139+
installMonitorAgentCmd.Flags().StringArrayVar(&installMonitorAgentCmdOptions.templateValues, "set-value", []string{}, "Set values for templates, example: --set-value LocalVolumesDir=/mnt/disks/ssd0/codefresh-volumes")
140+
installMonitorAgentCmd.Flags().StringArrayVar(&installMonitorAgentCmdOptions.templateFileValues, "set-file", []string{}, "Set values for templates from file, example: --set-file Storage.GoogleServiceAccount=/path/to/service-account.json")
141+
installMonitorAgentCmd.Flags().StringArrayVarP(&installMonitorAgentCmdOptions.templateValueFiles, "values", "f", []string{}, "specify values in a YAML file")
114142

115143
}

0 commit comments

Comments
 (0)