Skip to content

Commit 53cc350

Browse files
committed
fix: nil pointer dereferences in private cluster reconciliation
Fixes two nil pointer dereference issues when creating/reconciling private GKE clusters: 1. Creation path: Initialize NetworkConfig before accessing DefaultEnablePrivateNodes. Also set EnablePrivateNodes on PrivateClusterConfig to match (GCP SDK requires both to be equal). 2. Reconciliation path: Initialize DesiredControlPlaneEndpointsConfig and IpEndpointsConfig before assigning AuthorizedNetworksConfig in checkDiffAndPrepareUpdate. Both issues occur when using private clusters with PSC (Private Service Connect) mode, i.e., enablePrivateEndpoint: true without specifying controlPlaneCidrBlock. Related issues: - kubernetes-sigs#1497 - kubernetes-sigs#1503 Signed-off-by: Piotr Kieszczyński <piotr.kieszczynski@gmail.com>
1 parent 3b04ea4 commit 53cc350

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

cloud/services/container/clusters/reconcile.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,27 +291,29 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
291291
}
292292

293293
if cn.PrivateCluster != nil {
294-
cluster.PrivateClusterConfig = &containerpb.PrivateClusterConfig{}
295-
296294
enablePublicEndpoint := !cn.PrivateCluster.EnablePrivateEndpoint
297295
cluster.ControlPlaneEndpointsConfig.IpEndpointsConfig.EnablePublicEndpoint = &enablePublicEndpoint
298-
299296
if cn.PrivateCluster.EnablePrivateEndpoint {
300297
cluster.ControlPlaneEndpointsConfig.IpEndpointsConfig.AuthorizedNetworksConfig = &containerpb.MasterAuthorizedNetworksConfig{
301298
Enabled: true,
302299
}
303300
}
304301

305-
cluster.NetworkConfig.DefaultEnablePrivateNodes = &cn.PrivateCluster.EnablePrivateNodes
306-
307-
cluster.PrivateClusterConfig.MasterIpv4CidrBlock = cn.PrivateCluster.ControlPlaneCidrBlock
308-
cluster.ControlPlaneEndpointsConfig.IpEndpointsConfig.GlobalAccess = &cn.PrivateCluster.ControlPlaneGlobalAccess
309-
302+
// Initialize NetworkConfig before accessing DefaultEnablePrivateNodes
310303
cluster.NetworkConfig = &containerpb.NetworkConfig{
311304
DefaultSnatStatus: &containerpb.DefaultSnatStatus{
312305
Disabled: cn.PrivateCluster.DisableDefaultSNAT,
313306
},
314307
}
308+
cluster.NetworkConfig.DefaultEnablePrivateNodes = &cn.PrivateCluster.EnablePrivateNodes
309+
310+
cluster.PrivateClusterConfig = &containerpb.PrivateClusterConfig{
311+
MasterIpv4CidrBlock: cn.PrivateCluster.ControlPlaneCidrBlock,
312+
// EnablePrivateNodes is deprecated but GCP SDK raises an error if the value
313+
// of this field is different from the value of NetworkConfig.DefaultEnablePrivateNodes
314+
EnablePrivateNodes: cn.PrivateCluster.EnablePrivateNodes,
315+
}
316+
cluster.ControlPlaneEndpointsConfig.IpEndpointsConfig.GlobalAccess = &cn.PrivateCluster.ControlPlaneGlobalAccess
315317
}
316318
}
317319

@@ -516,6 +518,12 @@ func (s *Service) checkDiffAndPrepareUpdate(existingCluster *containerpb.Cluster
516518
desiredMasterAuthorizedNetworksConfig := convertToSdkMasterAuthorizedNetworksConfig(s.scope.GCPManagedControlPlane.Spec.MasterAuthorizedNetworksConfig)
517519
if !compareMasterAuthorizedNetworksConfig(desiredMasterAuthorizedNetworksConfig, existingCluster.GetControlPlaneEndpointsConfig().GetIpEndpointsConfig().GetAuthorizedNetworksConfig()) {
518520
needUpdate = true
521+
if clusterUpdate.DesiredControlPlaneEndpointsConfig == nil {
522+
clusterUpdate.DesiredControlPlaneEndpointsConfig = &containerpb.ControlPlaneEndpointsConfig{}
523+
}
524+
if clusterUpdate.DesiredControlPlaneEndpointsConfig.IpEndpointsConfig == nil {
525+
clusterUpdate.DesiredControlPlaneEndpointsConfig.IpEndpointsConfig = &containerpb.ControlPlaneEndpointsConfig_IPEndpointsConfig{}
526+
}
519527
clusterUpdate.DesiredControlPlaneEndpointsConfig.IpEndpointsConfig.AuthorizedNetworksConfig = desiredMasterAuthorizedNetworksConfig
520528
log.V(2).Info("Master authorized networks config update required", "current", existingCluster.GetControlPlaneEndpointsConfig().GetIpEndpointsConfig().GetAuthorizedNetworksConfig(), "desired", desiredMasterAuthorizedNetworksConfig)
521529
}

0 commit comments

Comments
 (0)