-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1360 from weaveworks/add-profile
Implement `add profile` to install a profile to a cluster
- Loading branch information
Showing
35 changed files
with
1,886 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ ginkgo.report | |
.deps | ||
test/library/wego-library-test | ||
tilt_modules | ||
.envrc |
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,122 @@ | ||
package profiles | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"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/models" | ||
"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.AddOptions | ||
|
||
// AddCommand provides support for adding a profile to a cluster. | ||
func AddCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "profile", | ||
Short: "Add a profile to a cluster", | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
Example: ` | ||
# Add a profile to a cluster | ||
gitops add profile --name=podinfo --cluster=prod --version=1.0.0 --config-repo=ssh://[email protected]/owner/config-repo.git | ||
`, | ||
RunE: addProfileCmdRunE(), | ||
} | ||
|
||
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 external repository (if any) which will hold automation manifests") | ||
cmd.Flags().StringVar(&opts.Cluster, "cluster", "", "Name of the cluster to add the profile to") | ||
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 add profile' will merge automatically into the repository's default 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"} | ||
for _, f := range requiredFlags { | ||
if err := cobra.MarkFlagRequired(cmd.Flags(), f); err != nil { | ||
panic(fmt.Errorf("unexpected error: %w", err)) | ||
} | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func addProfileCmdRunE() func(*cobra.Command, []string) error { | ||
return func(cmd *cobra.Command, args []string) error { | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
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 err := validateAddOptions(opts); err != nil { | ||
return 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).Add(context.Background(), gitProvider, opts) | ||
} | ||
} | ||
|
||
func validateAddOptions(opts profiles.AddOptions) error { | ||
if models.ApplicationNameTooLong(opts.Name) { | ||
return fmt.Errorf("--name value is too long: %s; must be <= %d characters", | ||
opts.Name, models.MaxKubernetesResourceNameLength) | ||
} | ||
|
||
if opts.Version != "latest" { | ||
if _, err := semver.StrictNewVersion(opts.Version); err != nil { | ||
return fmt.Errorf("error parsing --version=%s: %w", opts.Version, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,90 @@ | ||
package profiles_test | ||
|
||
import ( | ||
"github.com/go-resty/resty/v2" | ||
"github.com/jarcoal/httpmock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/spf13/cobra" | ||
"github.com/weaveworks/weave-gitops/cmd/gitops/root" | ||
) | ||
|
||
var _ = Describe("Add Profiles", func() { | ||
var ( | ||
cmd *cobra.Command | ||
) | ||
|
||
BeforeEach(func() { | ||
client := resty.New() | ||
httpmock.ActivateNonDefault(client.GetClient()) | ||
cmd = root.RootCmd(client) | ||
}) | ||
|
||
AfterEach(func() { | ||
httpmock.DeactivateAndReset() | ||
}) | ||
|
||
When("the flags are valid", func() { | ||
It("accepts all known flags for adding a profile", func() { | ||
cmd.SetArgs([]string{ | ||
"add", "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, and --config-repo are not provided", func() { | ||
cmd.SetArgs([]string{ | ||
"add", "profile", | ||
}) | ||
|
||
err := cmd.Execute() | ||
Expect(err).To(MatchError("required flag(s) \"cluster\", \"config-repo\", \"name\" not set")) | ||
}) | ||
|
||
It("fails if --name value is <= 63 characters in length", func() { | ||
cmd.SetArgs([]string{ | ||
"add", "profile", | ||
"--name", "a234567890123456789012345678901234567890123456789012345678901234", | ||
"--cluster", "cluster", | ||
"--config-repo", "config-repo", | ||
}) | ||
err := cmd.Execute() | ||
Expect(err).To(MatchError("--name value is too long: a234567890123456789012345678901234567890123456789012345678901234; must be <= 63 characters")) | ||
}) | ||
|
||
It("fails if given version is not valid semver", func() { | ||
cmd.SetArgs([]string{ | ||
"add", "profile", | ||
"--name", "podinfo", | ||
"--config-repo", "ssh://[email protected]/owner/config-repo.git", | ||
"--cluster", "prod", | ||
"--version", "&%*/v", | ||
}) | ||
|
||
err := cmd.Execute() | ||
Expect(err).To(MatchError("error parsing --version=&%*/v: Invalid Semantic Version")) | ||
}) | ||
}) | ||
|
||
When("a flag is unknown", func() { | ||
It("fails", func() { | ||
cmd.SetArgs([]string{ | ||
"add", "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
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 TestProfile(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
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
Oops, something went wrong.