diff --git a/cloud/services/container/clusters/reconcile.go b/cloud/services/container/clusters/reconcile.go index 3adcc40d7..94a81a559 100644 --- a/cloud/services/container/clusters/reconcile.go +++ b/cloud/services/container/clusters/reconcile.go @@ -291,27 +291,29 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error { } if cn.PrivateCluster != nil { - cluster.PrivateClusterConfig = &containerpb.PrivateClusterConfig{} - enablePublicEndpoint := !cn.PrivateCluster.EnablePrivateEndpoint cluster.ControlPlaneEndpointsConfig.IpEndpointsConfig.EnablePublicEndpoint = &enablePublicEndpoint - if cn.PrivateCluster.EnablePrivateEndpoint { cluster.ControlPlaneEndpointsConfig.IpEndpointsConfig.AuthorizedNetworksConfig = &containerpb.MasterAuthorizedNetworksConfig{ Enabled: true, } } - cluster.NetworkConfig.DefaultEnablePrivateNodes = &cn.PrivateCluster.EnablePrivateNodes - - cluster.PrivateClusterConfig.MasterIpv4CidrBlock = cn.PrivateCluster.ControlPlaneCidrBlock - cluster.ControlPlaneEndpointsConfig.IpEndpointsConfig.GlobalAccess = &cn.PrivateCluster.ControlPlaneGlobalAccess - + // Initialize NetworkConfig before accessing DefaultEnablePrivateNodes cluster.NetworkConfig = &containerpb.NetworkConfig{ DefaultSnatStatus: &containerpb.DefaultSnatStatus{ Disabled: cn.PrivateCluster.DisableDefaultSNAT, }, } + cluster.NetworkConfig.DefaultEnablePrivateNodes = &cn.PrivateCluster.EnablePrivateNodes + + cluster.PrivateClusterConfig = &containerpb.PrivateClusterConfig{ + MasterIpv4CidrBlock: cn.PrivateCluster.ControlPlaneCidrBlock, + // EnablePrivateNodes is deprecated but GCP SDK raises an error if the value + // of this field is different from the value of NetworkConfig.DefaultEnablePrivateNodes + EnablePrivateNodes: cn.PrivateCluster.EnablePrivateNodes, + } + cluster.ControlPlaneEndpointsConfig.IpEndpointsConfig.GlobalAccess = &cn.PrivateCluster.ControlPlaneGlobalAccess } } @@ -516,6 +518,12 @@ func (s *Service) checkDiffAndPrepareUpdate(existingCluster *containerpb.Cluster desiredMasterAuthorizedNetworksConfig := convertToSdkMasterAuthorizedNetworksConfig(s.scope.GCPManagedControlPlane.Spec.MasterAuthorizedNetworksConfig) if !compareMasterAuthorizedNetworksConfig(desiredMasterAuthorizedNetworksConfig, existingCluster.GetControlPlaneEndpointsConfig().GetIpEndpointsConfig().GetAuthorizedNetworksConfig()) { needUpdate = true + if clusterUpdate.DesiredControlPlaneEndpointsConfig == nil { + clusterUpdate.DesiredControlPlaneEndpointsConfig = &containerpb.ControlPlaneEndpointsConfig{} + } + if clusterUpdate.DesiredControlPlaneEndpointsConfig.IpEndpointsConfig == nil { + clusterUpdate.DesiredControlPlaneEndpointsConfig.IpEndpointsConfig = &containerpb.ControlPlaneEndpointsConfig_IPEndpointsConfig{} + } clusterUpdate.DesiredControlPlaneEndpointsConfig.IpEndpointsConfig.AuthorizedNetworksConfig = desiredMasterAuthorizedNetworksConfig log.V(2).Info("Master authorized networks config update required", "current", existingCluster.GetControlPlaneEndpointsConfig().GetIpEndpointsConfig().GetAuthorizedNetworksConfig(), "desired", desiredMasterAuthorizedNetworksConfig) }