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

Add provider eks for template cluster #1098

Merged
merged 4 commits into from
Aug 9, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s
### Added

- Add `--login-timeout` flag to control the time period of OIDC login timeout
- Add experimental support for templating `cluster-eks` with provider `eks`.

### Changed

Expand Down
6 changes: 1 addition & 5 deletions cmd/template/cluster/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const (

// AWS only.
flagAWSExternalSNAT = "external-snat"
flagAWSEKS = "aws-eks"
flagAWSControlPlaneSubnet = "control-plane-subnet"

flagAWSClusterRoleIdentityName = "aws-cluster-role-identity-name"
Expand Down Expand Up @@ -153,7 +152,6 @@ func (f *flag) Init(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.AWS.AWSClusterRoleIdentityName, flagAWSClusterRoleIdentityName, "", "Name of the AWSClusterRoleIdentity that will be used for cluster creation.")
cmd.Flags().IntVar(&f.AWS.NetworkAZUsageLimit, flagNetworkAZUsageLimit, 3, "Amount of AZs that will be used for VPC.")
cmd.Flags().StringVar(&f.AWS.NetworkVPCCIDR, flagNetworkVPCCidr, "", "CIDR for the VPC.")
cmd.Flags().BoolVar(&f.AWS.EKS, flagAWSEKS, false, "Enable AWSEKS. Only available for AWS Release v20.0.0 (CAPA)")
cmd.Flags().BoolVar(&f.AWS.ExternalSNAT, flagAWSExternalSNAT, false, "AWS CNI configuration.")
cmd.Flags().StringVar(&f.AWS.ClusterType, flagAWSClusterType, "public", "Cluster type to be created (public,proxy-private)")
cmd.Flags().StringVar(&f.AWS.HttpsProxy, flagAWSHttpsProxy, "", "'HTTPS_PROXY' env value configuration for the cluster (required if cluster-type is set to proxy-private)")
Expand Down Expand Up @@ -309,9 +307,6 @@ func (f *flag) Init(cmd *cobra.Command) {
_ = cmd.Flags().MarkHidden(flagEnableLongNames)
_ = cmd.Flags().MarkDeprecated(flagEnableLongNames, "Long names are supported by default, so this flag is not needed anymore and will be removed in the next major version.")

// TODO: Make this flag visible when we roll CAPA/EKS out for customers
_ = cmd.Flags().MarkHidden(flagAWSEKS)

f.print = genericclioptions.NewPrintFlags("")
f.print.OutputFormat = nil

Expand All @@ -327,6 +322,7 @@ func (f *flag) Validate() error {
key.ProviderAzure,
key.ProviderCAPA,
key.ProviderCAPZ,
key.ProviderEKS,
key.ProviderGCP,
key.ProviderOpenStack,
key.ProviderVSphere,
Expand Down
46 changes: 3 additions & 43 deletions cmd/template/cluster/provider/capa.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
gsannotation "github.com/giantswarm/k8smetadata/pkg/annotation"
k8smetadata "github.com/giantswarm/k8smetadata/pkg/label"

"github.com/giantswarm/kubectl-gs/v2/cmd/template/cluster/provider/templates/aws"
"github.com/giantswarm/kubectl-gs/v2/cmd/template/cluster/provider/templates/capa"
"github.com/giantswarm/kubectl-gs/v2/internal/key"
templateapp "github.com/giantswarm/kubectl-gs/v2/pkg/template/app"
Expand All @@ -27,51 +26,12 @@ const (
)

func WriteCAPATemplate(ctx context.Context, client k8sclient.Interface, output io.Writer, config ClusterConfig) error {
var err error

if config.AWS.EKS {
err = WriteCAPAEKSTemplate(ctx, client, output, config)
if err != nil {
return microerror.Mask(err)
}
} else {
err = templateClusterAWS(ctx, client, output, config)
if err != nil {
return microerror.Mask(err)
}

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

return nil
}

func WriteCAPAEKSTemplate(ctx context.Context, client k8sclient.Interface, out io.Writer, config ClusterConfig) error {
var err error

data := struct {
Description string
KubernetesVersion string
Name string
Namespace string
Organization string
ReleaseVersion string
}{
Description: config.Description,
KubernetesVersion: "v1.21",
Name: config.Name,
Namespace: key.OrganizationNamespaceFromName(config.Organization),
Organization: config.Organization,
ReleaseVersion: config.ReleaseVersion,
}

var templates []templateConfig
for _, t := range aws.GetEKSTemplates() {
templates = append(templates, templateConfig(t))
}

err = runMutation(ctx, client, data, templates, out)
err = templateDefaultAppsAWS(ctx, client, output, config)
if err != nil {
return microerror.Mask(err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/template/cluster/provider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
)

type AWSConfig struct {
EKS bool
ExternalSNAT bool
ControlPlaneSubnet string

Expand Down
190 changes: 190 additions & 0 deletions cmd/template/cluster/provider/eks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package provider

import (
"context"
"fmt"
"io"
"text/template"

"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"

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

"github.com/giantswarm/kubectl-gs/v2/cmd/template/cluster/provider/templates/capa"
"github.com/giantswarm/kubectl-gs/v2/cmd/template/cluster/provider/templates/eks"
"github.com/giantswarm/kubectl-gs/v2/internal/key"
templateapp "github.com/giantswarm/kubectl-gs/v2/pkg/template/app"
)

const (
DefaultAppsEKSRepoName = "default-apps-eks"
ClusterEKSRepoName = "cluster-eks"
)

func WriteEKSTemplate(ctx context.Context, client k8sclient.Interface, output io.Writer, config ClusterConfig) error {
err := templateClusterEKS(ctx, client, output, config)
if err != nil {
return microerror.Mask(err)
}

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

return nil
}

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

var configMapYAML []byte
{
flagValues := BuildEKSClusterConfig(config)
configData, err := eks.GenerateClusterValues(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.ClusterVersion
if appVersion == "" {
var err error
appVersion, err = getLatestVersion(ctx, k8sClient.CtrlClient(), ClusterEKSRepoName, config.App.ClusterCatalog)
if err != nil {
return microerror.Mask(err)
}
}

clusterAppConfig := templateapp.Config{
AppName: config.Name,
Catalog: config.App.ClusterCatalog,
InCluster: true,
Name: ClusterEKSRepoName,
Namespace: organizationNamespace(config.Organization),
Version: appVersion,
UserConfigConfigMapName: configMapName,
}

var err error
appYAML, err = templateapp.NewAppCR(clusterAppConfig)
if err != nil {
return microerror.Mask(err)
}
}

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

err := t.Execute(output, templateapp.AppCROutput{
AppCR: string(appYAML),
UserConfigConfigMap: string(configMapYAML),
})
return microerror.Mask(err)
}

func BuildEKSClusterConfig(config ClusterConfig) eks.ClusterConfig {
return eks.ClusterConfig{
Metadata: &eks.Metadata{
Name: config.Name,
Description: config.Description,
Organization: config.Organization,
},
}
}

func templateDefaultAppsEKS(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(), DefaultAppsEKSRepoName, 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: DefaultAppsEKSRepoName,
Namespace: organizationNamespace(config.Organization),
Version: appVersion,
UserConfigConfigMapName: configMapName,
UseClusterValuesConfig: true,
ExtraLabels: map[string]string{
k8smetadata.ManagedBy: "cluster",
},
})
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)
}
53 changes: 53 additions & 0 deletions cmd/template/cluster/provider/templates/eks/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package eks

import (
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"
)

func GenerateClusterValues(flagInputs ClusterConfig) (string, error) {

var flagConfigData map[string]interface{}

{
flagConfigYAML, err := yaml.Marshal(flagInputs)
if err != nil {
return "", microerror.Mask(err)
}

err = yaml.Unmarshal(flagConfigYAML, &flagConfigData)
if err != nil {
return "", microerror.Mask(err)
}
}

finalConfigString, err := yaml.Marshal(flagInputs)
if err != nil {
return "", microerror.Mask(err)
}

return string(finalConfigString), nil
}

func GenerateDefaultAppsValues(flagConfig DefaultAppsConfig) (string, error) {
var flagConfigData map[string]interface{}

{
flagConfigYAML, err := yaml.Marshal(flagConfig)
if err != nil {
return "", microerror.Mask(err)
}

err = yaml.Unmarshal(flagConfigYAML, &flagConfigData)
if err != nil {
return "", microerror.Mask(err)
}
}

finalConfigString, err := yaml.Marshal(flagConfigData)
if err != nil {
return "", microerror.Mask(err)
}

return string(finalConfigString), nil
}
16 changes: 16 additions & 0 deletions cmd/template/cluster/provider/templates/eks/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package eks

type DefaultAppsConfig struct {
ClusterName string `json:"clusterName,omitempty"`
Organization string `json:"organization,omitempty"`
}

type ClusterConfig struct {
Metadata *Metadata `json:"metadata,omitempty"`
}

type Metadata struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Organization string `json:"organization,omitempty"`
}
5 changes: 5 additions & 0 deletions cmd/template/cluster/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func (r *runner) run(ctx context.Context, client k8sclient.Interface) error {
if err != nil {
return microerror.Mask(err)
}
case key.ProviderEKS:
err = provider.WriteEKSTemplate(ctx, client, output, config)
if err != nil {
return microerror.Mask(err)
}
case key.ProviderGCP:
err = provider.WriteGCPTemplate(ctx, client, output, config)
if err != nil {
Expand Down
Loading
Loading