Skip to content

Commit 861fc62

Browse files
authored
feat: Add more MachinePool flexibility (#94)
This will now: - keep track of machine pool instances in status - variablize allowing public ips for the machine pool instances
1 parent 527ed06 commit 861fc62

File tree

9 files changed

+102
-37
lines changed

9 files changed

+102
-37
lines changed

cloud/scope/cluster.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,13 @@ func (s *ClusterScope) setFailureDomains(ctx context.Context) error {
168168
})
169169
}
170170
} else {
171-
req := identity.ListFaultDomainsRequest{
172-
CompartmentId: common.String(s.GetCompartmentId()),
173-
AvailabilityDomain: respAd.Items[0].Name,
174-
}
175-
resp, err := s.IdentityClient.ListFaultDomains(ctx, req)
176-
if err != nil {
177-
s.Logger.Error(err, "failed to list fault domains")
178-
return err
179-
}
180-
for i, fd := range resp.Items {
171+
adName := *respAd.Items[0].Name
172+
for i, fd := range s.OCICluster.Status.AvailabilityDomains[adName].FaultDomains {
181173
s.SetFailureDomain(strconv.Itoa(i+1), clusterv1.FailureDomainSpec{
182174
ControlPlane: true,
183175
Attributes: map[string]string{
184-
AvailabilityDomain: *fd.AvailabilityDomain,
185-
FaultDomain: *fd.Name,
176+
AvailabilityDomain: adName,
177+
FaultDomain: fd,
186178
},
187179
})
188180
}

cloud/scope/cluster_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,6 @@ func TestClusterScope_ReconcileFailureDomains(t *testing.T) {
151151
},
152152
}}, nil)
153153

154-
identityClient.EXPECT().ListFaultDomains(gomock.Any(), gomock.Eq(identity.ListFaultDomainsRequest{
155-
CompartmentId: common.String("1ad"),
156-
AvailabilityDomain: common.String("ad1"),
157-
})).Return(identity.ListFaultDomainsResponse{Items: []identity.FaultDomain{
158-
{
159-
Name: common.String("fd1"),
160-
AvailabilityDomain: common.String("ad1"),
161-
},
162-
{
163-
Name: common.String("fd2"),
164-
AvailabilityDomain: common.String("ad1"),
165-
},
166-
{
167-
Name: common.String("fd3"),
168-
AvailabilityDomain: common.String("ad1"),
169-
},
170-
}}, nil)
171-
172154
identityClient.EXPECT().ListFaultDomains(gomock.Any(), gomock.Eq(identity.ListFaultDomainsRequest{
173155
CompartmentId: common.String("2ad"),
174156
AvailabilityDomain: common.String("ad1"),

cloud/scope/machine_pool.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ func (m *MachinePoolScope) SetReady() {
140140
m.OCIMachinePool.Status.Ready = true
141141
}
142142

143+
func (m *MachinePoolScope) SetReplicaCount(count int32) {
144+
m.OCIMachinePool.Status.Replicas = count
145+
}
146+
143147
// GetWorkerMachineSubnet returns the WorkerRole core.Subnet id for the cluster
144148
func (m *MachinePoolScope) GetWorkerMachineSubnet() *string {
145149
for _, subnet := range m.OCICluster.Spec.NetworkSpec.Vcn.Subnets {
@@ -150,6 +154,42 @@ func (m *MachinePoolScope) GetWorkerMachineSubnet() *string {
150154
return nil
151155
}
152156

157+
// ListMachinePoolInstances returns the WorkerRole core.Subnet id for the cluster
158+
func (m *MachinePoolScope) ListMachinePoolInstances(ctx context.Context) ([]core.InstanceSummary, error) {
159+
poolOid := m.OCIMachinePool.Spec.OCID
160+
if len(poolOid) <= 0 {
161+
return nil, errors.New("OCIMachinePool OCID can't be empty")
162+
}
163+
164+
req := core.ListInstancePoolInstancesRequest{
165+
CompartmentId: common.String(m.OCICluster.Spec.CompartmentId),
166+
InstancePoolId: common.String(poolOid),
167+
}
168+
resp, err := m.ComputeManagementClient.ListInstancePoolInstances(ctx, req)
169+
if err != nil {
170+
return nil, err
171+
}
172+
173+
return resp.Items, nil
174+
}
175+
176+
// SetListandSetMachinePoolInstances retrieves a machine pools instances and sets them in the ProviderIDList
177+
func (m *MachinePoolScope) SetListandSetMachinePoolInstances(ctx context.Context) (int32, error) {
178+
poolInstanceSummaries, err := m.ListMachinePoolInstances(ctx)
179+
if err != nil {
180+
return 0, errors.New("Unable to list machine pool's instances")
181+
}
182+
providerIDList := make([]string, len(poolInstanceSummaries))
183+
184+
for i, instance := range poolInstanceSummaries {
185+
providerIDList[i] = fmt.Sprintf("oci://%s", *instance.Id)
186+
}
187+
188+
m.OCIMachinePool.Spec.ProviderIDList = providerIDList
189+
190+
return int32(len(providerIDList)), nil
191+
}
192+
153193
// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
154194
func (m *MachinePoolScope) GetBootstrapData() (string, error) {
155195
if m.MachinePool.Spec.Template.Spec.Bootstrap.DataSecretName == nil {
@@ -330,9 +370,8 @@ func (m *MachinePoolScope) ReconcileInstanceConfiguration(ctx context.Context) e
330370
},
331371
Metadata: metadata,
332372
CreateVnicDetails: &core.InstanceConfigurationCreateVnicDetails{
333-
SubnetId: subnetId,
334-
// TODO variablize AssignPublicIp in the future
335-
AssignPublicIp: common.Bool(false),
373+
SubnetId: subnetId,
374+
AssignPublicIp: common.Bool(m.OCIMachinePool.Spec.VNICAssignPublicIp),
336375
NsgIds: []string{*nsgId},
337376
},
338377
},
@@ -472,7 +511,9 @@ func (m *MachinePoolScope) UpdatePool(ctx context.Context, instancePool *core.In
472511
}
473512

474513
req := core.UpdateInstancePoolRequest{InstancePoolId: instancePool.Id,
475-
UpdateInstancePoolDetails: core.UpdateInstancePoolDetails{Size: common.Int(replicas)}}
514+
UpdateInstancePoolDetails: core.UpdateInstancePoolDetails{
515+
Size: common.Int(replicas),
516+
}}
476517

477518
if _, err := m.ComputeManagementClient.UpdateInstancePool(ctx, req); err != nil {
478519
return errors.Wrap(err, "unable to update instance pool")
@@ -492,7 +533,7 @@ func (m *MachinePoolScope) TerminateInstancePool(ctx context.Context, instancePo
492533
return nil
493534
}
494535

495-
// instancePoolNeedsUpdates compares incoming OCIMachinePool and compares against existing ASG.
536+
// instancePoolNeedsUpdates compares incoming OCIMachinePool and compares against existing instance pool.
496537
func instancePoolNeedsUpdates(machinePoolScope *MachinePoolScope, instancePool *core.InstancePool) bool {
497538

498539
// Allow pool resize

cloud/services/computemanagement/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Client interface {
1313
TerminateInstancePool(ctx context.Context, request core.TerminateInstancePoolRequest) (response core.TerminateInstancePoolResponse, err error)
1414
UpdateInstancePool(ctx context.Context, request core.UpdateInstancePoolRequest) (response core.UpdateInstancePoolResponse, err error)
1515
ListInstancePools(ctx context.Context, request core.ListInstancePoolsRequest) (response core.ListInstancePoolsResponse, err error)
16+
ListInstancePoolInstances(ctx context.Context, request core.ListInstancePoolInstancesRequest) (response core.ListInstancePoolInstancesResponse, err error)
1617

1718
// Instance Configuration
1819
CreateInstanceConfiguration(ctx context.Context, request core.CreateInstanceConfigurationRequest) (response core.CreateInstanceConfigurationResponse, err error)

config/crd/bases/infrastructure.cluster.x-k8s.io_ocimachinepools.yaml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ spec:
5454
description: Custom metadata key/value pairs that you provide, such
5555
as the SSH public key required to connect to the instance.
5656
type: object
57+
ocid:
58+
description: OCID is the OCID of the associated InstancePool
59+
type: string
5760
placementDetails:
5861
items:
5962
properties:
@@ -65,8 +68,17 @@ spec:
6568
type: object
6669
type: array
6770
providerID:
68-
description: ProviderID is the ARN of the associated InstancePool
71+
description: ProviderID is the OCID of the associated InstancePool
72+
in a provider format
6973
type: string
74+
providerIDList:
75+
description: ProviderIDList are the identification IDs of machine
76+
instances provided by the provider. This field must match the provider
77+
IDs as seen on the node objects corresponding to a machine pool's
78+
machine instances.
79+
items:
80+
type: string
81+
type: array
7082
shapeConfig:
7183
description: The shape configuration of the instance, applicable for
7284
flex instances.
@@ -88,6 +100,10 @@ spec:
88100
description: The total number of OCPUs available to the instance.
89101
type: string
90102
type: object
103+
vnicAssignPublicIp:
104+
default: false
105+
description: Whether the VNIC should be assigned a public IP address.
106+
type: boolean
91107
type: object
92108
status:
93109
description: OCIMachinePoolStatus defines the observed state of OCIMachinePool

exp/api/v1beta1/ocimachinepool_types.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ const (
3333

3434
// OCIMachinePoolSpec defines the desired state of OCIMachinePool
3535
type OCIMachinePoolSpec struct {
36-
// ProviderID is the ARN of the associated InstancePool
36+
// ProviderID is the OCID of the associated InstancePool in a provider format
3737
// +optional
3838
ProviderID *string `json:"providerID,omitempty"`
3939

40+
// OCID is the OCID of the associated InstancePool
41+
// +optional
42+
OCID string `json:"ocid,omitempty"`
43+
4044
// OCID of the image to be used to launch the instance.
4145
ImageId string `json:"imageId,omitempty"`
4246

@@ -47,9 +51,19 @@ type OCIMachinePoolSpec struct {
4751
// The shape configuration of the instance, applicable for flex instances.
4852
ShapeConfig ShapeConfig `json:"shapeConfig,omitempty"`
4953

54+
// Whether the VNIC should be assigned a public IP address.
55+
// +kubebuilder:default=false
56+
// +optional
57+
VNICAssignPublicIp bool `json:"vnicAssignPublicIp,omitempty"`
58+
5059
PlacementDetails []PlacementDetails `json:"placementDetails,omitempty"`
5160

5261
InstanceConfiguration InstanceConfiguration `json:"instanceConfiguration,omitempty"`
62+
63+
// ProviderIDList are the identification IDs of machine instances provided by the provider.
64+
// This field must match the provider IDs as seen on the node objects corresponding to a machine pool's machine instances.
65+
// +optional
66+
ProviderIDList []string `json:"providerIDList,omitempty"`
5367
}
5468

5569
type InstanceConfiguration struct {

exp/api/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/controllers/ocimachinepool_controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,31 @@ func (r *OCIMachinePoolReconciler) reconcileNormal(ctx context.Context, logger l
301301

302302
machinePoolScope.Info("OCI Compute Instance Pool found", "InstancePoolId", *instancePool.Id)
303303
machinePoolScope.OCIMachinePool.Spec.ProviderID = common.String(fmt.Sprintf("oci://%s", *instancePool.Id))
304+
machinePoolScope.OCIMachinePool.Spec.OCID = *instancePool.Id
304305

305306
switch instancePool.LifecycleState {
306307
case core.InstancePoolLifecycleStateProvisioning, core.InstancePoolLifecycleStateStarting:
307308
machinePoolScope.Info("Instance Pool is pending")
308309
conditions.MarkFalse(machinePoolScope.OCIMachinePool, infrav1exp.InstancePoolReadyCondition, infrav1exp.InstancePoolNotReadyReason, clusterv1.ConditionSeverityInfo, "")
309310
return reconcile.Result{RequeueAfter: 10 * time.Second}, nil
311+
case core.InstancePoolLifecycleStateScaling:
312+
machinePoolScope.Info("Instance Pool is scaling")
313+
conditions.MarkFalse(machinePoolScope.OCIMachinePool, infrav1exp.InstancePoolReadyCondition, infrav1exp.InstancePoolNotReadyReason, clusterv1.ConditionSeverityInfo, "")
314+
return reconcile.Result{RequeueAfter: 10 * time.Second}, nil
310315
case core.InstancePoolLifecycleStateRunning:
311316
machinePoolScope.Info("Instance pool is active")
312317

313318
// record the event only when pool goes from not ready to ready state
314319
r.Recorder.Eventf(machinePoolScope.OCIMachinePool, corev1.EventTypeNormal, "InstancePoolReady",
315320
"Instance pool is in ready state")
316321
conditions.MarkTrue(machinePoolScope.OCIMachinePool, infrav1exp.InstancePoolReadyCondition)
322+
323+
instanceCount, err := machinePoolScope.SetListandSetMachinePoolInstances(ctx)
324+
if err != nil {
325+
return reconcile.Result{}, err
326+
}
327+
328+
machinePoolScope.SetReplicaCount(instanceCount)
317329
machinePoolScope.SetReady()
318330
default:
319331
conditions.MarkFalse(machinePoolScope.OCIMachinePool, infrav1exp.InstancePoolReadyCondition, infrav1exp.InstancePoolProvisionFailedReason, clusterv1.ConditionSeverityError, "")

templates/cluster-template-machinepool.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ spec:
123123
instanceConfiguration:
124124
instanceDetails:
125125
shape: "${OCI_NODE_MACHINE_TYPE=VM.Standard.E4.Flex}"
126+
# uncomment to assign a public IP address once you setup the VCN to allow public ips
127+
# vnicAssignPublicIp: true
126128
# uncomment if you want to set a specific availability domain
127129
# placementDetails:
128130
# - availabilityDomain: 1

0 commit comments

Comments
 (0)