From 68a3e8394e083ec788f776b2a45895f8ccc3b615 Mon Sep 17 00:00:00 2001 From: Lionel Villard Date: Thu, 13 Aug 2020 17:07:54 -0400 Subject: [PATCH 1/3] add post 017 job to remove pingsource finalizer --- cmd/v0.17/pingsource-cleanup/README.md | 82 +++++++++++++++++++ cmd/v0.17/pingsource-cleanup/injection.go | 40 +++++++++ cmd/v0.17/pingsource-cleanup/main.go | 82 +++++++++++++++++++ config/post-install/v0.17.0/dummy.go | 19 +++++ .../v0.17.0/pingsource-cleanup.yaml | 28 +++++++ hack/release.sh | 6 +- 6 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 cmd/v0.17/pingsource-cleanup/README.md create mode 100644 cmd/v0.17/pingsource-cleanup/injection.go create mode 100644 cmd/v0.17/pingsource-cleanup/main.go create mode 100644 config/post-install/v0.17.0/dummy.go create mode 100644 config/post-install/v0.17.0/pingsource-cleanup.yaml diff --git a/cmd/v0.17/pingsource-cleanup/README.md b/cmd/v0.17/pingsource-cleanup/README.md new file mode 100644 index 00000000000..5133ce81ca2 --- /dev/null +++ b/cmd/v0.17/pingsource-cleanup/README.md @@ -0,0 +1,82 @@ +# PingSource cleanup post-0.17.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 { + fmt.Printf("# Found PingSource %s/%s, need to remove finalizer.\n", pingsource.Namespace, pingsource.Name) + cleanups = append(cleanups, pingsource) + } + } + } + + if !env.DryRun { + for _, ref := range cleanups { + fmt.Printf("# will remove finalizer for %s/%s\n", ref.Namespace, ref.Name) + ref.Finalizers = []string{} + + if _, err := client.SourcesV1alpha2().PingSources(ref.Namespace).Update(&ref); 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/v0.17.0/dummy.go b/config/post-install/v0.17.0/dummy.go new file mode 100644 index 00000000000..30f6c5d46f0 --- /dev/null +++ b/config/post-install/v0.17.0/dummy.go @@ -0,0 +1,19 @@ +/* +Copyright 2020 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 + + https://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 preinstall is a placeholder that allows us to pull in config files +// via go mod vendor. +package preinstall diff --git a/config/post-install/v0.17.0/pingsource-cleanup.yaml b/config/post-install/v0.17.0/pingsource-cleanup.yaml new file mode 100644 index 00000000000..7d54857bc57 --- /dev/null +++ b/config/post-install/v0.17.0/pingsource-cleanup.yaml @@ -0,0 +1,28 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: v0.17.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: eventing-controller + restartPolicy: Never + containers: + - name: pingsource + image: ko://knative.dev/eventing/cmd/v0.17/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. diff --git a/hack/release.sh b/hack/release.sh index 7f9a93ad4e7..ba9cbea7bed 100755 --- a/hack/release.sh +++ b/hack/release.sh @@ -26,6 +26,7 @@ readonly MT_CHANNEL_BROKER_YAML="mt-channel-broker.yaml" readonly IN_MEMORY_CHANNEL="in-memory-channel.yaml" readonly PRE_INSTALL_V_0_16="eventing-pre-install-jobs.yaml" readonly POST_INSTALL_V_0_16="eventing-post-install-jobs.yaml" +readonly POST_INSTALL_V_0_17="eventing-post-install-jobs.yaml" declare -A RELEASES RELEASES=( @@ -66,7 +67,10 @@ function build_release() { # Create v0.16.0 post-install job yaml. Cleans up old broker resources from deleted namespaced brokers. ko resolve ${KO_FLAGS} -f config/post-install/v0.16.0/ | "${LABEL_YAML_CMD[@]}" > "${POST_INSTALL_V_0_16}" - local all_yamls=(${EVENTING_CORE_YAML} ${EVENTING_CRDS_YAML} ${SUGAR_CONTROLLER_YAML} ${MT_CHANNEL_BROKER_YAML} ${IN_MEMORY_CHANNEL} ${PRE_INSTALL_V_0_16} ${POST_INSTALL_V_0_16}) + # Create v0.17.0 post-install job yaml. Cleans up pingsources with finalizers + ko resolve ${KO_FLAGS} -f config/post-install/v0.17.0/ | "${LABEL_YAML_CMD[@]}" > "${POST_INSTALL_V_0_17}" + + local all_yamls=(${EVENTING_CORE_YAML} ${EVENTING_CRDS_YAML} ${SUGAR_CONTROLLER_YAML} ${MT_CHANNEL_BROKER_YAML} ${IN_MEMORY_CHANNEL} ${PRE_INSTALL_V_0_16} ${POST_INSTALL_V_0_16} ${POST_INSTALL_V_0_17}) # Assemble the release for yaml in "${!RELEASES[@]}"; do From 5fabf2e79d77226b423c5335aeff6e2cc7bcb183 Mon Sep 17 00:00:00 2001 From: Lionel Villard Date: Mon, 17 Aug 2020 10:41:03 -0400 Subject: [PATCH 2/3] changes after review --- cmd/v0.17/pingsource-cleanup/main.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/v0.17/pingsource-cleanup/main.go b/cmd/v0.17/pingsource-cleanup/main.go index 36ba1a18914..336dda3e5bf 100644 --- a/cmd/v0.17/pingsource-cleanup/main.go +++ b/cmd/v0.17/pingsource-cleanup/main.go @@ -16,7 +16,6 @@ package main import ( "fmt" "log" - "os" "github.com/kelseyhightower/envconfig" @@ -38,8 +37,7 @@ func main() { var env envConfig if err := envconfig.Process("", &env); err != nil { - log.Printf("[ERROR] Failed to process env var: %s", err) - os.Exit(1) + log.Fatalf("[ERROR] Failed to process env var: %s", err) } k8s := kubeclient.Get(ctx) @@ -47,7 +45,7 @@ func main() { nss, err := k8s.CoreV1().Namespaces().List(metav1.ListOptions{}) if err != nil { - panic(err) + log.Fatalf("[ERROR] Failed to list namespaces: %s", err) } var cleanups []sourcesv1alpha2.PingSource From 92f3951cd8cc4ebbd7216f45969464ff397868fa Mon Sep 17 00:00:00 2001 From: Lionel Villard Date: Mon, 17 Aug 2020 13:51:47 -0400 Subject: [PATCH 3/3] only remove pingsource finalizer --- cmd/v0.17/pingsource-cleanup/main.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/v0.17/pingsource-cleanup/main.go b/cmd/v0.17/pingsource-cleanup/main.go index 336dda3e5bf..6abc04e863b 100644 --- a/cmd/v0.17/pingsource-cleanup/main.go +++ b/cmd/v0.17/pingsource-cleanup/main.go @@ -17,6 +17,8 @@ import ( "fmt" "log" + "k8s.io/apimachinery/pkg/util/sets" + "github.com/kelseyhightower/envconfig" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -60,8 +62,11 @@ func main() { for _, pingsource := range pingsources.Items { if len(pingsource.Finalizers) > 0 { - fmt.Printf("# Found PingSource %s/%s, need to remove finalizer.\n", pingsource.Namespace, pingsource.Name) - cleanups = append(cleanups, pingsource) + 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) + } } } } @@ -69,7 +74,10 @@ func main() { if !env.DryRun { for _, ref := range cleanups { fmt.Printf("# will remove finalizer for %s/%s\n", ref.Namespace, ref.Name) - ref.Finalizers = []string{} + + finalizers := sets.NewString(ref.Finalizers...) + finalizers.Delete("pingsources.sources.knative.dev") + ref.Finalizers = finalizers.List() if _, err := client.SourcesV1alpha2().PingSources(ref.Namespace).Update(&ref); err != nil { fmt.Printf("# [error] failed to update %s/%s %s\n", ref.Namespace, ref.Name, err)