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

Hook objects cleanup #1

Merged
merged 2 commits into from
Jan 20, 2025
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
71 changes: 71 additions & 0 deletions pkg/hook/cleanup.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@ func GetSecretNames() []string {
secretNames := strings.Split(secretNamesStr, ",")
return secretNames
}

func GetHookName() string {
return GetEnv("HOOK_NAME", "credentials-saver")
}