-
Notifications
You must be signed in to change notification settings - Fork 149
/
cmd.go
182 lines (150 loc) · 5.38 KB
/
cmd.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
package clusters
import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"github.com/go-resty/resty/v2"
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/gitops/cmderrors"
"github.com/weaveworks/weave-gitops/cmd/internal"
"github.com/weaveworks/weave-gitops/pkg/adapters"
"github.com/weaveworks/weave-gitops/pkg/capi"
"github.com/weaveworks/weave-gitops/pkg/gitproviders"
)
type clusterCommandFlags struct {
DryRun bool
Template string
ParameterValues []string
RepositoryURL string
BaseBranch string
HeadBranch string
Title string
Description string
CommitMessage string
Credentials string
Profiles []string
}
var flags clusterCommandFlags
func ClusterCommand(endpoint *string, client *resty.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Short: "Add a new cluster using a CAPI template",
Example: `
# Add a new cluster using a CAPI template
gitops add cluster --from-template <template-name> --set key=val
# View a CAPI template populated with parameter values
# without creating a pull request for it
gitops add cluster --from-template <template-name> --set key=val --dry-run
# Add a new cluster supplied with profiles versions and values files
gitops add cluster --from-template <template-name> \
--profile 'name=foo-profile,version=0.0.1' --profile 'name=bar-profile,values=bar-values.yaml
`,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: getClusterCmdPreRunE(endpoint, client),
RunE: getClusterCmdRunE(endpoint, client),
}
cmd.Flags().BoolVar(&flags.DryRun, "dry-run", false, "View the populated template without creating a pull request")
cmd.Flags().StringVar(&flags.Template, "from-template", "", "Specify the CAPI template to create a cluster from")
cmd.Flags().StringSliceVar(&flags.ParameterValues, "set", []string{}, "Set parameter values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
cmd.Flags().StringVar(&flags.RepositoryURL, "url", "", "URL of remote repository to create the pull request")
cmd.Flags().StringVar(&flags.Credentials, "set-credentials", "", "The CAPI credentials to use")
cmd.Flags().StringArrayVar(&flags.Profiles, "profile", []string{}, "Set profiles values files on the command line (--profile 'name=foo-profile,version=0.0.1' --profile 'name=bar-profile,values=bar-values.yaml')")
internal.AddPRFlags(cmd, &flags.HeadBranch, &flags.BaseBranch, &flags.Description, &flags.CommitMessage, &flags.Title)
return cmd
}
func getClusterCmdPreRunE(endpoint *string, client *resty.Client) func(*cobra.Command, []string) error {
return func(c *cobra.Command, s []string) error {
if *endpoint == "" {
return cmderrors.ErrNoWGEEndpoint
}
return nil
}
}
func getClusterCmdRunE(endpoint *string, client *resty.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
r, err := adapters.NewHttpClient(*endpoint, client, os.Stdout)
if err != nil {
return err
}
vals := make(map[string]string)
for _, v := range flags.ParameterValues {
kv := strings.SplitN(v, "=", 2)
if len(kv) == 2 {
vals[kv[0]] = kv[1]
}
}
creds := capi.Credentials{}
if flags.Credentials != "" {
creds, err = r.RetrieveCredentialsByName(flags.Credentials)
if err != nil {
return err
}
}
profilesValues, err := parseProfileFlags(flags.Profiles)
if err != nil {
return fmt.Errorf("error parsing profiles: %w", err)
}
if flags.DryRun {
return capi.RenderTemplateWithParameters(flags.Template, vals, creds, r, os.Stdout)
}
if flags.RepositoryURL == "" {
return cmderrors.ErrNoURL
}
url, err := gitproviders.NewRepoURL(flags.RepositoryURL)
if err != nil {
return fmt.Errorf("cannot parse url: %w", err)
}
token, err := internal.GetToken(url, os.LookupEnv)
if err != nil {
return err
}
params := capi.CreatePullRequestFromTemplateParams{
GitProviderToken: token,
TemplateName: flags.Template,
ParameterValues: vals,
RepositoryURL: flags.RepositoryURL,
HeadBranch: flags.HeadBranch,
BaseBranch: flags.BaseBranch,
Title: flags.Title,
Description: flags.Description,
CommitMessage: flags.CommitMessage,
Credentials: creds,
ProfileValues: profilesValues,
}
return capi.CreatePullRequestFromTemplate(params, r, os.Stdout)
}
}
func parseProfileFlags(profiles []string) ([]capi.ProfileValues, error) {
var profilesValues []capi.ProfileValues
// Validate values include alphanumeric or - or .
r := regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$`)
for _, p := range flags.Profiles {
valuesPairs := strings.Split(p, ",")
profileMap := make(map[string]string)
for _, pair := range valuesPairs {
fmt.Println(pair)
kv := strings.Split(pair, "=")
if kv[0] != "name" && kv[0] != "version" && kv[0] != "values" {
return nil, fmt.Errorf("invalid key: %s", kv[0])
} else if !r.MatchString(kv[1]) {
return nil, fmt.Errorf("invalid value for %s: %s", kv[0], kv[1])
} else {
profileMap[kv[0]] = kv[1]
}
}
profileJson, err := json.Marshal(profileMap)
if err != nil {
return nil, err
}
var profileValues capi.ProfileValues
err = json.Unmarshal(profileJson, &profileValues)
if err != nil {
return nil, err
}
profilesValues = append(profilesValues, profileValues)
}
return profilesValues, nil
}