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

Use new releases for CAPA cluster templates #1352

Merged
merged 8 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

- Added `--prevent-deletion` flag to cluster template command for capa, capa-eks, capz clusters

### Changed

- CAPA only change for new releases: render release version in config instead of cluster-aws version in App resource.

## [2.56.0] - 2024-06-10

### Added
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/cluster/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ func (f *flag) Validate(cmd *cobra.Command) error {
}

if f.Release == "" {
if key.IsPureCAPIProvider(f.Provider) {
if key.IsPureCAPIProvider(f.Provider) && !key.IsCAPIProviderThatUsesReleases(f.Provider) {
// skip release validation
} else {
return microerror.Maskf(invalidFlagError, "--%s must not be empty", flagRelease)
Expand Down
107 changes: 5 additions & 102 deletions cmd/template/cluster/provider/capa.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"text/template"

"github.com/3th1nk/cidr"
"github.com/Masterminds/semver/v3"
"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"github.com/giantswarm/microerror"
"github.com/pkg/errors"
Expand All @@ -34,38 +33,15 @@ const (
)

func WriteCAPATemplate(ctx context.Context, client k8sclient.Interface, output io.Writer, config ClusterConfig) error {
appVersion := config.App.ClusterVersion
if appVersion == "" {
var err error
appVersion, err = getLatestVersion(ctx, client.CtrlClient(), ClusterAWSRepoName, config.App.ClusterCatalog)
if err != nil {
return microerror.Mask(err)
}
}

err := templateClusterCAPA(ctx, client, output, config, appVersion)
if err != nil {
return microerror.Mask(err)
}

minUnifiedClusterAwsVersion := semver.New(0, 76, 0, "", "")
desiredClusterAwsVersion, err := semver.StrictNewVersion(appVersion)
err := templateClusterCAPA(ctx, client, output, config)
if err != nil {
return microerror.Mask(err)
}

if desiredClusterAwsVersion.LessThan(minUnifiedClusterAwsVersion) {
// Render default-apps-aws only when cluster-aws version does not contain default apps.
err = templateDefaultAppsCAPA(ctx, client, output, config)
if err != nil {
return microerror.Mask(err)
}
}

return nil
}

func templateClusterCAPA(ctx context.Context, k8sClient k8sclient.Interface, output io.Writer, config ClusterConfig, appVersion string) error {
func templateClusterCAPA(ctx context.Context, k8sClient k8sclient.Interface, output io.Writer, config ClusterConfig) error {
appName := config.Name
configMapName := userConfigMapName(appName)

Expand Down Expand Up @@ -247,7 +223,6 @@ func templateClusterCAPA(ctx context.Context, k8sClient k8sclient.Interface, out
InCluster: true,
Name: ClusterAWSRepoName,
Namespace: organizationNamespace(config.Organization),
Version: appVersion,
UserConfigConfigMapName: configMapName,
}

Expand Down Expand Up @@ -300,81 +275,9 @@ func BuildCapaClusterConfig(config ClusterConfig) capa.ClusterConfig {
Region: config.Region,
AWSClusterRoleIdentityName: config.AWS.AWSClusterRoleIdentityName,
},
},
}
}

func templateDefaultAppsCAPA(ctx context.Context, k8sClient k8sclient.Interface, output io.Writer, config ClusterConfig) error {
appName := fmt.Sprintf("%s-default-apps", config.Name)
configMapName := userConfigMapName(appName)

var configMapYAML []byte
{
flagValues := capa.DefaultAppsConfig{
ClusterName: config.Name,
Organization: config.Organization,
}

configData, err := capa.GenerateDefaultAppsValues(flagValues)
if err != nil {
return microerror.Mask(err)
}

userConfigMap, err := templateapp.NewConfigMap(templateapp.UserConfig{
Name: configMapName,
Namespace: organizationNamespace(config.Organization),
Data: configData,
})
if err != nil {
return microerror.Mask(err)
}

userConfigMap.Labels = map[string]string{}
userConfigMap.Labels[k8smetadata.Cluster] = config.Name

configMapYAML, err = yaml.Marshal(userConfigMap)
if err != nil {
return microerror.Mask(err)
}
}

var appYAML []byte
{
appVersion := config.App.DefaultAppsVersion
if appVersion == "" {
var err error
appVersion, err = getLatestVersion(ctx, k8sClient.CtrlClient(), DefaultAppsAWSRepoName, config.App.DefaultAppsCatalog)
if err != nil {
return microerror.Mask(err)
}
}

var err error
appYAML, err = templateapp.NewAppCR(templateapp.Config{
AppName: appName,
Cluster: config.Name,
Catalog: config.App.DefaultAppsCatalog,
DefaultingEnabled: false,
InCluster: true,
Name: DefaultAppsAWSRepoName,
Namespace: organizationNamespace(config.Organization),
Version: appVersion,
UserConfigConfigMapName: configMapName,
UseClusterValuesConfig: true,
ExtraLabels: map[string]string{
k8smetadata.ManagedBy: "cluster",
Release: &capa.Release{
Version: config.ReleaseVersion,
},
})
if err != nil {
return microerror.Mask(err)
}
},
}

t := template.Must(template.New("appCR").Parse(key.AppCRTemplate))

err := t.Execute(output, templateapp.AppCROutput{
UserConfigConfigMap: string(configMapYAML),
AppCR: string(appYAML),
})
return microerror.Mask(err)
}
5 changes: 5 additions & 0 deletions cmd/template/cluster/provider/templates/capa/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Global struct {
Metadata *Metadata `json:"metadata,omitempty"`
NodePools *map[string]MachinePool `json:"nodePools,omitempty"`
ProviderSpecific *ProviderSpecific `json:"providerSpecific,omitempty"`
Release *Release `json:"release,omitempty"`
}

type ClusterConfig struct {
Expand All @@ -19,6 +20,10 @@ type Metadata struct {
PreventDeletion bool `json:"preventDeletion,omitempty"`
}

type Release struct {
Version string `json:"version,omitempty"`
}

type DefaultAppsConfig struct {
ClusterName string `json:"clusterName,omitempty"`
Organization string `json:"organization,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions cmd/template/cluster/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Test_run(t *testing.T) {
Name: "test1",
Provider: "capa",
Description: "just a test cluster",
Release: "25.0.0",
Region: "the-region",
Organization: "test",
ControlPlaneInstanceType: "control-plane-instance-type",
Expand Down Expand Up @@ -132,6 +133,7 @@ func Test_run(t *testing.T) {
Provider: "capa",
ManagementCluster: "my-mc",
Description: "just a test cluster",
Release: "25.0.0",
Region: "the-region",
Organization: "test",
ControlPlaneInstanceType: "control-plane-instance-type",
Expand Down Expand Up @@ -172,6 +174,7 @@ func Test_run(t *testing.T) {
Provider: "capa",
ManagementCluster: "my-mc",
Description: "just a test cluster",
Release: "25.0.0",
Region: "the-region",
Organization: "test",
ControlPlaneInstanceType: "control-plane-instance-type",
Expand Down Expand Up @@ -278,6 +281,7 @@ func Test_run(t *testing.T) {
Name: "test6",
Provider: "capa",
Description: "just a test cluster",
Release: "25.0.0",
Region: "the-region",
Organization: "test",
ControlPlaneInstanceType: "control-plane-instance-type",
Expand Down Expand Up @@ -313,6 +317,7 @@ func Test_run(t *testing.T) {
Name: "test7",
Provider: "capa",
Description: "just a test cluster",
Release: "25.0.0",
Region: "the-region",
Organization: "test",
ControlPlaneInstanceType: "control-plane-instance-type",
Expand Down Expand Up @@ -348,6 +353,7 @@ func Test_run(t *testing.T) {
Name: "test8",
Provider: "capa",
Description: "just a test cluster",
Release: "25.0.0",
Region: "the-region",
Organization: "test",
ControlPlaneInstanceType: "control-plane-instance-type",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ data:
providerSpecific:
awsClusterRoleIdentityName: default
region: the-region
release:
version: 25.0.0
kind: ConfigMap
metadata:
creationTimestamp: null
Expand Down Expand Up @@ -83,4 +85,4 @@ spec:
configMap:
name: test1-userconfig
namespace: org-test
version: 1.0.0
version: ""
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ data:
providerSpecific:
awsClusterRoleIdentityName: default
region: the-region
release:
version: 25.0.0
kind: ConfigMap
metadata:
creationTimestamp: null
Expand Down Expand Up @@ -85,4 +87,4 @@ spec:
configMap:
name: test1-userconfig
namespace: org-test
version: 1.0.0
version: ""
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ data:
providerSpecific:
awsClusterRoleIdentityName: other-identity
region: the-region
release:
version: 25.0.0
kind: ConfigMap
metadata:
creationTimestamp: null
Expand Down Expand Up @@ -88,4 +90,4 @@ spec:
configMap:
name: test1-userconfig
namespace: org-test
version: 1.0.0
version: ""
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ data:
providerSpecific:
awsClusterRoleIdentityName: default
region: the-region
release:
version: 25.0.0
kind: ConfigMap
metadata:
creationTimestamp: null
Expand Down Expand Up @@ -79,4 +81,4 @@ spec:
configMap:
name: test6-userconfig
namespace: org-test
version: 1.0.0
version: ""
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ data:
providerSpecific:
awsClusterRoleIdentityName: default
region: the-region
release:
version: 25.0.0
kind: ConfigMap
metadata:
creationTimestamp: null
Expand Down Expand Up @@ -83,4 +85,4 @@ spec:
configMap:
name: test7-userconfig
namespace: org-test
version: 1.0.0
version: ""
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ data:
providerSpecific:
awsClusterRoleIdentityName: default
region: the-region
release:
version: 25.0.0
kind: ConfigMap
metadata:
creationTimestamp: null
Expand Down Expand Up @@ -75,4 +77,4 @@ spec:
configMap:
name: test8-userconfig
namespace: org-test
version: 1.0.0
version: ""
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
dario.cat/mergo v1.0.0
github.com/3th1nk/cidr v0.2.0
github.com/Masterminds/semver/v3 v3.2.1
github.com/Masterminds/sprig/v3 v3.2.3
github.com/ProtonMail/gopenpgp/v2 v2.7.5
github.com/blang/semver v3.5.1+incompatible
Expand Down Expand Up @@ -51,6 +50,7 @@ require (
)

require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down
5 changes: 5 additions & 0 deletions internal/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func IsPureCAPIProvider(provider string) bool {
return contains(PureCAPIProviders(), provider)
}

// IsCAPIProviderThatUsesReleases returns whether a given provider is a CAPI provider that uses new releases.
func IsCAPIProviderThatUsesReleases(provider string) bool {
nprokopic marked this conversation as resolved.
Show resolved Hide resolved
return contains(CAPIProvidersThatUseReleases(), provider)
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
Expand Down
7 changes: 7 additions & 0 deletions internal/key/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ func PureCAPIProviders() []string {
ProviderCloudDirector,
}
}

// CAPIProvidersThatUseReleases is the list of CAPI providers which are using Release resources.
func CAPIProvidersThatUseReleases() []string {
nprokopic marked this conversation as resolved.
Show resolved Hide resolved
return []string{
ProviderCAPA,
}
}