-
Notifications
You must be signed in to change notification settings - Fork 785
/
edit_deploy.go
181 lines (155 loc) · 4.69 KB
/
edit_deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cmd
import (
"fmt"
"strings"
v1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/pkg/jx/cmd/opts"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
)
const (
// DeployKindKnative for knative serve based deployments
DeployKindKnative = "knative"
// DeployKindDefault for default kubernetes Deployment + Service deployment kinds
DeployKindDefault = "default"
)
var (
editDeployKindLong = templates.LongDesc(`
Edits the deploy kind to use for your project or team
`)
editDeployKindExample = templates.Examples(`
# Edit the deploy kind for your current project and prompts you to pick one of the available kinds
jx edit deploy
# to switch to use Knative Serve deployments
jx edit deploy knative
# to switch to normal kubernetes deployments
jx edit deploy default
# Edit the default deploy kind for your team
jx edit deploy --team
# Set the default for your team to use knative
jx edit deploy --team knative
`)
deployKinds = []string{DeployKindKnative, DeployKindDefault}
knativeDeployKey = "knativeDeploy:"
)
// EditDeployKindOptions the options for the create spring command
type EditDeployKindOptions struct {
EditOptions
Kind string
Dir string
Team bool
}
// NewCmdEditDeployKind creates a command object for the "create" command
func NewCmdEditDeployKind(commonOpts *opts.CommonOptions) *cobra.Command {
options := &EditDeployKindOptions{
EditOptions: EditOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "deploy",
Short: "Edits the deploy kind to use for your project or team",
Long: editDeployKindLong,
Example: editDeployKindExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Team, "team", "t", false, "Edits the team default")
cmd.Flags().StringVarP(&options.Kind, "kind", "k", "", fmt.Sprintf("The kind to use which should be one of: %s", strings.Join(deployKinds, ", ")))
return cmd
}
// Run implements the command
func (o *EditDeployKindOptions) Run() error {
settings, err := o.TeamSettings()
if err != nil {
return err
}
if o.Team {
name, err := o.pickDeployKind(settings.DeployKind)
if err != nil {
return err
}
callback := func(env *v1.Environment) error {
teamSettings := &env.Spec.TeamSettings
teamSettings.DeployKind = name
log.Infof("Setting the team deploy kind to: %s\n", util.ColorInfo(name))
return nil
}
return o.ModifyDevEnvironment(callback)
}
fn := func(text string) (string, error) {
defaultName := o.findDefaultDeployKindInValuesYaml(text)
name := ""
name, err := o.pickDeployKind(defaultName)
if err != nil {
return name, err
}
return o.setDeployKindInValuesYaml(text, name)
}
return o.ModifyHelmValuesFile(o.Dir, fn)
}
func (o *EditDeployKindOptions) findDefaultDeployKindInValuesYaml(yamlText string) string {
// lets try find the current setting
knativeFlag := ""
lines := strings.Split(yamlText, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, knativeDeployKey) {
knativeFlag = strings.TrimSpace(line[len(knativeDeployKey):])
break
}
}
if knativeFlag == "true" {
return DeployKindKnative
}
return DeployKindDefault
}
// setDeployKindInValuesYaml sets the `knativeDeployKey` key to true or false based on the deployment kind
func (o *EditDeployKindOptions) setDeployKindInValuesYaml(yamlText string, deployKind string) (string, error) {
var buffer strings.Builder
lines := strings.Split(yamlText, "\n")
for _, line := range lines {
if strings.HasPrefix(line, knativeDeployKey) {
buffer.WriteString(knativeDeployKey)
buffer.WriteString(" ")
if deployKind == DeployKindKnative {
buffer.WriteString("true")
} else {
buffer.WriteString("false")
}
} else {
buffer.WriteString(line)
}
buffer.WriteString("\n")
}
return buffer.String(), nil
}
func (o *EditDeployKindOptions) pickDeployKind(defaultName string) (string, error) {
if o.Kind != "" {
return o.Kind, nil
}
args := o.Args
if len(args) > 0 {
return args[0], nil
}
if o.BatchMode {
return "", util.MissingArgument("kind")
}
if defaultName == "" || util.StringArrayIndex(deployKinds, defaultName) < 0 {
defaultName = "default"
}
name, err := util.PickNameWithDefault(deployKinds, "Pick the deployment kind: ", defaultName, "lets you switch between knative serve based deployments and default kubernetes deployments", o.In, o.Out, o.Err)
if err != nil {
return name, err
}
if name == "" {
return name, fmt.Errorf("no kind chosen")
}
return name, nil
}