Skip to content

Commit 541c60e

Browse files
committed
Add GKE NetworkPolicy support
1 parent 9788374 commit 541c60e

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ manager_pull_policy.yaml-e
5252
# junit files
5353
junit.*.xml
5454

55+
# asdf
56+
.tool-versions
57+
5558
.DS_Store
5659

5760
# Tilt files.

cloud/services/container/clusters/reconcile.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
268268
if !s.scope.IsAutopilotCluster() {
269269
cluster.NodePools = scope.ConvertToSdkNodePools(nodePools, machinePools, isRegional, cluster.Name)
270270
}
271+
if s.scope.GCPManagedControlPlane.Spec.NetworkPolicy != nil {
272+
cluster.NetworkPolicy = convertToSdkNetworkPolicy(s.scope.GCPManagedControlPlane.Spec.NetworkPolicy)
273+
}
271274

272275
createClusterRequest := &containerpb.CreateClusterRequest{
273276
Cluster: cluster,
@@ -366,6 +369,24 @@ func convertToSdkMasterAuthorizedNetworksConfig(config *infrav1exp.MasterAuthori
366369
}
367370
}
368371

372+
// convertToSdkNetworkPolicy converts NetworkPolicy config to a value that is used by GCP SDK.
373+
func convertToSdkNetworkPolicy(networkPolicy *infrav1exp.NetworkPolicy) *containerpb.NetworkPolicy {
374+
sdkNetworkPolicy := containerpb.NetworkPolicy{}
375+
sdkNetworkPolicy.Provider = convertToSdkProvider(*networkPolicy.Provider)
376+
if networkPolicy.Enabled != nil {
377+
sdkNetworkPolicy.Enabled = *networkPolicy.Enabled
378+
}
379+
return &sdkNetworkPolicy
380+
}
381+
382+
// convertToSdkProvider converts NetworkPolicyProvider to a value that is used by GCP SDK.
383+
func convertToSdkProvider(provider infrav1exp.NetworkPolicyProvider) containerpb.NetworkPolicy_Provider {
384+
if provider == infrav1exp.Calico {
385+
return containerpb.NetworkPolicy_CALICO
386+
}
387+
return containerpb.NetworkPolicy_PROVIDER_UNSPECIFIED
388+
}
389+
369390
func (s *Service) checkDiffAndPrepareUpdate(existingCluster *containerpb.Cluster, log *logr.Logger) (bool, *containerpb.UpdateClusterRequest) {
370391
log.V(4).Info("Checking diff and preparing update.")
371392

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmanagedcontrolplanes.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ spec:
117117
Public IP addresses.
118118
type: boolean
119119
type: object
120+
networkPolicy:
121+
description: NetworkPolicy represents configuration options for NetworkPolicy
122+
feature of the GKE cluster. This feature is disabled if this field
123+
is not specified.
124+
properties:
125+
enabled:
126+
description: Whether network policy is enabled on the cluster.
127+
type: boolean
128+
provider:
129+
description: The selected network policy provider.
130+
enum:
131+
- calico
132+
type: string
133+
type: object
120134
project:
121135
description: Project is the name of the project to deploy the cluster
122136
to.

exp/api/v1beta1/gcpmanagedcontrolplane_types.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ type GCPManagedControlPlaneSpec struct {
5757
// This feature is disabled if this field is not specified.
5858
// +optional
5959
MasterAuthorizedNetworksConfig *MasterAuthorizedNetworksConfig `json:"master_authorized_networks_config,omitempty"`
60+
// NetworkPolicy represents configuration options for NetworkPolicy feature of the GKE cluster.
61+
// This feature is disabled if this field is not specified.
62+
// +optional
63+
NetworkPolicy *NetworkPolicy `json:"networkPolicy,omitempty"`
6064
}
6165

6266
// GCPManagedControlPlaneStatus defines the observed state of GCPManagedControlPlane.
@@ -142,6 +146,25 @@ type MasterAuthorizedNetworksConfigCidrBlock struct {
142146
CidrBlock string `json:"cidr_block,omitempty"`
143147
}
144148

149+
// NetworkPolicy represents configuration options for NetworkPolicy feature of the GKE cluster.
150+
type NetworkPolicy struct {
151+
// The selected network policy provider.
152+
// +optional
153+
Provider *NetworkPolicyProvider `json:"provider,omitempty"`
154+
// Whether network policy is enabled on the cluster.
155+
// +optional
156+
Enabled *bool `json:"enabled,omitempty"`
157+
}
158+
159+
// Allowed Network Policy providers.
160+
// +kubebuilder:validation:Enum=calico
161+
type NetworkPolicyProvider string
162+
163+
const (
164+
// Tigera (Calico Felix).
165+
Calico NetworkPolicyProvider = "calico"
166+
)
167+
145168
// GetConditions returns the control planes conditions.
146169
func (r *GCPManagedControlPlane) GetConditions() clusterv1.Conditions {
147170
return r.Status.Conditions

exp/api/v1beta1/gcpmanagedcontrolplane_webhook.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ func (r *GCPManagedControlPlane) ValidateUpdate(oldRaw runtime.Object) (admissio
130130
)
131131
}
132132

133+
if !cmp.Equal(r.Spec.NetworkPolicy, old.Spec.NetworkPolicy) {
134+
allErrs = append(allErrs,
135+
field.Invalid(field.NewPath("spec", "NetworkPolicy"),
136+
r.Spec.NetworkPolicy, "field is immutable"),
137+
)
138+
}
139+
133140
if len(allErrs) == 0 {
134141
return nil, nil
135142
}

exp/api/v1beta1/zz_generated.deepcopy.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)