|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package scope |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "sigs.k8s.io/cluster-api-provider-gcp/util/location" |
| 24 | + |
| 25 | + "google.golang.org/api/option" |
| 26 | + "sigs.k8s.io/cluster-api/util/conditions" |
| 27 | + |
| 28 | + compute "cloud.google.com/go/compute/apiv1" |
| 29 | + container "cloud.google.com/go/container/apiv1" |
| 30 | + "cloud.google.com/go/container/apiv1/containerpb" |
| 31 | + "github.com/pkg/errors" |
| 32 | + infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1" |
| 33 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 34 | + clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1" |
| 35 | + "sigs.k8s.io/cluster-api/util/patch" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 37 | +) |
| 38 | + |
| 39 | +// ManagedMachinePoolScopeParams defines the input parameters used to create a new Scope. |
| 40 | +type ManagedMachinePoolScopeParams struct { |
| 41 | + ManagedClusterClient *container.ClusterManagerClient |
| 42 | + InstanceGroupManagersClient *compute.InstanceGroupManagersClient |
| 43 | + Client client.Client |
| 44 | + Cluster *clusterv1.Cluster |
| 45 | + MachinePool *clusterv1exp.MachinePool |
| 46 | + GCPManagedCluster *infrav1exp.GCPManagedCluster |
| 47 | + GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane |
| 48 | + GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool |
| 49 | +} |
| 50 | + |
| 51 | +// NewManagedMachinePoolScope creates a new Scope from the supplied parameters. |
| 52 | +// This is meant to be called for each reconcile iteration. |
| 53 | +func NewManagedMachinePoolScope(ctx context.Context, params ManagedMachinePoolScopeParams) (*ManagedMachinePoolScope, error) { |
| 54 | + if params.Cluster == nil { |
| 55 | + return nil, errors.New("failed to generate new scope from nil Cluster") |
| 56 | + } |
| 57 | + if params.MachinePool == nil { |
| 58 | + return nil, errors.New("failed to generate new scope from nil MachinePool") |
| 59 | + } |
| 60 | + if params.GCPManagedCluster == nil { |
| 61 | + return nil, errors.New("failed to generate new scope from nil GCPManagedCluster") |
| 62 | + } |
| 63 | + if params.GCPManagedControlPlane == nil { |
| 64 | + return nil, errors.New("failed to generate new scope from nil GCPManagedControlPlane") |
| 65 | + } |
| 66 | + if params.GCPManagedMachinePool == nil { |
| 67 | + return nil, errors.New("failed to generate new scope from nil GCPManagedMachinePool") |
| 68 | + } |
| 69 | + |
| 70 | + var credentialData []byte |
| 71 | + var err error |
| 72 | + if params.GCPManagedCluster.Spec.CredentialsRef != nil { |
| 73 | + credentialData, err = getCredentialDataFromRef(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client) |
| 74 | + } else { |
| 75 | + credentialData, err = getCredentialDataFromMount() |
| 76 | + } |
| 77 | + if err != nil { |
| 78 | + return nil, errors.Errorf("failed to get credential data: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + if params.ManagedClusterClient == nil { |
| 82 | + var managedClusterClient *container.ClusterManagerClient |
| 83 | + managedClusterClient, err = container.NewClusterManagerClient(ctx, option.WithCredentialsJSON(credentialData)) |
| 84 | + if err != nil { |
| 85 | + return nil, errors.Errorf("failed to create gcp managed cluster client: %v", err) |
| 86 | + } |
| 87 | + params.ManagedClusterClient = managedClusterClient |
| 88 | + } |
| 89 | + if params.InstanceGroupManagersClient == nil { |
| 90 | + var instanceGroupManagersClient *compute.InstanceGroupManagersClient |
| 91 | + instanceGroupManagersClient, err = compute.NewInstanceGroupManagersRESTClient(ctx, option.WithCredentialsJSON(credentialData)) |
| 92 | + if err != nil { |
| 93 | + return nil, errors.Errorf("failed to create gcp instance group manager client: %v", err) |
| 94 | + } |
| 95 | + params.InstanceGroupManagersClient = instanceGroupManagersClient |
| 96 | + } |
| 97 | + |
| 98 | + helper, err := patch.NewHelper(params.GCPManagedMachinePool, params.Client) |
| 99 | + if err != nil { |
| 100 | + return nil, errors.Wrap(err, "failed to init patch helper") |
| 101 | + } |
| 102 | + |
| 103 | + return &ManagedMachinePoolScope{ |
| 104 | + client: params.Client, |
| 105 | + Cluster: params.Cluster, |
| 106 | + MachinePool: params.MachinePool, |
| 107 | + GCPManagedControlPlane: params.GCPManagedControlPlane, |
| 108 | + GCPManagedMachinePool: params.GCPManagedMachinePool, |
| 109 | + mcClient: params.ManagedClusterClient, |
| 110 | + migClient: params.InstanceGroupManagersClient, |
| 111 | + patchHelper: helper, |
| 112 | + }, nil |
| 113 | +} |
| 114 | + |
| 115 | +// ManagedMachinePoolScope defines the basic context for an actuator to operate upon. |
| 116 | +type ManagedMachinePoolScope struct { |
| 117 | + client client.Client |
| 118 | + patchHelper *patch.Helper |
| 119 | + |
| 120 | + Cluster *clusterv1.Cluster |
| 121 | + MachinePool *clusterv1exp.MachinePool |
| 122 | + GCPManagedCluster *infrav1exp.GCPManagedCluster |
| 123 | + GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane |
| 124 | + GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool |
| 125 | + mcClient *container.ClusterManagerClient |
| 126 | + migClient *compute.InstanceGroupManagersClient |
| 127 | +} |
| 128 | + |
| 129 | +// PatchObject persists the managed control plane configuration and status. |
| 130 | +func (s *ManagedMachinePoolScope) PatchObject() error { |
| 131 | + return s.patchHelper.Patch( |
| 132 | + context.TODO(), |
| 133 | + s.GCPManagedMachinePool, |
| 134 | + patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{ |
| 135 | + infrav1exp.GKEMachinePoolReadyCondition, |
| 136 | + infrav1exp.GKEMachinePoolCreatingCondition, |
| 137 | + infrav1exp.GKEMachinePoolUpdatingCondition, |
| 138 | + infrav1exp.GKEMachinePoolDeletingCondition, |
| 139 | + }}) |
| 140 | +} |
| 141 | + |
| 142 | +// Close closes the current scope persisting the managed control plane configuration and status. |
| 143 | +func (s *ManagedMachinePoolScope) Close() error { |
| 144 | + s.mcClient.Close() |
| 145 | + s.migClient.Close() |
| 146 | + return s.PatchObject() |
| 147 | +} |
| 148 | + |
| 149 | +// ConditionSetter return a condition setter (which is GCPManagedMachinePool itself). |
| 150 | +func (s *ManagedMachinePoolScope) ConditionSetter() conditions.Setter { |
| 151 | + return s.GCPManagedMachinePool |
| 152 | +} |
| 153 | + |
| 154 | +// ManagedMachinePoolClient returns a client used to interact with GKE. |
| 155 | +func (s *ManagedMachinePoolScope) ManagedMachinePoolClient() *container.ClusterManagerClient { |
| 156 | + return s.mcClient |
| 157 | +} |
| 158 | + |
| 159 | +// InstanceGroupManagersClient returns a client used to interact with GCP MIG. |
| 160 | +func (s *ManagedMachinePoolScope) InstanceGroupManagersClient() *compute.InstanceGroupManagersClient { |
| 161 | + return s.migClient |
| 162 | +} |
| 163 | + |
| 164 | +// NodePoolVersion returns the k8s version of the node pool. |
| 165 | +func (s *ManagedMachinePoolScope) NodePoolVersion() *string { |
| 166 | + return s.MachinePool.Spec.Template.Spec.Version |
| 167 | +} |
| 168 | + |
| 169 | +// ConvertToSdkNodePool converts a node pool to format that is used by GCP SDK. |
| 170 | +func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool clusterv1exp.MachinePool) *containerpb.NodePool { |
| 171 | + nodePoolName := nodePool.Spec.NodePoolName |
| 172 | + if len(nodePoolName) == 0 { |
| 173 | + nodePoolName = nodePool.Name |
| 174 | + } |
| 175 | + sdkNodePool := containerpb.NodePool{ |
| 176 | + Name: nodePoolName, |
| 177 | + InitialNodeCount: nodePool.Spec.InitialNodeCount, |
| 178 | + Config: &containerpb.NodeConfig{ |
| 179 | + Labels: nodePool.Spec.KubernetesLabels, |
| 180 | + Taints: infrav1exp.ConvertToSdkTaint(nodePool.Spec.KubernetesTaints), |
| 181 | + Metadata: nodePool.Spec.AdditionalLabels, |
| 182 | + }, |
| 183 | + } |
| 184 | + if nodePool.Spec.Scaling != nil { |
| 185 | + sdkNodePool.Autoscaling = &containerpb.NodePoolAutoscaling{ |
| 186 | + Enabled: true, |
| 187 | + MinNodeCount: *nodePool.Spec.Scaling.MinCount, |
| 188 | + MaxNodeCount: *nodePool.Spec.Scaling.MaxCount, |
| 189 | + } |
| 190 | + } |
| 191 | + if machinePool.Spec.Template.Spec.Version != nil { |
| 192 | + sdkNodePool.Version = *machinePool.Spec.Template.Spec.Version |
| 193 | + } |
| 194 | + return &sdkNodePool |
| 195 | +} |
| 196 | + |
| 197 | +// ConvertToSdkNodePools converts node pools to format that is used by GCP SDK. |
| 198 | +func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool, machinePools []clusterv1exp.MachinePool) []*containerpb.NodePool { |
| 199 | + res := []*containerpb.NodePool{} |
| 200 | + for i := range nodePools { |
| 201 | + res = append(res, ConvertToSdkNodePool(nodePools[i], machinePools[i])) |
| 202 | + } |
| 203 | + return res |
| 204 | +} |
| 205 | + |
| 206 | +// SetReplicas sets the replicas count in status. |
| 207 | +func (s *ManagedMachinePoolScope) SetReplicas(replicas int32) { |
| 208 | + s.GCPManagedMachinePool.Status.Replicas = replicas |
| 209 | +} |
| 210 | + |
| 211 | +// NodePoolName returns the node pool name. |
| 212 | +func (s *ManagedMachinePoolScope) NodePoolName() string { |
| 213 | + if len(s.GCPManagedMachinePool.Spec.NodePoolName) > 0 { |
| 214 | + return s.GCPManagedMachinePool.Spec.NodePoolName |
| 215 | + } |
| 216 | + return s.GCPManagedMachinePool.Name |
| 217 | +} |
| 218 | + |
| 219 | +// Region returns the region of the GKE node pool. |
| 220 | +func (s *ManagedMachinePoolScope) Region() string { |
| 221 | + loc, _ := location.Parse(s.GCPManagedControlPlane.Spec.Location) |
| 222 | + return loc.Region |
| 223 | +} |
| 224 | + |
| 225 | +// NodePoolLocation returns the location of the node pool. |
| 226 | +func (s *ManagedMachinePoolScope) NodePoolLocation() string { |
| 227 | + return fmt.Sprintf("projects/%s/locations/%s/clusters/%s", s.GCPManagedControlPlane.Spec.Project, s.Region(), s.GCPManagedControlPlane.Spec.ClusterName) |
| 228 | +} |
| 229 | + |
| 230 | +// NodePoolFullName returns the full name of the node pool. |
| 231 | +func (s *ManagedMachinePoolScope) NodePoolFullName() string { |
| 232 | + return fmt.Sprintf("%s/nodePools/%s", s.NodePoolLocation(), s.NodePoolName()) |
| 233 | +} |
0 commit comments