Skip to content

Commit

Permalink
Allow adding a public gitlab project (#992)
Browse files Browse the repository at this point in the history
* Allow adding a public gitlab project

* add acceptance test

* fix acceptance test

* this auto change

* Update testsuite_common_test.go

undo change

* fix acceptance test

* add public group

* update test readme

* Update testsuite_common_test.go

* add test log

* add missing variable

Co-authored-by: Justin Thompson <justin@weave.works>
  • Loading branch information
J-Thompson12 and Justin Thompson committed Nov 3, 2021
1 parent c0d130b commit 105715d
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 49 deletions.
1 change: 1 addition & 0 deletions .github/actions/run-acceptance-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ runs:
GITHUB_KEY: ${{ inputs.github-key }}
IS_TEST_ENV: "true"
GITLAB_ORG: weave-gitops
GITLAB_PUBLIC_GROUP: public-weave-gitops
GITLAB_SUBGROUP: weave-gitops-sub
GITLAB_TOKEN: ${{ inputs.gitlab-token }}
GITLAB_KEY: ${{ inputs.gitlab-key }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
GITHUB_TOKEN: "${{ secrets.WEAVE_GITOPS_TEST_WEAVEWORKS_WEAVE_GITOPS_BOT_TOKEN }}"
GITHUB_KEY: "${{ secrets.WEAVE_GITOPS_TEST_WEAVEWORKS_WEAVE_GITOPS_BOT_SSH_KEY }}"
GITLAB_ORG: weave-gitops
GITLAB_PUBLIC_GROUP: public-weave-gitops
GITLAB_SUBGROUP: weave-gitops-sub
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
GITLAB_KEY: ${{ secrets.GITLAB_KEY }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
GITHUB_TOKEN: "${{ secrets.WEAVE_GITOPS_TEST_WEAVEWORKS_WEAVE_GITOPS_BOT_TOKEN }}"
GITHUB_KEY: "${{ secrets.WEAVE_GITOPS_TEST_WEAVEWORKS_WEAVE_GITOPS_BOT_SSH_KEY }}"
GITLAB_ORG: weave-gitops
GITLAB_PUBLIC_GROUP: public-weave-gitops
GITLAB_SUBGROUP: weave-gitops-sub
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
GITLAB_KEY: ${{ secrets.GITLAB_KEY }}
Expand Down Expand Up @@ -159,6 +160,7 @@ jobs:
GITHUB_TOKEN: "${{ secrets.WEAVE_GITOPS_TEST_WEAVEWORKS_WEAVE_GITOPS_BOT_TOKEN }}"
GITHUB_KEY: "${{ secrets.WEAVE_GITOPS_TEST_WEAVEWORKS_WEAVE_GITOPS_BOT_SSH_KEY }}"
GITLAB_ORG: weave-gitops
GITLAB_PUBLIC_GROUP: public-weave-gitops
GITLAB_SUBGROUP: weave-gitops-sub
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
GITLAB_KEY: ${{ secrets.GITLAB_KEY }}
Expand Down
26 changes: 15 additions & 11 deletions pkg/flux/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/pkg/errors"
wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/pkg/gitproviders"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/version"
Expand All @@ -20,12 +21,12 @@ type Flux interface {
GetExePath() (string, error)
Install(namespace string, export bool) ([]byte, error)
Uninstall(namespace string, export bool) error
CreateSourceGit(name string, url string, branch string, secretRef string, namespace string) ([]byte, error)
CreateSourceGit(name string, repoUrl gitproviders.RepoURL, branch string, secretRef string, namespace string) ([]byte, error)
CreateSourceHelm(name string, url string, namespace string) ([]byte, error)
CreateKustomization(name string, source string, path string, namespace string) ([]byte, error)
CreateHelmReleaseGitRepository(name, source, path, namespace, targetNamespace string) ([]byte, error)
CreateHelmReleaseHelmRepository(name, chart, namespace, targetNamespace string) ([]byte, error)
CreateSecretGit(name string, url string, namespace string) ([]byte, error)
CreateSecretGit(name string, repoUrl gitproviders.RepoURL, namespace string) ([]byte, error)
GetVersion() (string, error)
GetAllResourcesStatus(name string, namespace string) ([]byte, error)
SuspendOrResumeApp(pause wego.SuspendActionType, name, namespace, deploymentType string) ([]byte, error)
Expand Down Expand Up @@ -90,7 +91,7 @@ func (f *FluxClient) Uninstall(namespace string, dryRun bool) error {
return nil
}

func (f *FluxClient) CreateSourceGit(name string, url string, branch string, secretRef string, namespace string) ([]byte, error) {
func (f *FluxClient) CreateSourceGit(name string, repoUrl gitproviders.RepoURL, branch string, secretRef string, namespace string) ([]byte, error) {
args := []string{
"create", "source", "git", name,
"--branch", branch,
Expand All @@ -100,9 +101,9 @@ func (f *FluxClient) CreateSourceGit(name string, url string, branch string, sec
}

if secretRef != "" {
args = append(args, "--secret-ref", secretRef, "--url", url)
args = append(args, "--secret-ref", secretRef, "--url", repoUrl.String())
} else {
args = append(args, "--url", makePublicUrl(url))
args = append(args, "--url", makePublicUrl(repoUrl))
}

out, err := f.runFluxCmd(args...)
Expand All @@ -113,15 +114,18 @@ func (f *FluxClient) CreateSourceGit(name string, url string, branch string, sec
return out, nil
}

func makePublicUrl(url string) string {
func makePublicUrl(repoUrl gitproviders.RepoURL) string {
trimmed := ""

url := repoUrl.String()
provider := repoUrl.Provider()

if !strings.HasSuffix(url, ".git") {
url = url + ".git"
}

gitSshPrefix := "git@github.com:"
sshPrefix := "ssh://git@github.com/"
gitSshPrefix := fmt.Sprintf("git@%scom:", provider)
sshPrefix := fmt.Sprintf("ssh://git@%s.com/", provider)

if strings.HasPrefix(url, gitSshPrefix) {
trimmed = strings.TrimPrefix(url, gitSshPrefix)
Expand All @@ -130,7 +134,7 @@ func makePublicUrl(url string) string {
}

if trimmed != "" {
return "https://github.com/" + trimmed
return fmt.Sprintf("https://%s.com/%s", provider, trimmed)
}

return url
Expand Down Expand Up @@ -217,10 +221,10 @@ func (f *FluxClient) CreateHelmReleaseHelmRepository(name, chart, namespace, tar
}

// CreatSecretGit Creates a Git secret returns the deploy key
func (f *FluxClient) CreateSecretGit(name string, url string, namespace string) ([]byte, error) {
func (f *FluxClient) CreateSecretGit(name string, repoUrl gitproviders.RepoURL, namespace string) ([]byte, error) {
args := []string{
"create", "secret", "git", name,
"--url", url,
"--url", repoUrl.String(),
"--namespace", namespace,
"--export",
}
Expand Down
37 changes: 32 additions & 5 deletions pkg/flux/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
. "github.com/onsi/gomega"
wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/gitproviders"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner/runnerfakes"
)
Expand Down Expand Up @@ -82,22 +83,26 @@ var _ = Describe("CreateSourceGit", func() {
runner.RunStub = func(s1 string, s2 ...string) ([]byte, error) {
return []byte("out"), nil
}
out, err := fluxClient.CreateSourceGit("my-name", "https://github.com/foo/my-name", "main", "my-secret", wego.DefaultNamespace)

repoUrl, err := gitproviders.NewRepoURL("https://github.com/foo/my-name")
Expect(err).ShouldNot(HaveOccurred())
out, err := fluxClient.CreateSourceGit("my-name", repoUrl, "main", "my-secret", wego.DefaultNamespace)
Expect(err).ShouldNot(HaveOccurred())
Expect(out).To(Equal([]byte("out")))

Expect(runner.RunCallCount()).To(Equal(1))

cmd, args := runner.RunArgsForCall(0)
Expect(cmd).To(Equal(fluxPath()))

Expect(strings.Join(args, " ")).To(Equal(fmt.Sprintf("create source git my-name --branch main --namespace %s --interval 30s --export --secret-ref my-secret --url https://github.com/foo/my-name", wego.DefaultNamespace)))
Expect(strings.Join(args, " ")).To(Equal(fmt.Sprintf("create source git my-name --branch main --namespace %s --interval 30s --export --secret-ref my-secret --url ssh://git@github.com/foo/my-name.git", wego.DefaultNamespace)))
})
It("creates a git source for a public repo", func() {
runner.RunStub = func(s1 string, s2 ...string) ([]byte, error) {
return []byte("out"), nil
}
out, err := fluxClient.CreateSourceGit("my-name", "ssh://git@github.com/foo/my-name", "main", "", wego.DefaultNamespace)
repoUrl, err := gitproviders.NewRepoURL("ssh://git@github.com/foo/my-name")
Expect(err).ShouldNot(HaveOccurred())
out, err := fluxClient.CreateSourceGit("my-name", repoUrl, "main", "", wego.DefaultNamespace)
Expect(err).ShouldNot(HaveOccurred())
Expect(out).To(Equal([]byte("out")))

Expand All @@ -108,6 +113,25 @@ var _ = Describe("CreateSourceGit", func() {

Expect(strings.Join(args, " ")).To(Equal(fmt.Sprintf("create source git my-name --branch main --namespace %s --interval 30s --export --url https://github.com/foo/my-name.git", wego.DefaultNamespace)))
})

It("creates a git source for a public gitlab repo", func() {
runner.RunStub = func(s1 string, s2 ...string) ([]byte, error) {
return []byte("out"), nil
}

repoUrl, err := gitproviders.NewRepoURL("ssh://git@gitlab.com/foo/my-name")
Expect(err).ShouldNot(HaveOccurred())
out, err := fluxClient.CreateSourceGit("my-name", repoUrl, "main", "", wego.DefaultNamespace)
Expect(err).ShouldNot(HaveOccurred())
Expect(out).To(Equal([]byte("out")))

Expect(runner.RunCallCount()).To(Equal(1))

cmd, args := runner.RunArgsForCall(0)
Expect(cmd).To(Equal(fluxPath()))

Expect(strings.Join(args, " ")).To(Equal(fmt.Sprintf("create source git my-name --branch main --namespace %s --interval 30s --export --url https://gitlab.com/foo/my-name.git", wego.DefaultNamespace)))
})
})

var _ = Describe("CreateSourceHelm", func() {
Expand Down Expand Up @@ -219,7 +243,10 @@ var _ = Describe("CreateSecretGit", func() {
runner.RunStub = func(s1 string, s2 ...string) ([]byte, error) {
return []byte(`ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCh...`), nil
}
out, err := fluxClient.CreateSecretGit("my-secret", "ssh://git@github.com/foo/bar.git", wego.DefaultNamespace)

repoUrl, err := gitproviders.NewRepoURL("ssh://git@github.com/foo/bar.git")
Expect(err).ShouldNot(HaveOccurred())
out, err := fluxClient.CreateSecretGit("my-secret", repoUrl, wego.DefaultNamespace)
Expect(err).ShouldNot(HaveOccurred())
Expect(out).To(Equal([]byte("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCh...")))

Expand Down
25 changes: 13 additions & 12 deletions pkg/flux/fluxfakes/fake_flux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/services/app/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func (a *App) generateExternalRepoManifests(ctx context.Context, info *AppResour
secretRef = info.repoSecretName(info.Spec.ConfigURL).String()
}

targetSource, err := a.Flux.CreateSourceGit(repoName, info.Spec.ConfigURL, branch, secretRef, info.Namespace)
targetSource, err := a.Flux.CreateSourceGit(repoName, info.configRepoUrl, branch, secretRef, info.Namespace)
if err != nil {
return nil, fmt.Errorf("could not generate target source manifests: %w", err)
}
Expand Down Expand Up @@ -612,7 +612,7 @@ func (a *App) generateSource(info *AppResourceInfo, secretRef string) (Source, e

switch info.Spec.SourceType {
case wego.SourceTypeGit:
b, err = a.Flux.CreateSourceGit(info.Name, info.Spec.URL, info.Spec.Branch, secretRef, info.Namespace)
b, err = a.Flux.CreateSourceGit(info.Name, info.appRepoUrl, info.Spec.Branch, secretRef, info.Namespace)
case wego.SourceTypeHelm:
b, err = a.Flux.CreateSourceHelm(info.Name, info.Spec.URL, info.Namespace)

Expand Down

0 comments on commit 105715d

Please sign in to comment.