Skip to content

Commit

Permalink
Incorporate feedback from Madhav's review
Browse files Browse the repository at this point in the history
  • Loading branch information
seshachalam-yv committed Aug 12, 2024
1 parent 0a1edad commit 801abdf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 66 deletions.
2 changes: 1 addition & 1 deletion hack/e2e-test/run-e2e-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function test_e2e {
echo "-------------------"

SOURCE_PATH=$PWD \
ginkgo -v -procs=4 -r -timeout=30m0s --randomize-all --randomize-suites --fail-on-pending ./test/e2e
ginkgo -v -p ./test/e2e
fi
}

Expand Down
31 changes: 15 additions & 16 deletions test/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,31 @@ var _ = BeforeSuite(func() {
Name: etcdNamespace,
},
})

if apierrors.IsAlreadyExists(err) {
err = nil
}
Expect(err).NotTo(HaveOccurred())

// sync.Once to ensure that secrets are deployed only once.
once.Do(func() {
// Deploy TLS secrets
certsPath := path.Join(sourcePath, certsBasePath)
err := buildAndDeployTLSSecrets(ctx, cl, logger, etcdNamespace, certsPath, providers)
certsPath := path.Join(sourcePath, certsBasePath)
err = buildAndDeployTLSSecrets(ctx, cl, logger, etcdNamespace, certsPath, providers)
if !apierrors.IsAlreadyExists(err) {
Expect(err).To(Succeed())
}

// Deploy backup secrets
storageContainer := getEnvAndExpectNoError(envStorageContainer)
for _, provider := range providers {
err = deployBackupSecret(ctx, cl, logger, provider, etcdNamespace, storageContainer)
if !apierrors.IsAlreadyExists(err) {
Expect(err).To(Succeed())
}

// Deploy backup secrets
storageContainer := getEnvAndExpectNoError(envStorageContainer)
for _, provider := range providers {
err = deployBackupSecret(ctx, cl, logger, provider, etcdNamespace, storageContainer)
if !apierrors.IsAlreadyExists(err) {
Expect(err).To(Succeed())
}
}
})
}
})

var _ = AfterSuite(func() {
var _ = SynchronizedAfterSuite(func() {

}, func() {
ctx := context.Background()

// Ensure that the KUBECONFIG path is properly set
Expand Down
67 changes: 18 additions & 49 deletions test/e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/go-logr/logr"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -424,30 +423,15 @@ func getKubernetesClient(kubeconfigPath string) (client.Client, error) {
}

func deploySecret(ctx context.Context, cl client.Client, logger logr.Logger, name, namespace string, labels map[string]string, secretType corev1.SecretType, secretData map[string][]byte) error {
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Type: secretType,
Data: secretData,
}

logger.Info("checking if secret exists", "secret", client.ObjectKeyFromObject(&secret))

if err := cl.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, &secret); err != nil {
if errors.IsNotFound(err) {
logger.Info("secret does not exist, creating new secret", "secret", name)
return cl.Create(ctx, &secret)
}
logger.Error(err, "unable to check if secret exists")
return err
}
secret := corev1.Secret{}
secret.Name = name
secret.Namespace = namespace
secret.Labels = labels
secret.Type = secretType
secret.Data = secretData

logger.Info("secret already exists, updating existing secret", "secret", name)
secret.Data = secretData // Update the data field in case it has changed
return cl.Update(ctx, &secret)
logger.Info("creating secret", "secret", client.ObjectKeyFromObject(&secret))
return cl.Create(ctx, &secret)
}

func buildAndDeployTLSSecrets(ctx context.Context, cl client.Client, logger logr.Logger, namespace, certsPath string, providers []TestProvider) error {
Expand Down Expand Up @@ -523,40 +507,25 @@ func buildAndDeployTLSSecrets(ctx context.Context, cl client.Client, logger logr
}

func deployBackupSecret(ctx context.Context, cl client.Client, logger logr.Logger, provider TestProvider, namespace, storageContainer string) error {

if provider.Storage == nil || provider.Storage.SecretData == nil {
logger.Info("no secret data provided, skipping secret deployment")
return nil // or return an error if this situation should not occur
return nil
}
secretData := provider.Storage.SecretData
providerSuffix := provider.Suffix
secretName := fmt.Sprintf("%s-%s", etcdBackupSecretPrefix, providerSuffix)

etcdBackupSecret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: namespace,
Labels: labels, // Ensure labels is defined and valid
},
Type: corev1.SecretTypeOpaque,
Data: secretData,
}
etcdBackupSecret := corev1.Secret{}
etcdBackupSecret.Name = secretName
etcdBackupSecret.Namespace = namespace
etcdBackupSecret.Labels = labels
etcdBackupSecret.Type = corev1.SecretTypeOpaque
etcdBackupSecret.Data = secretData
etcdBackupSecret.Data["bucketName"] = []byte(storageContainer)

logger.Info("checking if secret exists", "secret", client.ObjectKeyFromObject(&etcdBackupSecret))

if err := cl.Get(ctx, client.ObjectKey{Name: secretName, Namespace: namespace}, &etcdBackupSecret); err != nil {
if errors.IsNotFound(err) {
logger.Info("secret does not exist, creating new secret", "secret", secretName)
return cl.Create(ctx, &etcdBackupSecret)
}
logger.Error(err, "unable to check if secret exists")
return err
}
logger.Info("creating secret", "secret", client.ObjectKeyFromObject(&etcdBackupSecret))

logger.Info("secret already exists, updating existing secret", "secret", secretName)
etcdBackupSecret.Data = secretData // Update the data field in case it has changed
etcdBackupSecret.Data["bucketName"] = []byte(storageContainer) // Ensure this is always up-to-date
return cl.Update(ctx, &etcdBackupSecret)
return cl.Create(ctx, &etcdBackupSecret)
}

// getRemoteCommandExecutor builds and returns a remote command Executor from the given command on the specified container
Expand Down

0 comments on commit 801abdf

Please sign in to comment.