Skip to content
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.22/pingsource-cleanup/README.md
Original file line number Diff line number Diff line change
@@ -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.
Comment thread
zhaojizhuang marked this conversation as resolved.

---

## 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.22:

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

## monitor

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

## app install

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

## Upgrade to Eventing 0.22

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

# upgrade to Eventing 0.22 (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/
```

# clean up

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

## Notes:

PingSource finalizers should be removed
43 changes: 43 additions & 0 deletions cmd/v0.22/pingsource-cleanup/injection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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 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.Sprint("Failed to start informers - ", err))
}
<-ctx.Done()
}(ctx)

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

sourcesv1beta2 "knative.dev/eventing/pkg/apis/sources/v1beta2"
)

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(ctx, metav1.ListOptions{})
Comment thread
zhaojizhuang marked this conversation as resolved.
if err != nil {
log.Fatalf("[ERROR] Failed to list namespaces: %s", err)
}

var cleanups []sourcesv1beta2.PingSource

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

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)
}

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 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.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)
}
}
}
fmt.Printf("# Done, cleaned %d resources.\n", len(cleanups))
}
7 changes: 7 additions & 0 deletions config/post-install/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ rules:
- "delete"
- "patch"
- "watch"
- apiGroups:
- ""
resources:
- "namespaces"
verbs:
- "get"
- "list"
42 changes: 42 additions & 0 deletions config/post-install/pingsource-cleanup.yaml
Original file line number Diff line number Diff line change
@@ -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.