1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-15 00:36:28 +00:00

use camel case for ForEach naming (#5660)

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

Signed-off-by: Jim Bugwadia <jim@nirmata.com>
This commit is contained in:
Jim Bugwadia 2022-12-12 11:24:13 -08:00 committed by GitHub
parent f927b51b86
commit af4c8ed886
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 25 deletions

View file

@ -353,7 +353,7 @@ func GetVariable(variablesString, valuesFile string, fs billy.Filesystem, isGit
storeRules = append(storeRules, store.Rule{
Name: rule.Name,
Values: rule.Values,
ForeachValues: rule.ForeachValues,
ForEachValues: rule.ForeachValues,
})
}
storePolicies = append(storePolicies, store.Policy{

View file

@ -22,7 +22,7 @@ func GetMock() bool {
return Mock
}
func SetForeachElement(foreachElement int) {
func SetForEachElement(foreachElement int) {
ForeachElement = foreachElement
}
@ -86,7 +86,7 @@ type Policy struct {
type Rule struct {
Name string `json:"name"`
Values map[string]interface{} `json:"values"`
ForeachValues map[string][]interface{} `json:"foreachValues"`
ForEachValues map[string][]interface{} `json:"foreachValues"`
}
func SetSubjects(subjects Subject) {

View file

@ -106,7 +106,7 @@ func generateRule(name string, rule *kyvernov1.Rule, tplKey, shift string, kinds
return rule
}
if len(rule.Mutation.ForEachMutation) > 0 && rule.Mutation.ForEachMutation != nil {
var newForeachMutation []kyvernov1.ForEachMutation
var newForEachMutation []kyvernov1.ForEachMutation
for _, foreach := range rule.Mutation.ForEachMutation {
temp := kyvernov1.ForEachMutation{
List: foreach.List,
@ -120,10 +120,10 @@ func generateRule(name string, rule *kyvernov1.Rule, tplKey, shift string, kinds
},
},
)
newForeachMutation = append(newForeachMutation, temp)
newForEachMutation = append(newForEachMutation, temp)
}
rule.Mutation = kyvernov1.Mutation{
ForEachMutation: newForeachMutation,
ForEachMutation: newForEachMutation,
}
return rule
}

View file

@ -38,7 +38,7 @@ func ForceMutate(ctx context.Interface, policy kyvernov1.PolicyInterface, resour
}
if r.Mutation.ForEachMutation != nil {
patchedResource, err = applyForeachMutate(r.Name, r.Mutation.ForEachMutation, patchedResource, ctx, logger)
patchedResource, err = applyForEachMutate(r.Name, r.Mutation.ForEachMutation, patchedResource, ctx, logger)
if err != nil {
return patchedResource, err
}
@ -54,16 +54,16 @@ func ForceMutate(ctx context.Interface, policy kyvernov1.PolicyInterface, resour
return patchedResource, nil
}
func applyForeachMutate(name string, foreach []kyvernov1.ForEachMutation, resource unstructured.Unstructured, ctx context.Interface, logger logr.Logger) (patchedResource unstructured.Unstructured, err error) {
func applyForEachMutate(name string, foreach []kyvernov1.ForEachMutation, resource unstructured.Unstructured, ctx context.Interface, logger logr.Logger) (patchedResource unstructured.Unstructured, err error) {
patchedResource = resource
for _, fe := range foreach {
if fe.ForEachMutation != nil {
nestedForeach, err := api.DeserializeJSONArray[kyvernov1.ForEachMutation](fe.ForEachMutation)
nestedForEach, err := api.DeserializeJSONArray[kyvernov1.ForEachMutation](fe.ForEachMutation)
if err != nil {
return patchedResource, errors.Wrapf(err, "failed to deserialize foreach")
}
return applyForeachMutate(name, nestedForeach, patchedResource, ctx, logger)
return applyForEachMutate(name, nestedForEach, patchedResource, ctx, logger)
}
patchedResource, err = applyPatches(name, fe.GetPatchStrategicMerge(), fe.PatchesJSON6902, patchedResource, ctx, logger)

View file

@ -51,8 +51,8 @@ func LoadContext(ctx context.Context, logger logr.Logger, rclient registryclient
}
}
if rule != nil && len(rule.ForeachValues) > 0 {
for key, value := range rule.ForeachValues {
if rule != nil && len(rule.ForEachValues) > 0 {
for key, value := range rule.ForEachValues {
if err := enginectx.jsonContext.AddVariable(key, value[store.ForeachElement]); err != nil {
return err
}

View file

@ -257,7 +257,7 @@ func (f *forEachMutator) mutateElements(ctx context.Context, foreach kyvernov1.F
policyContext := f.policyContext.Copy()
// TODO - this needs to be refactored. The engine should not have a dependency to the CLI code
store.SetForeachElement(i)
store.SetForEachElement(i)
falseVar := false
if err := addElementToContext(policyContext, e, i, f.nesting, &falseVar); err != nil {
@ -280,7 +280,7 @@ func (f *forEachMutator) mutateElements(ctx context.Context, foreach kyvernov1.F
var mutateResp *mutate.Response
if foreach.ForEachMutation != nil {
nestedForeach, err := api.DeserializeJSONArray[kyvernov1.ForEachMutation](foreach.ForEachMutation)
nestedForEach, err := api.DeserializeJSONArray[kyvernov1.ForEachMutation](foreach.ForEachMutation)
if err != nil {
return mutate.NewErrorResponse("failed to deserialize foreach", err)
}
@ -290,7 +290,7 @@ func (f *forEachMutator) mutateElements(ctx context.Context, foreach kyvernov1.F
policyContext: f.policyContext,
resource: patchedResource,
log: f.log,
foreach: nestedForeach,
foreach: nestedForEach,
nesting: f.nesting + 1,
}

View file

@ -177,7 +177,7 @@ type validator struct {
anyPattern apiextensions.JSON
deny *kyvernov1.Deny
podSecurity *kyvernov1.PodSecurity
foreach []kyvernov1.ForEachValidation
forEach []kyvernov1.ForEachValidation
rclient registryclient.Client
nesting int
}
@ -195,18 +195,18 @@ func newValidator(log logr.Logger, rclient registryclient.Client, ctx *PolicyCon
anyPattern: ruleCopy.Validation.GetAnyPattern(),
deny: ruleCopy.Validation.Deny,
podSecurity: ruleCopy.Validation.PodSecurity,
foreach: ruleCopy.Validation.ForEachValidation,
forEach: ruleCopy.Validation.ForEachValidation,
}
}
func newForeachValidator(foreach kyvernov1.ForEachValidation, rclient registryclient.Client, nesting int, rule *kyvernov1.Rule, ctx *PolicyContext, log logr.Logger) (*validator, error) {
func newForEachValidator(foreach kyvernov1.ForEachValidation, rclient registryclient.Client, nesting int, rule *kyvernov1.Rule, ctx *PolicyContext, log logr.Logger) (*validator, error) {
ruleCopy := rule.DeepCopy()
anyAllConditions, err := utils.ToMap(foreach.AnyAllConditions)
if err != nil {
return nil, errors.Wrap(err, "failed to convert ruleCopy.Validation.ForEachValidation.AnyAllConditions")
}
nestedForeach, err := api.DeserializeJSONArray[kyvernov1.ForEachValidation](foreach.ForEachValidation)
nestedForEach, err := api.DeserializeJSONArray[kyvernov1.ForEachValidation](foreach.ForEachValidation)
if err != nil {
return nil, errors.Wrap(err, "failed to convert ruleCopy.Validation.ForEachValidation.AnyAllConditions")
}
@ -221,7 +221,7 @@ func newForeachValidator(foreach kyvernov1.ForEachValidation, rclient registrycl
pattern: foreach.GetPattern(),
anyPattern: foreach.GetAnyPattern(),
deny: foreach.Deny,
foreach: nestedForeach,
forEach: nestedForEach,
nesting: nesting,
}, nil
}
@ -260,7 +260,7 @@ func (v *validator) validate(ctx context.Context) *response.RuleResponse {
}
}
if v.foreach != nil {
if v.forEach != nil {
ruleResponse := v.validateForEach(ctx)
return ruleResponse
}
@ -271,7 +271,7 @@ func (v *validator) validate(ctx context.Context) *response.RuleResponse {
func (v *validator) validateForEach(ctx context.Context) *response.RuleResponse {
applyCount := 0
for _, foreach := range v.foreach {
for _, foreach := range v.forEach {
elements, err := evaluateList(foreach.List, (v.policyContext.JSONContext()))
if err != nil {
v.log.V(2).Info("failed to evaluate list", "list", foreach.List, "error", err.Error())
@ -287,7 +287,7 @@ func (v *validator) validateForEach(ctx context.Context) *response.RuleResponse
}
if applyCount == 0 {
if v.foreach == nil {
if v.forEach == nil {
return nil
}
@ -308,7 +308,7 @@ func (v *validator) validateElements(ctx context.Context, rclient registryclient
}
// TODO - this needs to be refactored. The engine should not have a dependency to the CLI code
store.SetForeachElement(i)
store.SetForEachElement(i)
v.policyContext.JSONContext().Reset()
policyContext := v.policyContext.Copy()
@ -317,7 +317,7 @@ func (v *validator) validateElements(ctx context.Context, rclient registryclient
return ruleError(v.rule, response.Validation, "failed to process foreach", err), applyCount
}
foreachValidator, err := newForeachValidator(foreach, rclient, v.nesting+1, v.rule, policyContext, v.log)
foreachValidator, err := newForEachValidator(foreach, rclient, v.nesting+1, v.rule, policyContext, v.log)
if err != nil {
v.log.Error(err, "failed to create foreach validator")
return ruleError(v.rule, response.Validation, "failed to create foreach validator", err), applyCount