Skip to content

Commit

Permalink
Merge af587cf into 8a9c186
Browse files Browse the repository at this point in the history
  • Loading branch information
luizbafilho committed Jan 26, 2022
2 parents 8a9c186 + af587cf commit a039237
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 265 deletions.
9 changes: 9 additions & 0 deletions cmd/gitops/add/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ func runCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to create app service: %w", err)
}

wegoConfig, err := kubeClient.GetWegoConfig(ctx, params.Namespace)
if err != nil {
return fmt.Errorf("failed getting wego config")
}

if params.ConfigRepo == "" {
params.ConfigRepo = wegoConfig.ConfigRepo
}

gitClient, gitProvider, err := factory.GetGitClients(ctx, kubeClient, providerClient, services.GitConfigParams{
URL: params.Url,
ConfigRepo: params.ConfigRepo,
Expand Down
25 changes: 22 additions & 3 deletions cmd/gitops/install/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ package install
import (
"context"
_ "embed"
"errors"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"

wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/cmd/gitops/version"
"github.com/weaveworks/weave-gitops/cmd/internal"
Expand All @@ -26,6 +25,8 @@ import (
"github.com/weaveworks/weave-gitops/pkg/services/auth"
"github.com/weaveworks/weave-gitops/pkg/services/gitopswriter"
"github.com/weaveworks/weave-gitops/pkg/services/install"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type params struct {
Expand Down Expand Up @@ -84,7 +85,25 @@ func installRunCmd(cmd *cobra.Command, args []string) error {
}

if installParams.DryRun {
manifests, err := models.BootstrapManifests(fluxClient, clusterName, namespace, configURL)
fluxNamespace, err := kubeClient.FetchNamespaceWithLabel(ctx, flux.PartOfLabelKey, flux.PartOfLabelValue)
if err != nil {
if !errors.Is(err, kube.ErrNamespaceNotFound) {
return fmt.Errorf("failed fetching flux namespace: %w", err)
}
}

if fluxNamespace == nil {
fluxNamespace = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{
Name: namespace,
}}
}

manifests, err := models.BootstrapManifests(fluxClient, models.BootstrapManifestsParams{
ClusterName: clusterName,
WegoNamespace: namespace,
FluxNamespace: fluxNamespace.Name,
ConfigRepo: configURL,
})
if err != nil {
return fmt.Errorf("failed getting gitops manifests: %w", err)
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/gitops/upgrade/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ var Cmd = &cobra.Command{
}

func init() {
Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.ConfigRepo, "config-repo", "", "URL of external repository that will hold automation manifests")
Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.Version, "version", "", "Version of Weave GitOps Enterprise to be installed")
Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.BaseBranch, "base", "", "The base branch to open the pull request against")
Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.HeadBranch, "branch", "tier-upgrade-enterprise", "The branch to create the pull request from")
Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.CommitMessage, "commit-message", "Upgrade to WGE", "The commit message")
Cmd.PersistentFlags().StringArrayVar(&upgradeCmdFlags.Values, "set", []string{}, "set profile values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
Cmd.PersistentFlags().BoolVar(&upgradeCmdFlags.DryRun, "dry-run", false, "Output the generated profile without creating a pull request")

cobra.CheckErr(Cmd.MarkPersistentFlagRequired("config-repo"))
cobra.CheckErr(Cmd.MarkPersistentFlagRequired("version"))
}

Expand All @@ -70,6 +68,15 @@ func upgradeCmdRunE() func(*cobra.Command, []string) error {
fluxClient := flux.New(osys.New(), &runner.CLIRunner{})
factory := services.NewFactory(fluxClient, log)

wegoConfig, err := kubeClient.GetWegoConfig(ctx, namespace)
if err != nil {
return fmt.Errorf("failed getting wego config")
}

if upgradeCmdFlags.ConfigRepo == "" {
upgradeCmdFlags.ConfigRepo = wegoConfig.ConfigRepo
}

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

gitClient, gitProvider, err := factory.GetGitClients(ctx, kubeClient, providerClient, services.GitConfigParams{
Expand Down
1 change: 1 addition & 0 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var toStatusString = map[ClusterStatus]string{
type WegoConfig struct {
FluxNamespace string
WegoNamespace string
ConfigRepo string
}

//counterfeiter:generate . Kube
Expand Down
68 changes: 40 additions & 28 deletions pkg/models/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ const (
WegoConfigMapName = "weave-gitops-config"
)

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

// 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) {
runtimeManifests, err := fluxClient.Install(namespace, true)
func BootstrapManifests(fluxClient flux.Flux, params BootstrapManifestsParams) ([]Manifest, error) {
runtimeManifests, err := fluxClient.Install(params.WegoNamespace, true)
if err != nil {
return nil, fmt.Errorf("failed getting runtime manifests: %w", err)
}
Expand All @@ -58,31 +65,31 @@ func BootstrapManifests(fluxClient flux.Flux, clusterName string, namespace stri
version = "latest"
}

wegoAppManifests, err := manifests.GenerateWegoAppManifests(manifests.Params{AppVersion: version, Namespace: namespace})
wegoAppManifests, err := manifests.GenerateWegoAppManifests(manifests.Params{AppVersion: version, Namespace: params.WegoNamespace})
if err != nil {
return nil, fmt.Errorf("error generating wego-app manifest: %w", err)
}

wegoAppManifest := bytes.Join(wegoAppManifests, []byte("---\n"))

sourceName := CreateClusterSourceName(configURL)
systemResourceName := ConstrainResourceName(fmt.Sprintf("%s-system", clusterName))
sourceName := CreateClusterSourceName(params.ConfigRepo)
systemResourceName := ConstrainResourceName(fmt.Sprintf("%s-system", params.ClusterName))

systemKustResourceManifest, err := fluxClient.CreateKustomization(systemResourceName, sourceName,
workAroundFluxDroppingDot(git.GetSystemPath(clusterName)), namespace)
workAroundFluxDroppingDot(git.GetSystemPath(params.ClusterName)), params.WegoNamespace)
if err != nil {
return nil, err
}

userResourceName := ConstrainResourceName(fmt.Sprintf("%s-user", clusterName))
userResourceName := ConstrainResourceName(fmt.Sprintf("%s-user", params.ClusterName))

userKustResourceManifest, err := fluxClient.CreateKustomization(userResourceName, sourceName,
workAroundFluxDroppingDot(git.GetUserPath(clusterName)), namespace)
workAroundFluxDroppingDot(git.GetUserPath(params.ClusterName)), params.WegoNamespace)
if err != nil {
return nil, err
}

gitopsConfigMap, err := CreateGitopsConfigMap(namespace, namespace)
gitopsConfigMap, err := CreateGitopsConfigMap(params.WegoNamespace, params.WegoNamespace, params.ConfigRepo.String())
if err != nil {
return nil, err
}
Expand All @@ -94,63 +101,66 @@ func BootstrapManifests(fluxClient flux.Flux, clusterName string, namespace stri

return []Manifest{
{
Path: git.GetSystemQualifiedPath(clusterName, AppCRDPath),
Path: git.GetSystemQualifiedPath(params.ClusterName, AppCRDPath),
Content: manifests.AppCRD,
},
{
Path: git.GetSystemQualifiedPath(clusterName, RuntimePath),
Path: git.GetSystemQualifiedPath(params.ClusterName, RuntimePath),
Content: runtimeManifests,
},
{
Path: git.GetSystemQualifiedPath(clusterName, SystemKustResourcePath),
Path: git.GetSystemQualifiedPath(params.ClusterName, SystemKustResourcePath),
Content: systemKustResourceManifest,
},
{
Path: git.GetSystemQualifiedPath(clusterName, UserKustResourcePath),
Path: git.GetSystemQualifiedPath(params.ClusterName, UserKustResourcePath),
Content: userKustResourceManifest,
},
{
Path: git.GetSystemQualifiedPath(clusterName, WegoAppPath),
Path: git.GetSystemQualifiedPath(params.ClusterName, WegoAppPath),
Content: wegoAppManifest,
},
{
Path: git.GetSystemQualifiedPath(clusterName, WegoConfigPath),
Path: git.GetSystemQualifiedPath(params.ClusterName, WegoConfigPath),
Content: wegoConfigManifest,
},
}, 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)
if err != nil {
return nil, err
}
type GitopsManifestsParams struct {
FluxClient flux.Flux
GitProvider gitproviders.GitProvider
ClusterName string
WegoNamespace string
ConfigRepo gitproviders.RepoURL
}

systemKustomization := CreateKustomization(clusterName, namespace, RuntimePath, SourcePath, SystemKustResourcePath, UserKustResourcePath, WegoAppPath)
// 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) {
systemKustomization := CreateKustomization(params.ClusterName, params.WegoNamespace, RuntimePath, SourcePath, SystemKustResourcePath, UserKustResourcePath, WegoAppPath)

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

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

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

return append(bootstrapManifest, sourceManifest, // TODO: Move this to boostrap manifests until getGitClients is refactored
return append(bootstrapManifests, sourceManifest, // TODO: Move this to boostrap manifests until getGitClients is refactored
Manifest{
Path: git.GetSystemQualifiedPath(clusterName, SystemKustomizationPath),
Path: git.GetSystemQualifiedPath(params.ClusterName, SystemKustomizationPath),
Content: systemKustomizationManifest,
},
Manifest{
Path: filepath.Join(git.GetUserPath(clusterName), ".keep"),
Path: filepath.Join(git.GetUserPath(params.ClusterName), ".keep"),
Content: strconv.AppendQuote(nil, "# keep"),
}), nil
}
Expand Down Expand Up @@ -232,9 +242,11 @@ func workAroundFluxDroppingDot(str string) string {
return "." + str
}

func CreateGitopsConfigMap(fluxNamespace string, wegoNamespace string) (corev1.ConfigMap, error) {
func CreateGitopsConfigMap(fluxNamespace string, wegoNamespace string, configRepo string) (corev1.ConfigMap, error) {
config := kube.WegoConfig{
FluxNamespace: fluxNamespace,
WegoNamespace: wegoNamespace,
ConfigRepo: configRepo,
}

configBytes, err := yaml.Marshal(config)
Expand Down
Loading

0 comments on commit a039237

Please sign in to comment.