-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
120 lines (97 loc) · 3.29 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright 2017 The Kubernetes 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 controller
import (
"context"
"fmt"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
crv1 "k8s.io/apiextensions-apiserver/examples/client-go/apis/cr/v1"
)
// Watcher is an example of watching on resource create/update/delete events
type ExampleController struct {
ExampleClient *rest.RESTClient
ExampleScheme *runtime.Scheme
}
// Run starts an Example resource controller
func (c *ExampleController) Run(ctx context.Context) error {
fmt.Print("Watch Example objects\n")
// Watch Example objects
_, err := c.watchExamples(ctx)
if err != nil {
fmt.Printf("Failed to register watch for Example resource: %v\n", err)
return err
}
<-ctx.Done()
return ctx.Err()
}
func (c *ExampleController) watchExamples(ctx context.Context) (cache.Controller, error) {
source := cache.NewListWatchFromClient(
c.ExampleClient,
crv1.ExampleResourcePlural,
apiv1.NamespaceAll,
fields.Everything())
_, controller := cache.NewInformer(
source,
// The object type.
&crv1.Example{},
// resyncPeriod
// Every resyncPeriod, all resources in the cache will retrigger events.
// Set to 0 to disable the resync.
0,
// Your custom resource event handlers.
cache.ResourceEventHandlerFuncs{
AddFunc: c.onAdd,
UpdateFunc: c.onUpdate,
DeleteFunc: c.onDelete,
})
go controller.Run(ctx.Done())
return controller, nil
}
func (c *ExampleController) onAdd(obj interface{}) {
example := obj.(*crv1.Example)
fmt.Printf("[CONTROLLER] OnAdd %s\n", example.ObjectMeta.SelfLink)
// NEVER modify objects from the store. It's a read-only, local cache.
// You can use DeepCopy() to make a deep copy of original object and modify this copy
// Or create a copy manually for better performance
exampleCopy := example.DeepCopy()
exampleCopy.Status = crv1.ExampleStatus{
State: crv1.ExampleStateProcessed,
Message: "Successfully processed by controller",
}
err := c.ExampleClient.Put().
Name(example.ObjectMeta.Name).
Namespace(example.ObjectMeta.Namespace).
Resource(crv1.ExampleResourcePlural).
Body(exampleCopy).
Do().
Error()
if err != nil {
fmt.Printf("ERROR updating status: %v\n", err)
} else {
fmt.Printf("UPDATED status: %#v\n", exampleCopy)
}
}
func (c *ExampleController) onUpdate(oldObj, newObj interface{}) {
oldExample := oldObj.(*crv1.Example)
newExample := newObj.(*crv1.Example)
fmt.Printf("[CONTROLLER] OnUpdate oldObj: %s\n", oldExample.ObjectMeta.SelfLink)
fmt.Printf("[CONTROLLER] OnUpdate newObj: %s\n", newExample.ObjectMeta.SelfLink)
}
func (c *ExampleController) onDelete(obj interface{}) {
example := obj.(*crv1.Example)
fmt.Printf("[CONTROLLER] OnDelete %s\n", example.ObjectMeta.SelfLink)
}