2020-09-01 09:11:20 -07:00
|
|
|
package policymutation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-01 17:23:54 -07:00
|
|
|
"reflect"
|
2020-09-01 09:11:20 -07:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine"
|
2020-09-01 09:11:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func generateCronJobRule(rule kyverno.Rule, controllers string, log logr.Logger) kyvernoRule {
|
|
|
|
logger := log.WithName("handleCronJob")
|
|
|
|
|
|
|
|
hasCronJob := strings.Contains(controllers, engine.PodControllerCronJob) || strings.Contains(controllers, "all")
|
|
|
|
if !hasCronJob {
|
|
|
|
return kyvernoRule{}
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:25:51 -08:00
|
|
|
logger.V(3).Info("generating rule for cronJob")
|
2020-09-01 09:11:20 -07:00
|
|
|
jobRule := generateRuleForControllers(rule, "Job", logger)
|
|
|
|
|
2020-09-01 17:23:54 -07:00
|
|
|
if reflect.DeepEqual(jobRule, kyvernoRule{}) {
|
|
|
|
return kyvernoRule{}
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:11:20 -07:00
|
|
|
cronJobRule := &jobRule
|
|
|
|
cronJobRule.Name = fmt.Sprintf("autogen-cronjob-%s", rule.Name)
|
|
|
|
cronJobRule.MatchResources.Kinds = []string{engine.PodControllerCronJob}
|
|
|
|
if (jobRule.ExcludeResources) != nil && (len(jobRule.ExcludeResources.Kinds) > 0) {
|
|
|
|
cronJobRule.ExcludeResources.Kinds = []string{engine.PodControllerCronJob}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jobRule.Mutation != nil) && (jobRule.Mutation.Overlay != nil) {
|
|
|
|
newMutation := &kyverno.Mutation{
|
|
|
|
Overlay: map[string]interface{}{
|
|
|
|
"spec": map[string]interface{}{
|
|
|
|
"jobTemplate": jobRule.Mutation.Overlay,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cronJobRule.Mutation = newMutation.DeepCopy()
|
|
|
|
return *cronJobRule
|
|
|
|
}
|
|
|
|
|
2020-09-05 04:50:20 +05:30
|
|
|
if (jobRule.Mutation != nil) && (jobRule.Mutation.PatchStrategicMerge != nil) {
|
|
|
|
newMutation := &kyverno.Mutation{
|
|
|
|
PatchStrategicMerge: map[string]interface{}{
|
|
|
|
"spec": map[string]interface{}{
|
|
|
|
"jobTemplate": jobRule.Mutation.PatchStrategicMerge,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cronJobRule.Mutation = newMutation.DeepCopy()
|
|
|
|
return *cronJobRule
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:11:20 -07:00
|
|
|
if (jobRule.Validation != nil) && (jobRule.Validation.Pattern != nil) {
|
|
|
|
newValidate := &kyverno.Validation{
|
|
|
|
Message: rule.Validation.Message,
|
|
|
|
Pattern: map[string]interface{}{
|
|
|
|
"spec": map[string]interface{}{
|
|
|
|
"jobTemplate": jobRule.Validation.Pattern,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cronJobRule.Validation = newValidate.DeepCopy()
|
|
|
|
return *cronJobRule
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:25:51 -08:00
|
|
|
if (jobRule.Validation != nil) && (jobRule.Validation.AnyPattern != nil) {
|
2020-09-01 09:11:20 -07:00
|
|
|
var patterns []interface{}
|
2020-11-13 16:25:51 -08:00
|
|
|
anyPatterns, err := rule.Validation.DeserializeAnyPattern()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to deserialze anyPattern, expect tyepe array")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pattern := range anyPatterns {
|
2020-09-01 09:11:20 -07:00
|
|
|
newPattern := map[string]interface{}{
|
|
|
|
"spec": map[string]interface{}{
|
|
|
|
"jobTemplate": pattern,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
patterns = append(patterns, newPattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
cronJobRule.Validation = &kyverno.Validation{
|
|
|
|
Message: rule.Validation.Message,
|
|
|
|
AnyPattern: patterns,
|
|
|
|
}
|
|
|
|
return *cronJobRule
|
|
|
|
}
|
|
|
|
|
|
|
|
return kyvernoRule{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stripCronJob removes CronJob from controllers
|
|
|
|
func stripCronJob(controllers string) string {
|
|
|
|
var newControllers []string
|
|
|
|
|
|
|
|
controllerArr := strings.Split(controllers, ",")
|
|
|
|
for _, c := range controllerArr {
|
|
|
|
if c == engine.PodControllerCronJob {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newControllers = append(newControllers, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newControllers) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(newControllers, ",")
|
|
|
|
}
|