Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent GCPManagedCluster reconciler to not crash if Network.Name and Network.Subnets is not passed to it #1261

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cloud/services/container/clusters/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
// ErrAutopilotClusterMachinePoolsNotAllowed is used when there are machine pools specified for an autopilot enabled cluster.
var ErrAutopilotClusterMachinePoolsNotAllowed = errors.New("cannot use machine pools with an autopilot enabled cluster")

// ErrGCPManagedClusterHasNoNetworkDefined is used when there is no Network definition provided in a GCPManagedCluster.
var ErrGCPManagedClusterHasNoNetworkDefined = errors.New("cannot reconcile GCPManagedCluster if Network is not defined")

// NewErrUnexpectedClusterStatus creates a new error for an unexpected cluster status.
func NewErrUnexpectedClusterStatus(status string) error {
return &UnexpectedClusterStatusError{status}
Expand Down
18 changes: 18 additions & 0 deletions cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/googleapis/gax-go/v2/apierror"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand All @@ -55,6 +56,14 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
s.scope.GCPManagedControlPlane.Status.Initialized = false
s.scope.GCPManagedControlPlane.Status.Ready = false

if !isNetworkSpecValid(s.scope.GCPManagedCluster.Spec.Network) {
log.Error(ErrGCPManagedClusterHasNoNetworkDefined, "Error Reconciling GCPManagedControlPlane", "name", s.scope.ClusterName())
conditions.MarkFalse(s.scope.ConditionSetter(), clusterv1.ReadyCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, ErrGCPManagedClusterHasNoNetworkDefined.Error())
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneReadyCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, ErrGCPManagedClusterHasNoNetworkDefined.Error())
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneCreatingCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, ErrGCPManagedClusterHasNoNetworkDefined.Error())
return ctrl.Result{}, ErrGCPManagedClusterHasNoNetworkDefined
}

nodePools, _, err := s.scope.GetAllNodePools(ctx)
if err != nil {
conditions.MarkFalse(s.scope.ConditionSetter(), clusterv1.ReadyCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error())
Expand Down Expand Up @@ -449,3 +458,12 @@ func compareMasterAuthorizedNetworksConfig(a, b *containerpb.MasterAuthorizedNet
}
return true
}

// isNetworkSpecValid checks for the Name and Subnets field inside infrav1.NetworkSpec and returns true
// if both are present. As of now only checking for Name and Subnets field as that is what is being used in the createCluster flow.
func isNetworkSpecValid(network infrav1.NetworkSpec) bool {
if network.Name == nil || len(network.Subnets) == 0 {
return false
}
return true
}