Skip to content

Commit e10b6a3

Browse files
NikiJake KleinSkarlsocPu1
committed
Address review feedback for Add Profile
- removed all `Expect(err).NotTo(BeNil)` before `MatchError()` assertions - refactored how we discover version of available profiles to return it rather than set it on pb profile - pass in all values for MakeHelmRelease to remove Profiles dependency - renamed --port to --profiles-port - use a single const for profiles path (git.ProfilesManifestFileName) instead of models.WegoProfilePath - seed with time at the start of the cmd for late use of rand - improve function name and documentation comment for splitYAML() and GetRandomString() - use Cobra's MarkFlagRequired feature for name, cluster, and config-repo flags - change gitproviders.MergePullRequest() to always use method Merge - uses ConvertStringListToSemanticVersionList() and SortVersions() to sort semver versions of Profile's available versions - checkout package-lock.json to reset it to that of main - removed *resty.Client from addRunE command - moved profiles.yaml const to pkg models - added --kubeconfig flag - renamed GetRepoFiles() to GetRepoDirFiles() - renamed MakeManifestFile() to AppendProfileToFile() - renamed GetAvailableProfile() to GetProfile() - MakeHelmRelease compares HelmReleases with go-cmp (very cool) Co-authored-by: Jake Klein <[email protected]> Co-authored-by: Gergely Brautigam <[email protected]> Co-authored-by: Chetan Patwal <[email protected]>
1 parent 0a8c2fa commit e10b6a3

26 files changed

+701
-50485
lines changed

cmd/gitops/add/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ gitops add cluster`,
2222

2323
cmd.AddCommand(clusters.ClusterCommand(endpoint, client))
2424
cmd.AddCommand(app.Cmd)
25-
cmd.AddCommand(profiles.AddCommand(client))
25+
cmd.AddCommand(profiles.AddCommand())
2626

2727
return cmd
2828
}

cmd/gitops/add/profiles/cmd.go

+21-30
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package profiles
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
6+
"math/rand"
77
"os"
88
"path/filepath"
9-
"strings"
9+
"time"
1010

1111
"github.com/Masterminds/semver/v3"
12-
"github.com/go-resty/resty/v2"
1312
"github.com/spf13/cobra"
1413
"github.com/weaveworks/weave-gitops/cmd/internal"
1514
"github.com/weaveworks/weave-gitops/pkg/flux"
@@ -29,32 +28,41 @@ import (
2928
var opts profiles.AddOptions
3029

3130
// AddCommand provides support for adding a profile to a cluster.
32-
func AddCommand(client *resty.Client) *cobra.Command {
31+
func AddCommand() *cobra.Command {
3332
cmd := &cobra.Command{
3433
Use: "profile",
35-
Aliases: []string{"profiles"},
3634
Short: "Add a profile to a cluster",
3735
SilenceUsage: true,
3836
SilenceErrors: true,
3937
Example: `
4038
# Add a profile to a cluster
4139
gitops add profile --name=podinfo --cluster=prod --version=1.0.0 --config-repo=ssh://[email protected]/owner/config-repo.git
4240
`,
43-
RunE: addProfileCmdRunE(client),
41+
RunE: addProfileCmdRunE(),
4442
}
4543

4644
cmd.Flags().StringVar(&opts.Name, "name", "", "Name of the profile")
47-
cmd.Flags().StringVar(&opts.Version, "version", "latest", "Version of the profile")
45+
cmd.Flags().StringVar(&opts.Version, "version", "latest", "Version of the profile specified as semver (e.g.: 0.1.0) or as 'latest'")
4846
cmd.Flags().StringVar(&opts.ConfigRepo, "config-repo", "", "URL of external repository (if any) which will hold automation manifests")
4947
cmd.Flags().StringVar(&opts.Cluster, "cluster", "", "Name of the cluster to add the profile to")
50-
cmd.Flags().StringVar(&opts.Port, "port", server.DefaultPort, "Port the profiles API is running on")
48+
cmd.Flags().StringVar(&opts.ProfilesPort, "profiles-port", server.DefaultPort, "Port the Profiles API is running on")
5149
cmd.Flags().BoolVar(&opts.AutoMerge, "auto-merge", false, "If set, 'gitops add profile' will merge automatically into the set --branch")
50+
cmd.Flags().StringVar(&opts.Kubeconfig, "kubeconfig", filepath.Join(homedir.HomeDir(), ".kube", "config"), "Absolute path to the kubeconfig file")
51+
52+
requiredFlags := []string{"name", "config-repo", "cluster"}
53+
for _, f := range requiredFlags {
54+
if err := cobra.MarkFlagRequired(cmd.Flags(), f); err != nil {
55+
panic(fmt.Errorf("unexpected error: %w", err))
56+
}
57+
58+
}
5259

5360
return cmd
5461
}
5562

56-
func addProfileCmdRunE(client *resty.Client) func(*cobra.Command, []string) error {
63+
func addProfileCmdRunE() func(*cobra.Command, []string) error {
5764
return func(cmd *cobra.Command, args []string) error {
65+
rand.Seed(time.Now().UnixNano())
5866
log := internal.NewCLILogger(os.Stdout)
5967
fluxClient := flux.New(osys.New(), &runner.CLIRunner{})
6068
factory := services.NewFactory(fluxClient, log)
@@ -64,13 +72,13 @@ func addProfileCmdRunE(client *resty.Client) func(*cobra.Command, []string) erro
6472
return err
6573
}
6674

67-
ns, err := cmd.Parent().Parent().Flags().GetString("namespace")
75+
ns, err := cmd.Flags().GetString("namespace")
6876
if err != nil {
6977
return err
7078
}
7179
opts.Namespace = ns
7280

73-
config, err := clientcmd.BuildConfigFromFlags("", filepath.Join(homedir.HomeDir(), ".kube", "config"))
81+
config, err := clientcmd.BuildConfigFromFlags("", opts.Kubeconfig)
7482
if err != nil {
7583
return fmt.Errorf("error initializing kubernetes config: %w", err)
7684
}
@@ -96,36 +104,19 @@ func addProfileCmdRunE(client *resty.Client) func(*cobra.Command, []string) erro
96104
return fmt.Errorf("failed to get git clients: %w", err)
97105
}
98106

99-
profilesService := profiles.NewService(clientSet, log)
100-
return profilesService.Add(ctx, gitProvider, opts)
107+
return profiles.NewService(clientSet, log).Add(ctx, gitProvider, opts)
101108
}
102109
}
103110

104111
func validateAddOptions(opts profiles.AddOptions) error {
105-
if opts.Name == "" {
106-
return errors.New("--name should be provided")
107-
}
108-
109112
if models.ApplicationNameTooLong(opts.Name) {
110113
return fmt.Errorf("--name value is too long: %s; must be <= %d characters",
111114
opts.Name, models.MaxKubernetesResourceNameLength)
112115
}
113116

114-
if strings.HasPrefix(opts.Name, "wego") {
115-
return fmt.Errorf("the prefix 'wego' is used by weave gitops and is not allowed for a profile name")
116-
}
117-
118-
if opts.ConfigRepo == "" {
119-
return errors.New("--config-repo should be provided")
120-
}
121-
122-
if opts.Cluster == "" {
123-
return errors.New("--cluster should be provided")
124-
}
125-
126117
if opts.Version != "latest" {
127118
if _, err := semver.StrictNewVersion(opts.Version); err != nil {
128-
return fmt.Errorf("error parsing --version=%s: %s", opts.Version, err)
119+
return fmt.Errorf("error parsing --version=%s: %w", opts.Version, err)
129120
}
130121
}
131122
return nil

cmd/gitops/add/profiles/cmd_test.go

+11-39
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ var _ = Describe("Add Profiles", func() {
1717
BeforeEach(func() {
1818
client := resty.New()
1919
httpmock.ActivateNonDefault(client.GetClient())
20-
defer httpmock.DeactivateAndReset()
2120
cmd = root.RootCmd(client)
2221
})
2322

23+
AfterEach(func() {
24+
httpmock.DeactivateAndReset()
25+
})
26+
2427
When("the flags are valid", func() {
2528
It("accepts all known flags for adding a profile", func() {
2629
cmd.SetArgs([]string{
@@ -39,54 +42,24 @@ var _ = Describe("Add Profiles", func() {
3942
})
4043

4144
When("flags are not valid", func() {
42-
It("fails if --name is not provided", func() {
43-
cmd.SetArgs([]string{
44-
"add", "profile",
45-
})
46-
47-
err := cmd.Execute()
48-
Expect(err).To(MatchError("--name should be provided"))
49-
})
50-
51-
When("--name is specified", func() {
52-
It("fails if --name value is <= 63 characters in length", func() {
53-
cmd.SetArgs([]string{
54-
"add", "profile",
55-
"--name", "a234567890123456789012345678901234567890123456789012345678901234",
56-
})
57-
err := cmd.Execute()
58-
Expect(err).To(MatchError("--name value is too long: a234567890123456789012345678901234567890123456789012345678901234; must be <= 63 characters"))
59-
})
60-
61-
It("fails if --name is prefixed by 'wego'", func() {
62-
cmd.SetArgs([]string{
63-
"add", "profile",
64-
"--name", "wego-app",
65-
})
66-
err := cmd.Execute()
67-
Expect(err).To(MatchError("the prefix 'wego' is used by weave gitops and is not allowed for a profile name"))
68-
})
69-
})
70-
71-
It("fails if --config-repo is not provided", func() {
45+
It("fails if --name, --cluster, and --config-repo are not provided", func() {
7246
cmd.SetArgs([]string{
7347
"add", "profile",
74-
"--name", "podinfo",
7548
})
7649

7750
err := cmd.Execute()
78-
Expect(err).To(MatchError("--config-repo should be provided"))
51+
Expect(err).To(MatchError("required flag(s) \"cluster\", \"config-repo\", \"name\" not set"))
7952
})
8053

81-
It("fails if --config-repo is not provided", func() {
54+
It("fails if --name value is <= 63 characters in length", func() {
8255
cmd.SetArgs([]string{
8356
"add", "profile",
84-
"--name", "podinfo",
85-
"--config-repo", "ssh://[email protected]/owner/config-repo.git",
57+
"--name", "a234567890123456789012345678901234567890123456789012345678901234",
58+
"--cluster", "cluster",
59+
"--config-repo", "config-repo",
8660
})
87-
8861
err := cmd.Execute()
89-
Expect(err).To(MatchError("--cluster should be provided"))
62+
Expect(err).To(MatchError("--name value is too long: a234567890123456789012345678901234567890123456789012345678901234; must be <= 63 characters"))
9063
})
9164

9265
It("fails if given version is not valid semver", func() {
@@ -111,7 +84,6 @@ var _ = Describe("Add Profiles", func() {
11184
})
11285

11386
err := cmd.Execute()
114-
Expect(err).To(HaveOccurred())
11587
Expect(err).To(MatchError("unknown flag: --unknown"))
11688
})
11789
})

0 commit comments

Comments
 (0)