2022-03-01 23:19:31 +00:00
|
|
|
package autogen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2022-03-10 15:51:29 +00:00
|
|
|
"strings"
|
2022-03-01 23:19:31 +00:00
|
|
|
|
2022-05-17 11:12:43 +00:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-03-28 14:01:27 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/toggle"
|
2022-03-10 15:51:29 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
2022-04-01 05:26:47 +00:00
|
|
|
jsonutils "github.com/kyverno/kyverno/pkg/utils/json"
|
2022-04-05 15:12:22 +00:00
|
|
|
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
|
2022-03-10 15:51:29 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-04-05 15:12:22 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2022-03-09 13:48:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// PodControllerCronJob represent CronJob string
|
|
|
|
PodControllerCronJob = "CronJob"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PodControllers stores the list of Pod-controllers in csv string
|
2022-03-09 13:48:04 +00:00
|
|
|
PodControllers = "DaemonSet,Deployment,Job,StatefulSet,CronJob"
|
2022-03-01 23:19:31 +00:00
|
|
|
)
|
|
|
|
|
2022-05-17 06:19:03 +00:00
|
|
|
var podControllersKindsSet = sets.NewString(append(strings.Split(PodControllers, ","), "Pod")...)
|
2022-04-05 15:12:22 +00:00
|
|
|
|
|
|
|
func isKindOtherthanPod(kinds []string) bool {
|
|
|
|
if len(kinds) > 1 && kubeutils.ContainsKind(kinds, "Pod") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-17 11:12:43 +00:00
|
|
|
func checkAutogenSupport(needed *bool, subjects ...kyvernov1.ResourceDescription) bool {
|
2022-04-05 15:12:22 +00:00
|
|
|
for _, subject := range subjects {
|
|
|
|
if subject.Name != "" || subject.Selector != nil || subject.Annotations != nil || isKindOtherthanPod(subject.Kinds) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if needed != nil {
|
|
|
|
*needed = *needed || podControllersKindsSet.HasAny(subject.Kinds...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// stripCronJob removes CronJob from controllers
|
|
|
|
func stripCronJob(controllers string) string {
|
|
|
|
var newControllers []string
|
|
|
|
controllerArr := strings.Split(controllers, ",")
|
|
|
|
for _, c := range controllerArr {
|
|
|
|
if c == PodControllerCronJob {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newControllers = append(newControllers, c)
|
|
|
|
}
|
|
|
|
if len(newControllers) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.Join(newControllers, ",")
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:19:31 +00:00
|
|
|
// CanAutoGen checks whether the rule(s) (in policy) can be applied to Pod controllers
|
|
|
|
// returns controllers as:
|
|
|
|
// - "" if:
|
|
|
|
// - name or selector is defined
|
|
|
|
// - mixed kinds (Pod + pod controller) is defined
|
|
|
|
// - Pod and PodControllers are not defined
|
|
|
|
// - mutate.Patches/mutate.PatchesJSON6902/validate.deny/generate rule is defined
|
|
|
|
// - otherwise it returns all pod controllers
|
2022-05-17 11:12:43 +00:00
|
|
|
func CanAutoGen(spec *kyvernov1.Spec) (applyAutoGen bool, controllers string) {
|
2022-04-05 15:12:22 +00:00
|
|
|
needed := false
|
|
|
|
for _, rule := range spec.Rules {
|
|
|
|
if rule.Mutation.PatchesJSON6902 != "" || rule.HasGenerate() {
|
|
|
|
return false, "none"
|
2022-03-01 23:19:31 +00:00
|
|
|
}
|
2022-04-05 15:12:22 +00:00
|
|
|
match, exclude := rule.MatchResources, rule.ExcludeResources
|
|
|
|
if !checkAutogenSupport(&needed, match.ResourceDescription, exclude.ResourceDescription) {
|
2022-04-29 09:12:21 +00:00
|
|
|
logger.V(3).Info("skip generating rule on pod controllers: Name / Selector in resource description may not be applicable.", "rule", rule.Name)
|
2022-03-01 23:19:31 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
for _, value := range match.Any {
|
2022-04-05 15:12:22 +00:00
|
|
|
if !checkAutogenSupport(&needed, value.ResourceDescription) {
|
2022-04-29 09:12:21 +00:00
|
|
|
logger.V(3).Info("skip generating rule on pod controllers: Name / Selector in match any block is not be applicable.", "rule", rule.Name)
|
2022-03-01 23:19:31 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, value := range match.All {
|
2022-04-05 15:12:22 +00:00
|
|
|
if !checkAutogenSupport(&needed, value.ResourceDescription) {
|
2022-04-29 09:12:21 +00:00
|
|
|
logger.V(3).Info("skip generating rule on pod controllers: Name / Selector in match all block is not be applicable.", "rule", rule.Name)
|
2022-03-01 23:19:31 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, value := range exclude.Any {
|
2022-04-05 15:12:22 +00:00
|
|
|
if !checkAutogenSupport(&needed, value.ResourceDescription) {
|
2022-04-29 09:12:21 +00:00
|
|
|
logger.V(3).Info("skip generating rule on pod controllers: Name / Selector in exclude any block is not be applicable.", "rule", rule.Name)
|
2022-03-01 23:19:31 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, value := range exclude.All {
|
2022-04-05 15:12:22 +00:00
|
|
|
if !checkAutogenSupport(&needed, value.ResourceDescription) {
|
2022-04-29 09:12:21 +00:00
|
|
|
logger.V(3).Info("skip generating rule on pod controllers: Name / Selector in exclud all block is not be applicable.", "rule", rule.Name)
|
2022-03-01 23:19:31 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 15:12:22 +00:00
|
|
|
if !needed {
|
2022-03-01 23:19:31 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
2022-03-09 13:48:04 +00:00
|
|
|
return true, PodControllers
|
2022-03-01 23:19:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 15:51:29 +00:00
|
|
|
// GetSupportedControllers returns the supported autogen controllers for a given spec.
|
2022-05-17 11:12:43 +00:00
|
|
|
func GetSupportedControllers(spec *kyvernov1.Spec) []string {
|
2022-04-29 09:12:21 +00:00
|
|
|
apply, controllers := CanAutoGen(spec)
|
2022-03-10 15:51:29 +00:00
|
|
|
if !apply || controllers == "none" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return strings.Split(controllers, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRequestedControllers returns the requested autogen controllers based on object annotations.
|
2022-03-25 14:43:47 +00:00
|
|
|
func GetRequestedControllers(meta *metav1.ObjectMeta) []string {
|
2022-03-10 15:51:29 +00:00
|
|
|
annotations := meta.GetAnnotations()
|
|
|
|
if annotations == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-17 11:12:43 +00:00
|
|
|
controllers, ok := annotations[kyvernov1.PodControllersAnnotation]
|
2022-03-10 15:51:29 +00:00
|
|
|
if !ok || controllers == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if controllers == "none" {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return strings.Split(controllers, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetControllers computes the autogen controllers that should be applied to a policy.
|
|
|
|
// It returns the requested, supported and effective controllers (intersection of requested and supported ones).
|
2022-05-17 11:12:43 +00:00
|
|
|
func GetControllers(meta *metav1.ObjectMeta, spec *kyvernov1.Spec) ([]string, []string, []string) {
|
2022-03-10 15:51:29 +00:00
|
|
|
// compute supported and requested controllers
|
2022-04-29 09:12:21 +00:00
|
|
|
supported, requested := GetSupportedControllers(spec), GetRequestedControllers(meta)
|
2022-03-10 15:51:29 +00:00
|
|
|
// no specific request, we can return supported controllers without further filtering
|
|
|
|
if requested == nil {
|
|
|
|
return requested, supported, supported
|
|
|
|
}
|
|
|
|
// filter supported controllers, keeping only those that have been requested
|
|
|
|
var activated []string
|
|
|
|
for _, controller := range supported {
|
|
|
|
if utils.ContainsString(requested, controller) {
|
|
|
|
activated = append(activated, controller)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return requested, supported, activated
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:19:31 +00:00
|
|
|
// podControllersKey annotation could be:
|
|
|
|
// scenario A: not exist, set default to "all", which generates on all pod controllers
|
|
|
|
// - if name / selector exist in resource description -> skip
|
|
|
|
// as these fields may not be applicable to pod controllers
|
|
|
|
// scenario B: "none", user explicitly disable this feature -> skip
|
|
|
|
// scenario C: some certain controllers that user set -> generate on defined controllers
|
|
|
|
// copy entire match / exclude block, it's users' responsibility to
|
|
|
|
// make sure all fields are applicable to pod controllers
|
|
|
|
|
|
|
|
// GenerateRulePatches generates rule for podControllers based on scenario A and C
|
2022-05-17 11:12:43 +00:00
|
|
|
func GenerateRulePatches(spec *kyvernov1.Spec, controllers string) (rulePatches [][]byte, errs []error) {
|
2022-05-17 06:19:03 +00:00
|
|
|
ruleIndex := make(map[string]int)
|
2022-04-01 05:26:47 +00:00
|
|
|
for index, rule := range spec.Rules {
|
2022-03-01 23:19:31 +00:00
|
|
|
ruleIndex[rule.Name] = index
|
|
|
|
}
|
2022-04-01 05:26:47 +00:00
|
|
|
insertIdx := len(spec.Rules)
|
2022-04-29 09:12:21 +00:00
|
|
|
genRules := generateRules(spec, controllers)
|
2022-04-05 15:12:22 +00:00
|
|
|
for i := range genRules {
|
2022-03-01 23:19:31 +00:00
|
|
|
patchPostion := insertIdx
|
|
|
|
convertToPatches := func(genRule kyvernoRule, patchPostion int) []byte {
|
|
|
|
operation := "add"
|
2022-04-05 15:12:22 +00:00
|
|
|
if existingIndex, alreadyExists := ruleIndex[genRule.Name]; alreadyExists {
|
2022-03-01 23:19:31 +00:00
|
|
|
operation = "replace"
|
2022-04-05 15:12:22 +00:00
|
|
|
patchPostion = existingIndex
|
2022-03-01 23:19:31 +00:00
|
|
|
}
|
2022-07-11 03:56:31 +00:00
|
|
|
patch := jsonutils.NewPatchOperation(fmt.Sprintf("/spec/rules/%s", strconv.Itoa(patchPostion)), operation, genRule)
|
2022-04-01 05:26:47 +00:00
|
|
|
pbytes, err := patch.Marshal()
|
2022-03-01 23:19:31 +00:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-01 05:26:47 +00:00
|
|
|
if err := jsonutils.CheckPatch(pbytes); err != nil {
|
2022-03-01 23:19:31 +00:00
|
|
|
errs = append(errs, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pbytes
|
|
|
|
}
|
2022-04-05 15:12:22 +00:00
|
|
|
genRule := createRule(&genRules[i])
|
2022-03-02 17:20:27 +00:00
|
|
|
if genRule != nil {
|
|
|
|
pbytes := convertToPatches(*genRule, patchPostion)
|
2022-03-01 23:19:31 +00:00
|
|
|
if pbytes != nil {
|
|
|
|
rulePatches = append(rulePatches, pbytes)
|
|
|
|
}
|
|
|
|
insertIdx++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-03-28 14:01:27 +00:00
|
|
|
|
|
|
|
// podControllersKey annotation could be:
|
|
|
|
// scenario A: not exist, set default to "all", which generates on all pod controllers
|
|
|
|
// - if name / selector exist in resource description -> skip
|
|
|
|
// as these fields may not be applicable to pod controllers
|
|
|
|
// scenario B: "none", user explicitly disable this feature -> skip
|
|
|
|
// scenario C: some certain controllers that user set -> generate on defined controllers
|
|
|
|
// copy entire match / exclude block, it's users' responsibility to
|
|
|
|
// make sure all fields are applicable to pod controllers
|
|
|
|
|
2022-04-05 15:12:22 +00:00
|
|
|
// generateRules generates rule for podControllers based on scenario A and C
|
2022-05-17 11:12:43 +00:00
|
|
|
func generateRules(spec *kyvernov1.Spec, controllers string) []kyvernov1.Rule {
|
|
|
|
var rules []kyvernov1.Rule
|
2022-04-05 15:12:22 +00:00
|
|
|
for i := range spec.Rules {
|
2022-03-28 14:01:27 +00:00
|
|
|
// handle all other controllers other than CronJob
|
2022-04-29 09:12:21 +00:00
|
|
|
if genRule := createRule(generateRuleForControllers(&spec.Rules[i], stripCronJob(controllers))); genRule != nil {
|
2022-03-28 14:01:27 +00:00
|
|
|
if convRule, err := convertRule(*genRule, "Pod"); err == nil {
|
|
|
|
rules = append(rules, *convRule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// handle CronJob, it appends an additional rule
|
2022-04-29 09:12:21 +00:00
|
|
|
if genRule := createRule(generateCronJobRule(&spec.Rules[i], controllers)); genRule != nil {
|
2022-03-28 14:01:27 +00:00
|
|
|
if convRule, err := convertRule(*genRule, "Cronjob"); err == nil {
|
|
|
|
rules = append(rules, *convRule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
2022-05-17 11:12:43 +00:00
|
|
|
func convertRule(rule kyvernoRule, kind string) (*kyvernov1.Rule, error) {
|
2022-03-28 14:01:27 +00:00
|
|
|
if bytes, err := json.Marshal(rule); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
2022-04-05 15:12:22 +00:00
|
|
|
bytes = updateGenRuleByte(bytes, kind)
|
2022-03-28 14:01:27 +00:00
|
|
|
if err := json.Unmarshal(bytes, &rule); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 11:12:43 +00:00
|
|
|
out := kyvernov1.Rule{
|
2022-03-28 14:01:27 +00:00
|
|
|
Name: rule.Name,
|
|
|
|
VerifyImages: rule.VerifyImages,
|
|
|
|
}
|
|
|
|
if rule.MatchResources != nil {
|
|
|
|
out.MatchResources = *rule.MatchResources
|
|
|
|
}
|
|
|
|
if rule.ExcludeResources != nil {
|
|
|
|
out.ExcludeResources = *rule.ExcludeResources
|
|
|
|
}
|
|
|
|
if rule.Context != nil {
|
|
|
|
out.Context = *rule.Context
|
|
|
|
}
|
|
|
|
if rule.AnyAllConditions != nil {
|
|
|
|
out.SetAnyAllConditions(*rule.AnyAllConditions)
|
|
|
|
}
|
|
|
|
if rule.Mutation != nil {
|
|
|
|
out.Mutation = *rule.Mutation
|
|
|
|
}
|
|
|
|
if rule.Validation != nil {
|
|
|
|
out.Validation = *rule.Validation
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 11:12:43 +00:00
|
|
|
func ComputeRules(p kyvernov1.PolicyInterface) []kyvernov1.Rule {
|
2022-03-28 14:01:27 +00:00
|
|
|
if !toggle.AutogenInternals() {
|
2022-04-29 11:48:22 +00:00
|
|
|
spec := p.GetSpec()
|
2022-03-28 14:01:27 +00:00
|
|
|
return spec.Rules
|
|
|
|
}
|
2022-04-29 11:48:22 +00:00
|
|
|
return computeRules(p)
|
|
|
|
}
|
|
|
|
|
2022-05-17 11:12:43 +00:00
|
|
|
func computeRules(p kyvernov1.PolicyInterface) []kyvernov1.Rule {
|
2022-04-29 11:48:22 +00:00
|
|
|
spec := p.GetSpec()
|
2022-04-29 09:12:21 +00:00
|
|
|
applyAutoGen, desiredControllers := CanAutoGen(spec)
|
2022-03-28 14:01:27 +00:00
|
|
|
if !applyAutoGen {
|
|
|
|
desiredControllers = "none"
|
|
|
|
}
|
|
|
|
ann := p.GetAnnotations()
|
2022-05-17 11:12:43 +00:00
|
|
|
actualControllers, ok := ann[kyvernov1.PodControllersAnnotation]
|
2022-03-28 14:01:27 +00:00
|
|
|
if !ok || !applyAutoGen {
|
|
|
|
actualControllers = desiredControllers
|
|
|
|
} else {
|
|
|
|
if !applyAutoGen {
|
|
|
|
actualControllers = desiredControllers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if actualControllers == "none" {
|
|
|
|
return spec.Rules
|
|
|
|
}
|
2022-04-29 09:12:21 +00:00
|
|
|
genRules := generateRules(spec.DeepCopy(), actualControllers)
|
2022-03-28 14:01:27 +00:00
|
|
|
if len(genRules) == 0 {
|
|
|
|
return spec.Rules
|
|
|
|
}
|
2022-05-17 11:12:43 +00:00
|
|
|
var out []kyvernov1.Rule
|
2022-08-23 06:42:46 +00:00
|
|
|
for _, rule := range spec.Rules {
|
|
|
|
if !isAutogenRuleName(rule.Name) {
|
|
|
|
out = append(out, rule)
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 14:01:27 +00:00
|
|
|
out = append(out, genRules...)
|
|
|
|
return out
|
|
|
|
}
|