From f909c4fe597fcc01791b2bfba71a53240fc732a2 Mon Sep 17 00:00:00 2001 From: Kalya Subramanian <42158129+ksubrmnn@users.noreply.github.com> Date: Thu, 10 Jun 2021 20:46:03 -0400 Subject: [PATCH] tests(e2e): Create ServiceAccount for Vault (#3571) Signed-off-by: Kalya Subramanian (cherry picked from commit c298f7b68e87c23ce1fdebd91dcaa6ed31377f8e) --- tests/framework/common.go | 26 ++++++++++++++++++-------- tests/framework/common_apps.go | 20 +++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/tests/framework/common.go b/tests/framework/common.go index 254a5482f7..0120e21c1e 100644 --- a/tests/framework/common.go +++ b/tests/framework/common.go @@ -736,29 +736,39 @@ func (td *OsmTestData) LoadOSMImagesIntoKind() error { func (td *OsmTestData) installVault(instOpts InstallOSMOpts) error { td.T.Log("Installing Vault") + + appName := "vault" replicas := int32(1) terminationGracePeriodSeconds := int64(10) + + serviceAccountDefinition := Td.SimpleServiceAccount(appName, td.OsmNamespace) + svcAccount, err := Td.CreateServiceAccount(serviceAccountDefinition.Namespace, &serviceAccountDefinition) + if err != nil { + return errors.Wrap(err, "failed to create vault service account") + } + vaultDep := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ - Name: "vault", + Name: appName, Labels: map[string]string{ - "app": "vault", + "app": appName, }, }, Spec: appsv1.DeploymentSpec{ Replicas: &replicas, Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ - "app": "vault", + "app": appName, }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ - "app": "vault", + "app": appName, }, }, Spec: corev1.PodSpec{ + ServiceAccountName: svcAccount.Name, TerminationGracePeriodSeconds: &terminationGracePeriodSeconds, Containers: []corev1.Container{ { @@ -853,22 +863,22 @@ tail /dev/random; }, }, } - _, err := td.Client.AppsV1().Deployments(instOpts.ControlPlaneNS).Create(context.TODO(), vaultDep, metav1.CreateOptions{}) + _, err = td.Client.AppsV1().Deployments(instOpts.ControlPlaneNS).Create(context.TODO(), vaultDep, metav1.CreateOptions{}) if err != nil { return errors.Wrap(err, "failed to create vault deployment") } vaultSvc := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: "vault", + Name: appName, Labels: map[string]string{ - "app": "vault", + "app": appName, }, }, Spec: corev1.ServiceSpec{ Type: corev1.ServiceTypeLoadBalancer, Selector: map[string]string{ - "app": "vault", + "app": appName, }, Ports: []corev1.ServicePort{ { diff --git a/tests/framework/common_apps.go b/tests/framework/common_apps.go index 1ce17fc61d..04074b794f 100644 --- a/tests/framework/common_apps.go +++ b/tests/framework/common_apps.go @@ -150,15 +150,10 @@ type SimplePodAppDef struct { AppProtocol string } -// SimplePodApp creates returns a set of k8s typed definitions for a pod-based k8s definition. +// SimplePodApp returns a set of k8s typed definitions for a pod-based k8s definition. // Includes Pod, Service and ServiceAccount types func (td *OsmTestData) SimplePodApp(def SimplePodAppDef) (corev1.ServiceAccount, corev1.Pod, corev1.Service) { - serviceAccountDefinition := corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{ - Name: def.Name, - Namespace: def.Namespace, - }, - } + serviceAccountDefinition := Td.SimpleServiceAccount(def.Name, def.Namespace) podDefinition := corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ @@ -251,6 +246,17 @@ func (td *OsmTestData) SimplePodApp(def SimplePodAppDef) (corev1.ServiceAccount, return serviceAccountDefinition, podDefinition, serviceDefinition } +// SimpleServiceAccount returns a k8s typed definition for a service account. +func (td *OsmTestData) SimpleServiceAccount(name string, namespace string) corev1.ServiceAccount { + serviceAccountDefinition := corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + }, + } + return serviceAccountDefinition +} + // getKubernetesServerVersionNumber returns the version number in chunks, ex. v1.19.3 => [1, 19, 3] func (td *OsmTestData) getKubernetesServerVersionNumber() ([]int, error) { version, err := td.Client.Discovery().ServerVersion()