Skip to content

Commit

Permalink
vm-operator: migrate to v1alpha2
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Jun 14, 2024
1 parent bace233 commit f3f4627
Show file tree
Hide file tree
Showing 21 changed files with 241 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ linters-settings:
- pkg: sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1
alias: vmwarev1
# VMware Operator
- pkg: "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
- pkg: "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
alias: vmoprv1
# CABPK
- pkg: sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1
Expand Down
20 changes: 14 additions & 6 deletions controllers/vmware/test/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -463,7 +463,7 @@ var _ = Describe("Reconciliation tests", func() {
Eventually(func() error {
return k8sClient.Get(ctx, rpKey, resourcePolicy)
}, time.Second*30).Should(Succeed())
Expect(len(resourcePolicy.Spec.ClusterModules)).To(BeEquivalentTo(2))
Expect(len(resourcePolicy.Spec.ClusterModuleGroups)).To(BeEquivalentTo(2))

By("Create the CAPI Machine and wait for it to exist")
machineKey, machine := deployCAPIMachine(ns.Name, cluster, k8sClient)
Expand Down Expand Up @@ -636,7 +636,12 @@ var _ = Describe("Reconciliation tests", func() {
}
// These two lines must be initialized as requirements of having valid Status
newVM.Status.Volumes = []vmoprv1.VirtualMachineVolumeStatus{}
newVM.Status.Phase = vmoprv1.Created
newVM.Status.Conditions = append(newVM.Status.Conditions, metav1.Condition{
Type: vmoprv1.VirtualMachineConditionCreated,
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.NewTime(time.Now().UTC().Truncate(time.Second)),
Reason: string(metav1.ConditionTrue),
})
return k8sClient.Status().Update(ctx, newVM)
}, time.Second*30).Should(Succeed())

Expand All @@ -649,7 +654,7 @@ var _ = Describe("Reconciliation tests", func() {
if err != nil {
return err
}
newVM.Status.PowerState = vmoprv1.VirtualMachinePoweredOn
newVM.Status.PowerState = vmoprv1.VirtualMachinePowerStateOn
return k8sClient.Status().Update(ctx, newVM)
}, time.Second*30).Should(Succeed())

Expand All @@ -662,7 +667,10 @@ var _ = Describe("Reconciliation tests", func() {
if err != nil {
return err
}
newVM.Status.VmIp = "1.2.3.4"
if newVM.Status.Network == nil {
newVM.Status.Network = &vmoprv1.VirtualMachineNetworkStatus{}
}
newVM.Status.Network.PrimaryIP4 = "1.2.3.4"
newVM.Status.BiosUUID = "test-bios-uuid"
return k8sClient.Status().Update(ctx, newVM)
}, time.Second*30).Should(Succeed())
Expand All @@ -675,7 +683,7 @@ var _ = Describe("Reconciliation tests", func() {
// control_plane_endpoint_test.go
if !isLB {
By("Expect the Cluster to have the IP from the VM as an APIEndpoint")
assertEventuallyControlPlaneEndpoint(infraClusterKey, infraCluster, newVM.Status.VmIp)
assertEventuallyControlPlaneEndpoint(infraClusterKey, infraCluster, newVM.Status.Network.PrimaryIP4)
}
},
Entry("With no load balancer", dontUseLoadBalancer),
Expand Down
2 changes: 1 addition & 1 deletion controllers/vspheremachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"time"

"github.com/pkg/errors"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,6 @@ github.com/vmware-tanzu/vm-operator/external/ncp v0.0.0-20240404200847-de75746a9
github.com/vmware-tanzu/vm-operator/external/ncp v0.0.0-20240404200847-de75746a9505/go.mod h1:5rqRJ9zGR+KnKbkGx373WgN8xJpvAj99kHnfoDYRO5I=
github.com/vmware-tanzu/vm-operator/external/tanzu-topology v0.0.0-20240404200847-de75746a9505 h1:/6vFL20UMHOeTf/mb+dKf5sFG0FBbyrCBY/71QKAIE0=
github.com/vmware-tanzu/vm-operator/external/tanzu-topology v0.0.0-20240404200847-de75746a9505/go.mod h1:dfYrWS8DMRN+XZfhu8M4LVHmeGvYB29Ipd7j4uIq+mU=
github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels v0.0.0-20240404200847-de75746a9505 h1:LRMZ+zzb944d6r+uYsE+CuJ3zVBE6Hc8vjH7IJX2QY4=
github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels v0.0.0-20240404200847-de75746a9505/go.mod h1:M+6bZCS8vSjCN9OOppnAyYxeBxY1U0wG6+j3CvIrOHY=
github.com/vmware/govmomi v0.37.3 h1:L2y2Ba09tYiZwdPtdF64Ox9QZeJ8vlCUGcAF9SdODn4=
github.com/vmware/govmomi v0.37.3/go.mod h1:mtGWtM+YhTADHlCgJBiskSRPOZRsN9MSjPzaZLte/oQ=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
Expand Down
2 changes: 1 addition & 1 deletion internal/test/helpers/vmware/unit_test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down
2 changes: 1 addition & 1 deletion pkg/context/fake/fake_controller_manager_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package fake

import (
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/pkg/errors"
netopv1 "github.com/vmware-tanzu/net-operator-api/api/v1alpha1"
nsxopv1 "github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
ncpv1 "github.com/vmware-tanzu/vm-operator/external/ncp/api/v1alpha1"
topologyv1 "github.com/vmware-tanzu/vm-operator/external/tanzu-topology/api/v1alpha1"
"gopkg.in/fsnotify.v1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package services
import (
"context"

vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down
22 changes: 17 additions & 5 deletions pkg/services/network/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ limitations under the License.
// Package network contains code for configuring network services.
package network

import (
netopv1 "github.com/vmware-tanzu/net-operator-api/api/v1alpha1"
nsxopv1 "github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha1"
ncpv1 "github.com/vmware-tanzu/vm-operator/external/ncp/api/v1alpha1"
)

const (
// NSXTTypeNetwork is the name of the NSX-T network type.
NSXTTypeNetwork = "nsx-t"
// NSXTVNetSelectorKey is also defined in VM Operator.
NSXTVNetSelectorKey = "ncp.vmware.com/virtual-network-name"
// NSXTVPCSubnetSetNetworkType is the name of the NSX VPC network type. Please refer to:
// https://github.com/vmware-tanzu/vm-operator/blob/main/api/v1alpha1/virtualmachine_types.go#L149.
NSXTVPCSubnetSetNetworkType = "nsx-t-subnetset"

// CAPVDefaultNetworkLabel is a label used to identify the default network.
CAPVDefaultNetworkLabel = "capv.vmware.com/is-default-network"
Expand All @@ -39,3 +40,14 @@ const (
// Deprecated: legacyDefaultNetworkLabel will be removed in a future release.
legacyDefaultNetworkLabel = "capw.vmware.com/is-default-network"
)

var (
// NetworkGVKNetOperator is the GVK used for networks in net-operator mode.
NetworkGVKNetOperator = netopv1.SchemeGroupVersion.WithKind("Network")

// NetworkGVKNSXT is the GVK used for networks in NSX-T mode.
NetworkGVKNSXT = ncpv1.SchemeGroupVersion.WithKind("VirtualNetwork")

// NetworkGVKNSXTVPC is the GVK used for networks in NSX-T VPC mode.
NetworkGVKNSXTVPC = nsxopv1.SchemeGroupVersion.WithKind("SubnetSet")
)
2 changes: 1 addition & 1 deletion pkg/services/network/dummy_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package network
import (
"context"

vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
"k8s.io/apimachinery/pkg/runtime"

"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context/vmware"
Expand Down
23 changes: 17 additions & 6 deletions pkg/services/network/netop_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"fmt"

netopv1 "github.com/vmware-tanzu/net-operator-api/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
vmoprv1common "github.com/vmware-tanzu/vm-operator/api/v1alpha2/common"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/cluster-api/util/conditions"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -122,16 +124,25 @@ func (np *netopNetworkProvider) ConfigureVirtualMachine(ctx context.Context, clu
return err
}

for _, vnif := range vm.Spec.NetworkInterfaces {
if vnif.NetworkType == string(network.Spec.Type) && vnif.NetworkName == network.Name {
if vm.Spec.Network == nil {
vm.Spec.Network = &vmoprv1.VirtualMachineNetworkSpec{}
}
for _, vnif := range vm.Spec.Network.Interfaces {
if vnif.Network.TypeMeta.GroupVersionKind() == NetworkGVKNetOperator && vnif.Network.Name == network.Name {
// Expected network interface already exists.
return nil
}
}

vm.Spec.NetworkInterfaces = append(vm.Spec.NetworkInterfaces, vmoprv1.VirtualMachineNetworkInterface{
NetworkName: network.Name,
NetworkType: string(network.Spec.Type),
vm.Spec.Network.Interfaces = append(vm.Spec.Network.Interfaces, vmoprv1.VirtualMachineNetworkInterfaceSpec{
Name: fmt.Sprintf("eth%d", len(vm.Spec.Network.Interfaces)),
Network: vmoprv1common.PartialObjectRef{
TypeMeta: metav1.TypeMeta{
Kind: NetworkGVKNetOperator.Kind,
APIVersion: NetworkGVKNetOperator.GroupVersion().String(),
},
Name: network.Name,
},
})

return nil
Expand Down
56 changes: 33 additions & 23 deletions pkg/services/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
netopv1alpha1 "github.com/vmware-tanzu/net-operator-api/api/v1alpha1"
netopv1 "github.com/vmware-tanzu/net-operator-api/api/v1alpha1"
nsxopv1 "github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
ncpv1 "github.com/vmware-tanzu/vm-operator/external/ncp/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -165,39 +165,41 @@ var _ = Describe("Network provider", func() {
})
It("should not add network interface", func() {
Expect(err).ToNot(HaveOccurred())
Expect(vm.Spec.NetworkInterfaces).To(BeNil())
Expect(vm.Spec.Network).To(BeNil())
})
})

Context("with netop network provider", func() {
var defaultNetwork *netopv1alpha1.Network
var defaultNetwork *netopv1.Network

testWithLabelFunc := func(label string) {
BeforeEach(func() {
defaultNetwork = &netopv1alpha1.Network{
defaultNetwork = &netopv1.Network{
ObjectMeta: metav1.ObjectMeta{
Name: "dummy-network",
Namespace: dummyNs,
Labels: map[string]string{label: "true"},
},
Spec: netopv1alpha1.NetworkSpec{
Type: netopv1alpha1.NetworkTypeVDS,
Spec: netopv1.NetworkSpec{
Type: netopv1.NetworkTypeVDS,
},
}
})

Context("ConfigureVirtualMachine", func() {
BeforeEach(func() {
scheme := runtime.NewScheme()
Expect(netopv1alpha1.AddToScheme(scheme)).To(Succeed())
Expect(netopv1.AddToScheme(scheme)).To(Succeed())
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(defaultNetwork).Build()
np = NetOpNetworkProvider(client)
})

AfterEach(func() {
Expect(err).ToNot(HaveOccurred())
Expect(vm.Spec.NetworkInterfaces).To(HaveLen(1))
Expect(vm.Spec.NetworkInterfaces[0].NetworkType).To(Equal("vsphere-distributed"))
Expect(vm.Spec.Network).ToNot(BeNil())
Expect(vm.Spec.Network.Interfaces).To(HaveLen(1))
Expect(vm.Spec.Network.Interfaces[0].Network.TypeMeta.Kind).To(Equal("Network"))
Expect(vm.Spec.Network.Interfaces[0].Network.TypeMeta.APIVersion).To(Equal(netopv1.SchemeGroupVersion.String()))
})

It("should add vds type network interface", func() {
Expand Down Expand Up @@ -233,8 +235,11 @@ var _ = Describe("Network provider", func() {
})
AfterEach(func() {
Expect(err).ToNot(HaveOccurred())
Expect(vm.Spec.NetworkInterfaces[0].NetworkName).To(Equal(GetNSXTVirtualNetworkName(vSphereCluster.Name)))
Expect(vm.Spec.NetworkInterfaces[0].NetworkType).To(Equal("nsx-t"))
Expect(vm.Spec.Network).ToNot(BeNil())
Expect(vm.Spec.Network.Interfaces).To(HaveLen(1))
Expect(vm.Spec.Network.Interfaces[0].Network.Name).To(Equal(GetNSXTVirtualNetworkName(vSphereCluster.Name)))
Expect(vm.Spec.Network.Interfaces[0].Network.TypeMeta.Kind).To(Equal("VirtualNetwork"))
Expect(vm.Spec.Network.Interfaces[0].Network.TypeMeta.APIVersion).To(Equal(ncpv1.SchemeGroupVersion.String()))
})
})

Expand All @@ -249,19 +254,24 @@ var _ = Describe("Network provider", func() {
It("should add nsx-t-subnetset type network interface", func() {
err = np.ConfigureVirtualMachine(ctx, clusterCtx, vm)
Expect(err).ToNot(HaveOccurred())
Expect(vm.Spec.NetworkInterfaces).To(HaveLen(1))
Expect(vm.Spec.Network.Interfaces).To(HaveLen(1))
})

It("nsx-t-subnetset type network interface already exists", func() {
err = np.ConfigureVirtualMachine(ctx, clusterCtx, vm)
Expect(err).ToNot(HaveOccurred())
Expect(vm.Spec.NetworkInterfaces).To(HaveLen(1))
Expect(vm.Spec.Network).ToNot(BeNil())
Expect(vm.Spec.Network.Interfaces).To(HaveLen(1))
})

AfterEach(func() {
Expect(err).ToNot(HaveOccurred())
Expect(vm.Spec.NetworkInterfaces[0].NetworkName).To(Equal(vSphereCluster.Name))
Expect(vm.Spec.NetworkInterfaces[0].NetworkType).To(Equal("nsx-t-subnetset"))
Expect(vm.Spec.Network).ToNot(BeNil())
Expect(vm.Spec.Network.Interfaces).To(HaveLen(1))
Expect(vm.Spec.Network.Interfaces[0].Network.Name).To(Equal(vSphereCluster.Name))
Expect(vm.Spec.Network.Interfaces[0].Network.TypeMeta.Kind).To(Equal("SubnetSet"))
Expect(vm.Spec.Network.Interfaces[0].Network.TypeMeta.APIVersion).To(Equal(nsxopv1.SchemeGroupVersion.String()))
// TODO(chrischdi): also assert group version
})
})
})
Expand Down Expand Up @@ -348,7 +358,7 @@ var _ = Describe("Network provider", func() {
Context("with netop network provider", func() {
BeforeEach(func() {
scheme := runtime.NewScheme()
Expect(netopv1alpha1.AddToScheme(scheme)).To(Succeed())
Expect(netopv1.AddToScheme(scheme)).To(Succeed())
client = fake.NewClientBuilder().WithScheme(scheme).Build()
np = NetOpNetworkProvider(client)
})
Expand Down Expand Up @@ -744,22 +754,22 @@ var _ = Describe("Network provider", func() {

Context("GetVMServiceAnnotations", func() {
Context("with netop network provider", func() {
var defaultNetwork *netopv1alpha1.Network
var defaultNetwork *netopv1.Network

testWithLabelFunc := func(label string) {
BeforeEach(func() {
defaultNetwork = &netopv1alpha1.Network{
defaultNetwork = &netopv1.Network{
ObjectMeta: metav1.ObjectMeta{
Name: "dummy-network",
Namespace: dummyNs,
Labels: map[string]string{label: "true"},
},
Spec: netopv1alpha1.NetworkSpec{
Type: netopv1alpha1.NetworkTypeVDS,
Spec: netopv1.NetworkSpec{
Type: netopv1.NetworkTypeVDS,
},
}
scheme := runtime.NewScheme()
Expect(netopv1alpha1.AddToScheme(scheme)).To(Succeed())
Expect(netopv1.AddToScheme(scheme)).To(Succeed())
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(defaultNetwork).Build()
np = NetOpNetworkProvider(client)
})
Expand Down Expand Up @@ -810,7 +820,7 @@ var _ = Describe("Network provider", func() {
Context("with netop network provider", func() {
BeforeEach(func() {
scheme := runtime.NewScheme()
Expect(netopv1alpha1.AddToScheme(scheme)).To(Succeed())
Expect(netopv1.AddToScheme(scheme)).To(Succeed())
client := fake.NewClientBuilder().WithScheme(scheme).Build()
np = NetOpNetworkProvider(client)
})
Expand Down
Loading

0 comments on commit f3f4627

Please sign in to comment.