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

add update time webhook #534

Merged
merged 1 commit into from
Feb 20, 2024
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
2 changes: 2 additions & 0 deletions apis/meta/v1alpha1/labels_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const (
DisplayNameAnnotationKey = "katanomi.dev/displayName"
// CreatedTimeAnnotationKey creation time for objects
CreatedTimeAnnotationKey = "katanomi.dev/creationTime"
// UpdatedTimeAnnotationKey update time for objects
UpdatedTimeAnnotationKey = "katanomi.dev/updateTime"
// DeletedTimeAnnotationKey deletion time for objects
DeletedTimeAnnotationKey = "katanomi.dev/deletionTime"
// CrossClusterAnnotationKey annotates a cross cluster resource/action
Expand Down
17 changes: 17 additions & 0 deletions webhook/admission/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package admission

import (
"context"
"time"

mv1alpha1 "github.com/katanomi/pkg/apis/meta/v1alpha1"
admissionv1 "k8s.io/api/admission/v1"
Expand Down Expand Up @@ -175,6 +176,22 @@ func WithCancelledBy(scheme *runtime.Scheme, isCancelled func(oldObj runtime.Obj
}
}

// WithUpdateTime adds a updateTime annotation to the object
func WithUpdateTime() TransformFunc {
return func(ctx context.Context, obj runtime.Object, req admission.Request) {
if req.Operation != admissionv1.Update {
return
}
newObj := obj.(metav1.Object)
annotations := newObj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[mv1alpha1.UpdatedTimeAnnotationKey] = time.Now().Format(time.RFC3339)
newObj.SetAnnotations(annotations)
}
}

// setCancelledBy will set obj annotation base on the request information
func setCancelledBy(ctx context.Context, obj runtime.Object, req admission.Request) {
logger := logging.FromContext(ctx)
Expand Down
41 changes: 41 additions & 0 deletions webhook/admission/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package admission
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"

"github.com/katanomi/pkg/apis/meta/v1alpha1"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/matchers"
v1 "k8s.io/api/admission/v1"
authenticationv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -93,3 +96,41 @@ func TestWithCancelledBy(t *testing.T) {
Expect(pod.Annotations[v1alpha1.CancelledByAnnotationKey]).To(Equal(`{"user":{"kind":"User","name":"admin"}}`))

}

func TestWithUpdateTime(t *testing.T) {
g := NewGomegaWithT(t)
ctx := context.Background()
obj := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "pod",
Namespace: "default",
UID: types.UID("abc"),
},
}

req := admission.Request{
AdmissionRequest: v1.AdmissionRequest{
Operation: v1.Create,
},
}
WithUpdateTime()(ctx, obj, req)
g.Expect(obj.Annotations).NotTo(HaveKey(v1alpha1.UpdatedTimeAnnotationKey))

req.Operation = v1.Update
WithUpdateTime()(ctx, obj, req)
beTimeString := matchers.NewWithTransformMatcher(checkTimeString, BeTrue())
g.Expect(obj.Annotations[v1alpha1.UpdatedTimeAnnotationKey]).To(beTimeString)
}

func checkTimeString(actual interface{}) (interface{}, error) {
str, ok := actual.(string)
if !ok {
return false, fmt.Errorf("expected string value, got %v", actual)
}
_, err := time.Parse(time.RFC3339, str)
return err == nil, err
}