Skip to content

Commit

Permalink
Print out GitSource from config repository on dry-run (#1320)
Browse files Browse the repository at this point in the history
* Make dry-run in install working

* write manifests to repo after applying them

* Moving gitOpsWriter out of Install

* Run raw flux install to get fancy progress logging

* Removed creation of namespace as `flux install` already creates it

* Removed unused osysClient parameter

* Create namespace before creating git clients

* Created new repoWriter layer to simplify the code

Added function to convert from Manifest to CommitFile

* Moving generation of manifests files to models folder.

Removed AssociateCluster method from GitOpsDirectoryWriter

* Pack all constants in the same place

* Removed cluster_generator.go file

* Removed unused constants

* Added error paths unit tests for installer

* Added missing files changes

* Fixed lint issues

* Added unit tests manifests functions

* Added initial code for install success when auto-merge is true

* Added rest of code for install success when auto-merge is true

* Fix lint issues

* Removed old install tests

* Remove unused variables

* Added success path for install when auto-merge is false

* Added missing validate wego installation functionality

* Use the right yaml library

* Fixed yaml dependency

* Added missing keep and source manifests and fixed unit tests

* Fixed manifests order in unit tests

* Fixed acceptance test "Verify that gitops quits if flux-system namespace is present"

* Fixed `install --dry-run` it was printing out raw bytes instead of string

Fixed regex on acceptance test

* Fixed Kustomize file on unit test

* Fixed Kustomize file on unit test

* Fixed typo and added specif error matches

* Clean up and fixed typo on assertion message

* Made some lines more readable

* Rename function CreateKustomize to CreateKustomization

Co-authored-by: Jordan Pellizzari <jordan@weave.works>

* Renamed function from GitopsConfigMap to CreateGitopsConfigMap

* Fixed references to old function

A function was renamed but their calls were not

* Fixed references to old function

A function was renamed but their calls were not

* Initial work to get token and deploy key right before setting git clients

This work could let us use flags to pass in some info like
1.- Secret names(in case they are already in the cluster)
2.- Deploy keys path. In case the user has the deploy key saved in a file
3.- Provider deploy key name. In case the user already has a deploy key in the provider. That we can later save to the cluster as secret. We would need to authenticate the first time to retrieve it.

Instantiating git clients separately

* Added source flux resource back to dry-run output

* Fixed lint issues

* Fixed references to old function

A function was renamed but their calls were not

* Use the right manifests when creating the PR

* Fixed unit test it was not generating adding the wego app path to the kustomization of the expected file

* Clean up comments

* Updated comments

* Fixed installer unit tests

* Fixed manifests unit tests

* Removed commented code

* Removed old SetupDeployKey2 function

* Fixed lint issue

* Fixed install unit tests

* Fixed manifests unit tests

* Fixed auth unit tests

* Removed targetName field as it wasn't used

* Fixed auth unit tests

* Organize imports on install command

* Fixed parameters on install/cmd.go

Co-authored-by: Jordan Pellizzari <jordan@weave.works>
  • Loading branch information
josecordaz and jpellizzari committed Mar 3, 2022
1 parent fb28582 commit bf6dc53
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 287 deletions.
50 changes: 30 additions & 20 deletions cmd/gitops/install/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,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 @@ -72,7 +73,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 @@ -82,8 +83,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, kubeClient, models.BootstrapManifestsParams{
manifests, err := models.BootstrapManifests(ctx, fluxClient, gitProvider, kubeClient, models.ManifestsParams{
ClusterName: clusterName,
WegoNamespace: namespace,
ConfigRepo: configURL,
Expand All @@ -99,34 +116,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{
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
46 changes: 20 additions & 26 deletions pkg/models/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ const (
WegoConfigMapName = "weave-gitops-config"
)

type BootstrapManifestsParams struct {
type ManifestsParams struct {
ClusterName string
WegoNamespace string
ConfigRepo gitproviders.RepoURL
}

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

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

sourceManifest, err := GetSourceManifest(ctx, fluxClient, gitProvider, params.ClusterName, params.WegoNamespace, params.ConfigRepo, configBranch)
if err != nil {
return nil, err
}

return []Manifest{
{
Path: git.GetSystemQualifiedPath(params.ClusterName, AppCRDPath),
Expand All @@ -138,45 +148,29 @@ func BootstrapManifests(fluxClient flux.Flux, kubeClient kube.Kube, params Boots
Path: git.GetSystemQualifiedPath(params.ClusterName, WegoConfigPath),
Content: wegoConfigManifest,
},
sourceManifest,
}, nil
}

type GitopsManifestsParams struct {
FluxClient flux.Flux
GitProvider gitproviders.GitProvider
ClusterName string
WegoNamespace string
ConfigRepo gitproviders.RepoURL
}

// GitopsManifests generates all yaml files that are going to be written in the config repo
func GitopsManifests(ctx context.Context, bootstrapManifests []Manifest, params GitopsManifestsParams) ([]Manifest, error) {
// NoClusterApplicableManifests generates all yaml files that are going to be written in the config repo and cannot be applied to the cluster directly
func NoClusterApplicableManifests(params ManifestsParams) ([]Manifest, error) {
systemKustomization := CreateKustomization(params.ClusterName, params.WegoNamespace, RuntimePath, SourcePath, SystemKustResourcePath, UserKustResourcePath, WegoAppPath)

systemKustomizationManifest, err := yaml.Marshal(systemKustomization)
if err != nil {
return nil, err
}

configBranch, err := params.GitProvider.GetDefaultBranch(ctx, params.ConfigRepo)
if err != nil {
return nil, err
}

sourceManifest, err := GetSourceManifest(ctx, params.FluxClient, params.GitProvider, params.ClusterName, params.WegoNamespace, params.ConfigRepo, configBranch)
if err != nil {
return nil, err
}

return append(bootstrapManifests, sourceManifest, // TODO: Move this to boostrap manifests until getGitClients is refactored
Manifest{
return []Manifest{
{
Path: git.GetSystemQualifiedPath(params.ClusterName, SystemKustomizationPath),
Content: systemKustomizationManifest,
},
Manifest{
{
Path: filepath.Join(git.GetUserPath(params.ClusterName), ".keep"),
Content: strconv.AppendQuote(nil, "# keep"),
}), nil
},
}, nil
}

func CreateKustomization(name, namespace string, resources ...string) types.Kustomization {
Expand Down

0 comments on commit bf6dc53

Please sign in to comment.