Skip to content
Merged
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
65 changes: 62 additions & 3 deletions internal/controller/kconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import (
"context"
"fmt"
"sort"
"strings"
"time"

"github.com/go-logr/logr"
"github.com/google/uuid"
v1 "k8s.io/api/core/v1"
Expand All @@ -29,8 +33,6 @@
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"

kconfigcontrollerv1beta1 "github.com/att-cloudnative-labs/kconfig-controller/api/v1beta1"
)
Expand Down Expand Up @@ -138,7 +140,7 @@
return nil
}

func (r *KconfigReconciler) processValueEnvConfig(ec kconfigcontrollerv1beta1.EnvConfig, envVars *[]v1.EnvVar, updatedECs *[]kconfigcontrollerv1beta1.EnvConfig) error {

Check failure on line 143 in internal/controller/kconfig_controller.go

View workflow job for this annotation

GitHub Actions / Run on Ubuntu

(*KconfigReconciler).processValueEnvConfig - result 0 (error) is always nil (unparam)
if ec.Key == "" || ec.Value == nil {
r.Recorder.Event(&kconfigcontrollerv1beta1.Kconfig{}, WarningEventType, InvalidEnvConfigEvent, "Either key or value is empty for value type EnvConfig. This entry will be removed")
return nil
Expand All @@ -148,7 +150,7 @@
return nil
}

func (r *KconfigReconciler) processConfigMapEnvConfig(kc *kconfigcontrollerv1beta1.Kconfig, ec kconfigcontrollerv1beta1.EnvConfig, actions *[]ExternalAction, envVars *[]v1.EnvVar, updatedECs *[]kconfigcontrollerv1beta1.EnvConfig) error {

Check failure on line 153 in internal/controller/kconfig_controller.go

View workflow job for this annotation

GitHub Actions / Run on Ubuntu

(*KconfigReconciler).processConfigMapEnvConfig - result 0 (error) is always nil (unparam)
envVar := v1.EnvVar{}
if ec.Value != nil {
refName := fmt.Sprintf("%s%s", r.ConfigMapPrefix, kc.Name)
Expand Down Expand Up @@ -185,11 +187,11 @@
return nil
}

func (r *KconfigReconciler) processSecretEnvConfig(kc *kconfigcontrollerv1beta1.Kconfig, ec kconfigcontrollerv1beta1.EnvConfig, actions *[]ExternalAction, envVars *[]v1.EnvVar, updatedECs *[]kconfigcontrollerv1beta1.EnvConfig) error {

Check failure on line 190 in internal/controller/kconfig_controller.go

View workflow job for this annotation

GitHub Actions / Run on Ubuntu

(*KconfigReconciler).processSecretEnvConfig - result 0 (error) is always nil (unparam)
envVar := v1.EnvVar{}
if ec.Value != nil {
refName := fmt.Sprintf("%s%s", r.SecretPrefix, kc.Name)
timestamp := time.Now().Format("20060102")
timestamp := time.Now().Format("20060102150405") // YYYYMMDDHHMMSS
refKey := fmt.Sprintf("%s_%s", ec.Key, timestamp)
secretKeyRef := &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Expand Down Expand Up @@ -221,7 +223,7 @@
return nil
}

func (r *KconfigReconciler) processFieldRefEnvConfig(ec kconfigcontrollerv1beta1.EnvConfig, envVars *[]v1.EnvVar, updatedECs *[]kconfigcontrollerv1beta1.EnvConfig) error {

Check failure on line 226 in internal/controller/kconfig_controller.go

View workflow job for this annotation

GitHub Actions / Run on Ubuntu

(*KconfigReconciler).processFieldRefEnvConfig - result 0 (error) is always nil (unparam)
if ec.Value != nil {
envVar := v1.EnvVar{
Name: ec.Key,
Expand Down Expand Up @@ -251,7 +253,7 @@
return nil
}

func (r *KconfigReconciler) processResourceFieldRefEnvConfig(ec kconfigcontrollerv1beta1.EnvConfig, envVars *[]v1.EnvVar, updatedECs *[]kconfigcontrollerv1beta1.EnvConfig) error {

Check failure on line 256 in internal/controller/kconfig_controller.go

View workflow job for this annotation

GitHub Actions / Run on Ubuntu

(*KconfigReconciler).processResourceFieldRefEnvConfig - result 0 (error) is always nil (unparam)
if ec.Value != nil {
envVar := v1.EnvVar{
Name: ec.Key,
Expand Down Expand Up @@ -319,6 +321,61 @@
return nil
}

// Helper function to cleanup stale secret keys when a new key is added
func secretsGarbageCollection(secret *v1.Secret) {
groupedKeys := make(map[string][]struct {
key string
date time.Time
})

const dateFormat = "20060102150405"
const retainCount = 3 // Set this number to desired unique keys to keep

for key := range secret.Data {
// Check for an underscore in the key
lastUnderscoreIndex := strings.LastIndex(key, "_")
if lastUnderscoreIndex == -1 {
fmt.Printf("Skippping key (%s): doesn't contain an underscore, so no date present\n", key)
continue
}
// Check the key is longer than the date format length
const dateFormatLength = len(dateFormat)
if len(key) < (dateFormatLength + 1) {
fmt.Printf("Skipping key (%s): doesn't fit date format length at the end", key)
continue
}
// Check the date at the end of the key to see if it follows the correct format of YYYYMMDDHHMMSS
dateStr := key[len(key)-dateFormatLength:]
date, err := time.Parse(dateFormat, dateStr)
if err != nil {
fmt.Printf("Skipping key (%s): doesn't fit date format of YYYYMMDDHHMMSS\n", key)
continue
}

prefix := key[:len(key)-dateFormatLength]
fmt.Println(prefix, dateStr)

groupedKeys[prefix] = append(groupedKeys[prefix], struct {
key string
date time.Time
}{key: key, date: date})
}

for _, entries := range groupedKeys {
sort.Slice(entries, func(i, j int) bool {
return entries[i].date.After(entries[j].date)
})

if len(entries) > retainCount {
for i := retainCount; i < len(entries); i++ {
oldKey := entries[i].key
fmt.Printf("Deleting old key as %d newer entries exist: %s\n", retainCount, oldKey)
delete(secret.Data, oldKey)
}
}
}
}

func (r *KconfigReconciler) executeSecretActions(ctx context.Context, kc *kconfigcontrollerv1beta1.Kconfig, actions []ExternalAction) error {
if len(actions) == 0 {
return nil
Expand All @@ -345,6 +402,8 @@
sec.Data[action.Key] = []byte(action.Value)
}

secretsGarbageCollection(&sec)

if existing {
if err := r.Update(ctx, &sec); err != nil {
return fmt.Errorf("error updating secret: %s", err.Error())
Expand Down
Loading