Skip to content

Commit

Permalink
Support multiple NIC for machines
Browse files Browse the repository at this point in the history
  • Loading branch information
Erkan Erol committed Sep 14, 2022
1 parent 56f1cc6 commit 4e69c0a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
5 changes: 5 additions & 0 deletions api/v1beta1/vcdmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ type VCDMachineSpec struct {
// If true, then an appropriate placement policy should be set
// +optional
NvidiaGPU bool `json:"nvidiaGPU,omitempty"`

// AdditionalOvdcNetworks is the list of additional Ovdc Networks that are mounted to machines.
// VCDClusterSpec.OvdcNetwork is always attached regardless of this field.
// +optional
AdditionalOvdcNetworks []string `json:"additionalOvdcNetworks"`
}

// VCDMachineStatus defines the observed state of VCDMachine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ spec:
spec:
description: VCDMachineSpec defines the desired state of VCDMachine
properties:
additionalOvdcNetworks:
description: AdditionalOvdcNetworks is the list of additional Ovdc
Networks that are mounted to machines. VCDClusterSpec.OvdcNetwork
is always attached regardless of this field.
items:
type: string
type: array
bootstrapped:
description: Bootstrapped is true when the kubeadm bootstrapping has
been run against this machine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ spec:
description: Spec is the specification of the desired behavior
of the machine.
properties:
additionalOvdcNetworks:
description: AdditionalOvdcNetworks is the list of additional
Ovdc Networks that are mounted to machines. VCDClusterSpec.OvdcNetwork
is always attached regardless of this field.
items:
type: string
type: array
bootstrapped:
description: Bootstrapped is true when the kubeadm bootstrapping
has been run against this machine
Expand Down
83 changes: 77 additions & 6 deletions controllers/vcdmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
_ "embed" // this needs go 1.16+
b64 "encoding/base64"
"fmt"
"io/ioutil"
"math"
"strconv"
"strings"
"text/template"
"time"

"github.com/pkg/errors"
"github.com/replicatedhq/troubleshoot/pkg/redact"
cpiutil "github.com/vmware/cloud-provider-for-cloud-director/pkg/util"
Expand All @@ -19,12 +26,11 @@ import (
"github.com/vmware/cluster-api-provider-cloud-director/pkg/capisdk"
"github.com/vmware/cluster-api-provider-cloud-director/release"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
"gopkg.in/yaml.v2"
"io/ioutil"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog"
"math"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
Expand All @@ -37,10 +43,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
"strconv"
"strings"
"text/template"
"time"
)

type CloudInitScriptInput struct {
Expand Down Expand Up @@ -502,6 +504,33 @@ func (r *VCDMachineReconciler) reconcileNormal(ctx context.Context, cluster *clu
// VCDResourceSet can get bloated with VMs if the cluster contains a large number of worker nodes
}

if len(vcdMachine.Spec.AdditionalOvdcNetworks) > 0 {
connections, err1 := vm.GetNetworkConnectionSection()
if err1 != nil {
log.Error(err1, "failed to get attached networks to vm", "vcdMachine", vcdMachine.Name)
return ctrl.Result{}, errors.Wrapf(err, "Failed to get attached networks to VM [%s]", vcdMachine.Name)
}

vmMustBeUpdated := false
for _, ovdcNetwork := range vcdMachine.Spec.AdditionalOvdcNetworks {
err1 = ensureNetworkIsAttachedToVApp(vdcManager, vApp, ovdcNetwork)
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "Error ensuring network [%s] is attached to vApp", ovdcNetwork)
}

vmMustBeUpdated = vmMustBeUpdated || appendNetworkIfNecessary(connections, ovdcNetwork)
}

if vmMustBeUpdated {
err1 = vm.UpdateNetworkConnectionSection(connections)
if err1 != nil {
log.Error(err1, "failed to update networks of VM", "vcdMachine", vcdMachine.Name)
return ctrl.Result{}, errors.Wrapf(err, "failed to update networks of VM [%s]", vcdMachine.Name)
}
}

}

// checks before setting address in machine status
if vm.VM == nil {
log.Error(nil, fmt.Sprintf("Requeuing...; vm.VM should not be nil: [%#v]", vm))
Expand Down Expand Up @@ -745,6 +774,48 @@ func (r *VCDMachineReconciler) reconcileNormal(ctx context.Context, cluster *clu
return ctrl.Result{}, nil
}

// appendNetworkIfNecessary checks whether ovdcNetwork exists in the connections array
// and append if necessary. Returns true when connections array is updated
func appendNetworkIfNecessary(connections *types.NetworkConnectionSection, ovdcNetwork string) bool {
for _, existingConnection := range connections.NetworkConnection {
if existingConnection.Network == ovdcNetwork {
return false
}
}

newConnection := &types.NetworkConnection{
Network: ovdcNetwork,
NeedsCustomization: false,
IsConnected: true,
IPAddressAllocationMode: "POOL",
NetworkAdapterType: "VMXNET3",
}
connections.NetworkConnection = append(connections.NetworkConnection, newConnection)

return true
}

func ensureNetworkIsAttachedToVApp(vdcManager *vcdsdk.VdcManager, vApp *govcd.VApp, ovdcNetworkName string) error {
for _, vAppNetwork := range vApp.VApp.NetworkConfigSection.NetworkNames() {
if vAppNetwork == ovdcNetworkName {
return nil
}
}

ovdcNetwork, err := vdcManager.Vdc.GetOrgVdcNetworkByName(ovdcNetworkName, true)
if err != nil {
return fmt.Errorf("unable to get ovdc network [%s]: [%v]", ovdcNetworkName, err)
}

_, err = vApp.AddOrgNetwork(&govcd.VappNetworkSettings{}, ovdcNetwork.OrgVDCNetwork, false)
if err != nil {
return fmt.Errorf("unable to add ovdc network [%s] to vApp [%s]: [%v]",
ovdcNetwork, vApp.VApp.Name, err)
}

return nil
}

func (r *VCDMachineReconciler) getBootstrapData(ctx context.Context, machine *clusterv1.Machine) (string, error) {
log := ctrl.LoggerFrom(ctx)
if machine.Spec.Bootstrap.DataSecretName == nil {
Expand Down

0 comments on commit 4e69c0a

Please sign in to comment.