|
| 1 | +/** |
| 2 | + * Copyright 2023 The KusionStack Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package initializer |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "sync" |
| 23 | + |
| 24 | + "github.com/spf13/pflag" |
| 25 | + "k8s.io/apimachinery/pkg/util/sets" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 27 | +) |
| 28 | + |
| 29 | +// InitFunc is used to launch a particular controller. It may run additional "should I activate checks". |
| 30 | +// Any error returned will cause the controller process to `Fatal` |
| 31 | +// The bool indicates whether the controller was enabled. |
| 32 | +type InitFunc func(manager.Manager) (enabled bool, err error) |
| 33 | + |
| 34 | +// InitOption configures how we set up initializer |
| 35 | +type InitOption interface { |
| 36 | + apply(*options) |
| 37 | +} |
| 38 | + |
| 39 | +type options struct { |
| 40 | + disableByDefault bool |
| 41 | +} |
| 42 | +type optionFunc func(*options) |
| 43 | + |
| 44 | +func (o optionFunc) apply(opt *options) { |
| 45 | + o(opt) |
| 46 | +} |
| 47 | + |
| 48 | +// WithDisableByDefault disable controller by default |
| 49 | +func WithDisableByDefault() InitOption { |
| 50 | + return optionFunc(func(o *options) { |
| 51 | + o.disableByDefault = true |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +// Interface knows how to set up controllers with manager |
| 56 | +type Interface interface { |
| 57 | + // Add add new controller setup function to initializer. |
| 58 | + Add(controllerName string, setup InitFunc, options ...InitOption) error |
| 59 | + |
| 60 | + // KnownControllers returns a slice of strings describing the ControllerInitialzier's known controllers. |
| 61 | + KnownControllers() []string |
| 62 | + |
| 63 | + // Enabled returns true if the controller is enabled. |
| 64 | + Enabled(name string) bool |
| 65 | + |
| 66 | + // SetupWithManager add all enabled controllers to manager |
| 67 | + SetupWithManager(mgr manager.Manager) error |
| 68 | + |
| 69 | + // BindFlag adds a flag for setting global feature gates to the specified FlagSet. |
| 70 | + BindFlag(fs *pflag.FlagSet) |
| 71 | +} |
| 72 | + |
| 73 | +// New returns a new instance of initializer interface |
| 74 | +func New() Interface { |
| 75 | + return &controllerInitializer{ |
| 76 | + initializers: make(map[string]InitFunc), |
| 77 | + all: sets.NewString(), |
| 78 | + enabled: sets.NewString(), |
| 79 | + disableByDefault: sets.NewString(), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +var _ Interface = &controllerInitializer{} |
| 84 | + |
| 85 | +type controllerInitializer struct { |
| 86 | + lock sync.RWMutex |
| 87 | + |
| 88 | + initializers map[string]InitFunc |
| 89 | + all sets.String |
| 90 | + enabled sets.String |
| 91 | + disableByDefault sets.String |
| 92 | +} |
| 93 | + |
| 94 | +func (m *controllerInitializer) Add(controllerName string, setup InitFunc, opts ...InitOption) error { |
| 95 | + m.lock.Lock() |
| 96 | + defer m.lock.Unlock() |
| 97 | + |
| 98 | + if m.all.Has(controllerName) { |
| 99 | + return fmt.Errorf("controller %q has already been registered", controllerName) |
| 100 | + } |
| 101 | + |
| 102 | + opt := &options{} |
| 103 | + for _, o := range opts { |
| 104 | + o.apply(opt) |
| 105 | + } |
| 106 | + |
| 107 | + m.all.Insert(controllerName) |
| 108 | + |
| 109 | + if opt.disableByDefault { |
| 110 | + m.disableByDefault.Insert(controllerName) |
| 111 | + } else { |
| 112 | + m.enabled.Insert(controllerName) |
| 113 | + } |
| 114 | + |
| 115 | + m.initializers[controllerName] = setup |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func (m *controllerInitializer) BindFlag(fs *pflag.FlagSet) { |
| 120 | + all := m.all.List() |
| 121 | + disabled := m.disableByDefault.List() |
| 122 | + fs.Var(m, "controllers", fmt.Sprintf(""+ |
| 123 | + "A list of controllers to enable. '*' enables all on-by-default controllers, 'foo' enables the controller "+ |
| 124 | + "named 'foo', '-foo' disables the controller named 'foo'.\nAll controllers: %s\nDisabled-by-default controllers: %s", |
| 125 | + strings.Join(all, ", "), strings.Join(disabled, ", "))) |
| 126 | +} |
| 127 | + |
| 128 | +// KnownControllers implements ControllerInitialzier. |
| 129 | +func (m *controllerInitializer) KnownControllers() []string { |
| 130 | + m.lock.RLock() |
| 131 | + defer m.lock.RUnlock() |
| 132 | + return m.all.List() |
| 133 | +} |
| 134 | + |
| 135 | +func (m *controllerInitializer) SetupWithManager(mgr manager.Manager) error { |
| 136 | + m.lock.RLock() |
| 137 | + defer m.lock.RUnlock() |
| 138 | + |
| 139 | + for _, name := range m.enabled.List() { |
| 140 | + _, err := m.initializers[name](mgr) |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("failed to initialize controller %q: %v", name, err) |
| 143 | + } |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func (m *controllerInitializer) Enabled(name string) bool { |
| 149 | + m.lock.RLock() |
| 150 | + defer m.lock.RUnlock() |
| 151 | + |
| 152 | + return m.enabled.Has(name) |
| 153 | +} |
| 154 | + |
| 155 | +func (m *controllerInitializer) isControllerEnabled(name string, controllers []string) bool { |
| 156 | + hasStar := false |
| 157 | + for _, ctrl := range controllers { |
| 158 | + if ctrl == name { |
| 159 | + return true |
| 160 | + } |
| 161 | + if ctrl == "-"+name { |
| 162 | + return false |
| 163 | + } |
| 164 | + if ctrl == "*" { |
| 165 | + hasStar = true |
| 166 | + } |
| 167 | + } |
| 168 | + // if we get here, there was no explicit choice |
| 169 | + if !hasStar { |
| 170 | + // nothing on by default |
| 171 | + return false |
| 172 | + } |
| 173 | + |
| 174 | + return !m.disableByDefault.Has(name) |
| 175 | +} |
| 176 | + |
| 177 | +// Set implements pflag.Value interface. |
| 178 | +func (m *controllerInitializer) Set(value string) error { |
| 179 | + m.lock.Lock() |
| 180 | + defer m.lock.Unlock() |
| 181 | + |
| 182 | + controllers := strings.Split(strings.TrimSpace(value), ",") |
| 183 | + all := m.all.List() |
| 184 | + for _, name := range all { |
| 185 | + if m.isControllerEnabled(name, controllers) { |
| 186 | + m.enabled.Insert(name) |
| 187 | + } else { |
| 188 | + m.enabled.Delete(name) |
| 189 | + } |
| 190 | + } |
| 191 | + return nil |
| 192 | +} |
| 193 | + |
| 194 | +// Type implements pflag.Value interface. |
| 195 | +func (m *controllerInitializer) Type() string { |
| 196 | + return "stringSlice" |
| 197 | +} |
| 198 | + |
| 199 | +// String implements pflag.Value interface. |
| 200 | +func (m *controllerInitializer) String() string { |
| 201 | + m.lock.RLock() |
| 202 | + defer m.lock.RUnlock() |
| 203 | + |
| 204 | + pairs := []string{} |
| 205 | + for _, name := range m.all.List() { |
| 206 | + if m.enabled.Has(name) { |
| 207 | + pairs = append(pairs, name) |
| 208 | + } else { |
| 209 | + pairs = append(pairs, "-"+name) |
| 210 | + } |
| 211 | + } |
| 212 | + return strings.Join(pairs, ",") |
| 213 | +} |
0 commit comments