Skip to content

Commit

Permalink
test: ensure tags for failure domains on VCSim for govmomi
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Aug 15, 2024
1 parent 7f87d35 commit e5fbbf2
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions test/e2e/ownerrefs_finalizers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"github.com/vmware/govmomi/find"
govmomirest "github.com/vmware/govmomi/vapi/rest"
govmomitags "github.com/vmware/govmomi/vapi/tags"
"github.com/vmware/govmomi/vim25/mo"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -83,6 +87,11 @@ var _ = Describe("Ensure OwnerReferences and Finalizers are resilient [vcsim] [s

// Before running the test create the secret used by the VSphereClusterIdentity to connect to the vCenter.
createVsphereIdentitySecret(ctx, bootstrapClusterProxy, username, password)

if testTarget == VCSimTestTarget {
// Create the necessary k8s-region and k8s-zone tags in VCSim for the failure domain.
createGovmomiFailureDomainTags(ctx, restClient, vsphereFinder, e2eConfig.GetVariable("VSPHERE_DATACENTER"), e2eConfig.GetVariable("VSPHERE_COMPUTE_CLUSTER"))
}
}
},
PostMachinesProvisioned: func(proxy framework.ClusterProxy, namespace, clusterName string) {
Expand Down Expand Up @@ -366,6 +375,84 @@ func createVsphereIdentitySecret(ctx context.Context, bootstrapClusterProxy fram
})).To(Succeed())
}

func ensureTagOnObject(ctx context.Context, tm *govmomitags.Manager, categoryName, tagName string, ref mo.Reference) error {
var categoryID string
var tagID string

// Create category if it does not exist.
categories, err := tm.GetCategories(ctx)
if err != nil {
return err
}

for _, category := range categories {
if category.Name == categoryName {
categoryID = category.ID
break
}
}

if categoryID == "" {
categoryID, err = tm.CreateCategory(ctx, &govmomitags.Category{Name: categoryName})
if err != nil {
return errors.Wrapf(err, "create category %q", categoryName)
}
}

// Create tag if it does not exist.
tags, err := tm.GetTagsForCategory(ctx, categoryID)
if err != nil {
return err
}

for _, tag := range tags {
if tag.Name == tagName {
tagID = tag.ID
break
}
}

if tagID == "" {
tagID, err = tm.CreateTag(ctx, &govmomitags.Tag{Name: tagName, CategoryID: categoryID})
if err != nil {
return errors.Wrapf(err, "create tag %q for category %q", tagName, categoryName)
}
}

// Ensure tag is set on object.
objTags, err := tm.GetAttachedTags(ctx, ref)
if err != nil {
return err
}

for _, objTag := range objTags {
if objTag.ID == tagID {
// Return early if tag is already attached to object.
return nil
}
}

return tm.AttachTag(ctx, tagID, ref)
}

func createGovmomiFailureDomainTags(ctx context.Context, restClient *govmomirest.Client, finder *find.Finder, datacenterName, computeClusterName string) {
// Get datacenter.
dc, err := finder.Datacenter(ctx, fmt.Sprintf("/%s", datacenterName))
Expect(err).ToNot(HaveOccurred())

// Get ComputeCluster.
computeCluster, err := finder.ClusterComputeResource(ctx, fmt.Sprintf("/%s/host/%s", datacenterName, computeClusterName))
Expect(err).ToNot(HaveOccurred())

m := govmomitags.NewManager(restClient)

// Ensure k8s-region tag on datacenter object.
Expect(ensureTagOnObject(ctx, m, "k8s-region", datacenterName, dc.Reference())).To(Succeed(), "ensure tag for k8s-region on datacenter")

// Ensure k8s-zone tag on computeCluster object.
Expect(ensureTagOnObject(ctx, m, "k8s-zone", computeClusterName, computeCluster.Reference())).To(Succeed(), "ensure tag for k8s-zone on cluster")
}

func checkGovmomiVSphereClusterFailureDomains(ctx context.Context, proxy framework.ClusterProxy, namespace, clusterName string) {
vSphereCluster := &infrav1.VSphereCluster{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit e5fbbf2

Please sign in to comment.