diff --git a/cmd/wego/add/cmd.go b/cmd/wego/add/cmd.go index 8690e041ed..37b4166f3d 100644 --- a/cmd/wego/add/cmd.go +++ b/cmd/wego/add/cmd.go @@ -33,10 +33,11 @@ var Cmd = &cobra.Command{ func init() { Cmd.Flags().StringVar(¶ms.Owner, "owner", "", "Owner of remote git repository") Cmd.Flags().StringVar(¶ms.Name, "name", "", "Name of remote git repository") - Cmd.Flags().StringVar(¶ms.Url, "url", "", "URL of remote git repository") + Cmd.Flags().StringVar(¶ms.Url, "url", "", "URL of remote repository") Cmd.Flags().StringVar(¶ms.Path, "path", "./", "Path of files within git repository") Cmd.Flags().StringVar(¶ms.Branch, "branch", "main", "Branch to watch within git repository") Cmd.Flags().StringVar(¶ms.DeploymentType, "deployment-type", "kustomize", "deployment type [kustomize, helm]") + Cmd.Flags().StringVar(¶ms.Chart, "chart", "", "Specify chart for helm source") Cmd.Flags().StringVar(¶ms.PrivateKey, "private-key", filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa"), "Private key that provides access to git repository") Cmd.Flags().BoolVar(¶ms.DryRun, "dry-run", false, "If set, 'wego add' will not make any changes to the system; it will just display the actions that would have been taken") Cmd.Flags().BoolVar(¶ms.IsPrivate, "private", true, "Set access control on the repo") diff --git a/pkg/cmdimpl/add.go b/pkg/cmdimpl/add.go index c25949a97d..bf0bb6534c 100644 --- a/pkg/cmdimpl/add.go +++ b/pkg/cmdimpl/add.go @@ -48,6 +48,7 @@ type AddParamSet struct { PrivateKey string PrivateKeyPass string DeploymentType string + Chart string Namespace string DryRun bool IsPrivate bool @@ -194,7 +195,7 @@ func generateWegoKustomizeManifest() ([]byte, error) { return kustomizeManifest, nil } -func generateSourceManifest() ([]byte, error) { +func generateSourceManifestGit() ([]byte, error) { secretName := params.Name cmd := fmt.Sprintf(`create secret git "%s" \ @@ -235,6 +236,23 @@ func generateSourceManifest() ([]byte, error) { return sourceManifest, nil } +func generateSourceManifestHelm() ([]byte, error) { + cmd := fmt.Sprintf(`create source helm %s \ + --url="%s" \ + --interval=30s \ + --export \ + --namespace=%s `, + params.Name, + params.Url, + params.Namespace) + + sourceManifest, err := fluxops.CallFlux(cmd) + if err != nil { + return nil, wrapError(err, "could not create git source") + } + return sourceManifest, nil +} + func generateKustomizeManifest() ([]byte, error) { cmd := fmt.Sprintf(`create kustomization "%s" \ --path="%s" \ @@ -256,7 +274,7 @@ func generateKustomizeManifest() ([]byte, error) { return kustomizeManifest, nil } -func generateHelmManifest() ([]byte, error) { +func generateHelmManifestGit() ([]byte, error) { cmd := fmt.Sprintf(`create helmrelease %s \ --source="GitRepository/%s" \ --chart="%s" \ @@ -271,6 +289,22 @@ func generateHelmManifest() ([]byte, error) { return fluxops.CallFlux(cmd) } +func generateHelmManifestHelm() ([]byte, error) { + cmd := fmt.Sprintf(`create helmrelease %s \ + --source="HelmRepository/%s" \ + --chart="%s" \ + --interval=5m \ + --export \ + --namespace=%s`, + params.Name, + params.Name, + params.Chart, + params.Namespace, + ) + + return fluxops.CallFlux(cmd) +} + func getOwner() (string, error) { owner, err := fluxops.GetOwnerFromEnv() if err != nil || owner == "" { @@ -433,22 +467,36 @@ func Add(args []string, allParams AddParamSet, deps *AddDependencies) error { } // Create flux custom resources for new repo being added - source, err := generateSourceManifest() - if err != nil { - return wrapError(err, "could not generate source manifest") - } - + var source []byte var appManifests []byte - switch params.DeploymentType { - case string(DeployTypeHelm): - appManifests, err = generateHelmManifest() - case string(DeployTypeKustomize): - appManifests, err = generateKustomizeManifest() - default: - return fmt.Errorf("deployment type not supported: %s", params.DeploymentType) - } - if err != nil { - return wrapError(err, "error generating manifest") + + // If chart is set ignore deployment type. Going to revisit later + if params.Chart != "" { + source, err = generateSourceManifestHelm() + if err != nil { + return wrapError(err, "could not generate source manifest") + } + appManifests, err = generateHelmManifestHelm() + if err != nil { + return wrapError(err, "error generating manifest") + } + } else { + source, err = generateSourceManifestGit() + if err != nil { + return wrapError(err, "could not generate source manifest") + } + + switch params.DeploymentType { + case string(DeployTypeHelm): + appManifests, err = generateHelmManifestGit() + case string(DeployTypeKustomize): + appManifests, err = generateKustomizeManifest() + default: + return fmt.Errorf("deployment type not supported: %s", params.DeploymentType) + } + if err != nil { + return wrapError(err, "error generating manifest") + } } appSubdir := filepath.Join("apps", params.Name) diff --git a/pkg/cmdimpl/add_test.go b/pkg/cmdimpl/add_test.go index 6724225118..c9c0751d20 100644 --- a/pkg/cmdimpl/add_test.go +++ b/pkg/cmdimpl/add_test.go @@ -173,8 +173,8 @@ func handleGitLsRemote(arglist ...interface{}) ([]byte, []byte, error) { var fakeGitClient = gitfakes.FakeGit{} -var _ = Describe("Test helm manifest", func() { - It("Verify helm manifest files generation ", func() { +var _ = Describe("Test helm manifest from git repo", func() { + It("Verify helm manifest files generation from git ", func() { expected := `create helmrelease simple-name \ --source="GitRepository/simple-name" \ @@ -192,13 +192,74 @@ var _ = Describe("Test helm manifest", func() { fluxops.SetFluxHandler(fakeHandler) - params.Name = "simple-name" params.Name = "simple-name" params.Path = "./my-chart" params.Namespace = "wego-system" - Expect(generateHelmManifest()).Should(Equal([]byte("foo"))) + Expect(generateHelmManifestGit()).Should(Equal([]byte("foo"))) + }) +}) + +var _ = Describe("Test helm manifest from helm repo", func() { + It("Verify helm manifest generation from helm ", func() { + + expected := `create helmrelease simple-name \ + --source="HelmRepository/simple-name" \ + --chart="testchart" \ + --interval=5m \ + --export \ + --namespace=wego-system` + + fakeHandler := &fluxopsfakes.FakeFluxHandler{ + HandleStub: func(args string) ([]byte, error) { + Expect(args).Should(Equal(expected)) + return []byte("foo"), nil + }, + } + + _ = override.WithOverrides( + func() override.Result { + params.DryRun = false + params.Name = "simple-name" + params.Namespace = "wego-system" + params.Chart = "testchart" + Expect(generateHelmManifestHelm()).Should(Equal([]byte("foo"))) + return override.Result{} + }, + fluxops.Override(fakeHandler)) + }) + +}) + +var _ = Describe("Test helm source from helm repo", func() { + It("Verify helm source generation from helm ", func() { + + expected := `create source helm test \ + --url="https://github.io/testrepo" \ + --interval=30s \ + --export \ + --namespace=wego-system ` + + fakeHandler := &fluxopsfakes.FakeFluxHandler{ + HandleStub: func(args string) ([]byte, error) { + Expect(args).Should(Equal(expected)) + return []byte("foo"), nil + }, + } + + _ = override.WithOverrides( + func() override.Result { + params.DryRun = false + params.Name = "test" + params.Url = "https://github.io/testrepo" + params.Namespace = "wego-system" + params.Chart = "testChart" + Expect(generateSourceManifestHelm()).Should(Equal([]byte("foo"))) + return override.Result{} + }, + fluxops.Override(fakeHandler)) }) + }) var _ = Describe("Dry Run Add Test", func() {