diff --git a/README.md b/README.md index 675ca2d..44f2185 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,9 @@ Credential manager consists of several packages. ## environment variables The next environment variables must be configured: -`IS_HOOK` - Required for hook module `IsHook() bool` function. -`SECRET_NAMES` - List of coma separated secret names to work with. +`IS_HOOK` - Required for hook module `IsHook() bool` function. +`SECRET_NAMES` - List of coma separated secret names to work with. +`HOOK_NAME` - Prefix for hook Job objects. By default `credentials-saver`. # Modules @@ -23,6 +24,7 @@ API: New secrets with the same content and name with postfix `-old` will be created for all of the provided secrets. Secrets also will be locked with `locked-for-watcher=true` annotation on them. +`ClearHooks()` - This function deletes all Kubernetes Job and Pod objects in current namespace with prefix from `HOOK_NAME` environment variable. ## informer This module allows you to create watcher for secret. diff --git a/pkg/hook/cleanup.go b/pkg/hook/cleanup.go new file mode 100644 index 0000000..be47d62 --- /dev/null +++ b/pkg/hook/cleanup.go @@ -0,0 +1,71 @@ +package hook + +import ( + "context" + "fmt" + "strings" + + "github.com/Netcracker/qubership-credential-manager/pkg/utils" + "go.uber.org/zap" + batchv1 "k8s.io/api/batch/v1" + corev1 "k8s.io/api/core/v1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func ClearHooks() error { + ctx := context.Background() + hookObjects, err := getHookObjects() + if err != nil { + return err + } + for _, hookObject := range hookObjects { + err = k8sClient.Delete(ctx, hookObject) + if err != nil { + logger.Error(fmt.Sprintf("cannot delete hook object %s", hookObject.GetName()), zap.Error(err)) + return err + } + logger.Info(fmt.Sprintf("credential hook object %s has been deleted", hookObject.GetName())) + } + return nil +} + +func getHookObjects() ([]client.Object, error) { + resultList := make([]client.Object, 0) + jobObjects, err := getJobsAndPods() + if err != nil { + return nil, err + } + credHookName := utils.GetHookName() + for _, credHook := range jobObjects { + if strings.HasPrefix(credHook.GetName(), credHookName) { + resultList = append(resultList, credHook) + } + } + + return resultList, nil +} + +func getJobsAndPods() ([]client.Object, error) { + objects := make([]client.Object, 0) + opts := []client.ListOption{ + client.InNamespace(namespace), + } + jobList := &batchv1.JobList{} + if err := k8sClient.List(context.Background(), jobList, opts...); err != nil { + logger.Error("cannot get Job list", zap.Error(err)) + return nil, err + } + for _, job := range jobList.Items { + objects = append(objects, &job) + } + + podList := &corev1.PodList{} + if err := k8sClient.List(context.Background(), podList, opts...); err != nil { + logger.Error("cannot get Pod list", zap.Error(err)) + return nil, err + } + for _, pod := range podList.Items { + objects = append(objects, &pod) + } + return objects, nil +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 0a07be1..6a5bc29 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -128,3 +128,7 @@ func GetSecretNames() []string { secretNames := strings.Split(secretNamesStr, ",") return secretNames } + +func GetHookName() string { + return GetEnv("HOOK_NAME", "credentials-saver") +}