Skip to content

Commit

Permalink
Add GKE NetworkPolicy support
Browse files Browse the repository at this point in the history
  • Loading branch information
kahun committed Feb 7, 2024
1 parent 9788374 commit 541c60e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ manager_pull_policy.yaml-e
# junit files
junit.*.xml

# asdf
.tool-versions

.DS_Store

# Tilt files.
Expand Down
21 changes: 21 additions & 0 deletions cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
if !s.scope.IsAutopilotCluster() {
cluster.NodePools = scope.ConvertToSdkNodePools(nodePools, machinePools, isRegional, cluster.Name)
}
if s.scope.GCPManagedControlPlane.Spec.NetworkPolicy != nil {
cluster.NetworkPolicy = convertToSdkNetworkPolicy(s.scope.GCPManagedControlPlane.Spec.NetworkPolicy)
}

createClusterRequest := &containerpb.CreateClusterRequest{
Cluster: cluster,
Expand Down Expand Up @@ -366,6 +369,24 @@ func convertToSdkMasterAuthorizedNetworksConfig(config *infrav1exp.MasterAuthori
}
}

// convertToSdkNetworkPolicy converts NetworkPolicy config to a value that is used by GCP SDK.
func convertToSdkNetworkPolicy(networkPolicy *infrav1exp.NetworkPolicy) *containerpb.NetworkPolicy {
sdkNetworkPolicy := containerpb.NetworkPolicy{}
sdkNetworkPolicy.Provider = convertToSdkProvider(*networkPolicy.Provider)
if networkPolicy.Enabled != nil {
sdkNetworkPolicy.Enabled = *networkPolicy.Enabled
}
return &sdkNetworkPolicy
}

// convertToSdkProvider converts NetworkPolicyProvider to a value that is used by GCP SDK.
func convertToSdkProvider(provider infrav1exp.NetworkPolicyProvider) containerpb.NetworkPolicy_Provider {
if provider == infrav1exp.Calico {
return containerpb.NetworkPolicy_CALICO
}
return containerpb.NetworkPolicy_PROVIDER_UNSPECIFIED
}

func (s *Service) checkDiffAndPrepareUpdate(existingCluster *containerpb.Cluster, log *logr.Logger) (bool, *containerpb.UpdateClusterRequest) {
log.V(4).Info("Checking diff and preparing update.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ spec:
Public IP addresses.
type: boolean
type: object
networkPolicy:
description: NetworkPolicy represents configuration options for NetworkPolicy
feature of the GKE cluster. This feature is disabled if this field
is not specified.
properties:
enabled:
description: Whether network policy is enabled on the cluster.
type: boolean
provider:
description: The selected network policy provider.
enum:
- calico
type: string
type: object
project:
description: Project is the name of the project to deploy the cluster
to.
Expand Down
23 changes: 23 additions & 0 deletions exp/api/v1beta1/gcpmanagedcontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type GCPManagedControlPlaneSpec struct {
// This feature is disabled if this field is not specified.
// +optional
MasterAuthorizedNetworksConfig *MasterAuthorizedNetworksConfig `json:"master_authorized_networks_config,omitempty"`
// NetworkPolicy represents configuration options for NetworkPolicy feature of the GKE cluster.
// This feature is disabled if this field is not specified.
// +optional
NetworkPolicy *NetworkPolicy `json:"networkPolicy,omitempty"`
}

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

// NetworkPolicy represents configuration options for NetworkPolicy feature of the GKE cluster.
type NetworkPolicy struct {
// The selected network policy provider.
// +optional
Provider *NetworkPolicyProvider `json:"provider,omitempty"`
// Whether network policy is enabled on the cluster.
// +optional
Enabled *bool `json:"enabled,omitempty"`
}

// Allowed Network Policy providers.

Check warning on line 159 in exp/api/v1beta1/gcpmanagedcontrolplane_types.go

View workflow job for this annotation

GitHub Actions / lint

exported: comment on exported type NetworkPolicyProvider should be of the form "NetworkPolicyProvider ..." (with optional leading article) (revive)
// +kubebuilder:validation:Enum=calico
type NetworkPolicyProvider string

const (
// Tigera (Calico Felix).

Check warning on line 164 in exp/api/v1beta1/gcpmanagedcontrolplane_types.go

View workflow job for this annotation

GitHub Actions / lint

exported: comment on exported const Calico should be of the form "Calico ..." (revive)
Calico NetworkPolicyProvider = "calico"
)

// GetConditions returns the control planes conditions.
func (r *GCPManagedControlPlane) GetConditions() clusterv1.Conditions {
return r.Status.Conditions
Expand Down
7 changes: 7 additions & 0 deletions exp/api/v1beta1/gcpmanagedcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ func (r *GCPManagedControlPlane) ValidateUpdate(oldRaw runtime.Object) (admissio
)
}

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

if len(allErrs) == 0 {
return nil, nil
}
Expand Down
30 changes: 30 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 541c60e

Please sign in to comment.