1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 01:16:55 +00:00
kyverno/pkg/controller/cleanup.go

45 lines
1.2 KiB
Go
Raw Normal View History

2019-07-17 23:13:28 -07:00
package controller
import (
"github.com/golang/glog"
"github.com/nirmata/kyverno/pkg/annotations"
v1alpha1 "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
client "github.com/nirmata/kyverno/pkg/dclient"
2019-07-23 23:34:03 -04:00
"github.com/nirmata/kyverno/pkg/engine"
2019-07-17 23:13:28 -07:00
"k8s.io/apimachinery/pkg/runtime"
)
func cleanAnnotations(client *client.Client, obj interface{}) {
// get the policy struct from interface
unstr, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
glog.Error(err)
return
}
policy := v1alpha1.Policy{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstr, &policy); err != nil {
glog.Error(err)
return
}
// Get the resources that apply to the policy
2019-07-23 23:34:03 -04:00
resourceMap := engine.ListResourcesThatApplyToPolicy(client, &policy)
2019-07-17 23:13:28 -07:00
// remove annotations for the resources
for _, obj := range resourceMap {
// get annotations
2019-07-23 23:34:03 -04:00
ann := obj.Resource.GetAnnotations()
2019-07-17 23:13:28 -07:00
2019-07-19 15:10:40 -07:00
_, patch, err := annotations.RemovePolicyJSONPatch(ann, annotations.BuildKey(policy.Name))
2019-07-17 23:13:28 -07:00
if err != nil {
glog.Error(err)
continue
}
// patch the resource
2019-07-23 23:34:03 -04:00
_, err = client.PatchResource(obj.Resource.GetKind(), obj.Resource.GetNamespace(), obj.Resource.GetName(), patch)
2019-07-17 23:13:28 -07:00
if err != nil {
glog.Error(err)
continue
}
}
}