Skip to content

Commit

Permalink
Remove NodeVersion field
Browse files Browse the repository at this point in the history
  • Loading branch information
richardchen331 committed Jan 26, 2023
1 parent ee03800 commit eaca583
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 33 deletions.
24 changes: 17 additions & 7 deletions cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pkg/errors"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -125,7 +126,8 @@ type ManagedControlPlaneScope struct {
credentialsClient *credentials.IamCredentialsClient
credential *Credential

AllNodePools []infrav1exp.GCPManagedMachinePool
AllMachinePools []clusterv1exp.MachinePool
AllManagedMachinePools []infrav1exp.GCPManagedMachinePool
}

// PatchObject persists the managed control plane configuration and status.
Expand Down Expand Up @@ -174,21 +176,29 @@ func (s *ManagedControlPlaneScope) GetCredential() *Credential {
}

// GetAllNodePools gets all node pools for the control plane.
func (s *ManagedControlPlaneScope) GetAllNodePools(ctx context.Context) ([]infrav1exp.GCPManagedMachinePool, error) {
if s.AllNodePools == nil {
func (s *ManagedControlPlaneScope) GetAllNodePools(ctx context.Context) ([]infrav1exp.GCPManagedMachinePool, []clusterv1exp.MachinePool, error) {
if s.AllManagedMachinePools == nil || len(s.AllManagedMachinePools) == 0 {
opt1 := client.InNamespace(s.GCPManagedControlPlane.Namespace)
opt2 := client.MatchingLabels(map[string]string{
clusterv1.ClusterLabelName: s.Cluster.Name,
})

machinePoolList := &infrav1exp.GCPManagedMachinePoolList{}
machinePoolList := &clusterv1exp.MachinePoolList{}
if err := s.client.List(ctx, machinePoolList, opt1, opt2); err != nil {
return nil, err
return nil, nil, err
}
s.AllNodePools = machinePoolList.Items
managedMachinePoolList := &infrav1exp.GCPManagedMachinePoolList{}
if err := s.client.List(ctx, managedMachinePoolList, opt1, opt2); err != nil {
return nil, nil, err
}
if len(machinePoolList.Items) != len(managedMachinePoolList.Items) {
return nil, nil, fmt.Errorf("machinePoolList length (%d) != managedMachinePoolList length (%d)", len(machinePoolList.Items), len(managedMachinePoolList.Items))
}
s.AllMachinePools = machinePoolList.Items
s.AllManagedMachinePools = managedMachinePoolList.Items
}

return s.AllNodePools, nil
return s.AllManagedMachinePools, s.AllMachinePools, nil
}

func parseLocation(location string) string {
Expand Down
23 changes: 17 additions & 6 deletions cloud/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pkg/errors"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -40,6 +41,7 @@ type ManagedMachinePoolScopeParams struct {
InstanceGroupManagersClient *compute.InstanceGroupManagersClient
Client client.Client
Cluster *clusterv1.Cluster
MachinePool *clusterv1exp.MachinePool
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
Expand All @@ -51,6 +53,9 @@ func NewManagedMachinePoolScope(ctx context.Context, params ManagedMachinePoolSc
if params.Cluster == nil {
return nil, errors.New("failed to generate new scope from nil Cluster")
}
if params.MachinePool == nil {
return nil, errors.New("failed to generate new scope from nil MachinePool")
}
if params.GCPManagedCluster == nil {
return nil, errors.New("failed to generate new scope from nil GCPManagedCluster")
}
Expand Down Expand Up @@ -111,6 +116,7 @@ type ManagedMachinePoolScope struct {
patchHelper *patch.Helper

Cluster *clusterv1.Cluster
MachinePool *clusterv1exp.MachinePool
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
Expand Down Expand Up @@ -153,6 +159,11 @@ func (s *ManagedMachinePoolScope) InstanceGroupManagersClient() *compute.Instanc
return s.migClient
}

// NodePoolVersion returns the k8s version of the node pool.
func (s *ManagedMachinePoolScope) NodePoolVersion() *string {
return s.MachinePool.Spec.Template.Spec.Version
}

// ParseInstanceGroupURL parses an instance group URL.
func ParseInstanceGroupURL(url string) (project string, zone string, mig string) {
parts := strings.Split(url, "/")
Expand All @@ -163,7 +174,7 @@ func ParseInstanceGroupURL(url string) (project string, zone string, mig string)
}

// ConvertToSdkNodePool convertsSetReplicas a node pool to format that is used by GCP SDK.
func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool) *containerpb.NodePool {
func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool clusterv1exp.MachinePool) *containerpb.NodePool {
nodePoolName := nodePool.Spec.NodePoolName
if len(nodePoolName) == 0 {
nodePoolName = nodePool.Name
Expand All @@ -177,17 +188,17 @@ func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool) *containerp
Metadata: nodePool.Spec.AdditionalLabels,
},
}
if nodePool.Spec.NodeVersion != nil {
sdkNodePool.Version = *nodePool.Spec.NodeVersion
if machinePool.Spec.Template.Spec.Version != nil {
sdkNodePool.Version = *machinePool.Spec.Template.Spec.Version
}
return &sdkNodePool
}

// ConvertToSdkNodePools converts node pools to format that is used by GCP SDK.
func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool) []*containerpb.NodePool {
func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool, machinePools []clusterv1exp.MachinePool) []*containerpb.NodePool {
res := []*containerpb.NodePool{}
for _, nodePool := range nodePools {
res = append(res, ConvertToSdkNodePool(nodePool))
for i := range nodePools {
res = append(res, ConvertToSdkNodePool(nodePools[i], machinePools[i]))
}
return res
}
Expand Down
6 changes: 3 additions & 3 deletions cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
log.Info("Cluster not found, creating")
s.scope.GCPManagedControlPlane.Status.Ready = false

nodePools, err := s.scope.GetAllNodePools(ctx)
nodePools, _, err := s.scope.GetAllNodePools(ctx)
if err != nil {
conditions.MarkFalse(s.scope.ConditionSetter(), clusterv1.ReadyCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error())
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneReadyCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error())
Expand Down Expand Up @@ -215,14 +215,14 @@ func (s *Service) describeCluster(ctx context.Context) (*containerpb.Cluster, er
func (s *Service) createCluster(ctx context.Context) error {
log := log.FromContext(ctx)

nodePools, _ := s.scope.GetAllNodePools(ctx)
nodePools, machinePools, _ := s.scope.GetAllNodePools(ctx)
cluster := &containerpb.Cluster{
Name: s.scope.GCPManagedControlPlane.Spec.ClusterName,
Network: *s.scope.GCPManagedCluster.Spec.Network.Name,
Autopilot: &containerpb.Autopilot{
Enabled: false,
},
NodePools: scope.ConvertToSdkNodePools(nodePools),
NodePools: scope.ConvertToSdkNodePools(nodePools, machinePools),
ReleaseChannel: &containerpb.ReleaseChannel{
Channel: convertToSdkReleaseChannel(s.scope.GCPManagedControlPlane.Spec.ReleaseChannel),
},
Expand Down
6 changes: 3 additions & 3 deletions cloud/services/container/nodepools/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (s *Service) getInstances(ctx context.Context, nodePool *containerpb.NodePo

func (s *Service) createNodePool(ctx context.Context) error {
createNodePoolRequest := &containerpb.CreateNodePoolRequest{
NodePool: scope.ConvertToSdkNodePool(*s.scope.GCPManagedMachinePool),
NodePool: scope.ConvertToSdkNodePool(*s.scope.GCPManagedMachinePool, *s.scope.MachinePool),
Parent: s.scope.NodePoolLocation(),
}
_, err := s.scope.ManagedMachinePoolClient().CreateNodePool(ctx, createNodePoolRequest)
Expand Down Expand Up @@ -293,9 +293,9 @@ func (s *Service) checkDiffAndPrepareUpdateVersionOrImage(existingNodePool *cont
Name: s.scope.NodePoolFullName(),
}
// Node version
if s.scope.GCPManagedMachinePool.Spec.NodeVersion != nil && *s.scope.GCPManagedMachinePool.Spec.NodeVersion != existingNodePool.Version {
if s.scope.NodePoolVersion() != nil && *s.scope.NodePoolVersion() != existingNodePool.Version {
needUpdate = true
updateNodePoolRequest.NodeVersion = *s.scope.GCPManagedMachinePool.Spec.NodeVersion
updateNodePoolRequest.NodeVersion = *s.scope.NodePoolVersion()
}
// Kubernetes labels
if !reflect.DeepEqual(map[string]string(s.scope.GCPManagedMachinePool.Spec.KubernetesLabels), existingNodePool.Config.Labels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ spec:
a default name will be created based on the namespace and name of
the managed machine pool.
type: string
nodeVersion:
description: NodeVersion represents the node version of the node pool.
If not specified, the GKE cluster control plane version will be
used.
type: string
providerIDList:
description: ProviderIDList are the provider IDs of instances in the
managed instance group corresponding to the nodegroup represented
Expand Down
4 changes: 0 additions & 4 deletions exp/api/v1beta1/gcpmanagedmachinepool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ type GCPManagedMachinePoolSpec struct {
// then a default name will be created based on the namespace and name of the managed machine pool.
// +optional
NodePoolName string `json:"nodePoolName,omitempty"`
// NodeVersion represents the node version of the node pool.
// If not specified, the GKE cluster control plane version will be used.
// +optional
NodeVersion *string `json:"nodeVersion,omitempty"`
// NodeCount represents the initial number of nodes for the pool.
// In regional or multi-zonal clusters, this is the number of nodes per zone.
NodeCount int32 `json:"nodeCount"`
Expand Down
5 changes: 0 additions & 5 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.

1 change: 1 addition & 0 deletions exp/controllers/gcpmanagedmachinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func (r *GCPManagedMachinePoolReconciler) Reconcile(ctx context.Context, req ctr
managedMachinePoolScope, err := scope.NewManagedMachinePoolScope(ctx, scope.ManagedMachinePoolScopeParams{
Client: r.Client,
Cluster: cluster,
MachinePool: machinePool,
GCPManagedCluster: gcpManagedCluster,
GCPManagedControlPlane: gcpManagedControlPlane,
GCPManagedMachinePool: gcpManagedMachinePool,
Expand Down

0 comments on commit eaca583

Please sign in to comment.