1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-08 10:04:25 +00:00

refactor: remove some api unnecessary pointers (#3704)

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-04-28 06:41:10 +02:00 committed by GitHub
parent b740e84f06
commit cf86887d55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 28 additions and 32 deletions

View file

@ -240,7 +240,7 @@ type Mutation struct {
// ForEach applies mutation rules to a list of sub-elements by creating a context for each entry in the list and looping over it to apply the specified logic.
// +optional
ForEachMutation []*ForEachMutation `json:"foreach,omitempty" yaml:"foreach,omitempty"`
ForEachMutation []ForEachMutation `json:"foreach,omitempty" yaml:"foreach,omitempty"`
}
type TargetMutation struct {
@ -302,7 +302,7 @@ type Validation struct {
// ForEach applies validate rules to a list of sub-elements by creating a context for each entry in the list and looping over it to apply the specified logic.
// +optional
ForEachValidation []*ForEachValidation `json:"foreach,omitempty" yaml:"foreach,omitempty"`
ForEachValidation []ForEachValidation `json:"foreach,omitempty" yaml:"foreach,omitempty"`
// Pattern specifies an overlay-style pattern used to check resources.
// +optional

View file

@ -752,13 +752,9 @@ func (in *Mutation) DeepCopyInto(out *Mutation) {
}
if in.ForEachMutation != nil {
in, out := &in.ForEachMutation, &out.ForEachMutation
*out = make([]*ForEachMutation, len(*in))
*out = make([]ForEachMutation, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(ForEachMutation)
(*in).DeepCopyInto(*out)
}
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
}
@ -1161,13 +1157,9 @@ func (in *Validation) DeepCopyInto(out *Validation) {
*out = *in
if in.ForEachValidation != nil {
in, out := &in.ForEachValidation, &out.ForEachValidation
*out = make([]*ForEachValidation, len(*in))
*out = make([]ForEachValidation, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(ForEachValidation)
(*in).DeepCopyInto(*out)
}
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.RawPattern != nil {

View file

@ -880,6 +880,10 @@ See: <a href="https://kyverno.io/docs/writing-policies/validate/#deny-rules">htt
<h3 id="kyverno.io/v1.ForEachMutation">ForEachMutation
</h3>
<p>
(<em>Appears on:</em>
<a href="#kyverno.io/v1.Mutation">Mutation</a>)
</p>
<p>
<p>ForEach applies mutation rules to a list of sub-elements by creating a context for each entry in the list and looping over it to apply the specified logic.</p>
</p>
<table class="table table-striped">
@ -965,6 +969,10 @@ See <a href="https://tools.ietf.org/html/rfc6902">https://tools.ietf.org/html/rf
<h3 id="kyverno.io/v1.ForEachValidation">ForEachValidation
</h3>
<p>
(<em>Appears on:</em>
<a href="#kyverno.io/v1.Validation">Validation</a>)
</p>
<p>
<p>ForEach applies validate rules to a list of sub-elements by creating a context for each entry in the list and looping over it to apply the specified logic.</p>
</p>
<table class="table table-striped">
@ -1912,8 +1920,8 @@ See <a href="https://tools.ietf.org/html/rfc6902">https://tools.ietf.org/html/rf
<td>
<code>foreach</code></br>
<em>
<a href="#kyverno.io/v1.*./api/kyverno/v1.ForEachMutation">
[]*./api/kyverno/v1.ForEachMutation
<a href="#kyverno.io/v1.ForEachMutation">
[]ForEachMutation
</a>
</em>
</td>
@ -2943,8 +2951,8 @@ string
<td>
<code>foreach</code></br>
<em>
<a href="#kyverno.io/v1.*./api/kyverno/v1.ForEachValidation">
[]*./api/kyverno/v1.ForEachValidation
<a href="#kyverno.io/v1.ForEachValidation">
[]ForEachValidation
</a>
</em>
</td>

View file

@ -106,7 +106,7 @@ func generateRule(logger logr.Logger, name string, rule *kyverno.Rule, tplKey, s
return rule
}
if len(rule.Mutation.ForEachMutation) > 0 && rule.Mutation.ForEachMutation != nil {
var newForeachMutation []*kyverno.ForEachMutation
var newForeachMutation []kyverno.ForEachMutation
for _, foreach := range rule.Mutation.ForEachMutation {
temp := kyverno.ForEachMutation{
List: foreach.List,
@ -120,7 +120,7 @@ func generateRule(logger logr.Logger, name string, rule *kyverno.Rule, tplKey, s
},
},
)
newForeachMutation = append(newForeachMutation, &temp)
newForeachMutation = append(newForeachMutation, temp)
}
rule.Mutation = kyverno.Mutation{
ForEachMutation: newForeachMutation,
@ -170,7 +170,7 @@ func generateRule(logger logr.Logger, name string, rule *kyverno.Rule, tplKey, s
return rule
}
if len(rule.Validation.ForEachValidation) > 0 && rule.Validation.ForEachValidation != nil {
newForeachValidate := make([]*kyverno.ForEachValidation, len(rule.Validation.ForEachValidation))
newForeachValidate := make([]kyverno.ForEachValidation, len(rule.Validation.ForEachValidation))
for i, foreach := range rule.Validation.ForEachValidation {
newForeachValidate[i] = foreach
}

View file

@ -63,7 +63,7 @@ func Mutate(rule *kyverno.Rule, ctx context.Interface, resource unstructured.Uns
return newResponse(response.RuleStatusPass, patchedResource, resp.Patches, resp.Message)
}
func ForEach(name string, foreach *kyverno.ForEachMutation, ctx context.Interface, resource unstructured.Unstructured, logger logr.Logger) *Response {
func ForEach(name string, foreach kyverno.ForEachMutation, ctx context.Interface, resource unstructured.Unstructured, logger logr.Logger) *Response {
fe, err := substituteAllInForEach(foreach, ctx, logger)
if err != nil {
return newErrorResponse("variable substitution failed", err)
@ -90,7 +90,7 @@ func ForEach(name string, foreach *kyverno.ForEachMutation, ctx context.Interfac
return newResponse(response.RuleStatusPass, patchedResource, resp.Patches, resp.Message)
}
func substituteAllInForEach(fe *kyverno.ForEachMutation, ctx context.Interface, logger logr.Logger) (*kyverno.ForEachMutation, error) {
func substituteAllInForEach(fe kyverno.ForEachMutation, ctx context.Interface, logger logr.Logger) (*kyverno.ForEachMutation, error) {
jsonObj, err := utils.ToMap(fe)
if err != nil {
return nil, err

View file

@ -201,7 +201,7 @@ func mutateForEach(rule *kyverno.Rule, ctx *PolicyContext, resource unstructured
return r, patchedResource
}
func mutateElements(name string, foreach *kyverno.ForEachMutation, ctx *PolicyContext, elements []interface{}, resource unstructured.Unstructured, logger logr.Logger) *mutate.Response {
func mutateElements(name string, foreach kyverno.ForEachMutation, ctx *PolicyContext, elements []interface{}, resource unstructured.Unstructured, logger logr.Logger) *mutate.Response {
ctx.JSONContext.Checkpoint()
defer ctx.JSONContext.Restore()

View file

@ -187,7 +187,7 @@ func newValidator(log logr.Logger, ctx *PolicyContext, rule *kyverno.Rule) *vali
}
}
func newForeachValidator(foreach *kyverno.ForEachValidation, rule *kyverno.Rule, ctx *PolicyContext, log logr.Logger) *validator {
func newForeachValidator(foreach kyverno.ForEachValidation, rule *kyverno.Rule, ctx *PolicyContext, log logr.Logger) *validator {
ruleCopy := rule.DeepCopy()
anyAllConditions, err := utils.ToMap(foreach.AnyAllConditions)
if err != nil {
@ -294,7 +294,7 @@ func (v *validator) validateForEach() *response.RuleResponse {
return ruleResponse(*v.rule, response.Validation, "rule passed", response.RuleStatusPass, nil)
}
func (v *validator) validateElements(foreach *kyverno.ForEachValidation, elements []interface{}, elementScope bool) (*response.RuleResponse, int) {
func (v *validator) validateElements(foreach kyverno.ForEachValidation, elements []interface{}, elementScope bool) (*response.RuleResponse, int) {
v.ctx.JSONContext.Checkpoint()
defer v.ctx.JSONContext.Restore()
applyCount := 0

View file

@ -98,7 +98,7 @@ func validationElemCount(v *kyverno.Validation) int {
return count
}
func (v *Validate) validateForEach(foreach *kyverno.ForEachValidation) error {
func (v *Validate) validateForEach(foreach kyverno.ForEachValidation) error {
if foreach.List == "" {
return fmt.Errorf("foreach.list is required")
}
@ -119,11 +119,7 @@ func (v *Validate) validateForEach(foreach *kyverno.ForEachValidation) error {
return nil
}
func foreachElemCount(foreach *kyverno.ForEachValidation) int {
if foreach == nil {
return 0
}
func foreachElemCount(foreach kyverno.ForEachValidation) int {
count := 0
if foreach.GetPattern() != nil {
count++