Skip to content

Commit

Permalink
fix(KONFLUX-3854): Cleanup MPC secrets as well if any
Browse files Browse the repository at this point in the history
  • Loading branch information
jhutar committed Nov 15, 2024
1 parent a1645ce commit 1dc4ae2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pkg/clients/common/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/labels"
)

// Creates a new secret in a specified namespace
Expand All @@ -25,11 +26,37 @@ func (s *SuiteController) GetSecret(ns string, name string) (*corev1.Secret, err
return s.KubeInterface().CoreV1().Secrets(ns).Get(context.Background(), name, metav1.GetOptions{})
}

// Deleted a secret in a specified namespace
// Delete a secret in a specified namespace
func (s *SuiteController) DeleteSecret(ns string, name string) error {
return s.KubeInterface().CoreV1().Secrets(ns).Delete(context.Background(), name, metav1.DeleteOptions{})
}

// ListSecrets return a list of secrets from a namespace by label and selection limits
func (s *SuiteController) ListSecrets(ns string, labelKey string, labelValue string, selectionLimit int64) (*corev1.SecretList, error) {
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{labelKey: labelValue}}
listOptions := metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
Limit: selectionLimit,
}
return s.KubeInterface().CoreV1().Secrets(ns).List(context.Background(), listOptions)
}

// Delete all secrets in a specified namespace matching to label
func (s *SuiteController) DeleteSecretsByLabel(ns string, labelKey string, labelValue string) error {
secretList, err := s.ListSecrets(ns, labelKey, labelValue, 1024)
if err != nil {
return err
}

for _, secret := range secretList.Items {
err = s.DeleteSecret(ns, secret.Name)
if err != nil {
return err
}
}
return nil
}

// Links a secret to a specified serviceaccount, if argument addImagePullSecrets is true secret will be added also to ImagePullSecrets of SA.
func (s *SuiteController) LinkSecretToServiceAccount(ns, secret, serviceaccount string, addImagePullSecrets bool) error {
timeout := 20 * time.Second
Expand Down
5 changes: 5 additions & 0 deletions tests/load-tests/pkg/journey/handle_purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func purgeStage(f *framework.Framework, namespace string) error {
return fmt.Errorf("Error when deleting pipeline runs in namespace %s: %v", namespace, err)
}

err = f.AsKubeDeveloper.CommonController.DeleteSecretsByLabel(namespace, "build.appstudio.redhat.com/multi-platform-secret", "true")
if err != nil {
return fmt.Errorf("Error when deleting MPC secrets in namespace %s: %v", namespace, err)
}

logging.Logger.Debug("Finished purging namespace %s", namespace)
return nil
}
Expand Down

0 comments on commit 1dc4ae2

Please sign in to comment.