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

Adapt gitops add ... commands to CAPI release structure (Breaking) #1403

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

- Use more portable, Bash specific shebang for GitOps pre-commit script template
- Schedule cluster upgrades for CAPI clusters.
- The way to specify a release in `kubectl gs gitops add workload-cluster` has changed. The flags `--default-apps-release` and `--cluster-release` have been replaced by `--release`.
- Print Release information in `get cluster` command.

## [3.1.0] - 2024-07-23
Expand All @@ -24,7 +25,7 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

### Changed

- **BREAKING** When templating cluster manifests for CAPZ clusters with `kubectl gs template cluster` command, now we set the workload cluster release version via the `--release` flag, instead setting cluster-azure version via `--cluster-version`.
- **BREAKING** When templating cluster manifests for CAPZ clusters with `kubectl gs template cluster` command, now we set the workload cluster release version via the `--release` flag, instead setting cluster-azure version via `--cluster-version`.

## [3.0.0] - 2024-06-27

Expand Down
114 changes: 2 additions & 112 deletions cmd/gitops/add/base/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"io"
"strconv"

k8smetadata "github.com/giantswarm/k8smetadata/pkg/label"

templateapp "github.com/giantswarm/kubectl-gs/v4/pkg/template/app"

clustercommon "github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/common"
Expand Down Expand Up @@ -118,7 +116,7 @@ func generateClusterAppCrTemplate(appName string) (string, error) {
Catalog: "cluster",
InCluster: true,
Namespace: "org-${organization}",
Version: "${cluster_release}",
Version: "",
UserConfigConfigMapName: "${cluster_name}-config",
})

Expand All @@ -129,30 +127,6 @@ func generateClusterAppCrTemplate(appName string) (string, error) {
return string(template), nil
}

func generateDefaultAppsAppCrTemplate(appName string) (string, error) {
template, err := templateapp.NewAppCR(templateapp.Config{
Name: appName,
AppName: "${cluster_name}-default-apps",
Cluster: "${cluster_name}",
Catalog: "cluster",
DefaultingEnabled: false,
InCluster: true,
Namespace: "org-${organization}",
Version: "${default_apps_release}",
UserConfigConfigMapName: "${cluster_name}-default-apps-config",
UseClusterValuesConfig: true,
ExtraLabels: map[string]string{
k8smetadata.ManagedBy: "cluster",
},
})

if err != nil {
return "", err
}

return string(template), nil
}

func generateCapAClusterBaseTemplates(structureConfig common.StructureConfig) (common.ClusterBaseTemplates, error) {
clusterBaseTemplates := common.ClusterBaseTemplates{}

Expand All @@ -170,32 +144,16 @@ func generateCapAClusterBaseTemplates(structureConfig common.StructureConfig) (c
Name: "nodepool0",
},
},
ReleaseVersion: "${release}",
})
clusterValues, err := capa.GenerateClusterValues(clusterConfig)

if err != nil {
return clusterBaseTemplates, err
}

defaultAppsAppCr, err := generateDefaultAppsAppCrTemplate("default-apps-aws")

if err != nil {
return clusterBaseTemplates, err
}

defaultAppsValues, err := capa.GenerateDefaultAppsValues(capa.DefaultAppsConfig{
ClusterName: "${cluster_name}",
Organization: "${organization}",
})

if err != nil {
return clusterBaseTemplates, err
}

clusterBaseTemplates.ClusterAppCr = clusterAppCr
clusterBaseTemplates.ClusterValues = clusterValues
clusterBaseTemplates.DefaultAppsAppCr = defaultAppsAppCr
clusterBaseTemplates.DefaultAppsValues = defaultAppsValues

return clusterBaseTemplates, nil
}
Expand Down Expand Up @@ -224,25 +182,8 @@ func generateCapGClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

defaultAppsAppCr, err := generateDefaultAppsAppCrTemplate("default-apps-gcp")

if err != nil {
return clusterBaseTemplates, err
}

defaultAppsValues, err := capg.GenerateDefaultAppsValues(capg.DefaultAppsConfig{
ClusterName: "${cluster_name}",
Organization: "${organization}",
})

if err != nil {
return clusterBaseTemplates, err
}

clusterBaseTemplates.ClusterAppCr = clusterAppCr
clusterBaseTemplates.ClusterValues = clusterValues
clusterBaseTemplates.DefaultAppsAppCr = defaultAppsAppCr
clusterBaseTemplates.DefaultAppsValues = defaultAppsValues

return clusterBaseTemplates, nil
}
Expand All @@ -266,25 +207,8 @@ func generateCapOClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

defaultAppsAppCr, err := generateDefaultAppsAppCrTemplate("default-apps-openstack")

if err != nil {
return clusterBaseTemplates, err
}

defaultAppsValues, err := openstack.GenerateDefaultAppsValues(openstack.DefaultAppsConfig{
ClusterName: "${cluster_name}",
Organization: "${organization}",
})

if err != nil {
return clusterBaseTemplates, err
}

clusterBaseTemplates.ClusterAppCr = clusterAppCr
clusterBaseTemplates.ClusterValues = clusterValues
clusterBaseTemplates.DefaultAppsAppCr = defaultAppsAppCr
clusterBaseTemplates.DefaultAppsValues = defaultAppsValues

return clusterBaseTemplates, nil
}
Expand All @@ -308,25 +232,8 @@ func generateCapVClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

defaultAppsAppCr, err := generateDefaultAppsAppCrTemplate("default-apps-vsphere")

if err != nil {
return clusterBaseTemplates, err
}

defaultAppsValues, err := capv.GenerateDefaultAppsValues(capv.DefaultAppsConfig{
ClusterName: "${cluster_name}",
Organization: "${organization}",
})

if err != nil {
return clusterBaseTemplates, err
}

clusterBaseTemplates.ClusterAppCr = clusterAppCr
clusterBaseTemplates.ClusterValues = clusterValues
clusterBaseTemplates.DefaultAppsAppCr = defaultAppsAppCr
clusterBaseTemplates.DefaultAppsValues = defaultAppsValues

return clusterBaseTemplates, nil
}
Expand Down Expand Up @@ -354,25 +261,8 @@ func generateCapZClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

defaultAppsAppCr, err := generateDefaultAppsAppCrTemplate("default-apps-azure")

if err != nil {
return clusterBaseTemplates, err
}

defaultAppsValues, err := capz.GenerateDefaultAppsValues(capz.DefaultAppsConfig{
ClusterName: "${cluster_name}",
Organization: "${organization}",
})

if err != nil {
return clusterBaseTemplates, err
}

clusterBaseTemplates.ClusterAppCr = clusterAppCr
clusterBaseTemplates.ClusterValues = clusterValues
clusterBaseTemplates.DefaultAppsAppCr = defaultAppsAppCr
clusterBaseTemplates.DefaultAppsValues = defaultAppsValues

return clusterBaseTemplates, nil
}
25 changes: 10 additions & 15 deletions cmd/gitops/add/workload-cluster/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ const (
name = "workload-cluster"
alias = "wc"

shortDescription = "Adds a new Workload Cluster to your GitOps directory structure"
longDescription = `Adds a new Workload Cluster to your GitOps directory structure.
shortDescription = "Adds a new workload cluster to your GitOps directory structure"
longDescription = `Adds a new workload cluster to your GitOps directory structure.

workload-cluster \
[--base <path_to_base] \
[--cluster-release <cluster_version>] \
[--base <path_to_base>] \
[--release <version>] \
[--cluster-user-config <path_to_values.yaml>] \
[--default-apps-release <default_apps_version>] \
[--default-apps-user-config <path_to_values.yaml>] \
--name <wc_id> \
--management-cluster <mc_code_name> \
--organization <org_name> \
Expand All @@ -34,34 +32,31 @@ https://github.com/giantswarm/gitops-template/blob/main/docs/repo_structure.md.
Steps it implements:
https://github.com/giantswarm/gitops-template/blob/main/docs/add_wc.md`

examples = ` # Add dummy Workload Cluster structure, without cluster definition
examples = ` # Add dummy workload cluster structure, without cluster definition
kubectl gs gitops add wc \
--name dummy \
--management-cluster mymc \
--organization myorg \
--repository-name gitops-demo

# Add dummy Workload Cluster structure with definition from base
# Add dummy workload cluster structure with definition from base
kubectl gs gitops add wc \
--name dummy \
--management-cluster mymc \
--organization myorg \
--repository-name gitops-demo \
--base bases/clusters/openstack \
--cluster-release 0.13.0 \
--default-apps-release 0.6.0
--release 29.0.0

# Add dummy Workload Cluster structure with definition from base and extra configuration
# Add dummy workload cluster structure with definition from base and extra configuration
kubectl gs gitops add wc \
--name dummy \
--management-cluster mymc \
--organization myorg \
--repository-name gitops-demo \
--base bases/clusters/openstack \
--cluster-release 0.13.0 \
--default-apps-release 0.6.0 \
--cluster-user-config /tmp/cluster_user_config.yaml \
--default-apps-user-config /tmp/default_apps_user_config.yaml`
--release 29.0.0 \
--cluster-user-config /tmp/cluster_user_config.yaml`
)

type Config struct {
Expand Down
30 changes: 11 additions & 19 deletions cmd/gitops/add/workload-cluster/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ const (
flagSkipMAPI = "skip-mapi"
flagRepositoryName = "repository-name"

//CAPx only
flagClusterRelease = "cluster-release"
flagClusterUserConfig = "cluster-user-config"
flagDefaultAppsRelease = "default-apps-release"
flagDefaultAppsUserConfig = "default-apps-user-config"
// CAPI only
flagRelease = "release"
flagClusterUserConfig = "cluster-user-config"
)

type flag struct {
Expand All @@ -28,24 +26,19 @@ type flag struct {
SkipMAPI bool
RepositoryName string

ClusterRelease string
ClusterUserConfig string
DefaultAppsRelease string
DefaultAppsUserConfig string
Release string
ClusterUserConfig string
}

func (f *flag) Init(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.Base, flagBase, "", "Path to the base directory. It must be relative to the repository root.")
cmd.Flags().StringVar(&f.ManagementCluster, flagManagementCluster, "", "Codename of the Management Cluster the Workload Cluster belongs to.")
cmd.Flags().StringVar(&f.ManagementCluster, flagManagementCluster, "", "Codename of the management cluster the workload cluster belongs to.")
cmd.Flags().StringVar(&f.Name, flagName, "", "Name of the Workload Cluster.")
cmd.Flags().StringVar(&f.Organization, flagOrganization, "", "Name of the Organization the Workload Cluster belongs to.")
cmd.Flags().StringVar(&f.Organization, flagOrganization, "", "Name of the Organization the workload cluster belongs to.")
cmd.Flags().BoolVar(&f.SkipMAPI, flagSkipMAPI, false, "Skip `mapi` directory when adding the app.")
cmd.Flags().StringVar(&f.RepositoryName, flagRepositoryName, "", "Name of the GitOps repository.")

cmd.Flags().StringVar(&f.ClusterRelease, flagClusterRelease, "", "Cluster app version.")
cmd.Flags().StringVar(&f.Release, flagRelease, "", "Workload cluster release version.")
cmd.Flags().StringVar(&f.ClusterUserConfig, flagClusterUserConfig, "", "Cluster app user configuration to patch the base with.")
cmd.Flags().StringVar(&f.DefaultAppsRelease, flagDefaultAppsRelease, "", "Default apps app version.")
cmd.Flags().StringVar(&f.DefaultAppsUserConfig, flagDefaultAppsUserConfig, "", "Default apps app user configuration to patch the base with.")
}

func (f *flag) Validate() error {
Expand All @@ -65,12 +58,11 @@ func (f *flag) Validate() error {
return microerror.Maskf(invalidFlagsError, "--%s must not be empty", flagRepositoryName)
}

if f.Base != "" && (f.ClusterRelease == "" || f.DefaultAppsRelease == "") {
if f.Base != "" && f.Release == "" {
return microerror.Maskf(
invalidFlagsError,
"both --%s and --%s must not be empty when referencing base",
flagClusterRelease,
flagDefaultAppsRelease,
"--%s must not be empty when referencing base",
flagRelease,
)
}

Expand Down
26 changes: 7 additions & 19 deletions cmd/gitops/add/workload-cluster/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) err
var err error

config := common.StructureConfig{
ClusterBase: r.flag.Base,
ClusterRelease: r.flag.ClusterRelease,
DefaultAppsRelease: r.flag.DefaultAppsRelease,
ManagementCluster: r.flag.ManagementCluster,
WorkloadCluster: r.flag.Name,
Organization: r.flag.Organization,
SkipMAPI: r.flag.SkipMAPI,
RepositoryName: r.flag.RepositoryName,
ClusterBase: r.flag.Base,
Release: r.flag.Release,
ManagementCluster: r.flag.ManagementCluster,
WorkloadCluster: r.flag.Name,
Organization: r.flag.Organization,
SkipMAPI: r.flag.SkipMAPI,
RepositoryName: r.flag.RepositoryName,
}

if r.flag.ClusterUserConfig != "" {
Expand All @@ -65,17 +64,6 @@ func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) err

config.ClusterUserConfig = strings.TrimSpace(config.ClusterUserConfig)
}
if r.flag.DefaultAppsUserConfig != "" {
config.DefaultAppsUserConfig, err = commonkey.ReadConfigMapYamlFromFile(
afero.NewOsFs(),
r.flag.DefaultAppsUserConfig,
)
if err != nil {
return microerror.Mask(err)
}

config.DefaultAppsUserConfig = strings.TrimSpace(config.DefaultAppsUserConfig)
}

creatorConfig, err := structure.NewWorkloadCluster(config)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ spec:
substitute:
cluster_name: "demowc"
organization: "demoorg"
cluster_release: "0.13.0"
default_apps_release: "0.6.0"
release: "0.13.0"
prune: false
serviceAccountName: automation
sourceRef:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ spec:
substitute:
cluster_name: "demowc"
organization: "demoorg"
cluster_release: "0.13.0"
default_apps_release: "0.6.0"
release: "0.13.0"
prune: false
serviceAccountName: automation
sourceRef:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ spec:
substitute:
cluster_name: "demowc"
organization: "demoorg"
cluster_release: "0.13.0"
default_apps_release: "0.6.0"
release: "0.13.0"
prune: false
serviceAccountName: automation
sourceRef:
Expand Down
Loading