Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add deploy key when needed #1334

Merged
merged 16 commits into from
Feb 1, 2022
5 changes: 3 additions & 2 deletions cmd/gitops/add/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func init() {
Cmd.Flags().StringVar(&params.Branch, "branch", "", "Branch to watch within git repository")
Cmd.Flags().StringVar(&params.DeploymentType, "deployment-type", app.DefaultDeploymentType, "Deployment type [kustomize, helm]")
Cmd.Flags().StringVar(&params.Chart, "chart", "", "Specify chart for helm source")
Cmd.Flags().StringVar(&params.ConfigRepo, "config-repo", "", "URL of external repository (if any) which will hold automation manifests")
Cmd.Flags().StringVar(&params.HelmReleaseTargetNamespace, "helm-release-target-namespace", "", "Namespace in which to deploy a helm chart; defaults to the gitops installation namespace")
Cmd.Flags().BoolVar(&params.DryRun, "dry-run", false, "If set, 'gitops add app' will not make any changes to the system; it will just display the actions that would have been taken")
Cmd.Flags().BoolVar(&params.AutoMerge, "auto-merge", false, "If set, 'gitops add app' will merge automatically into the set --branch")
Expand Down Expand Up @@ -123,9 +122,11 @@ func runCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed getting wego config")
}

params.ConfigRepo = wegoConfig.ConfigRepo

gitClient, gitProvider, err := factory.GetGitClients(ctx, kubeClient, providerClient, services.GitConfigParams{
URL: params.Url,
ConfigRepo: wegoConfig.ConfigRepo,
ConfigRepo: params.ConfigRepo,
Namespace: params.Namespace,
IsHelmRepository: params.IsHelmRepository(),
DryRun: params.DryRun,
Expand Down
2 changes: 1 addition & 1 deletion cmd/gitops/add/clusters/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func getClusterCmdRunE(endpoint *string, client *resty.Client) func(*cobra.Comma
return cmderrors.ErrNoURL
}

url, err := gitproviders.NewRepoURL(flags.RepositoryURL, true)
url, err := gitproviders.NewRepoURL(flags.RepositoryURL)
if err != nil {
return fmt.Errorf("cannot parse url: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/gitops/beta/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func installRunCmd(cmd *cobra.Command, args []string) error {
}

gitClient, gitProvider, err := factory.GetGitClients(context.Background(), k, providerClient, services.GitConfigParams{
URL: installParams.ConfigRepo,
Namespace: namespace,
DryRun: installParams.DryRun,
ConfigRepo: installParams.ConfigRepo,
Namespace: namespace,
DryRun: installParams.DryRun,
})
if err != nil {
return fmt.Errorf("error creating git clients: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gitops/delete/clusters/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func getClusterCmdRunE(endpoint *string, client *resty.Client) func(*cobra.Comma
return cmderrors.ErrNoURL
}

url, err := gitproviders.NewRepoURL(flags.RepositoryURL, true)
url, err := gitproviders.NewRepoURL(flags.RepositoryURL)
if err != nil {
return fmt.Errorf("cannot parse url: %w", err)
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/gitops/install/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func installRunCmd(cmd *cobra.Command, args []string) error {
ctx := context.Background()
namespace, _ := cmd.Parent().Flags().GetString("namespace")

configURL, err := gitproviders.NewRepoURL(installParams.ConfigRepo, true)
configURL, err := gitproviders.NewRepoURL(installParams.ConfigRepo)
if err != nil {
return err
}
Expand Down Expand Up @@ -119,8 +119,6 @@ func installRunCmd(cmd *cobra.Command, args []string) error {
// This is going to be broken up to reduce complexity
// and then generates the source yaml of the config repo when using dry-run option
gitClient, gitProvider, err := factory.GetGitClients(context.Background(), kubeClient, providerClient, services.GitConfigParams{
// We need to set URL and ConfigRepo to the same value so a deploy key is created for public config repos
URL: installParams.ConfigRepo,
ConfigRepo: installParams.ConfigRepo,
Namespace: namespace,
DryRun: installParams.DryRun,
Expand Down
6 changes: 3 additions & 3 deletions cmd/gitops/upgrade/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func upgradeCmdRunE() func(*cobra.Command, []string) error {
providerClient := internal.NewGitProviderClient(os.Stdout, os.LookupEnv, auth.NewAuthCLIHandler, log)

gitClient, gitProvider, err := factory.GetGitClients(ctx, kubeClient, providerClient, services.GitConfigParams{
URL: upgradeCmdFlags.ConfigRepo,
Namespace: upgradeCmdFlags.Namespace,
DryRun: upgradeCmdFlags.DryRun,
ConfigRepo: upgradeCmdFlags.ConfigRepo,
Namespace: upgradeCmdFlags.Namespace,
DryRun: upgradeCmdFlags.DryRun,
})
if err != nil {
return fmt.Errorf("failed to get git clients: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/internal/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var _ = Describe("Get git provider", func() {
It("invalid token key returns an error", func() {
fakeLogger = &loggerfakes.FakeLogger{}
client = NewGitProviderClient(os.Stdout, fakeEnvLookupExists, fakeAuthHandlerFuncError, fakeLogger)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@some-bucket.com/weaveworks/weave-gitops.git", false)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@some-bucket.com/weaveworks/weave-gitops.git")

provider, err := client.GetProvider(repoUrl, fakeAccountGetterSuccess)
Expect(provider).To(BeNil())
Expand All @@ -87,7 +87,7 @@ var _ = Describe("Get git provider", func() {
BeforeEach(func() {
fakeLogger = &loggerfakes.FakeLogger{}
client = NewGitProviderClient(os.Stdout, fakeEnvLookupExists, fakeAuthHandlerFuncError, fakeLogger)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@github.com/weaveworks/weave-gitops.git", false)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@github.com/weaveworks/weave-gitops.git")
})

It("gitproviders.New returns an error", func() {
Expand All @@ -111,7 +111,7 @@ var _ = Describe("Get git provider", func() {
BeforeEach(func() {
fakeLogger = &loggerfakes.FakeLogger{}
client = NewGitProviderClient(os.Stdout, fakeEnvLookupExists, fakeAuthHandlerFuncError, fakeLogger)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@gitlab.com/weaveworks/weave-gitops.git", false)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@gitlab.com/weaveworks/weave-gitops.git")
})

It("gitproviders.New returns an error", func() {
Expand All @@ -135,7 +135,7 @@ var _ = Describe("Get git provider", func() {
fakeLogger = &loggerfakes.FakeLogger{
WarningfStub: func(fmtArg string, restArgs ...interface{}) {},
}
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@github.com/weaveworks/weave-gitops.git", false)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@github.com/weaveworks/weave-gitops.git")
})

AfterEach(func() {
Expand Down
8 changes: 4 additions & 4 deletions pkg/flux/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var _ = Describe("CreateSourceGit", func() {
return []byte("out"), nil
}

repoUrl, err := gitproviders.NewRepoURL("https://github.com/foo/my-name", false)
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())
Expand All @@ -100,7 +100,7 @@ var _ = Describe("CreateSourceGit", func() {
runner.RunStub = func(s1 string, s2 ...string) ([]byte, error) {
return []byte("out"), nil
}
repoUrl, err := gitproviders.NewRepoURL("ssh://git@github.com/foo/my-name", false)
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())
Expand All @@ -119,7 +119,7 @@ var _ = Describe("CreateSourceGit", func() {
return []byte("out"), nil
}

repoUrl, err := gitproviders.NewRepoURL("ssh://git@gitlab.com/foo/my-name", false)
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())
Expand Down Expand Up @@ -244,7 +244,7 @@ var _ = Describe("CreateSecretGit", func() {
return []byte(`ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCh...`), nil
}

repoUrl, err := gitproviders.NewRepoURL("ssh://git@github.com/foo/bar.git", false)
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())
Expand Down
2 changes: 1 addition & 1 deletion pkg/gitproviders/provider_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var _ = Describe("Org Provider", func() {
}

var err error
repoUrl, err = NewRepoURL("http://github.com/owner/repo-name", false)
repoUrl, err = NewRepoURL("http://github.com/owner/repo-name")
Expect(err).ToNot(HaveOccurred())
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/gitproviders/provider_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = Describe("User Provider", func() {
}

var err error
repoUrl, err = NewRepoURL("http://github.com/owner/repo-name", false)
repoUrl, err = NewRepoURL("http://github.com/owner/repo-name")
Expect(err).ToNot(HaveOccurred())
})

Expand Down
32 changes: 13 additions & 19 deletions pkg/gitproviders/repo_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ const RepositoryURLProtocolHTTPS RepositoryURLProtocol = "https"
const RepositoryURLProtocolSSH RepositoryURLProtocol = "ssh"

type RepoURL struct {
repoName string
owner string
url *url.URL
normalized string
provider GitProviderName
protocol RepositoryURLProtocol
isConfigRepo bool
repoName string
owner string
url *url.URL
normalized string
provider GitProviderName
protocol RepositoryURLProtocol
}

func NewRepoURL(uri string, isConfigRepo bool) (RepoURL, error) {
func NewRepoURL(uri string) (RepoURL, error) {
providerName, err := detectGitProviderFromUrl(uri, ViperGetStringMapString("git-host-types"))
if err != nil {
return RepoURL{}, fmt.Errorf("could not get provider name from URL %s: %w", uri, err)
Expand All @@ -53,13 +52,12 @@ func NewRepoURL(uri string, isConfigRepo bool) (RepoURL, error) {
}

return RepoURL{
repoName: utils.UrlToRepoName(uri),
owner: owner,
url: u,
normalized: normalized,
provider: providerName,
protocol: protocol,
isConfigRepo: isConfigRepo,
repoName: utils.UrlToRepoName(uri),
owner: owner,
url: u,
normalized: normalized,
provider: providerName,
protocol: protocol,
}, nil
}

Expand Down Expand Up @@ -87,10 +85,6 @@ func (n RepoURL) Protocol() RepositoryURLProtocol {
return n.protocol
}

func (n RepoURL) IsConfigRepo() bool {
return n.isConfigRepo
}

func getOwnerFromUrl(url url.URL, providerName GitProviderName) (string, error) {
url.Path = strings.TrimPrefix(url.Path, "/")

Expand Down
2 changes: 1 addition & 1 deletion pkg/gitproviders/repo_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var _ = DescribeTable("NewRepoURL", func(input, gitProviderEnv string, expected
if gitProviderEnv != "" {
viper.Set("git-host-types", gitProviderEnv)
}
result, err := NewRepoURL(input, false)
result, err := NewRepoURL(input)
Expect(err).NotTo(HaveOccurred())

Expect(result.String()).To(Equal(expected.s))
Expand Down
4 changes: 2 additions & 2 deletions pkg/models/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var _ = Describe("Installer", func() {
}
var err error
var _ = BeforeEach(func() {
params.ConfigRepo, err = gitproviders.NewRepoURL("ssh://git@github.com/test-user/test-repo", true)
params.ConfigRepo, err = gitproviders.NewRepoURL("ssh://git@github.com/test-user/test-repo")

fakeFluxClient = &fluxfakes.FakeFlux{}
fakeKubeClient = &kubefakes.FakeKube{}
Expand Down Expand Up @@ -145,7 +145,7 @@ var _ = Describe("Installer", func() {

boostrapManifests = []Manifest{}

configRepo, err := gitproviders.NewRepoURL("ssh://git@github.com/test-user/test-repo", true)
configRepo, err := gitproviders.NewRepoURL("ssh://git@github.com/test-user/test-repo")
Expect(err).ShouldNot(HaveOccurred())

params = GitopsManifestsParams{
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/internal/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var _ = Describe("Get git provider", func() {
var repoUrl gitproviders.RepoURL
BeforeEach(func() {
client = NewGitProviderClient(fakeToken)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@github.com/weaveworks/weave-gitops.git", false)
repoUrl, _ = gitproviders.NewRepoURL("ssh://git@github.com/weaveworks/weave-gitops.git")
})

It("gitproviders.New throws an error", func() {
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (s *applicationServer) AddApplication(ctx context.Context, msg *pb.AddAppli
return nil, fmt.Errorf("failed to create kube service: %w", err)
}

appUrl, err := gitproviders.NewRepoURL(msg.Url, false)
appUrl, err := gitproviders.NewRepoURL(msg.Url)
if err != nil {
return nil, grpcStatus.Errorf(codes.InvalidArgument, "unable to parse app url %q: %s", msg.Url, err)
}
Expand All @@ -314,7 +314,7 @@ func (s *applicationServer) AddApplication(ctx context.Context, msg *pb.AddAppli
return nil, fmt.Errorf("failed getting wego config")
}

configRepo, err := gitproviders.NewRepoURL(wegoConfig.ConfigRepo, true)
configRepo, err := gitproviders.NewRepoURL(wegoConfig.ConfigRepo)
if err != nil {
return nil, grpcStatus.Errorf(codes.InvalidArgument, "unable to parse config url %q: %s", wegoConfig.ConfigRepo, err)
}
Expand Down Expand Up @@ -677,7 +677,7 @@ func (s *applicationServer) Authenticate(_ context.Context, msg *pb.Authenticate
}

func (s *applicationServer) ParseRepoURL(ctx context.Context, msg *pb.ParseRepoURLRequest) (*pb.ParseRepoURLResponse, error) {
u, err := gitproviders.NewRepoURL(msg.Url, false)
u, err := gitproviders.NewRepoURL(msg.Url)
if err != nil {
return nil, grpcStatus.Errorf(codes.InvalidArgument, "could not parse url: %s", err.Error())
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/services/app/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -141,10 +142,13 @@ func (a *AppSvc) updateParametersIfNecessary(ctx context.Context, gitProvider gi
return params, fmt.Errorf("--url must be specified for helm repositories")
}

if params.ConfigRepo == "" {
return params, errors.New("--config-repo should be provided")
}
default:
var err error

appRepoUrl, err = gitproviders.NewRepoURL(params.Url, false)
appRepoUrl, err = gitproviders.NewRepoURL(params.Url)
if err != nil {
return params, fmt.Errorf("error normalizing url: %w", err)
}
Expand All @@ -157,7 +161,7 @@ func (a *AppSvc) updateParametersIfNecessary(ctx context.Context, gitProvider gi

// making sure the config url is in good format
if models.IsExternalConfigRepo(params.ConfigRepo) {
configRepoUrl, err := gitproviders.NewRepoURL(params.ConfigRepo, true)
configRepoUrl, err := gitproviders.NewRepoURL(params.ConfigRepo)
if err != nil {
return params, fmt.Errorf("error normalizing url: %w", err)
}
Expand Down Expand Up @@ -231,7 +235,7 @@ func makeApplication(params AddParams) (models.Application, error) {
if models.SourceType(params.SourceType) == models.SourceTypeHelm {
helmSourceURL = params.Url
} else {
gitSourceURL, err = gitproviders.NewRepoURL(params.Url, false)
gitSourceURL, err = gitproviders.NewRepoURL(params.Url)
if err != nil {
return models.Application{}, err
}
Expand All @@ -240,7 +244,7 @@ func makeApplication(params AddParams) (models.Application, error) {
configRepo := gitSourceURL

if params.ConfigRepo != "" {
curl, err := gitproviders.NewRepoURL(params.ConfigRepo, true)
curl, err := gitproviders.NewRepoURL(params.ConfigRepo)
if err != nil {
return models.Application{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/app/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (a *AppSvc) GetCommits(gitProvider gitproviders.GitProvider, params CommitP
return nil, fmt.Errorf("unable to get commits for a helm chart")
}

repoUrl, err := gitproviders.NewRepoURL(application.Spec.URL, false)
repoUrl, err := gitproviders.NewRepoURL(application.Spec.URL)
if err != nil {
return nil, fmt.Errorf("error creating normalized url: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/applicationv2/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func translateApp(app wego.Application) (models.Application, error) {
)

if wego.DeploymentType(app.Spec.SourceType) == wego.DeploymentType(wego.SourceTypeGit) {
appRepoUrl, err = gitproviders.NewRepoURL(app.Spec.URL, false)
appRepoUrl, err = gitproviders.NewRepoURL(app.Spec.URL)
if err != nil {
return models.Application{}, err
}
Expand All @@ -92,7 +92,7 @@ func translateApp(app wego.Application) (models.Application, error) {
}

if models.IsExternalConfigRepo(app.Spec.ConfigRepo) {
configRepoUrl, err = gitproviders.NewRepoURL(app.Spec.ConfigRepo, true)
configRepoUrl, err = gitproviders.NewRepoURL(app.Spec.ConfigRepo)
if err != nil {
return models.Application{}, err
}
Expand Down