-
Notifications
You must be signed in to change notification settings - Fork 50
/
versionstream.go
232 lines (191 loc) · 6.64 KB
/
versionstream.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package versionstream
import (
"fmt"
"path/filepath"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/cli"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"sigs.k8s.io/kustomize/kyaml/yaml"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/pkg/errors"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/spf13/cobra"
)
var (
createLong = templates.LongDesc(`
Administer the cluster version stream settings
`)
createExample = templates.Examples(`
# switch to LTS (stable) version stream
%s versionstream --lts
# switch to latest version stream
%s versionstream --latest
# switch to a custom version stream
%s versionstream --custom --url https://github.com/foo/bar.git --ref main
`)
)
const (
ltsVersionStreamURL = "https://github.com/jenkins-x/jx3-lts-versions"
latestVersionStreamURL = "https://github.com/jenkins-x/jxr-versions"
)
// Options the options for creating a repository
type Options struct {
LTS bool
Latest bool
Custom bool
DoGitCommit bool
GitURL string
GitRef string
GitDir string
Cmd *cobra.Command
Args []string
Dir string
Gitter gitclient.Interface
}
// NewCmdVersionstream administer the cluster version stream
func NewCmdVersionstream() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "versionstream",
Short: "Administer the cluster version stream settings",
Long: createLong,
Example: fmt.Sprintf(createExample, rootcmd.BinaryName, rootcmd.BinaryName, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
o.Cmd = cmd
o.Args = args
err := o.Run()
helper.CheckErr(err)
},
}
o.Cmd = cmd
cmd.Flags().BoolVarP(&o.LTS, "lts", "", false, "Switch the cluster version stream to the LTS (long term support on monthly release cadence) git repo, https://github.com/jenkins-x/jx3-lts-versions")
cmd.Flags().BoolVarP(&o.Latest, "latest", "", false, "Switch the cluster version stream to the latest (latest releases) git repo, https://github.com/jenkins-x/jxr-versions")
cmd.Flags().BoolVarP(&o.Custom, "custom", "", false, "Switch the cluster version stream to a custom version stream, requires url and ref flags set")
cmd.Flags().StringVarP(&o.GitURL, "url", "", "", "The git URL to clone to fetch the initial set of files for a helm 3 / helmfile based git configuration if this command is not run inside a git clone or against a GitOps based cluster")
cmd.Flags().StringVarP(&o.GitRef, "ref", "", "", "The kind of git server for the development environment")
cmd.Flags().StringVarP(&o.GitDir, "directory", "", "/", "The directory used in the versionstream, defaults to root")
return cmd, o
}
// Run implements the command
func (o *Options) Run() error {
err := o.switchVersionStream()
if err != nil {
return errors.Wrapf(err, "failed to switch versionstream")
}
err = o.GitCommit()
if err != nil {
return errors.Wrapf(err, "failed to commit updated Kptfile")
}
log.Logger().Infof("your version stream has been switched, please now run")
log.Logger().Infof("%s", termcolor.ColorInfo("jx upgrade cli"))
log.Logger().Infof("%s", termcolor.ColorInfo("jx gitops upgrade"))
return nil
}
// Git returns the gitter - lazily creating one if required
func (o *Options) Git() gitclient.Interface {
if o.Gitter == nil {
o.Gitter = cli.NewCLIClient("", cmdrunner.DefaultCommandRunner)
}
return o.Gitter
}
func (o *Options) switchVersionStream() error {
if !o.atLeastOneFlagSet() {
return errors.New("select at least one flag, lts, latest or custom")
}
if o.moreThanOneFlagSet() {
return errors.New("select only one flag, lts, latest or custom")
}
if o.Custom {
if o.GitURL == "" || o.GitRef == "" {
return errors.New("url and ref flags must be set if using custom")
}
}
if o.Dir == "" {
o.Dir = "."
}
kptFilePath := filepath.Join(o.Dir, "versionStream", "Kptfile")
kptFileExist, err := files.FileExists(kptFilePath)
if err != nil {
return errors.Wrapf(err, "failed to check if %s exists", kptFilePath)
}
if !kptFileExist {
return fmt.Errorf("file to find %s, clone you cluster git repository and rerun command", kptFilePath)
}
node, err := yaml.ReadFile(kptFilePath)
if err != nil {
return errors.Wrapf(err, "failed to load file %s", kptFilePath)
}
modified, err := o.modifyFn(node, kptFilePath)
if err != nil {
return errors.Wrapf(err, "failed to modify file %s", kptFilePath)
}
if !modified {
return nil
}
err = yaml.WriteFile(node, kptFilePath)
if err != nil {
return errors.Wrapf(err, "failed to save %s", kptFilePath)
}
return nil
}
func (o *Options) modifyFn(node *yaml.RNode, path string) (bool, error) {
repo := ""
ref := ""
directory := ""
switch true {
case o.LTS:
repo = ltsVersionStreamURL
ref = "main"
directory = "versionStream"
case o.Latest:
repo = latestVersionStreamURL
ref = "master"
directory = "/"
case o.Custom:
repo = o.GitURL
ref = o.GitRef
directory = o.GitDir
}
err := node.PipeE(yaml.LookupCreate(yaml.ScalarNode, "upstream", "git", "repo"), yaml.FieldSetter{StringValue: repo})
if err != nil {
return false, errors.Wrapf(err, "failed to set the git source repository to %s for %s", repo, path)
}
err = node.PipeE(yaml.LookupCreate(yaml.ScalarNode, "upstream", "git", "ref"), yaml.FieldSetter{StringValue: ref})
if err != nil {
return false, errors.Wrapf(err, "failed to set the git source ref to %s for %s", ref, path)
}
err = node.PipeE(yaml.LookupCreate(yaml.ScalarNode, "upstream", "git", "directory"), yaml.FieldSetter{StringValue: directory})
if err != nil {
return false, errors.Wrapf(err, "failed to set the git directory to %s for %s", ref, path)
}
return true, nil
}
func (o *Options) moreThanOneFlagSet() bool {
return ((o.LTS && o.Latest) || (o.Latest && o.Custom) || (o.LTS && o.Custom))
}
func (o *Options) atLeastOneFlagSet() bool {
return o.LTS || o.Latest || o.Custom
}
func (o *Options) GitCommit() error {
// git add / commit the kptfile change
message := "chore: switch verionsstream to %s"
switch true {
case o.LTS:
message = fmt.Sprintf(message, ltsVersionStreamURL)
case o.Latest:
message = fmt.Sprintf(message, latestVersionStreamURL)
case o.Custom:
message = fmt.Sprintf(message, o.GitURL)
}
gitter := o.Git()
dir := "versionStream"
_, err := gitclient.AddAndCommitFiles(gitter, dir, message)
if err != nil {
return errors.Wrapf(err, "failed to commit changes to git in dir %s", dir)
}
return nil
}