|
| 1 | +/* |
| 2 | + Copyright (c) 2022 Oracle and/or its affiliates. |
| 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 | + https://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 | + "github.com/oracle/cluster-api-provider-oci/cloud/ociutil" |
| 23 | + |
| 24 | + infrastructurev1beta1 "github.com/oracle/cluster-api-provider-oci/api/v1beta1" |
| 25 | + "github.com/oracle/oci-go-sdk/v65/common" |
| 26 | + "github.com/oracle/oci-go-sdk/v65/core" |
| 27 | + "github.com/pkg/errors" |
| 28 | +) |
| 29 | + |
| 30 | +func (m *MachineScope) ReconcileVnicAttachments(ctx context.Context) error { |
| 31 | + if m.IsControlPlane() { |
| 32 | + return errors.New("cannot attach multiple vnics to ControlPlane machines") |
| 33 | + } |
| 34 | + |
| 35 | + for index, vnicAttachment := range m.OCIMachine.Spec.VnicAttachments { |
| 36 | + if m.vnicAttachmentExists(ctx, vnicAttachment) { |
| 37 | + m.Logger.Info("vnicAttachment", ociutil.DerefString(vnicAttachment.DisplayName), " already exists and is immutable") |
| 38 | + continue |
| 39 | + } |
| 40 | + |
| 41 | + vnicId, err := m.createVnicAttachment(ctx, vnicAttachment) |
| 42 | + if err != nil { |
| 43 | + msg := fmt.Sprintf("Error creating VnicAttachment %s for cluster %s", |
| 44 | + *vnicAttachment.DisplayName, m.Cluster.Name) |
| 45 | + m.Logger.Error(err, msg) |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + m.OCIMachine.Spec.VnicAttachments[index].VnicAttachmentId = vnicId |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func (m *MachineScope) createVnicAttachment(ctx context.Context, spec infrastructurev1beta1.VnicAttachment) (*string, error) { |
| 56 | + vnicName := spec.DisplayName |
| 57 | + |
| 58 | + // Default to machine subnet if spec doesn't supply one |
| 59 | + subnetId := m.getWorkerMachineSubnet() |
| 60 | + if spec.SubnetName != "" { |
| 61 | + var err error |
| 62 | + subnetId, err = m.getMachineSubnet(spec.SubnetName) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + tags := m.getFreeFormTags(*m.OCICluster) |
| 69 | + |
| 70 | + definedTags := ConvertMachineDefinedTags(m.OCIMachine.Spec.DefinedTags) |
| 71 | + |
| 72 | + if spec.NicIndex == nil { |
| 73 | + spec.NicIndex = common.Int(0) |
| 74 | + } |
| 75 | + |
| 76 | + secondVnic := core.AttachVnicDetails{ |
| 77 | + DisplayName: vnicName, |
| 78 | + NicIndex: spec.NicIndex, |
| 79 | + InstanceId: m.OCIMachine.Spec.InstanceId, |
| 80 | + CreateVnicDetails: &core.CreateVnicDetails{ |
| 81 | + SubnetId: subnetId, |
| 82 | + AssignPublicIp: common.Bool(spec.AssignPublicIp), |
| 83 | + FreeformTags: tags, |
| 84 | + DefinedTags: definedTags, |
| 85 | + HostnameLabel: m.OCIMachine.Spec.NetworkDetails.HostnameLabel, |
| 86 | + NsgIds: m.getWorkerMachineNSGs(), |
| 87 | + SkipSourceDestCheck: m.OCIMachine.Spec.NetworkDetails.SkipSourceDestCheck, |
| 88 | + AssignPrivateDnsRecord: m.OCIMachine.Spec.NetworkDetails.AssignPrivateDnsRecord, |
| 89 | + DisplayName: vnicName, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + req := core.AttachVnicRequest{AttachVnicDetails: secondVnic} |
| 94 | + resp, err := m.ComputeClient.AttachVnic(ctx, req) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + return resp.Id, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (m *MachineScope) vnicAttachmentExists(ctx context.Context, vnic infrastructurev1beta1.VnicAttachment) bool { |
| 103 | + |
| 104 | + found := false |
| 105 | + var page *string |
| 106 | + for { |
| 107 | + resp, err := m.ComputeClient.ListVnicAttachments(ctx, core.ListVnicAttachmentsRequest{ |
| 108 | + InstanceId: m.GetInstanceId(), |
| 109 | + CompartmentId: common.String(m.getCompartmentId()), |
| 110 | + Page: page, |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + return false |
| 114 | + } |
| 115 | + for _, attachment := range resp.Items { |
| 116 | + if ociutil.DerefString(attachment.DisplayName) == ociutil.DerefString(vnic.DisplayName) { |
| 117 | + m.Logger.Info("Vnic is already attached ", attachment) |
| 118 | + return true |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if resp.OpcNextPage == nil { |
| 123 | + break |
| 124 | + } else { |
| 125 | + page = resp.OpcNextPage |
| 126 | + } |
| 127 | + } |
| 128 | + return found |
| 129 | +} |
0 commit comments