Skip to content

Commit

Permalink
Helpful error messages for invalid subnet split parameters of CAPA pr…
Browse files Browse the repository at this point in the history
…ivate clusters (#1382)
  • Loading branch information
AndiDog authored Jul 22, 2024
1 parent 06362fd commit a865d38
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 189 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

## [Unreleased]


### Added

- Added `--prevent-deletion` flag to cluster template command for capv clusters
- Add `--prevent-deletion` flag to cluster template command for capv clusters
- Helpful error messages for invalid subnet split parameters of CAPA private clusters

## [3.0.0] - 2024-06-27

Expand All @@ -20,7 +20,7 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

### Changed

- **BREAKING** When templating cluster manifests for CAPA clusters with `kubectl gs template cluster` command, now we set the workload cluster release version via the `--release` flag (like for vintage AWS), instead setting cluster-aws version via `--cluster-version`.
- **BREAKING** When templating cluster manifests for CAPA clusters with `kubectl gs template cluster` command, now we set the workload cluster release version via the `--release` flag (like for vintage AWS), instead setting cluster-aws version via `--cluster-version`.
- Update module version to v3.

## [2.57.0] - 2024-06-21
Expand Down
21 changes: 11 additions & 10 deletions cmd/gitops/add/base/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

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

clustercommon "github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/common"
"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/provider/templates/capv"
"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/provider/templates/capz"
"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/provider/templates/openstack"
Expand Down Expand Up @@ -161,11 +162,11 @@ func generateCapAClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

clusterConfig := providers.BuildCapaClusterConfig(providers.ClusterConfig{
clusterConfig := providers.BuildCapaClusterConfig(clustercommon.ClusterConfig{
Name: "${cluster_name}",
Organization: "${organization}",
AWS: providers.AWSConfig{
MachinePool: providers.AWSMachinePoolConfig{
AWS: clustercommon.AWSConfig{
MachinePool: clustercommon.AWSMachinePoolConfig{
Name: "nodepool0",
},
},
Expand Down Expand Up @@ -208,11 +209,11 @@ func generateCapGClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

clusterConfig := providers.BuildCapgClusterConfig(providers.ClusterConfig{
clusterConfig := providers.BuildCapgClusterConfig(clustercommon.ClusterConfig{
Name: "${cluster_name}",
Organization: "${organization}",
GCP: providers.GCPConfig{
MachineDeployment: providers.GCPMachineDeployment{
GCP: clustercommon.GCPConfig{
MachineDeployment: clustercommon.GCPMachineDeployment{
Name: "machine-pool0",
},
},
Expand Down Expand Up @@ -255,7 +256,7 @@ func generateCapOClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

clusterConfig := providers.BuildCapoClusterConfig(providers.ClusterConfig{
clusterConfig := providers.BuildCapoClusterConfig(clustercommon.ClusterConfig{
Name: "${cluster_name}",
Organization: "${organization}",
}, 1)
Expand Down Expand Up @@ -297,7 +298,7 @@ func generateCapVClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

clusterConfig := providers.BuildCapvClusterConfig(providers.ClusterConfig{
clusterConfig := providers.BuildCapvClusterConfig(clustercommon.ClusterConfig{
Name: "${cluster_name}",
Organization: "${organization}",
})
Expand Down Expand Up @@ -339,11 +340,11 @@ func generateCapZClusterBaseTemplates(structureConfig common.StructureConfig) (c
return clusterBaseTemplates, err
}

clusterConfig := providers.BuildCapzClusterConfig(providers.ClusterConfig{
clusterConfig := providers.BuildCapzClusterConfig(clustercommon.ClusterConfig{
Name: "${cluster_name}",
Organization: "${organization}",
Region: structureConfig.Region,
Azure: providers.AzureConfig{
Azure: clustercommon.AzureConfig{
SubscriptionID: structureConfig.AzureSubscriptionID,
},
})
Expand Down
3 changes: 2 additions & 1 deletion cmd/template/cluster/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/flags"
"github.com/giantswarm/kubectl-gs/v4/pkg/commonconfig"
"github.com/giantswarm/kubectl-gs/v4/pkg/middleware"
"github.com/giantswarm/kubectl-gs/v4/pkg/middleware/renewtoken"
Expand Down Expand Up @@ -41,7 +42,7 @@ func New(config Config) (*cobra.Command, error) {
return nil, microerror.Maskf(invalidConfigError, "%T.ConfigFlags must not be empty", config)
}

f := &flag{}
f := &flags.Flag{}

r := &runner{
commonConfig: &commonconfig.CommonConfig{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package common

import (
"context"
Expand All @@ -20,6 +20,10 @@ import (
"github.com/giantswarm/kubectl-gs/v4/pkg/app"
)

var invalidFlagError = &microerror.Error{
Kind: "invalidFlagError",
}

type AWSConfig struct {
ExternalSNAT bool
ControlPlaneSubnet string
Expand Down Expand Up @@ -177,7 +181,7 @@ type OIDC struct {
GroupsClaim string
}

func newcapiClusterCR(config ClusterConfig, infrastructureRef *corev1.ObjectReference) *capi.Cluster {
func NewCapiClusterCR(config ClusterConfig, infrastructureRef *corev1.ObjectReference) *capi.Cluster {
cluster := &capi.Cluster{
TypeMeta: metav1.TypeMeta{
Kind: "Cluster",
Expand Down Expand Up @@ -214,7 +218,7 @@ func newcapiClusterCR(config ClusterConfig, infrastructureRef *corev1.ObjectRefe
return cluster
}

func getLatestVersion(ctx context.Context, ctrlClient client.Client, app, catalog string) (string, error) {
func GetLatestVersion(ctx context.Context, ctrlClient client.Client, app, catalog string) (string, error) {
var catalogEntryList applicationv1alpha1.AppCatalogEntryList
err := ctrlClient.List(ctx, &catalogEntryList, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{
Expand All @@ -235,15 +239,15 @@ func getLatestVersion(ctx context.Context, ctrlClient client.Client, app, catalo
return catalogEntryList.Items[0].Spec.Version, nil
}

func organizationNamespace(org string) string {
func OrganizationNamespace(org string) string {
return fmt.Sprintf("org-%s", org)
}

func userConfigMapName(app string) string {
func UserConfigMapName(app string) string {
return fmt.Sprintf("%s-userconfig", app)
}

func defaultTo(value string, defaultValue string) string {
func DefaultTo(value string, defaultValue string) string {
if value != "" {
return value
}
Expand Down
9 changes: 0 additions & 9 deletions cmd/template/cluster/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ func IsInvalidConfig(err error) bool {
return microerror.Cause(err) == invalidConfigError
}

var invalidFlagError = &microerror.Error{
Kind: "invalidFlagError",
}

// IsInvalidFlag asserts invalidFlagError.
func IsInvalidFlag(err error) bool {
return microerror.Cause(err) == invalidFlagError
}

var templateFlagNotImplemented = &microerror.Error{
Kind: "templateFlagsNotImplementedError",
}
42 changes: 23 additions & 19 deletions cmd/template/cluster/flag.go → cmd/template/cluster/flags/flag.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cluster
package flags

import (
"fmt"
Expand All @@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/provider"
"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/common"
"github.com/giantswarm/kubectl-gs/v4/internal/key"
"github.com/giantswarm/kubectl-gs/v4/pkg/labels"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ const (
flagAWSTransitGatewayID = "aws-transit-gateway-id"
flagAWSControlPlaneLoadBalancerIngressAllowCIDRBlock = "control-plane-load-balancer-ingress-allow-cidr-block"
flagAWSPublicSubnetMask = "public-subnet-size"
flagAWSPrivateSubnetMask = "private-subnet-size"
FlagAWSPrivateSubnetMask = "private-subnet-size"

flagAWSMachinePoolMinSize = "machine-pool-min-size"
flagAWSMachinePoolMaxSize = "machine-pool-max-size"
Expand Down Expand Up @@ -139,7 +139,11 @@ const (
defaultVSphereKubernetesVersion = "v1.24.12"
)

type flag struct {
var invalidFlagError = &microerror.Error{
Kind: "invalidFlagError",
}

type Flag struct {
Provider string
ManagementCluster string
PreventDeletion bool
Expand All @@ -162,18 +166,18 @@ type flag struct {
ServicePriority string

// Provider-specific
AWS provider.AWSConfig
Azure provider.AzureConfig
GCP provider.GCPConfig
OpenStack provider.OpenStackConfig
VSphere provider.VSphereConfig
App provider.AppConfig
OIDC provider.OIDC

print *genericclioptions.PrintFlags
AWS common.AWSConfig
Azure common.AzureConfig
GCP common.GCPConfig
OpenStack common.OpenStackConfig
VSphere common.VSphereConfig
App common.AppConfig
OIDC common.OIDC

Print *genericclioptions.PrintFlags
}

func (f *flag) Init(cmd *cobra.Command) {
func (f *Flag) Init(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.Provider, flagProvider, "", "Installation infrastructure provider.")
cmd.Flags().StringVar(&f.ManagementCluster, flagManagementCluster, "", "Name of the management cluster. Only required in combination with certain parameters.")
cmd.Flags().BoolVar(&f.PreventDeletion, flagPreventDeletion, false, "Prevent cluster from getting deleted")
Expand All @@ -193,7 +197,7 @@ func (f *flag) Init(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.AWS.PrefixListID, flagAWSPrefixListID, "", "Prefix list ID to manage. Workload cluster will be able to reach the destinations in the prefix list via the transit gateway. If not specified, it will be looked up by name/namespace of the management cluster (ends with `-tgw-prefixlist`). Only applies to proxy-private clusters.")
cmd.Flags().StringVar(&f.AWS.TransitGatewayID, flagAWSTransitGatewayID, "", "ID of the transit gateway to attach the cluster VPC to. If not specified for workload clusters, the management cluster's transit gateway will be used. Only applies to proxy-private clusters.")
cmd.Flags().IntVar(&f.AWS.PublicSubnetMask, flagAWSPublicSubnetMask, 20, "Subnet mask of the public subnets. Minimum is 25 (128 IPs), default is 20.")
cmd.Flags().IntVar(&f.AWS.PrivateSubnetMask, flagAWSPrivateSubnetMask, 18, "Subnet mask of the private subnets. Minimum size is 25 (128 IPs), default is 18.")
cmd.Flags().IntVar(&f.AWS.PrivateSubnetMask, FlagAWSPrivateSubnetMask, 18, "Subnet mask of the private subnets. Minimum size is 25 (128 IPs), default is 18.")

// aws control plane
cmd.Flags().StringVar(&f.AWS.ControlPlaneSubnet, flagAWSControlPlaneSubnet, "", "Subnet used for the Control Plane.")
Expand Down Expand Up @@ -357,15 +361,15 @@ func (f *flag) Init(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.BastionInstanceType, flagBastionInstanceType, "", "Instance type used for the bastion node.")
cmd.Flags().IntVar(&f.BastionReplicas, flagBastionReplicas, 1, "Replica count for the bastion node")

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

// Merging current command flags and config flags,
// to be able to override kubectl-specific ones.
f.print.AddFlags(cmd)
f.Print.AddFlags(cmd)
}

func (f *flag) Validate(cmd *cobra.Command) error {
func (f *Flag) Validate(cmd *cobra.Command) error {
var err error
validProviders := []string{
key.ProviderAWS,
Expand Down
5 changes: 3 additions & 2 deletions cmd/template/cluster/provider/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"

"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/common"
"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/provider/templates/aws"
"github.com/giantswarm/kubectl-gs/v4/internal/key"
)

func WriteAWSTemplate(ctx context.Context, client k8sclient.Interface, out io.Writer, config ClusterConfig) error {
func WriteAWSTemplate(ctx context.Context, client k8sclient.Interface, out io.Writer, config common.ClusterConfig) error {
err := WriteGSAWSTemplate(ctx, client, out, config)
if err != nil {
return microerror.Mask(err)
Expand All @@ -23,7 +24,7 @@ func WriteAWSTemplate(ctx context.Context, client k8sclient.Interface, out io.Wr
return nil
}

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

crsConfig := aws.ClusterCRsConfig{
Expand Down
11 changes: 6 additions & 5 deletions cmd/template/cluster/provider/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

"github.com/giantswarm/kubectl-gs/v4/cmd/template/cluster/common"
"github.com/giantswarm/kubectl-gs/v4/internal/key"
"github.com/giantswarm/kubectl-gs/v4/pkg/scheme"
)
Expand All @@ -26,7 +27,7 @@ const (
defaultMasterVMSize = "Standard_D4s_v3"
)

func WriteAzureTemplate(ctx context.Context, client k8sclient.Interface, out io.Writer, config ClusterConfig) error {
func WriteAzureTemplate(ctx context.Context, client k8sclient.Interface, out io.Writer, config common.ClusterConfig) error {
err := WriteGSAzureTemplate(ctx, client, out, config)
if err != nil {
return microerror.Mask(err)
Expand All @@ -35,7 +36,7 @@ func WriteAzureTemplate(ctx context.Context, client k8sclient.Interface, out io.
return nil
}

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

config.ReleaseComponents, err = key.GetReleaseComponents(ctx, client.CtrlClient(), config.ReleaseVersion)
Expand All @@ -53,7 +54,7 @@ func WriteGSAzureTemplate(ctx context.Context, client k8sclient.Interface, out i

infrastructureRef := newCAPZClusterInfraRef(azureClusterCR)

clusterCR := newcapiClusterCR(config, infrastructureRef)
clusterCR := common.NewCapiClusterCR(config, infrastructureRef)
clusterCRYaml, err = yaml.Marshal(clusterCR)
if err != nil {
return microerror.Mask(err)
Expand Down Expand Up @@ -85,7 +86,7 @@ func WriteGSAzureTemplate(ctx context.Context, client k8sclient.Interface, out i
return nil
}

func newAzureClusterCR(config ClusterConfig) *capz.AzureCluster {
func newAzureClusterCR(config common.ClusterConfig) *capz.AzureCluster {
cr := &capz.AzureCluster{
TypeMeta: metav1.TypeMeta{
Kind: "AzureCluster",
Expand Down Expand Up @@ -124,7 +125,7 @@ func newAzureClusterCR(config ClusterConfig) *capz.AzureCluster {
return cr
}

func newAzureMasterMachineCR(config ClusterConfig) *capz.AzureMachine {
func newAzureMasterMachineCR(config common.ClusterConfig) *capz.AzureMachine {
var failureDomain *string
if len(config.ControlPlaneAZ) > 0 {
failureDomain = &config.ControlPlaneAZ[0]
Expand Down
Loading

0 comments on commit a865d38

Please sign in to comment.