Skip to content

Commit

Permalink
Fix gke master version
Browse files Browse the repository at this point in the history
  • Loading branch information
kahun committed Dec 19, 2023
1 parent 4dc19df commit eb6adc9
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package clusters
import (
"context"
"fmt"
"strings"

"sigs.k8s.io/cluster-api-provider-gcp/cloud/scope"
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/shared"
Expand Down Expand Up @@ -262,7 +263,7 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
MasterAuthorizedNetworksConfig: convertToSdkMasterAuthorizedNetworksConfig(s.scope.GCPManagedControlPlane.Spec.MasterAuthorizedNetworksConfig),
}
if s.scope.GCPManagedControlPlane.Spec.ControlPlaneVersion != nil {
cluster.InitialClusterVersion = *s.scope.GCPManagedControlPlane.Spec.ControlPlaneVersion
cluster.InitialClusterVersion = convertToSdkMasterVersion(*s.scope.GCPManagedControlPlane.Spec.ControlPlaneVersion)
}
if !s.scope.IsAutopilotCluster() {
cluster.NodePools = scope.ConvertToSdkNodePools(nodePools, machinePools, isRegional, cluster.Name)
Expand Down Expand Up @@ -333,6 +334,11 @@ func convertToSdkReleaseChannel(channel *infrav1exp.ReleaseChannel) containerpb.
}
}

func convertToSdkMasterVersion(masterVersion string) string {
// For example, the master version returned from GCP SDK can be 1.27.2-gke.2100, we want to convert it to 1.27.2
return strings.Replace(strings.Split(masterVersion, "-")[0], "v", "", 1)
}

// convertToSdkMasterAuthorizedNetworksConfig converts the MasterAuthorizedNetworksConfig defined in CRs to the SDK version.
func convertToSdkMasterAuthorizedNetworksConfig(config *infrav1exp.MasterAuthorizedNetworksConfig) *containerpb.MasterAuthorizedNetworksConfig {
// if config is nil, it means that the user wants to disable the feature.
Expand Down Expand Up @@ -376,11 +382,12 @@ func (s *Service) checkDiffAndPrepareUpdate(existingCluster *containerpb.Cluster
}
// Master version
if s.scope.GCPManagedControlPlane.Spec.ControlPlaneVersion != nil {
desiredMasterVersion := *s.scope.GCPManagedControlPlane.Spec.ControlPlaneVersion
if desiredMasterVersion != existingCluster.CurrentMasterVersion {
desiredMasterVersion := convertToSdkMasterVersion(*s.scope.GCPManagedControlPlane.Spec.ControlPlaneVersion)
existingClusterMasterVersion := convertToSdkMasterVersion(existingCluster.CurrentMasterVersion)
if desiredMasterVersion != existingClusterMasterVersion {
needUpdate = true
clusterUpdate.DesiredMasterVersion = desiredMasterVersion
log.V(2).Info("Master version update required", "current", existingCluster.CurrentMasterVersion, "desired", desiredMasterVersion)
log.V(2).Info("Master version update required", "current", existingClusterMasterVersion, "desired", desiredMasterVersion)
}
}

Expand Down

0 comments on commit eb6adc9

Please sign in to comment.