/
controller.go
166 lines (145 loc) · 4.75 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright Istio 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 memory
import (
"fmt"
"k8s.io/apimachinery/pkg/types"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/schema/collection"
"istio.io/istio/pkg/slices"
)
// Controller is an implementation of ConfigStoreController.
type Controller struct {
monitor Monitor
configStore model.ConfigStore
hasSynced func() bool
// If meshConfig.DiscoverySelectors are specified, the namespacesFilter tracks the namespaces this controller watches.
namespacesFilter func(obj interface{}) bool
}
// NewController return an implementation of ConfigStoreController
// This is a client-side monitor that dispatches events as the changes are being
// made on the client.
func NewController(cs model.ConfigStore) *Controller {
out := &Controller{
configStore: cs,
monitor: NewMonitor(cs),
}
return out
}
// NewSyncController return an implementation of model.ConfigStoreController which processes events synchronously
func NewSyncController(cs model.ConfigStore) *Controller {
out := &Controller{
configStore: cs,
monitor: NewSyncMonitor(cs),
}
return out
}
func (c *Controller) RegisterHasSyncedHandler(cb func() bool) {
c.hasSynced = cb
}
func (c *Controller) RegisterEventHandler(kind config.GroupVersionKind, f model.EventHandler) {
c.monitor.AppendEventHandler(kind, f)
}
// HasSynced return whether store has synced
// It can be controlled externally (such as by the data source),
// otherwise it'll always consider synced.
func (c *Controller) HasSynced() bool {
if c.hasSynced != nil {
return c.hasSynced()
}
return true
}
func (c *Controller) Run(stop <-chan struct{}) {
c.monitor.Run(stop)
}
func (c *Controller) Schemas() collection.Schemas {
return c.configStore.Schemas()
}
func (c *Controller) Get(kind config.GroupVersionKind, key, namespace string) *config.Config {
if c.namespacesFilter != nil && !c.namespacesFilter(namespace) {
return nil
}
return c.configStore.Get(kind, key, namespace)
}
func (c *Controller) Create(config config.Config) (revision string, err error) {
if revision, err = c.configStore.Create(config); err == nil {
c.monitor.ScheduleProcessEvent(ConfigEvent{
config: config,
event: model.EventAdd,
})
}
return
}
func (c *Controller) Update(config config.Config) (newRevision string, err error) {
oldconfig := c.configStore.Get(config.GroupVersionKind, config.Name, config.Namespace)
if newRevision, err = c.configStore.Update(config); err == nil {
c.monitor.ScheduleProcessEvent(ConfigEvent{
old: *oldconfig,
config: config,
event: model.EventUpdate,
})
}
return
}
func (c *Controller) UpdateStatus(config config.Config) (newRevision string, err error) {
oldconfig := c.configStore.Get(config.GroupVersionKind, config.Name, config.Namespace)
if newRevision, err = c.configStore.UpdateStatus(config); err == nil {
c.monitor.ScheduleProcessEvent(ConfigEvent{
old: *oldconfig,
config: config,
event: model.EventUpdate,
})
}
return
}
func (c *Controller) Patch(orig config.Config, patchFn config.PatchFunc) (newRevision string, err error) {
cfg, typ := patchFn(orig.DeepCopy())
switch typ {
case types.MergePatchType:
case types.JSONPatchType:
default:
return "", fmt.Errorf("unsupported merge type: %s", typ)
}
if newRevision, err = c.configStore.Patch(cfg, patchFn); err == nil {
c.monitor.ScheduleProcessEvent(ConfigEvent{
old: orig,
config: cfg,
event: model.EventUpdate,
})
}
return
}
func (c *Controller) Delete(kind config.GroupVersionKind, key, namespace string, resourceVersion *string) error {
if config := c.Get(kind, key, namespace); config != nil {
if err := c.configStore.Delete(kind, key, namespace, resourceVersion); err != nil {
return err
}
c.monitor.ScheduleProcessEvent(ConfigEvent{
config: *config,
event: model.EventDelete,
})
return nil
}
return fmt.Errorf("delete: config %v/%v/%v does not exist", kind, namespace, key)
}
func (c *Controller) List(kind config.GroupVersionKind, namespace string) []config.Config {
configs := c.configStore.List(kind, namespace)
if c.namespacesFilter != nil {
return slices.Filter(configs, func(config config.Config) bool {
return c.namespacesFilter(config)
})
}
return configs
}