From ffe74352abfc4b37a0063fec3c5cf12f9836ca85 Mon Sep 17 00:00:00 2001 From: Daniel Olsson Date: Thu, 14 May 2020 23:15:18 +0200 Subject: [PATCH] Improved output on secrets and added last-modified timestamp --- cmd/create_secret.go | 3 +++ cmd/get_secret.go | 19 ++++++++++++++----- cmd/patch_secret.go | 5 +++++ go.sum | 2 ++ tests/testdata/secrets.go | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/cmd/create_secret.go b/cmd/create_secret.go index d2af504..9d08c2f 100644 --- a/cmd/create_secret.go +++ b/cmd/create_secret.go @@ -75,6 +75,9 @@ func CreateSecret(clientSet kubernetes.Interface, secretName, container *string, "tbac.bisnode.com/container": *container, "tbac.bisnode.com/sandbox": fmt.Sprintf("%v", sandbox), }, + Annotations: map[string]string{ + "tbac.bisnode.com/last-modified": fmt.Sprintf("%v", metav1.Now().Rfc3339Copy()), + }, }, Data: util.AssembleInputData(data), } diff --git a/cmd/get_secret.go b/cmd/get_secret.go index 1020183..9827a49 100644 --- a/cmd/get_secret.go +++ b/cmd/get_secret.go @@ -31,6 +31,9 @@ type SecretDescription struct { Namespace string Name string CreationTimestamp metav1.Time + LastUpdated string + Service string + Container string Data map[string][]byte } @@ -69,13 +72,16 @@ var getSecretCmd = &cobra.Command{ // PrettyPrintSecretDesc pretty prints a secret as a table view func (s *SecretDescription) PrettyPrintSecretDesc() { - fmt.Printf("Namespace:\t%v\n", s.Namespace) - fmt.Printf("Secret name:\t%v\n", s.Name) - fmt.Printf("Created:\t%v\n", s.CreationTimestamp) + fmt.Printf("Secret name:%v%v\n", strings.Repeat(" ", 25-len("Secret Name:")), s.Name) + fmt.Printf("Service (app label):%v%v\n", strings.Repeat(" ", 25-len("Service (app label):")), s.Service) + fmt.Printf("Container:%v%v\n", strings.Repeat(" ", 25-len("Container:")), s.Container) + fmt.Printf("Namespace:%v%v\n", strings.Repeat(" ", 25-len("Namespace:")), s.Namespace) + fmt.Printf("Created:%v%v\n", strings.Repeat(" ", 25-len("Created:")), s.CreationTimestamp) + fmt.Printf("Last updated:%v%v\n\n", strings.Repeat(" ", 25-len("Last updated:")), s.LastUpdated) if len(s.Data) > 0 { - fmt.Println(strings.Repeat("-", 46)) + fmt.Println(strings.Repeat("-", 25), "DATA", strings.Repeat("-", 25)) for k, v := range s.Data { - fmt.Printf("%v:\n%v\n\n", k, string(v)) + fmt.Printf("%v=%v\n", k, string(v)) } } } @@ -117,6 +123,9 @@ func GetSecretDescription(clientSet kubernetes.Interface, secretName string) (se secretDesc = &SecretDescription{ Namespace: Namespace, Name: secretName, + LastUpdated: secrets.Items[0].Annotations["tbac.bisnode.com/last-modified"], + Service: secrets.Items[0].Labels["app"], + Container: secrets.Items[0].Labels["tbac.bisnode.com/container"], CreationTimestamp: secrets.Items[0].CreationTimestamp, Data: data, } diff --git a/cmd/patch_secret.go b/cmd/patch_secret.go index 5558586..0557725 100644 --- a/cmd/patch_secret.go +++ b/cmd/patch_secret.go @@ -119,6 +119,11 @@ func PatchSecret(clientSet kubernetes.Interface, secretName *string, removeData, patchSecret.Data[k] = v } + if patchSecret.Annotations == nil { + patchSecret.Annotations = make(map[string]string) + } + + patchSecret.Annotations["tbac.bisnode.com/last-modified"] = fmt.Sprintf("%v", metav1.Now().Rfc3339Copy()) patch, err := json.Marshal(patchSecret) if err != nil { return err diff --git a/go.sum b/go.sum index 91c69cc..487a38f 100644 --- a/go.sum +++ b/go.sum @@ -359,6 +359,8 @@ k8s.io/apimachinery v0.17.4 h1:UzM+38cPUJnzqSQ+E1PY4YxMHIzQyCg29LOoGfo79Zw= k8s.io/apimachinery v0.17.4/go.mod h1:gxLnyZcGNdZTCLnq3fgzyg2A5BVCHTNDFrw8AmuJ+0g= k8s.io/apimachinery v0.18.0 h1:fuPfYpk3cs1Okp/515pAf0dNhL66+8zk8RLbSX+EgAE= k8s.io/apimachinery v0.18.0/go.mod h1:9SnR/e11v5IbyPCGbvJViimtJ0SwHG4nfZFjU77ftcA= +k8s.io/apimachinery v0.18.1 h1:hKPYcQRPLQoG2e7fKkVl0YCvm9TBefXTfGILa9vjVVk= +k8s.io/apimachinery v0.18.2 h1:44CmtbmkzVDAhCpRVSiP2R5PPrC2RtlIv/MoB8xpdRA= k8s.io/client-go v0.15.11 h1:yujXordVnH33fhe1bddPnFWsGfl0Gq6FyZ335TC3qk4= k8s.io/client-go v0.15.11/go.mod h1:gkprEfouvgHvzeCvwwz2T8MTlfNuZn8vluW8orojRKI= k8s.io/client-go v1.5.1 h1:XaX/lo2/u3/pmFau8HN+sB5C/b4dc4Dmm2eXjBH4p1E= diff --git a/tests/testdata/secrets.go b/tests/testdata/secrets.go index 7ba96bc..73ba8ef 100644 --- a/tests/testdata/secrets.go +++ b/tests/testdata/secrets.go @@ -1,6 +1,8 @@ package testdata import ( + "fmt" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -11,6 +13,13 @@ var GenerateSecrets = []v1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: "my-credentials", Namespace: "default", + Labels: map[string]string{ + "app": "my-credentials", + "tbac.bisnode.com/container": "default", + }, + Annotations: map[string]string{ + "tbac.bisnode.com/last-modified": fmt.Sprintf("%v", metav1.Now().Rfc3339Copy()), + }, }, Data: map[string][]byte{ "USERNAME": []byte("foo"), @@ -22,6 +31,13 @@ var GenerateSecrets = []v1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: "my-api-key", Namespace: "default", + Labels: map[string]string{ + "app": "my-api-key", + "tbac.bisnode.com/container": "default", + }, + Annotations: map[string]string{ + "tbac.bisnode.com/last-modified": fmt.Sprintf("%v", metav1.Now().Rfc3339Copy()), + }, }, Data: map[string][]byte{ "URL": []byte("github.com"),