Skip to content

Commit

Permalink
Merge 4279701 into 8a9c186
Browse files Browse the repository at this point in the history
  • Loading branch information
josecordaz committed Jan 26, 2022
2 parents 8a9c186 + 4279701 commit 2cf4aad
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 161 deletions.
52 changes: 30 additions & 22 deletions cmd/gitops/install/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
"github.com/weaveworks/weave-gitops/cmd/gitops/version"
"github.com/weaveworks/weave-gitops/cmd/internal"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/git"
"github.com/weaveworks/weave-gitops/pkg/git/wrapper"
"github.com/weaveworks/weave-gitops/pkg/gitproviders"
"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/services"
"github.com/weaveworks/weave-gitops/pkg/services/auth"
"github.com/weaveworks/weave-gitops/pkg/services/gitopswriter"
"github.com/weaveworks/weave-gitops/pkg/services/install"
Expand Down Expand Up @@ -73,7 +74,7 @@ func installRunCmd(cmd *cobra.Command, args []string) error {
osysClient := osys.New()
fluxClient := flux.New(osysClient, &runner.CLIRunner{})

kubeClient, _, err := kube.NewKubeHTTPClient()
kubeClient, rawK8sClient, err := kube.NewKubeHTTPClient()
if err != nil {
return fmt.Errorf("error creating k8s http client: %w", err)
}
Expand All @@ -83,8 +84,24 @@ func installRunCmd(cmd *cobra.Command, args []string) error {
return err
}

log := internal.NewCLILogger(os.Stdout)

token, err := internal.GetToken(configURL, osysClient.Stdout(), osysClient.LookupEnv, auth.NewAuthCLIHandler, log)
if err != nil {
return err
}

gitProvider, err := gitproviders.New(gitproviders.Config{
Provider: configURL.Provider(),
Token: token,
Hostname: configURL.URL().Host,
}, configURL.Owner(), gitproviders.GetAccountType)
if err != nil {
return fmt.Errorf("error creating git provider client: %w", err)
}

if installParams.DryRun {
manifests, err := models.BootstrapManifests(fluxClient, clusterName, namespace, configURL)
manifests, err := models.BootstrapManifests(ctx, fluxClient, gitProvider, clusterName, namespace, configURL)
if err != nil {
return fmt.Errorf("failed getting gitops manifests: %w", err)
}
Expand All @@ -96,36 +113,27 @@ func installRunCmd(cmd *cobra.Command, args []string) error {
return nil
}

log := internal.NewCLILogger(os.Stdout)
providerClient := internal.NewGitProviderClient(osysClient.Stdout(), osysClient.LookupEnv, auth.NewAuthCLIHandler, log)

factory := services.NewFactory(fluxClient, log)

// We temporarily need this here otherwise GetGitClients is going to fail
// as it needs the namespace created to apply the secret
namespaceObj := &corev1.Namespace{}
namespaceObj.Name = namespace

if err := kubeClient.Raw().Create(ctx, namespaceObj); err != nil {
if err := rawK8sClient.Create(ctx, namespaceObj); err != nil {
if !strings.Contains(err.Error(), "already exists") {
return fmt.Errorf("failed creating namespace %s: %w", namespace, err)
}
}

// This is creating the secret, uploads it and applies it to the cluster
// 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,
})
authService, err := auth.NewAuthService(fluxClient, rawK8sClient, gitProvider, log)
if err != nil {
return fmt.Errorf("failed getting git clients: %w", err)
return err
}

deployKey, err := authService.SetupDeployKey(ctx, namespace, configURL)
if err != nil {
return err
}

gitClient := git.New(deployKey, wrapper.NewGoGit())

repoWriter := gitopswriter.NewRepoWriter(log, gitClient, gitProvider)
installer := install.NewInstaller(fluxClient, kubeClient, gitClient, gitProvider, log, repoWriter)

Expand Down
27 changes: 14 additions & 13 deletions pkg/models/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
)

// BootstrapManifests creates all yaml files that are going to be applied to the cluster
func BootstrapManifests(fluxClient flux.Flux, clusterName string, namespace string, configURL gitproviders.RepoURL) ([]Manifest, error) {
func BootstrapManifests(ctx context.Context, fluxClient flux.Flux, gitProvider gitproviders.GitProvider, clusterName string, namespace string, configURL gitproviders.RepoURL) ([]Manifest, error) {
runtimeManifests, err := fluxClient.Install(namespace, true)
if err != nil {
return nil, fmt.Errorf("failed getting runtime manifests: %w", err)
Expand Down Expand Up @@ -92,6 +92,16 @@ func BootstrapManifests(fluxClient flux.Flux, clusterName string, namespace stri
return nil, fmt.Errorf("failed marshalling wego config: %w", err)
}

configBranch, err := gitProvider.GetDefaultBranch(ctx, configURL)
if err != nil {
return nil, err
}

sourceManifest, err := GetSourceManifest(ctx, fluxClient, gitProvider, clusterName, namespace, configURL, configBranch)
if err != nil {
return nil, err
}

return []Manifest{
{
Path: git.GetSystemQualifiedPath(clusterName, AppCRDPath),
Expand All @@ -117,12 +127,13 @@ func BootstrapManifests(fluxClient flux.Flux, clusterName string, namespace stri
Path: git.GetSystemQualifiedPath(clusterName, WegoConfigPath),
Content: wegoConfigManifest,
},
sourceManifest,
}, nil
}

// GitopsManifests generates all yaml files that are going to be written in the config repo
func GitopsManifests(ctx context.Context, fluxClient flux.Flux, gitProvider gitproviders.GitProvider, clusterName string, namespace string, configURL gitproviders.RepoURL) ([]Manifest, error) {
bootstrapManifest, err := BootstrapManifests(fluxClient, clusterName, namespace, configURL)
bootstrapManifest, err := BootstrapManifests(ctx, fluxClient, gitProvider, clusterName, namespace, configURL)
if err != nil {
return nil, err
}
Expand All @@ -134,17 +145,7 @@ func GitopsManifests(ctx context.Context, fluxClient flux.Flux, gitProvider gitp
return nil, err
}

configBranch, err := gitProvider.GetDefaultBranch(ctx, configURL)
if err != nil {
return nil, err
}

sourceManifest, err := GetSourceManifest(ctx, fluxClient, gitProvider, clusterName, namespace, configURL, configBranch)
if err != nil {
return nil, err
}

return append(bootstrapManifest, sourceManifest, // TODO: Move this to boostrap manifests until getGitClients is refactored
return append(bootstrapManifest,
Manifest{
Path: git.GetSystemQualifiedPath(clusterName, SystemKustomizationPath),
Content: systemKustomizationManifest,
Expand Down
61 changes: 32 additions & 29 deletions pkg/models/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ var _ = Describe("Installer", func() {
const testNamespace = "test-namespace"
var configRepo gitproviders.RepoURL
var err error
var ctx context.Context
var _ = BeforeEach(func() {
ctx = context.Background()
fakeFluxClient = &fluxfakes.FakeFlux{}
fakeGitProvider = &gitprovidersfakes.FakeGitProvider{}
configRepo, err = gitproviders.NewRepoURL("ssh://git@github.com/test-user/test-repo", true)
Expect(err).ShouldNot(HaveOccurred())
})
Expand All @@ -41,7 +44,7 @@ var _ = Describe("Installer", func() {

fakeFluxClient.InstallReturns(nil, someError)

_, err = BootstrapManifests(fakeFluxClient, clusterName, testNamespace, configRepo)
_, err = BootstrapManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expect(err.Error()).Should(ContainSubstring(someError.Error()))
})

Expand All @@ -51,7 +54,7 @@ var _ = Describe("Installer", func() {

fakeFluxClient.CreateKustomizationReturns(nil, someError)

_, err = BootstrapManifests(fakeFluxClient, clusterName, testNamespace, configRepo)
_, err = BootstrapManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expect(err.Error()).Should(ContainSubstring(someError.Error()))
})

Expand All @@ -62,7 +65,7 @@ var _ = Describe("Installer", func() {
fakeFluxClient.CreateKustomizationReturnsOnCall(0, nil, nil)
fakeFluxClient.CreateKustomizationReturnsOnCall(1, nil, someError)

_, err = BootstrapManifests(fakeFluxClient, clusterName, testNamespace, configRepo)
_, err = BootstrapManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expect(err.Error()).Should(ContainSubstring(someError.Error()))
})
})
Expand All @@ -89,10 +92,18 @@ var _ = Describe("Installer", func() {
wegoConfigManifest, err := yaml.Marshal(gitopsConfigMap)
Expect(err).ShouldNot(HaveOccurred())

manifestsFiles, err := BootstrapManifests(fakeFluxClient, clusterName, testNamespace, configRepo)
gitSource := []byte("git source")
fakeFluxClient.CreateSourceGitReturns(gitSource, nil)

fakeGitProvider.GetDefaultBranchReturns("main", nil)

privateVisibility := gitprovider.RepositoryVisibilityPrivate
fakeGitProvider.GetRepoVisibilityReturns(&privateVisibility, nil)

manifestsFiles, err := BootstrapManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expect(err).ShouldNot(HaveOccurred())

Expect(len(manifestsFiles)).Should(Equal(6))
Expect(len(manifestsFiles)).Should(Equal(7))

expectedManifests := []Manifest{
{
Expand All @@ -119,6 +130,10 @@ var _ = Describe("Installer", func() {
Path: git.GetSystemQualifiedPath(clusterName, WegoConfigPath),
Content: wegoConfigManifest,
},
{
Path: git.GetSystemQualifiedPath(clusterName, SourcePath),
Content: gitSource,
},
}

for ind, manifest := range manifestsFiles {
Expand All @@ -136,45 +151,41 @@ var _ = Describe("Installer", func() {
ctx = context.Background()
fakeGitProvider = &gitprovidersfakes.FakeGitProvider{}
})

Context("error paths", func() {
someError := errors.New("some error")
It("should fail getting runtime manifests", func() {

BeforeEach(func() {
fakeFluxClient.InstallReturns(nil, nil)

privateVisibility := gitprovider.RepositoryVisibilityPrivate
fakeGitProvider.GetRepoVisibilityReturns(&privateVisibility, nil)

fakeGitProvider.GetDefaultBranchReturns("main", nil)
})

It("should fail getting runtime manifests", func() {
fakeFluxClient.InstallReturns(nil, someError)

_, err = GitopsManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expect(err.Error()).Should(ContainSubstring(someError.Error()))
})

It("should fail getting secret name for private git source", func() {
fakeFluxClient.InstallReturns(nil, nil)

fakeGitProvider.GetRepoVisibilityReturns(nil, someError)

_, err = GitopsManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expect(err.Error()).Should(ContainSubstring(someError.Error()))
})

It("should fail getting secret name for private git source", func() {
fakeFluxClient.InstallReturns(nil, nil)

privateVisibility := gitprovider.RepositoryVisibilityPrivate
fakeGitProvider.GetRepoVisibilityReturns(&privateVisibility, nil)

fakeGitProvider.GetDefaultBranchReturns("", someError)

_, err = GitopsManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expect(err.Error()).Should(ContainSubstring(someError.Error()))
})

It("should fail creating flux source", func() {
fakeFluxClient.InstallReturns(nil, nil)

privateVisibility := gitprovider.RepositoryVisibilityPrivate
fakeGitProvider.GetRepoVisibilityReturns(&privateVisibility, nil)

fakeGitProvider.GetDefaultBranchReturns("main", nil)

fakeFluxClient.CreateSourceGitReturns(nil, someError)

_, err = GitopsManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expand All @@ -196,19 +207,12 @@ var _ = Describe("Installer", func() {
systemKustomizationManifest, err := yaml.Marshal(systemKustomization)
Expect(err).ShouldNot(HaveOccurred())

gitSource := []byte("git source")
fakeFluxClient.CreateSourceGitReturns(gitSource, nil)

manifestsFiles, err := GitopsManifests(ctx, fakeFluxClient, fakeGitProvider, clusterName, testNamespace, configRepo)
Expect(err).ShouldNot(HaveOccurred())

Expect(len(manifestsFiles)).Should(Equal(9))

expectedManifests := []Manifest{
{
Path: git.GetSystemQualifiedPath(clusterName, SourcePath),
Content: gitSource,
},
{
Path: git.GetSystemQualifiedPath(clusterName, SystemKustomizationPath),
Content: systemKustomizationManifest,
Expand All @@ -219,12 +223,11 @@ var _ = Describe("Installer", func() {
},
}

for ind, manifest := range manifestsFiles[6:] {
for ind, manifest := range manifestsFiles[7:] {
Expect(manifest.Path).Should(Equal(expectedManifests[ind].Path))
Expect(string(manifest.Content)).Should(Equal(string(expectedManifests[ind].Content)))
}
})

})
})
})
Loading

0 comments on commit 2cf4aad

Please sign in to comment.