Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation: conversion functions from assign / assignmetadata to Mutator #938

Merged
merged 4 commits into from
Nov 25, 2020

Conversation

fedepaol
Copy link
Contributor

@fedepaol fedepaol commented Nov 4, 2020

What this PR does / why we need it:

This implements MutatorFor plus the type specific objects implementing the interfaces.
Depends on other prs to get merged.

Fixes #936

Special notes for your reviewer:

return res
}

func mutatorForAssign(assign *mutationsv1.Assign) (MutatorWithSchema, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return type assignMutator rather than the MutatorWithSchema interface, per golang conventions.

return res
}

func mutatorForAssignMetadata(assignMeta *mutationsv1.AssignMetadata) (Mutator, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment here WRT return type

@@ -61,3 +66,19 @@ func MakeID(obj runtime.Object) (ID, error) {
Namespace: meta.GetNamespace(),
}, nil
}

// MutatorFor returns a mutator generated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we removed the MutatorFor function after refactoring the MutationSystem interface

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, was a bit torn about having a single entry point that dispatches to the right function or two type safe ones.
Will remove it

@fedepaol fedepaol changed the title WIP Mutation: conversion functions from assign / assignmetadata to Mutator Mutation: conversion functions from assign / assignmetadata to Mutator Nov 11, 2020
@codecov-io
Copy link

codecov-io commented Nov 11, 2020

Codecov Report

Merging #938 (b497d52) into master (e47d95b) will increase coverage by 0.14%.
The diff coverage is 62.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #938      +/-   ##
==========================================
+ Coverage   46.78%   46.93%   +0.14%     
==========================================
  Files          56       58       +2     
  Lines        3454     3541      +87     
==========================================
+ Hits         1616     1662      +46     
- Misses       1636     1669      +33     
- Partials      202      210       +8     
Flag Coverage Δ
unittests 46.93% <62.50%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/mutation/mutator.go 77.77% <0.00%> (ø)
pkg/mutation/assign_mutator.go 59.18% <59.18%> (ø)
pkg/mutation/assignmeta_mutator.go 68.42% <68.42%> (ø)
...onstrainttemplate/constrainttemplate_controller.go 55.70% <0.00%> (-1.63%) ⬇️
pkg/readiness/object_tracker.go 77.91% <0.00%> (-1.23%) ⬇️
pkg/readiness/ready_tracker.go 69.09% <0.00%> (-0.73%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e47d95b...b497d52. Read the comment docs.

@fedepaol fedepaol force-pushed the mutationfor branch 2 times, most recently from 630eb87 to 9789800 Compare November 11, 2020 13:51
Copy link
Contributor

@maxsmythe maxsmythe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, 2 nits on the tests

@ritazh @sozercan @shomron thoughts

t.Fatal(tc.tname, "Failed to convert second to mutator", err)
}
hasDiff := firstMutator.HasDiff(secondMutator)
hasDiff1 := firstMutator.HasDiff(secondMutator)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we swap caller/callee here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed :-)

}
first.APIVersion, first.Kind = gvk.ToAPIVersionAndKind()

second := &mutationsv1.Assign{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might be able to write: second := first.DeepCopy()

}
first.APIVersion, first.Kind = gvk.ToAPIVersionAndKind()

second := &mutationsv1.AssignMetadata{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deepcopy?

t.Fatal(tc.tname, "Failed to convert second to mutator", err)
}
hasDiff := firstMutator.HasDiff(secondMutator)
hasDiff1 := firstMutator.HasDiff(secondMutator)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swap call order?

@fedepaol
Copy link
Contributor Author

@maxsmythe applied new changes

Copy link
Contributor

@maxsmythe maxsmythe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import (
"github.com/google/go-cmp/cmp"
mutationsv1 "github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent, let's use mutationsv1alpha1

id: m.id,
assign: m.assign.DeepCopy(),
path: make([]path.Entry, len(m.path)),
bindings: make([]SchemaBinding, 0),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this also be initialized with len(m.bindings)


import (
"github.com/google/go-cmp/cmp"
mutationsv1 "github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutationsv1alpha1

func MutatorForAssignMetadata(assignMeta *mutationsv1.AssignMetadata) (*AssignMetadataMutator, error) {
id, err := MakeID(assignMeta)
if err != nil {
return nil, errors.Wrapf(err, "Failed to retrieve id for assign type")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, errors.Wrapf(err, "Failed to retrieve id for assign type")
return nil, errors.Wrapf(err, "Failed to retrieve id for assignMetadata type")

"testing"

"github.com/google/go-cmp/cmp"
mutationsv1 "github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutationsv1alpha1


for _, tc := range table {
t.Run(tc.tname, func(t *testing.T) {
secondAssign := second.DeepCopy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: secondAssignMeta

@ritazh
Copy link
Member

ritazh commented Nov 18, 2020

@fedepaol just few nits. mostly looks good!

@fedepaol
Copy link
Contributor Author

@fedepaol just few nits. mostly looks good!

I think I applied all the suggestions, thanks for the review!

Copy link
Member

@ritazh ritazh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@ritazh ritazh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes from recently merged PRs will need to be reflected in this PR now

We expose a generic MutatorFor(o Object) to build a mutator instance
that wraps the cr.

Signed-off-by: Federico Paolinelli <fpaoline@redhat.com>
@fedepaol
Copy link
Contributor Author

Changes from recently merged PRs will need to be reflected in this PR now

Done

@maxsmythe
Copy link
Contributor

It looks like linting is failing?

@fedepaol
Copy link
Contributor Author

It looks like linting is failing?

Fixing it

Location parser is merged. Here we fill the path field for both the mutators.
We also add a new test validating that the conversion fails if an invalid location is specified.

Signed-off-by: Federico Paolinelli <fpaoline@redhat.com>
@fedepaol
Copy link
Contributor Author

Done, sorry. I took the change too lightly.
Here I integrate the parsing, and add a new test that validates that the conversion fails if we pass an invalid path.

@fedepaol
Copy link
Contributor Author

@maxsmythe I also added a minimal verification for assignmeta.

This is under the idea that we want to limit the mutation to labels and annotations, but we can make the constraint lighter and have it validate that the path is a valid metadata path.

@maxsmythe
Copy link
Contributor

Thanks for also checking the metadata prefix!

The changes LGTM. @ritazh, it looks like all your comments were addressed. Does this LGTY?

@maxsmythe
Copy link
Contributor

Also, @fedepaol, if this looks good to Rita tomorrow, are you ready for this to merge?

},
}

annotationValidiSubPath = []parser.Node{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is annotationValidiSubPath intentional or a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was a typo, fixed it

Copy link
Member

@ritazh ritazh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

just one nit

This checks that the only valid location for assignmetadata is related to labels or annotations.

Signed-off-by: Federico Paolinelli <fpaoline@redhat.com>
@fedepaol
Copy link
Contributor Author

Also, @fedepaol, if this looks good to Rita tomorrow, are you ready for this to merge?

It is, I fixed the nit.

@maxsmythe
Copy link
Contributor

Awesome! Merging in

@maxsmythe maxsmythe merged commit 964864d into open-policy-agent:master Nov 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mutation: conversion functions from assign / assignmetadata to mutator / mutatorwithschema
4 participants