2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-13 18:27:47 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-03 21:09:42 +00:00
|
|
|
"time"
|
|
|
|
|
2022-05-17 11:12:43 +00:00
|
|
|
kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
|
2022-03-28 14:01:27 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2023-01-30 11:41:09 +00:00
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2022-10-02 19:45:03 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2022-12-07 15:08:37 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/registryclient"
|
2022-04-25 12:20:40 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2019-05-13 18:27:47 +00:00
|
|
|
)
|
|
|
|
|
2022-03-29 13:04:33 +00:00
|
|
|
// GenerateResponse checks for validity of generate rule on the resource
|
2023-01-30 11:41:09 +00:00
|
|
|
func GenerateResponse(rclient registryclient.Client, policyContext *PolicyContext, gr kyvernov1beta1.UpdateRequest) (resp *engineapi.EngineResponse) {
|
2022-03-29 13:04:33 +00:00
|
|
|
policyStartTime := time.Now()
|
2022-12-07 15:08:37 +00:00
|
|
|
return filterGenerateRules(rclient, policyContext, gr.Spec.Policy, policyStartTime)
|
2022-03-29 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
func filterGenerateRules(rclient registryclient.Client, policyContext *PolicyContext, policyNameKey string, startTime time.Time) *engineapi.EngineResponse {
|
2022-12-02 08:14:23 +00:00
|
|
|
kind := policyContext.newResource.GetKind()
|
|
|
|
name := policyContext.newResource.GetName()
|
|
|
|
namespace := policyContext.newResource.GetNamespace()
|
|
|
|
apiVersion := policyContext.newResource.GetAPIVersion()
|
2022-03-29 13:04:33 +00:00
|
|
|
pNamespace, pName, err := cache.SplitMetaNamespaceKey(policyNameKey)
|
|
|
|
if err != nil {
|
2022-10-02 19:45:03 +00:00
|
|
|
logging.Error(err, "failed to spilt name and namespace", policyNameKey)
|
2022-03-29 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
resp := &engineapi.EngineResponse{
|
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
|
|
|
Policy: engineapi.PolicySpec{
|
2022-03-29 13:04:33 +00:00
|
|
|
Name: pName,
|
|
|
|
Namespace: pNamespace,
|
|
|
|
},
|
2023-01-30 11:41:09 +00:00
|
|
|
PolicyStats: engineapi.PolicyStats{
|
2023-01-30 14:49:44 +00:00
|
|
|
ExecutionStats: engineapi.ExecutionStats{
|
|
|
|
Timestamp: startTime.Unix(),
|
|
|
|
},
|
2022-03-29 13:04:33 +00:00
|
|
|
},
|
2023-01-30 11:41:09 +00:00
|
|
|
Resource: engineapi.ResourceSpec{
|
2022-03-29 13:04:33 +00:00
|
|
|
Kind: kind,
|
|
|
|
Name: name,
|
|
|
|
Namespace: namespace,
|
|
|
|
APIVersion: apiVersion,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-02 08:14:23 +00:00
|
|
|
if policyContext.excludeResourceFunc(kind, namespace, name) {
|
2022-10-02 19:45:03 +00:00
|
|
|
logging.WithName("Generate").Info("resource excluded", "kind", kind, "namespace", namespace, "name", name)
|
2022-03-29 13:04:33 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-12-02 08:14:23 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policyContext.policy) {
|
2022-12-07 15:08:37 +00:00
|
|
|
if ruleResp := filterRule(rclient, rule, policyContext); ruleResp != nil {
|
2022-03-29 13:04:33 +00:00
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|