From 57788fffd98e0dbad8549fbaaf61f890a9b4fb8d Mon Sep 17 00:00:00 2001 From: zhaojizhuang <571130360@qq.com> Date: Wed, 3 Mar 2021 19:07:04 +0800 Subject: [PATCH 1/4] add post 022 job to remove pingsource finalizer --- cmd/v0.22/pingsource-cleanup/README.md | 82 ++++++++++++++++++ cmd/v0.22/pingsource-cleanup/injection.go | 43 ++++++++++ cmd/v0.22/pingsource-cleanup/main.go | 92 +++++++++++++++++++++ config/post-install/pingsource-cleanup.yaml | 42 ++++++++++ 4 files changed, 259 insertions(+) create mode 100644 cmd/v0.22/pingsource-cleanup/README.md create mode 100644 cmd/v0.22/pingsource-cleanup/injection.go create mode 100644 cmd/v0.22/pingsource-cleanup/main.go create mode 100644 config/post-install/pingsource-cleanup.yaml diff --git a/cmd/v0.22/pingsource-cleanup/README.md b/cmd/v0.22/pingsource-cleanup/README.md new file mode 100644 index 00000000000..365db555fe2 --- /dev/null +++ b/cmd/v0.22/pingsource-cleanup/README.md @@ -0,0 +1,82 @@ +# PingSource cleanup post-0.22.0 install + +The following is a log of testing commands to try out this job. + +The job's log is the yaml of all resources that were deleted. The job uses +eventing's service account. + +--- + +## kind start cluster: + +``` +cat < 0 { + finalizers := sets.NewString(pingsource.Finalizers...) + if finalizers.Has("pingsources.sources.knative.dev") { + fmt.Printf("# Found PingSource %s/%s, need to remove finalizer.\n", pingsource.Namespace, pingsource.Name) + cleanups = append(cleanups, pingsource) + } + } + } + } + + if !env.DryRun { + for i := range cleanups { + ref := cleanups[i] + fmt.Printf("# will remove finalizer for %s/%s\n", ref.Namespace, ref.Name) + + finalizers := sets.NewString(ref.Finalizers...) + finalizers.Delete("pingsources.sources.knative.dev") + ref.Finalizers = finalizers.List() + + if _, err := client.SourcesV1alpha2().PingSources(ref.Namespace).Update(ctx, &ref, metav1.UpdateOptions{}); err != nil { + fmt.Printf("# [error] failed to update %s/%s %s\n", ref.Namespace, ref.Name, err) + } + } + } + fmt.Printf("# Done, cleaned %d resources.\n", len(cleanups)) +} diff --git a/config/post-install/pingsource-cleanup.yaml b/config/post-install/pingsource-cleanup.yaml new file mode 100644 index 00000000000..c75ea2a9ebd --- /dev/null +++ b/config/post-install/pingsource-cleanup.yaml @@ -0,0 +1,42 @@ +# 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. + +apiVersion: batch/v1 +kind: Job +metadata: + name: v0.22.0-pingsource-cleanup + namespace: knative-eventing + labels: + eventing.knative.dev/release: devel +spec: + ttlSecondsAfterFinished: 600 + template: + metadata: + annotations: + sidecar.istio.io/inject: "false" + spec: + serviceAccountName: knative-eventing-post-install-job + restartPolicy: Never + containers: + - name: pingsource + image: ko://knative.dev/eventing/cmd/v0.22/pingsource-cleanup + env: + - name: SYSTEM_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + +# Note the following ENVVAR settings exist: +# SYSTEM_NAMESPACE - the namespace of the control plane, defaults to knative-eventing +# DRY_RUN - a flag to run the script without deleting or updating, defaults to false. From 60e41df18f98278949aced4e0faf9eea24405302 Mon Sep 17 00:00:00 2001 From: zhaojizhuang <571130360@qq.com> Date: Mon, 29 Mar 2021 23:11:24 +0800 Subject: [PATCH 2/4] add new line --- cmd/v0.22/pingsource-cleanup/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/v0.22/pingsource-cleanup/README.md b/cmd/v0.22/pingsource-cleanup/README.md index 365db555fe2..1833096e2ed 100644 --- a/cmd/v0.22/pingsource-cleanup/README.md +++ b/cmd/v0.22/pingsource-cleanup/README.md @@ -79,4 +79,4 @@ kind delete cluster --name pingsource ## Notes: -PingSource finalizers should be removed \ No newline at end of file +PingSource finalizers should be removed From 3187fd73f70466bd6c2e646af2d8b0a0dfe2affc Mon Sep 17 00:00:00 2001 From: zhaojizhuang <571130360@qq.com> Date: Wed, 31 Mar 2021 22:33:11 +0800 Subject: [PATCH 3/4] change to v1beta2 --- cmd/v0.22/pingsource-cleanup/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/v0.22/pingsource-cleanup/main.go b/cmd/v0.22/pingsource-cleanup/main.go index 8b9c708508f..8d233dfb474 100644 --- a/cmd/v0.22/pingsource-cleanup/main.go +++ b/cmd/v0.22/pingsource-cleanup/main.go @@ -29,7 +29,7 @@ import ( eventingclient "knative.dev/eventing/pkg/client/injection/client" kubeclient "knative.dev/pkg/client/injection/kube/client" - sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" + sourcesv1beta2 "knative.dev/eventing/pkg/apis/sources/v1beta2" ) type envConfig struct { @@ -53,12 +53,12 @@ func main() { log.Fatalf("[ERROR] Failed to list namespaces: %s", err) } - var cleanups []sourcesv1alpha2.PingSource + var cleanups []sourcesv1beta2.PingSource for _, ns := range nss.Items { fmt.Printf("# processing namespace %s\n", ns.Name) - pingsources, err := client.SourcesV1alpha2().PingSources(ns.Name).List(ctx, metav1.ListOptions{}) + pingsources, err := client.Sourcesv1beta2().PingSources(ns.Name).List(ctx, metav1.ListOptions{}) if err != nil { fmt.Printf("# [error] failed to list pingsources in namespace %q, %s\n", ns.Name, err) } @@ -83,7 +83,7 @@ func main() { finalizers.Delete("pingsources.sources.knative.dev") ref.Finalizers = finalizers.List() - if _, err := client.SourcesV1alpha2().PingSources(ref.Namespace).Update(ctx, &ref, metav1.UpdateOptions{}); err != nil { + if _, err := client.Sourcesv1beta2().PingSources(ref.Namespace).Update(ctx, &ref, metav1.UpdateOptions{}); err != nil { fmt.Printf("# [error] failed to update %s/%s %s\n", ref.Namespace, ref.Name, err) } } From c88540307c64ae5648614a764173e36fcc25e5f0 Mon Sep 17 00:00:00 2001 From: zhaojizhuang <571130360@qq.com> Date: Wed, 31 Mar 2021 23:22:53 +0800 Subject: [PATCH 4/4] fix some typo --- cmd/v0.22/pingsource-cleanup/README.md | 2 +- cmd/v0.22/pingsource-cleanup/main.go | 4 ++-- config/post-install/clusterrole.yaml | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/v0.22/pingsource-cleanup/README.md b/cmd/v0.22/pingsource-cleanup/README.md index 1833096e2ed..8b18b28169d 100644 --- a/cmd/v0.22/pingsource-cleanup/README.md +++ b/cmd/v0.22/pingsource-cleanup/README.md @@ -68,7 +68,7 @@ kubectl apply --filename https://storage.googleapis.com/knative-nightly/eventing kubectl apply --filename https://storage.googleapis.com/knative-nightly/eventing/latest/eventing-post-install-jobs.yaml or -ko apply -f ./config/post-install/v0.22.0/ +ko apply -f ./config/post-install/ ``` # clean up diff --git a/cmd/v0.22/pingsource-cleanup/main.go b/cmd/v0.22/pingsource-cleanup/main.go index 8d233dfb474..430363f312c 100644 --- a/cmd/v0.22/pingsource-cleanup/main.go +++ b/cmd/v0.22/pingsource-cleanup/main.go @@ -58,7 +58,7 @@ func main() { for _, ns := range nss.Items { fmt.Printf("# processing namespace %s\n", ns.Name) - pingsources, err := client.Sourcesv1beta2().PingSources(ns.Name).List(ctx, metav1.ListOptions{}) + pingsources, err := client.SourcesV1beta2().PingSources(ns.Name).List(ctx, metav1.ListOptions{}) if err != nil { fmt.Printf("# [error] failed to list pingsources in namespace %q, %s\n", ns.Name, err) } @@ -83,7 +83,7 @@ func main() { finalizers.Delete("pingsources.sources.knative.dev") ref.Finalizers = finalizers.List() - if _, err := client.Sourcesv1beta2().PingSources(ref.Namespace).Update(ctx, &ref, metav1.UpdateOptions{}); err != nil { + if _, err := client.SourcesV1beta2().PingSources(ref.Namespace).Update(ctx, &ref, metav1.UpdateOptions{}); err != nil { fmt.Printf("# [error] failed to update %s/%s %s\n", ref.Namespace, ref.Name, err) } } diff --git a/config/post-install/clusterrole.yaml b/config/post-install/clusterrole.yaml index 6870bf3ffac..37001860507 100644 --- a/config/post-install/clusterrole.yaml +++ b/config/post-install/clusterrole.yaml @@ -44,3 +44,10 @@ rules: - "delete" - "patch" - "watch" + - apiGroups: + - "" + resources: + - "namespaces" + verbs: + - "get" + - "list"