Skip to content

Commit

Permalink
Retrieve worker subnets by MachineSet rather than by Machine
Browse files Browse the repository at this point in the history
  • Loading branch information
tsatam committed Nov 10, 2023
1 parent 0b7096d commit 4a2e27b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
11 changes: 6 additions & 5 deletions pkg/util/subnet/cluster_subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,27 @@ func (m *kubeManager) List(ctx context.Context) ([]Subnet, error) {
subnetMap := []Subnet{}

// select all workers by the machine.openshift.io/cluster-api-machine-role: not equal to master Label
// TODO: is this even necessary? Masters should not have a MachineSet
selector, _ := labels.Parse("machine.openshift.io/cluster-api-machine-role!=master")
machines := &machinev1beta1.MachineList{}
err := m.client.List(ctx, machines, &client.ListOptions{
machineSets := &machinev1beta1.MachineSetList{}
err := m.client.List(ctx, machineSets, &client.ListOptions{
Namespace: machineSetsNamespace,
LabelSelector: selector,
})
if err != nil {
return nil, err
}

for _, machine := range machines.Items {
subnetDesc, err := m.getDescriptorFromProviderSpec(machine.Spec.ProviderSpec.Value)
for _, machineSet := range machineSets.Items {
subnetDesc, err := m.getDescriptorFromProviderSpec(machineSet.Spec.Template.Spec.ProviderSpec.Value)
if err != nil {
return nil, err
}
subnetMap = append(subnetMap, *subnetDesc)
}

selector, _ = labels.Parse("machine.openshift.io/cluster-api-machine-role=master")
machines = &machinev1beta1.MachineList{}
machines := &machinev1beta1.MachineList{}
err = m.client.List(ctx, machines, &client.ListOptions{
Namespace: machineSetsNamespace,
LabelSelector: selector,
Expand Down
32 changes: 20 additions & 12 deletions pkg/util/subnet/cluster_subnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestListFromCluster(t *testing.T) {
name string
machinelabel string
expect []Subnet
modify func(*machinev1beta1.Machine, *machinev1beta1.Machine)
modify func(*machinev1beta1.MachineSet, *machinev1beta1.Machine)
wantErr string
}{
{
Expand All @@ -48,16 +48,16 @@ func TestListFromCluster(t *testing.T) {
{
name: "master missing providerSpec",
expect: nil,
modify: func(worker *machinev1beta1.Machine, master *machinev1beta1.Machine) {
modify: func(worker *machinev1beta1.MachineSet, master *machinev1beta1.Machine) {
master.Spec.ProviderSpec.Value.Raw = []byte("")
},
wantErr: "json: error calling MarshalJSON for type *runtime.RawExtension: unexpected end of JSON input",
},
{
name: "worker missing providerSpec",
expect: nil,
modify: func(worker *machinev1beta1.Machine, master *machinev1beta1.Machine) {
worker.Spec.ProviderSpec.Value.Raw = []byte("")
modify: func(worker *machinev1beta1.MachineSet, master *machinev1beta1.Machine) {
worker.Spec.Template.Spec.ProviderSpec.Value.Raw = []byte("")
},
wantErr: "json: error calling MarshalJSON for type *runtime.RawExtension: unexpected end of JSON input",
},
Expand All @@ -77,26 +77,34 @@ func TestListFromCluster(t *testing.T) {
},
},
}
workerMachine := machinev1beta1.Machine{
workerMachineSet := machinev1beta1.MachineSet{
ObjectMeta: metav1.ObjectMeta{
Name: "worker-0",
Name: "worker",
Namespace: "openshift-machine-api",
Labels: map[string]string{"machine.openshift.io/cluster-api-machine-role": "worker"},
},
Spec: machinev1beta1.MachineSpec{
ProviderSpec: machinev1beta1.ProviderSpec{
Value: &kruntime.RawExtension{
Raw: []byte("{\"resourceGroup\":\"workerRG\",\"publicIP\":false,\"osDisk\":{\"diskSizeGB\": 1024,\"managedDisk\":{\"storageAccountType\": \"Premium_LRS\"},\"osType\":\"Linux\"},\"image\":{\"offer\": \"aro4\",\"publisher\": \"azureopenshift\", \"resourceID\": \"\", \"sku\": \"aro_43\", \"version\": \"43.81.20200311\"},\"networkResourceGroup\":\"vnet-rg\",\"vnet\":\"vnet\",\"subnet\":\"workerSubnet\"}"),
Spec: machinev1beta1.MachineSetSpec{
Template: machinev1beta1.MachineTemplateSpec{
ObjectMeta: machinev1beta1.ObjectMeta{
Labels: map[string]string{"machine.openshift.io/cluster-api-machine-role": "worker"},
},
Spec: machinev1beta1.MachineSpec{
ProviderSpec: machinev1beta1.ProviderSpec{
Value: &kruntime.RawExtension{
Raw: []byte("{\"resourceGroup\":\"workerRG\",\"publicIP\":false,\"osDisk\":{\"diskSizeGB\": 1024,\"managedDisk\":{\"storageAccountType\": \"Premium_LRS\"},\"osType\":\"Linux\"},\"image\":{\"offer\": \"aro4\",\"publisher\": \"azureopenshift\", \"resourceID\": \"\", \"sku\": \"aro_43\", \"version\": \"43.81.20200311\"},\"networkResourceGroup\":\"vnet-rg\",\"vnet\":\"vnet\",\"subnet\":\"workerSubnet\"}"),
},
},
},
},
},
}

if tt.modify != nil {
tt.modify(&workerMachine, &masterMachine)
tt.modify(&workerMachineSet, &masterMachine)
}

m := kubeManager{
client: fake.NewClientBuilder().WithObjects(&workerMachine, &masterMachine).Build(),
client: fake.NewClientBuilder().WithObjects(&workerMachineSet, &masterMachine).Build(),
subscriptionID: subscriptionId,
}

Expand Down

0 comments on commit 4a2e27b

Please sign in to comment.