mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 07:26:55 +00:00
feat: add support for days in ttl labels (#8660)
This commit is contained in:
parent
1be3e6d763
commit
15a8970e23
4 changed files with 60 additions and 3 deletions
2
go.mod
2
go.mod
|
@ -400,7 +400,7 @@ require (
|
|||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a // indirect
|
||||
k8s.io/component-base v0.28.2 // indirect
|
||||
k8s.io/kube-openapi v0.0.0-20230816210353-14e408962443 // indirect
|
||||
k8s.io/kube-openapi v0.0.0-20230816210353-14e408962443
|
||||
k8s.io/kubectl v0.28.2 // indirect
|
||||
oras.land/oras-go/v2 v2.3.0 // indirect
|
||||
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.1.4 // indirect
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/client-go/discovery"
|
||||
"k8s.io/kube-openapi/pkg/validation/strfmt"
|
||||
)
|
||||
|
||||
func discoverResources(logger logr.Logger, discoveryClient discovery.DiscoveryInterface) ([]schema.GroupVersionResource, error) {
|
||||
|
@ -51,7 +52,7 @@ func HasResourcePermissions(logger logr.Logger, resource schema.GroupVersionReso
|
|||
}
|
||||
|
||||
func parseDeletionTime(metaObj metav1.Object, deletionTime *time.Time, ttlValue string) error {
|
||||
ttlDuration, err := time.ParseDuration(ttlValue)
|
||||
ttlDuration, err := strfmt.ParseDuration(ttlValue)
|
||||
if err == nil {
|
||||
creationTime := metaObj.GetCreationTimestamp().Time
|
||||
*deletionTime = creationTime.Add(ttlDuration)
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/kyverno/kyverno/api/kyverno"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/kube-openapi/pkg/validation/strfmt"
|
||||
)
|
||||
|
||||
func ValidateTtlLabel(_ context.Context, object metav1.Object) error {
|
||||
|
@ -16,7 +17,7 @@ func ValidateTtlLabel(_ context.Context, object metav1.Object) error {
|
|||
if ttl, ok := labels[kyverno.LabelCleanupTtl]; !ok {
|
||||
return nil
|
||||
} else {
|
||||
_, err := time.ParseDuration(ttl)
|
||||
_, err := strfmt.ParseDuration(ttl)
|
||||
if err != nil {
|
||||
// Try parsing ttlValue as a time in ISO 8601 format
|
||||
_, err := time.Parse(kyverno.ValueTtlDateTimeLayout, ttl)
|
||||
|
|
55
pkg/validation/resource/validate_test.go
Normal file
55
pkg/validation/resource/validate_test.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
admissionutils "github.com/kyverno/kyverno/pkg/utils/admission"
|
||||
"gotest.tools/assert"
|
||||
v1 "k8s.io/api/admission/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
ctx = context.Background()
|
||||
|
||||
pod = `{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Pod",
|
||||
"metadata": {
|
||||
"name": "test-pod",
|
||||
"namespace": "",
|
||||
"labels": {
|
||||
"cleanup.kyverno.io/ttl": "1d"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"name": "nginx",
|
||||
"image": "nginx:latest"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
admissionRequest = v1.AdmissionRequest{
|
||||
Operation: v1.Create,
|
||||
Kind: metav1.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"},
|
||||
Resource: metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"},
|
||||
Object: runtime.RawExtension{
|
||||
Raw: []byte(pod),
|
||||
},
|
||||
RequestResource: &metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"},
|
||||
}
|
||||
)
|
||||
|
||||
func Test_ValidateTTL(t *testing.T) {
|
||||
metadata, _, err := admissionutils.GetPartialObjectMetadatas(admissionRequest)
|
||||
assert.NilError(t, err)
|
||||
|
||||
err = ValidateTtlLabel(ctx, metadata)
|
||||
assert.NilError(t, err)
|
||||
}
|
Loading…
Add table
Reference in a new issue