-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
update profile
to patch a profile's HelmRelease (#1445)
* Add 'update profile' command - Add cobra CLI command func and required flags - Refactor Add Profile func to re-use some functionality for Update Profile - Add acceptance test * Add validation for updating profile - Refactor profiles funcs to fail if profiles.yaml contains no HelmReleases - Refactor func that appends HelmRelease to file so that update fails if HelmRelease does not exist * Updating an installed profile's version patches it in a list of HelmReleases * Update acceptance tests for updating profile * Address 'update profile' review * Remove sleep from profiles acceptance test * Address review feedback * Pend acceptance test 'SmokeTestLong - Verify that gitops can deploy an app with long name'
- Loading branch information
Niki
committed
Feb 16, 2022
1 parent
14800da
commit 4a48d1e
Showing
18 changed files
with
964 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package update | ||
|
||
import ( | ||
"github.com/weaveworks/weave-gitops/cmd/gitops/update/profiles" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func UpdateCommand(endpoint *string, client *resty.Client) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "update", | ||
Short: "Update a Weave GitOps resource", | ||
Example: ` | ||
# Update a profile that is installed on a cluster | ||
gitops update profile --name=podinfo --cluster=prod --config-repo=ssh://git@github.com/owner/config-repo.git --version=1.0.0 | ||
`, | ||
} | ||
|
||
cmd.AddCommand(profiles.UpdateCommand()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package profiles | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/spf13/cobra" | ||
"github.com/weaveworks/weave-gitops/cmd/internal" | ||
"github.com/weaveworks/weave-gitops/pkg/flux" | ||
"github.com/weaveworks/weave-gitops/pkg/kube" | ||
"github.com/weaveworks/weave-gitops/pkg/osys" | ||
"github.com/weaveworks/weave-gitops/pkg/runner" | ||
"github.com/weaveworks/weave-gitops/pkg/server" | ||
"github.com/weaveworks/weave-gitops/pkg/services" | ||
"github.com/weaveworks/weave-gitops/pkg/services/auth" | ||
"github.com/weaveworks/weave-gitops/pkg/services/profiles" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/client-go/util/homedir" | ||
) | ||
|
||
var opts profiles.UpdateOptions | ||
|
||
// UpdateCommand provides support for updating a profile that is installed on a cluster. | ||
func UpdateCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "profile", | ||
Short: "Update a profile installation", | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
Example: ` | ||
# Update a profile that is installed on a cluster | ||
gitops update profile --name=podinfo --cluster=prod --config-repo=ssh://git@github.com/owner/config-repo.git --version=1.0.0 | ||
`, | ||
RunE: updateProfileCmdRunE(), | ||
} | ||
|
||
cmd.Flags().StringVar(&opts.Name, "name", "", "Name of the profile") | ||
cmd.Flags().StringVar(&opts.Version, "version", "latest", "Version of the profile specified as semver (e.g.: 0.1.0) or as 'latest'") | ||
cmd.Flags().StringVar(&opts.ConfigRepo, "config-repo", "", "URL of the external repository that contains the automation manifests") | ||
cmd.Flags().StringVar(&opts.Cluster, "cluster", "", "Name of the cluster where the profile is installed") | ||
cmd.Flags().StringVar(&opts.ProfilesPort, "profiles-port", server.DefaultPort, "Port the Profiles API is running on") | ||
cmd.Flags().BoolVar(&opts.AutoMerge, "auto-merge", false, "If set, 'gitops update profile' will merge automatically into the repository's branch") | ||
cmd.Flags().StringVar(&opts.Kubeconfig, "kubeconfig", filepath.Join(homedir.HomeDir(), ".kube", "config"), "Absolute path to the kubeconfig file") | ||
|
||
requiredFlags := []string{"name", "config-repo", "cluster", "version"} | ||
for _, f := range requiredFlags { | ||
if err := cobra.MarkFlagRequired(cmd.Flags(), f); err != nil { | ||
panic(fmt.Errorf("unexpected error: %w", err)) | ||
} | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func updateProfileCmdRunE() func(*cobra.Command, []string) error { | ||
return func(cmd *cobra.Command, args []string) error { | ||
log := internal.NewCLILogger(os.Stdout) | ||
fluxClient := flux.New(osys.New(), &runner.CLIRunner{}) | ||
factory := services.NewFactory(fluxClient, log) | ||
providerClient := internal.NewGitProviderClient(os.Stdout, os.LookupEnv, auth.NewAuthCLIHandler, log) | ||
|
||
if opts.Version != "latest" { | ||
if _, err := semver.StrictNewVersion(opts.Version); err != nil { | ||
return fmt.Errorf("error parsing --version=%s: %w", opts.Version, err) | ||
} | ||
} | ||
|
||
var err error | ||
if opts.Namespace, err = cmd.Flags().GetString("namespace"); err != nil { | ||
return err | ||
} | ||
|
||
config, err := clientcmd.BuildConfigFromFlags("", opts.Kubeconfig) | ||
if err != nil { | ||
return fmt.Errorf("error initializing kubernetes config: %w", err) | ||
} | ||
|
||
clientSet, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return fmt.Errorf("error initializing kubernetes client: %w", err) | ||
} | ||
|
||
kubeClient, _, err := kube.NewKubeHTTPClient() | ||
if err != nil { | ||
return fmt.Errorf("failed to create kube client: %w", err) | ||
} | ||
|
||
_, gitProvider, err := factory.GetGitClients(context.Background(), kubeClient, providerClient, services.GitConfigParams{ | ||
ConfigRepo: opts.ConfigRepo, | ||
Namespace: opts.Namespace, | ||
IsHelmRepository: true, | ||
DryRun: false, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to get git clients: %w", err) | ||
} | ||
|
||
return profiles.NewService(clientSet, log).Update(context.Background(), gitProvider, opts) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package profiles_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestProfiles(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Profiles Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package profiles_test | ||
|
||
import ( | ||
"github.com/weaveworks/weave-gitops/cmd/gitops/root" | ||
|
||
"github.com/go-resty/resty/v2" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var _ = Describe("Update Profile(s)", func() { | ||
var cmd *cobra.Command | ||
|
||
BeforeEach(func() { | ||
cmd = root.RootCmd(resty.New()) | ||
}) | ||
|
||
When("the flags are valid", func() { | ||
It("accepts all known flags for updating a profile", func() { | ||
cmd.SetArgs([]string{ | ||
"update", "profile", | ||
"--name", "podinfo", | ||
"--version", "0.0.1", | ||
"--cluster", "prod", | ||
"--namespace", "test-namespace", | ||
"--config-repo", "https://ssh@github:test/test.git", | ||
"--auto-merge", "true", | ||
}) | ||
|
||
err := cmd.Execute() | ||
Expect(err.Error()).NotTo(ContainSubstring("unknown flag")) | ||
}) | ||
}) | ||
|
||
When("flags are not valid", func() { | ||
It("fails if --name, --cluster, --version or --config-repo are not provided", func() { | ||
cmd.SetArgs([]string{ | ||
"update", "profile", | ||
}) | ||
|
||
err := cmd.Execute() | ||
Expect(err).To(MatchError("required flag(s) \"cluster\", \"config-repo\", \"name\", \"version\" not set")) | ||
}) | ||
|
||
It("fails if given version is not valid semver", func() { | ||
cmd.SetArgs([]string{ | ||
"update", "profile", | ||
"--name", "podinfo", | ||
"--config-repo", "ssh://git@github.com/owner/config-repo.git", | ||
"--cluster", "prod", | ||
"--version", "&%*/v", | ||
}) | ||
|
||
err := cmd.Execute() | ||
Expect(err).To(MatchError(ContainSubstring("error parsing --version=&%*/v"))) | ||
}) | ||
}) | ||
|
||
When("a flag is unknown", func() { | ||
It("fails", func() { | ||
cmd.SetArgs([]string{ | ||
"update", "profile", | ||
"--unknown", "param", | ||
}) | ||
|
||
err := cmd.Execute() | ||
Expect(err).To(MatchError("unknown flag: --unknown")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.