Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.9] 🌱 controllers: fix ProviderServiceAccount tests to create objects on-demand #2997

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions controllers/serviceaccount_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package controllers

import (
"fmt"
"os"
"reflect"

Expand All @@ -30,7 +29,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -61,6 +59,12 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() {
targetNSObj *corev1.Namespace
)
BeforeEach(func() {
By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() {
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret)
})

pSvcAccount = getTestProviderServiceAccount(intCtx.Namespace, intCtx.VSphereCluster)
createTestResource(ctx, intCtx.Client, pSvcAccount)
assertEventuallyExistsInNamespace(ctx, intCtx.Client, intCtx.Namespace, pSvcAccount.GetName(), pSvcAccount)
Expand Down Expand Up @@ -129,13 +133,9 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() {

Context("With non-existent Cluster object", func() {
It("cannot reconcile the ProviderServiceAccount object", func() {
By("Deleting the CAPI cluster object", func() {
clusterName, ok := intCtx.VSphereCluster.GetLabels()[clusterv1.ClusterNameLabel]
Expect(ok).To(BeTrue())
cluster := &clusterv1.Cluster{}
key := client.ObjectKey{Namespace: intCtx.Namespace, Name: clusterName}
Expect(intCtx.Client.Get(ctx, key, cluster)).To(Succeed())
Expect(intCtx.Client.Delete(ctx, cluster)).To(Succeed())
By("Creating the vSphereCluster and KubeconfigSecret only", func() {
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret)
})

By("Creating the ProviderServiceAccount", func() {
Expand All @@ -155,16 +155,9 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() {

Context("With non-existent Cluster credentials secret", func() {
It("cannot reconcile the ProviderServiceAccount object", func() {
By("Deleting the CAPI kubeconfig secret object", func() {
clusterName, ok := intCtx.VSphereCluster.GetLabels()[clusterv1.ClusterNameLabel]
Expect(ok).To(BeTrue())
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: intCtx.Namespace,
Name: fmt.Sprintf("%s-kubeconfig", clusterName),
},
}
Expect(intCtx.Client.Delete(ctx, secret)).To(Succeed())
By("Creating the Cluster and vSphereCluster only", func() {
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster)
})

By("Creating the ProviderServiceAccount", func() {
Expand All @@ -187,6 +180,11 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() {
var role *rbacv1.Role
var roleBinding *rbacv1.RoleBinding
BeforeEach(func() {
By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() {
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret)
})
pSvcAccount = getTestProviderServiceAccount(intCtx.Namespace, intCtx.VSphereCluster)
pSvcAccount.Spec.TargetNamespace = "default"
// Pause the ProviderServiceAccount so we can create dependent but legacy resources
Expand Down
5 changes: 5 additions & 0 deletions controllers/servicediscovery_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ var _ = Describe("Service Discovery controller integration tests", func() {
)
BeforeEach(func() {
intCtx = helpers.NewIntegrationTestContextWithClusters(ctx, testEnv.Manager.GetClient())
By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() {
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret)
})
})
AfterEach(func() {
intCtx.AfterEach()
Expand Down
71 changes: 25 additions & 46 deletions internal/test/helpers/vmware/intg_test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type IntegrationTestContext struct {
Namespace string
VSphereCluster *vmwarev1.VSphereCluster
Cluster *clusterv1.Cluster
KubeconfigSecret *corev1.Secret
VSphereClusterKey client.ObjectKey
envTest *envtest.Environment
}
Expand Down Expand Up @@ -100,7 +101,7 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC
})

vsphereClusterName := capiutil.RandomString(6)
testCtx.Cluster = createCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName)
testCtx.Cluster = generateCluster(testCtx.Namespace, vsphereClusterName)

var config *rest.Config

Expand All @@ -126,8 +127,8 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC

testCtx.envTest = envTest
})
By("Create the kubeconfig secret for the cluster", func() {
buf, err := writeKubeConfig(config)
By("Generating the kubeconfig secret for the cluster", func() {
buf, err := generateKubeConfig(config)
Expect(err).ToNot(HaveOccurred())
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -138,34 +139,41 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: testCtx.Cluster.Name,
UID: testCtx.Cluster.UID,
// Using a random id in this case is okay because we don't rely on it to be the correct uid.
UID: "should-be-uid-of-cluster",
},
},
},
Data: map[string][]byte{
"value": buf,
},
}
Expect(integrationTestClient.Create(ctx, secret)).To(Succeed())
Eventually(func() error {
return integrationTestClient.Get(ctx, client.ObjectKeyFromObject(secret), secret)
}).Should(Succeed())
testCtx.KubeconfigSecret = secret
})

By("Create a vsphere cluster and wait for it to exist", func() {
testCtx.VSphereCluster = createVSphereCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName())
By("Generating a vsphere cluster", func() {
testCtx.VSphereCluster = generateVSphereCluster(testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName())
testCtx.VSphereClusterKey = client.ObjectKeyFromObject(testCtx.VSphereCluster)
})

return testCtx
}

func createCluster(ctx context.Context, integrationTestClient client.Client, namespace, name string) *clusterv1.Cluster {
By("Create the CAPI Cluster and wait for it to exist")
// CreateAndWait creates and waits for an object to exist.
func CreateAndWait(ctx context.Context, integrationTestClient client.Client, obj client.Object) {
GinkgoHelper()
Expect(integrationTestClient.Create(ctx, obj)).To(Succeed())
Eventually(func() error {
return integrationTestClient.Get(ctx, client.ObjectKeyFromObject(obj), obj)
}).Should(Succeed())
}

func generateCluster(namespace, name string) *clusterv1.Cluster {
By("Generate the CAPI Cluster")
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
GenerateName: name,
Namespace: namespace,
Name: fmt.Sprintf("%s-%s", name, capiutil.RandomString(6)),
},
Spec: clusterv1.ClusterSpec{
ClusterNetwork: &clusterv1.ClusterNetwork{
Expand All @@ -182,51 +190,22 @@ func createCluster(ctx context.Context, integrationTestClient client.Client, nam
},
},
}
Expect(integrationTestClient.Create(ctx, cluster)).To(Succeed())
Eventually(func() error {
return integrationTestClient.Get(ctx, client.ObjectKeyFromObject(cluster), cluster)
}).Should(Succeed())
return cluster
}

func createVSphereCluster(ctx context.Context, integrationTestClient client.Client, namespace, name, capiClusterName string) *vmwarev1.VSphereCluster {
func generateVSphereCluster(namespace, name, capiClusterName string) *vmwarev1.VSphereCluster {
vsphereCluster := &vmwarev1.VSphereCluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Labels: map[string]string{clusterv1.ClusterNameLabel: capiClusterName},
},
}
Expect(integrationTestClient.Create(ctx, vsphereCluster)).To(Succeed())
Eventually(func() error {
return integrationTestClient.Get(ctx, client.ObjectKeyFromObject(vsphereCluster), vsphereCluster)
}).Should(Succeed())

// TODO: remove if not needed
By("Creating a extensions ca", func() {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: vsphereCluster.Name + "-extensions-ca",
Namespace: namespace,
},
Data: map[string][]byte{
"ca.crt": []byte("test-ca"),
"tls.crt": []byte("test-tls.crt"),
"tls.key": []byte("test-tls.key"),
},
Type: corev1.SecretTypeTLS,
}
Expect(integrationTestClient.Create(ctx, secret)).To(Succeed())
secretKey := client.ObjectKey{Namespace: secret.Namespace, Name: secret.Name}
Eventually(func() error {
return integrationTestClient.Get(ctx, secretKey, secret)
}).Should(Succeed())
})
return vsphereCluster
}

// writeKubeConfig writes an existing *rest.Config out as the typical kubeconfig YAML data.
func writeKubeConfig(config *rest.Config) ([]byte, error) {
// generateKubeConfig writes an existing *rest.Config out as the typical kubeconfig YAML data.
func generateKubeConfig(config *rest.Config) ([]byte, error) {
return clientcmd.Write(api.Config{
Clusters: map[string]*api.Cluster{
config.ServerName: {
Expand Down
Loading