Skip to content

Commit

Permalink
[PLT-1246] [GKE] Manage Cluster Secondary Range (#33)
Browse files Browse the repository at this point in the history
* [GKE] Manage Cluster Secondary Range

* Modify changelog
  • Loading branch information
iamjanr authored Dec 5, 2024
1 parent 19aa42a commit 4c71544
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.6.1-0.2.1 (upcoming)

* [PLT-1313] Soportar rangos secundarios de CIDR
* [PLT-1246] CMEK Support

## 1.6.1-0.2.0 (2024-10-24)
Expand Down
9 changes: 9 additions & 0 deletions cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
}
}

// Add IPAllocationPolicy for CIDR support (PLT-1246)
if s.scope.GCPManagedControlPlane.Spec.ClusterIpv4Cidr != nil {
cluster.ClusterIpv4Cidr = *s.scope.GCPManagedControlPlane.Spec.ClusterIpv4Cidr
}

if s.scope.GCPManagedControlPlane.Spec.IPAllocationPolicy != nil {
cluster.IpAllocationPolicy = infrav1exp.ConvertToSdkIPAllocationPolicy(s.scope.GCPManagedControlPlane.Spec.IPAllocationPolicy)
}

// If the cluster is autopilot, we don't need to specify node pools.
if !s.scope.IsAutopilotCluster() {
cluster.NodePools = scope.ConvertToSdkNodePools(nodePools, machinePools, isRegional, cluster.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ spec:
spec:
description: GCPManagedControlPlaneSpec defines the desired state of GCPManagedControlPlane.
properties:
clusterIpv4Cidr:
description: |-
ClusterIpv4Cidr is the IP address range of the container pods in the GKE cluster, in
[CIDR](http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing)
notation (e.g. `10.96.0.0/14`).
If not specified then one will be automatically chosen.
If this field is specified then IPAllocationPolicy.ClusterIpv4CidrBlock should be left blank.
type: string
clusterName:
description: |-
ClusterName allows you to specify the name of the GKE cluster.
Expand Down Expand Up @@ -118,6 +126,42 @@ spec:
- host
- port
type: object
ipAllocationPolicy:
description: |-
IPAllocationPolicy represents configuration options for GKE cluster IP allocation.
If not specified then GKE default values will be used.
properties:
clusterIpv4CidrBlock:
description: |-
ClusterIpv4CidrBlock represents the IP address range for the GKE cluster pod IPs. If this field is set, then
GCPManagedControlPlaneSpec.ClusterIpv4Cidr must be left blank.
This field is only applicable when use_ip_aliases is set to true.
If not specified the range will be chosen with the default size.
type: string
clusterSecondaryRangeName:
description: |-
ClusterSecondaryRangeName represents the name of the secondary range to be used for the GKE cluster CIDR block.
The range will be used for pod IP addresses and must be an existing secondary range associated with the cluster subnetwork.
This field is only applicable when use_ip_aliases is set to true.
type: string
servicesIpv4CidrBlock:
description: |-
ServicesIpv4CidrBlock represents the IP address range for services IPs in the GKE cluster.
This field is only applicable when use_ip_aliases is set to true.
If not specified the range will be chosen with the default size.
type: string
servicesSecondaryRangeName:
description: |-
ServicesSecondaryRangeName represents the name of the secondary range to be used for the services CIDR block.
The range will be used for service ClusterIPs and must be an existing secondary range associated with the cluster subnetwork.
This field is only applicable when use_ip_aliases is set to true.
type: string
useIPAliases:
description: |-
UseIPAliases represents whether alias IPs will be used for pod IPs in the cluster.
If unspecified will default to false.
type: boolean
type: object
location:
description: |-
Location represents the location (region or zone) in which the GKE cluster
Expand Down
40 changes: 40 additions & 0 deletions exp/api/v1beta1/gcpmanagedcontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ type GCPManagedControlPlaneSpec struct {
// Location represents the location (region or zone) in which the GKE cluster
// will be created.
Location string `json:"location"`
// ClusterIpv4Cidr is the IP address range of the container pods in the GKE cluster, in
// [CIDR](http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing)
// notation (e.g. `10.96.0.0/14`).
// If not specified then one will be automatically chosen.
// If this field is specified then IPAllocationPolicy.ClusterIpv4CidrBlock should be left blank.
// +optional
ClusterIpv4Cidr *string `json:"clusterIpv4Cidr,omitempty"`
// IPAllocationPolicy represents configuration options for GKE cluster IP allocation.
// If not specified then GKE default values will be used.
// +optional
IPAllocationPolicy *IPAllocationPolicy `json:"ipAllocationPolicy,omitempty"`
// EnableAutopilot indicates whether to enable autopilot for this GKE cluster.
// +optional
EnableAutopilot bool `json:"enableAutopilot"`
Expand Down Expand Up @@ -173,6 +184,35 @@ const (
Stable ReleaseChannel = "stable"
)

// IPAllocationPolicy represents configuration options for GKE cluster IP allocation.
type IPAllocationPolicy struct {
// UseIPAliases represents whether alias IPs will be used for pod IPs in the cluster.
// If unspecified will default to false.
// +optional
UseIPAliases *bool `json:"useIPAliases,omitempty"`
// ClusterSecondaryRangeName represents the name of the secondary range to be used for the GKE cluster CIDR block.
// The range will be used for pod IP addresses and must be an existing secondary range associated with the cluster subnetwork.
// This field is only applicable when use_ip_aliases is set to true.
// +optional
ClusterSecondaryRangeName *string `json:"clusterSecondaryRangeName,omitempty"`
// ServicesSecondaryRangeName represents the name of the secondary range to be used for the services CIDR block.
// The range will be used for service ClusterIPs and must be an existing secondary range associated with the cluster subnetwork.
// This field is only applicable when use_ip_aliases is set to true.
// +optional
ServicesSecondaryRangeName *string `json:"servicesSecondaryRangeName,omitempty"`
// ClusterIpv4CidrBlock represents the IP address range for the GKE cluster pod IPs. If this field is set, then
// GCPManagedControlPlaneSpec.ClusterIpv4Cidr must be left blank.
// This field is only applicable when use_ip_aliases is set to true.
// If not specified the range will be chosen with the default size.
// +optional
ClusterIpv4CidrBlock *string `json:"clusterIpv4CidrBlock,omitempty"`
// ServicesIpv4CidrBlock represents the IP address range for services IPs in the GKE cluster.
// This field is only applicable when use_ip_aliases is set to true.
// If not specified the range will be chosen with the default size.
// +optional
ServicesIpv4CidrBlock *string `json:"servicesIpv4CidrBlock,omitempty"`
}

// MasterAuthorizedNetworksConfig contains configuration options for the master authorized networks feature.
// Enabled master authorized networks will disallow all external traffic to access
// Kubernetes master through HTTPS except traffic from the given CIDR blocks,
Expand Down
61 changes: 61 additions & 0 deletions exp/api/v1beta1/gcpmanagedcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/pointer"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -123,6 +124,15 @@ func (r *GCPManagedControlPlane) ValidateUpdate(oldRaw runtime.Object) (admissio
)
}

// Add IPAllocationPolicy for CIDR support (PLT-1246)

if !cmp.Equal(r.Spec.ClusterIpv4Cidr, old.Spec.ClusterIpv4Cidr) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "ClusterIpv4Cidr"),
pointer.StringDeref(r.Spec.ClusterIpv4Cidr, ""), "field is immutable"),
)
}

if !cmp.Equal(r.Spec.EnableAutopilot, old.Spec.EnableAutopilot) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "EnableAutopilot"),
Expand Down Expand Up @@ -167,3 +177,54 @@ func generateGKEName(resourceName, namespace string, maxLength int) (string, err

return fmt.Sprintf("%s%s", resourcePrefix, hashedName), nil
}

// Add IPAllocationPolicy for CIDR support (PLT-1246)

func validateIPAllocationPolicy(spec GCPManagedControlPlaneSpec) field.ErrorList {
var allErrs field.ErrorList

if spec.IPAllocationPolicy == nil {
return allErrs
}

path := field.NewPath("spec", "IPAllocationPolicy")

isUseIPAliases := pointer.BoolDeref(spec.IPAllocationPolicy.UseIPAliases, false)
if spec.IPAllocationPolicy.ClusterSecondaryRangeName != nil && !isUseIPAliases {
allErrs = append(allErrs,
field.Invalid(path.Child("ClusterSecondaryRangeName"),
spec.IPAllocationPolicy.ClusterSecondaryRangeName,
"field cannot be set unless UseIPAliases is set to true"),
)
}
if spec.IPAllocationPolicy.ServicesSecondaryRangeName != nil && !isUseIPAliases {
allErrs = append(allErrs,
field.Invalid(path.Child("ServicesSecondaryRangeName"),
spec.IPAllocationPolicy.ServicesSecondaryRangeName,
"field cannot be set unless UseIPAliases is set to true"),
)
}
if spec.IPAllocationPolicy.ServicesIpv4CidrBlock != nil && !isUseIPAliases {
allErrs = append(allErrs,
field.Invalid(path.Child("ServicesIpv4CidrBlock"),
spec.IPAllocationPolicy.ServicesIpv4CidrBlock,
"field cannot be set unless UseIPAliases is set to true"),
)
}
if spec.IPAllocationPolicy.ClusterIpv4CidrBlock != nil && !isUseIPAliases {
allErrs = append(allErrs,
field.Invalid(path.Child("ClusterIpv4CidrBlock"),
spec.IPAllocationPolicy.ClusterIpv4CidrBlock,
"field cannot be set unless UseIPAliases is set to true"),
)
}
if spec.IPAllocationPolicy.ClusterIpv4CidrBlock != nil && spec.ClusterIpv4Cidr != nil {
allErrs = append(allErrs,
field.Invalid(path.Child("ClusterIpv4CidrBlock"),
spec.IPAllocationPolicy.ClusterIpv4CidrBlock,
"only one of spec.ClusterIpv4Cidr and spec.IPAllocationPolicy.ClusterIpv4CidrBlock can be set"),
)
}

return allErrs
}
18 changes: 18 additions & 0 deletions exp/api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"cloud.google.com/go/container/apiv1/containerpb"
"k8s.io/utils/pointer"
)

// TaintEffect is the effect for a Kubernetes taint.
Expand Down Expand Up @@ -144,3 +145,20 @@ func ConvertToSdkLinuxNodeConfig(linuxNodeConfig *LinuxNodeConfig) *containerpb.
}
return &sdkLinuxNodeConfig
}

// Add IPAllocationPolicy for CIDR support (PLT-1246)

// ConvertToSdkIPAllocationPolicy converts the CAPG IPAllocationPolicy to a containerpb IPAllocationPolicy.
func ConvertToSdkIPAllocationPolicy(policy *IPAllocationPolicy) *containerpb.IPAllocationPolicy {
if policy == nil {
return nil
}

return &containerpb.IPAllocationPolicy{
UseIpAliases: pointer.BoolDeref(policy.UseIPAliases, false),
ClusterSecondaryRangeName: pointer.StringDeref(policy.ClusterSecondaryRangeName, ""),
ServicesSecondaryRangeName: pointer.StringDeref(policy.ServicesSecondaryRangeName, ""),
ClusterIpv4CidrBlock: pointer.StringDeref(policy.ClusterIpv4CidrBlock, ""),
ServicesIpv4CidrBlock: pointer.StringDeref(policy.ServicesIpv4CidrBlock, ""),
}
}
50 changes: 50 additions & 0 deletions exp/api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c71544

Please sign in to comment.