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

add post 017 job to remove pingsource finalizer #3836

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions cmd/v0.17/pingsource-cleanup/README.md
Original file line number Diff line number Diff line change
@@ -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 <<EOF | kind create cluster --name pingsource --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
image: kindest/node:v1.16.9@sha256:7175872357bc85847ec4b1aba46ed1d12fa054c83ac7a8a11f5c268957fd5765
- role: worker
image: kindest/node:v1.16.9@sha256:7175872357bc85847ec4b1aba46ed1d12fa054c83ac7a8a11f5c268957fd5765
EOF
```

## install knative eventing 0.16:

```
kubectl apply --filename https://github.com/knative/eventing/releases/download/v0.16.0/eventing-crds.yaml
kubectl apply --filename https://github.com/knative/eventing/releases/download/v0.16.0/eventing-core.yaml
```

## monitor

```
watch kubectl get pingsource,deployments,rolebindings,serviceaccounts,roles
```

## app install

```
cat <<EOF | kubectl apply -f -
apiVersion: sources.knative.dev/v1alpha2
kind: PingSource
metadata:
name: test-ping-source
spec:
schedule: "*/1 * * * *"
jsonData: '{"message": "OG- 0.16 PingSource"}'
sink:
ref:
# Deliver events to Deployment.
apiVersion: v1
kind: Service
name: event-display
EOF
```

## Upgrade to Eventing 0.17

```
kubectl apply --filename https://github.com/knative/eventing/releases/download/v0.17.0/eventing-core.yaml
kubectl apply --filename https://github.com/knative/eventing/releases/download/v0.17.0/eventing-post-install-jobs.yaml
```

# upgrade to Eventing 0.16 (nightly)

```
kubectl apply --filename https://storage.googleapis.com/knative-nightly/eventing/latest/eventing-core.yaml
kubectl apply --filename https://storage.googleapis.com/knative-nightly/eventing/latest/eventing-post-install-jobs.yaml

or
ko apply -f ./config/post-install/v0.17.0/
```

# clean up

```
kind delete cluster --name pingsource
```

## Notes:

PingSource finalizers should be removed
40 changes: 40 additions & 0 deletions cmd/v0.17/pingsource-cleanup/injection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
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
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 main

import (
"context"
"fmt"

"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/signals"
)

func injectionEnabled() context.Context {
ctx := signals.NewContext()
cfg := sharedmain.ParseAndGetConfigOrDie()
ctx, informers := injection.Default.SetupInformers(ctx, cfg)

// Start the injection clients and informers.
go func(ctx context.Context) {
if err := controller.StartInformers(ctx.Done(), informers...); err != nil {
panic(fmt.Sprintf("Failed to start informers - %s", err))
}
<-ctx.Done()
}(ctx)

return ctx
}
88 changes: 88 additions & 0 deletions cmd/v0.17/pingsource-cleanup/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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
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 main

import (
"fmt"
"log"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/kelseyhightower/envconfig"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

eventingclient "knative.dev/eventing/pkg/client/injection/client"
kubeclient "knative.dev/pkg/client/injection/kube/client"

sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
)

type envConfig struct {
SystemNamespace string `envconfig:"SYSTEM_NAMESPACE" default:"knative-eventing"`
DryRun bool `envconfig:"DRY_RUN" default:"false"`
}

func main() {
ctx := injectionEnabled()

var env envConfig
if err := envconfig.Process("", &env); err != nil {
log.Fatalf("[ERROR] Failed to process env var: %s", err)
}

k8s := kubeclient.Get(ctx)
client := eventingclient.Get(ctx)

nss, err := k8s.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
log.Fatalf("[ERROR] Failed to list namespaces: %s", err)
}

var cleanups []sourcesv1alpha2.PingSource

for _, ns := range nss.Items {
fmt.Printf("# processing namespace %s\n", ns.Name)

pingsources, err := client.SourcesV1alpha2().PingSources(ns.Name).List(metav1.ListOptions{})
if err != nil {
fmt.Printf("# [error] failed to list pingsources in namespace %q, %s\n", ns.Name, err)
}

for _, pingsource := range pingsources.Items {
if len(pingsource.Finalizers) > 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 _, ref := range cleanups {
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(&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))
}
19 changes: 19 additions & 0 deletions config/post-install/v0.17.0/dummy.go
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions config/post-install/v0.17.0/pingsource-cleanup.yaml
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 5 additions & 1 deletion hack/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=(
Expand Down Expand Up @@ -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
Expand Down