From 8565fcdcf1cb8b409935b28ab4052a612e0a1b4b Mon Sep 17 00:00:00 2001 From: Ville Aikas <11279988+vaikas@users.noreply.github.com> Date: Tue, 20 Apr 2021 18:24:36 -0700 Subject: [PATCH] Clean up resources created by Feature tests (#5289) * example usage * actually delete the resources * rebase --- go.mod | 2 +- go.sum | 4 +- test/rekt/features/broker/control_plane.go | 25 +++++++++++- .../reconciler-test/pkg/environment/magic.go | 1 + .../reconciler-test/pkg/feature/context.go | 39 +++++++++++++++++++ .../reconciler-test/pkg/feature/feature.go | 14 +++++++ .../reconciler-test/pkg/manifest/installer.go | 5 ++- vendor/modules.txt | 2 +- 8 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 vendor/knative.dev/reconciler-test/pkg/feature/context.go diff --git a/go.mod b/go.mod index fd18d32826..569c65b9f5 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,7 @@ require ( knative.dev/hack v0.0.0-20210325223819-b6ab329907d3 knative.dev/hack/schema v0.0.0-20210325223819-b6ab329907d3 knative.dev/pkg v0.0.0-20210420053235-1afd04993622 - knative.dev/reconciler-test v0.0.0-20210420201836-15b4e1222865 + knative.dev/reconciler-test v0.0.0-20210420215136-4598ca0f0602 sigs.k8s.io/yaml v1.2.0 ) diff --git a/go.sum b/go.sum index 07ad4c09da..e223225c37 100644 --- a/go.sum +++ b/go.sum @@ -1122,8 +1122,8 @@ knative.dev/hack/schema v0.0.0-20210325223819-b6ab329907d3 h1:F/pVm+rB+WpyVhH9cm knative.dev/hack/schema v0.0.0-20210325223819-b6ab329907d3/go.mod h1:ffjwmdcrH5vN3mPhO8RrF2KfNnbHeCE2C60A+2cv3U0= knative.dev/pkg v0.0.0-20210420053235-1afd04993622 h1:wSyDPp/LuOLeDCpvUHgKXqb4DfmCEPelsaYzC0Fojm0= knative.dev/pkg v0.0.0-20210420053235-1afd04993622/go.mod h1:UtcSLHy2XIz5blWoPTA40F87zk4O7erxkCwv+7Tsmws= -knative.dev/reconciler-test v0.0.0-20210420201836-15b4e1222865 h1:Flf/ZPKa76ynJhHltE1+RpB+0oDum44Wlfz9ednc7l4= -knative.dev/reconciler-test v0.0.0-20210420201836-15b4e1222865/go.mod h1:yZihS1XoBt7oxU6Jq+U2hMKmUvfKFEaj3vMqOMBt/tI= +knative.dev/reconciler-test v0.0.0-20210420215136-4598ca0f0602 h1:+EGg+hRVnOdgBPID/ngPjjN2BCOY1xV983BKan+SDhk= +knative.dev/reconciler-test v0.0.0-20210420215136-4598ca0f0602/go.mod h1:yZihS1XoBt7oxU6Jq+U2hMKmUvfKFEaj3vMqOMBt/tI= pgregory.net/rapid v0.3.3 h1:jCjBsY4ln4Atz78QoBWxUEvAHaFyNDQg9+WU62aCn1U= pgregory.net/rapid v0.3.3/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/test/rekt/features/broker/control_plane.go b/test/rekt/features/broker/control_plane.go index cc0cf2c2dc..42d0730ac9 100644 --- a/test/rekt/features/broker/control_plane.go +++ b/test/rekt/features/broker/control_plane.go @@ -27,6 +27,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/wait" v1 "knative.dev/eventing/pkg/apis/duck/v1" eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" @@ -36,7 +37,9 @@ import ( brokerresources "knative.dev/eventing/test/rekt/resources/broker" "knative.dev/eventing/test/rekt/resources/delivery" triggerresources "knative.dev/eventing/test/rekt/resources/trigger" + "knative.dev/pkg/apis" duckv1 "knative.dev/pkg/apis/duck/v1" + "knative.dev/pkg/injection/clients/dynamicclient" "knative.dev/pkg/ptr" "knative.dev/reconciler-test/pkg/environment" "knative.dev/reconciler-test/pkg/eventshub" @@ -364,12 +367,31 @@ func addControlPlaneDelivery(fs *feature.FeatureSet) { t.Failed() } }) - f.Stable("Conformance").Should(tt.name, assertExpectedEvents(prober, expectedEvents)) + f.Teardown("Delete feature resources", deleteFeatureResources()) fs.Features = append(fs.Features, *f) } } +func deleteFeatureResources() feature.StepFn { + return func(ctx context.Context, t feature.T) { + dc := dynamicclient.Get(ctx) + f := feature.FromContext(ctx) + for _, ref := range f.References() { + gv, err := schema.ParseGroupVersion(ref.APIVersion) + if err != nil { + t.Errorf("Could not parse GroupVersion for %+v", ref.APIVersion) + } else { + resource := apis.KindToResource(gv.WithKind(ref.Kind)) + t.Logf("Deleting %s/%s of GVR: %+v", ref.Namespace, ref.Name, resource) + if err := dc.Resource(resource).Namespace(ref.Namespace).Delete(ctx, ref.Name, *metav1.NewDeleteOptions(0)); err != nil { + t.Logf("Failed to delete during cleanup %s/%s of GVR: %+v", ref.Namespace, ref.Name, resource) + } + } + } + } +} + func addControlPlaneEventRouting(fs *feature.FeatureSet) { for i, tt := range []struct { name string @@ -557,6 +579,7 @@ func addControlPlaneEventRouting(fs *feature.FeatureSet) { }) f.Stable("Conformance").Should(tt.name, assertExpectedRoutedEvents(prober, expectedEvents)) + f.Teardown("Delete feature resources", deleteFeatureResources()) fs.Features = append(fs.Features, *f) } } diff --git a/vendor/knative.dev/reconciler-test/pkg/environment/magic.go b/vendor/knative.dev/reconciler-test/pkg/environment/magic.go index 30abdf9ab6..ceb6ebbb51 100644 --- a/vendor/knative.dev/reconciler-test/pkg/environment/magic.go +++ b/vendor/knative.dev/reconciler-test/pkg/environment/magic.go @@ -218,6 +218,7 @@ func (mr *MagicEnvironment) Test(ctx context.Context, originalT *testing.T, f *f f.State = &state.KVStore{} } ctx = state.ContextWith(ctx, f.State) + ctx = feature.ContextWith(ctx, f) steps := categorizeSteps(f.Steps) diff --git a/vendor/knative.dev/reconciler-test/pkg/feature/context.go b/vendor/knative.dev/reconciler-test/pkg/feature/context.go new file mode 100644 index 0000000000..ab1d16b35f --- /dev/null +++ b/vendor/knative.dev/reconciler-test/pkg/feature/context.go @@ -0,0 +1,39 @@ +/* +Copyright 2021 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package feature + +import ( + "context" +) + +type envKey struct{} + +// ContextWith decorates the given context with the provided Feature, and returns +// the resulting context. +func ContextWith(ctx context.Context, f *Feature) context.Context { + return context.WithValue(ctx, envKey{}, f) +} + +// FromContext returns the Feature from Context, if not found FromContext will +// panic. +// TODO: revisit if we really want to panic here... likely not. +func FromContext(ctx context.Context) *Feature { + if e, ok := ctx.Value(envKey{}).(*Feature); ok { + return e + } + panic("no Feature found in context") +} diff --git a/vendor/knative.dev/reconciler-test/pkg/feature/feature.go b/vendor/knative.dev/reconciler-test/pkg/feature/feature.go index 12e2a2d37b..32bf6f5248 100644 --- a/vendor/knative.dev/reconciler-test/pkg/feature/feature.go +++ b/vendor/knative.dev/reconciler-test/pkg/feature/feature.go @@ -22,6 +22,8 @@ import ( "runtime" "strings" + corev1 "k8s.io/api/core/v1" + "knative.dev/reconciler-test/pkg/state" ) @@ -30,6 +32,8 @@ type Feature struct { Name string Steps []Step State state.Store + // Contains all the resources created as part of this Feature. + refs []corev1.ObjectReference } // NewFeatureNamed creates a new feature with the provided name @@ -83,6 +87,16 @@ func (s *Step) TestName() string { } } +// Reference adds references to keep track of for example, for cleaning things +// after a Feature completes. +func (f *Feature) Reference(ref ...corev1.ObjectReference) { + f.refs = append(f.refs, ref...) +} + +func (f *Feature) References() []corev1.ObjectReference { + return f.refs +} + // Setup adds a step function to the feature set at the Setup timing phase. func (f *Feature) Setup(name string, fn StepFn) { f.AddStep(Step{ diff --git a/vendor/knative.dev/reconciler-test/pkg/manifest/installer.go b/vendor/knative.dev/reconciler-test/pkg/manifest/installer.go index c125ee853b..f907a37f78 100644 --- a/vendor/knative.dev/reconciler-test/pkg/manifest/installer.go +++ b/vendor/knative.dev/reconciler-test/pkg/manifest/installer.go @@ -28,6 +28,7 @@ import ( "knative.dev/pkg/injection/clients/dynamicclient" "knative.dev/reconciler-test/pkg/environment" + "knative.dev/reconciler-test/pkg/feature" ) // CfgFn is the function signature of configuration mutation options. @@ -36,6 +37,7 @@ type CfgFn func(map[string]interface{}) func InstallYaml(ctx context.Context, dir string, base map[string]interface{}) (Manifest, error) { env := environment.FromContext(ctx) cfg := env.TemplateConfig(base) + f := feature.FromContext(ctx) yamls, err := ParseTemplates(dir, env.Images(), cfg) if err != nil { @@ -54,8 +56,9 @@ func InstallYaml(ctx context.Context, dir string, base map[string]interface{}) ( return manifest, err } - // Save the refs. + // Save the refs to Environment and Feature env.Reference(manifest.References()...) + f.Reference(manifest.References()...) // Temp refs := manifest.References() diff --git a/vendor/modules.txt b/vendor/modules.txt index 1a93872dc0..ec3d016d74 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1110,7 +1110,7 @@ knative.dev/pkg/webhook/resourcesemantics knative.dev/pkg/webhook/resourcesemantics/conversion knative.dev/pkg/webhook/resourcesemantics/defaulting knative.dev/pkg/webhook/resourcesemantics/validation -# knative.dev/reconciler-test v0.0.0-20210420201836-15b4e1222865 +# knative.dev/reconciler-test v0.0.0-20210420215136-4598ca0f0602 ## explicit knative.dev/reconciler-test/cmd/eventshub knative.dev/reconciler-test/pkg/environment