Skip to content

Commit 5655dcd

Browse files
Adding component config proposal
Signed-off-by: Chris Hein <[email protected]>
1 parent 95c0327 commit 5655dcd

File tree

2 files changed

+317
-0
lines changed

2 files changed

+317
-0
lines changed

designs/component-config.md

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
# ComponentConfig Controller Runtime Support
2+
Author: @christopherhein
3+
4+
Last Updated on: 03/02/2020
5+
6+
## Table of Contents
7+
8+
<!--ts-->
9+
* [ComponentConfig Controller Runtime Support](#componentconfig-controller-runtime-support)
10+
* [Table of Contents](#table-of-contents)
11+
* [Summary](#summary)
12+
* [Motivation](#motivation)
13+
* [Links to Open Issues](#links-to-open-issues)
14+
* [Goals](#goals)
15+
* [Non-Goals/Future Work](#non-goalsfuture-work)
16+
* [Proposal](#proposal)
17+
* [ComponentConfig Load Order](#componentconfig-load-order)
18+
* [Embeddable ComponentConfig Type](#embeddable-componentconfig-type)
19+
* [Default ComponentConfig Type](#default-componentconfig-type)
20+
* [Using Flags w/ ComponentConfig](#using-flags-w-componentconfig)
21+
* [Kubebuilder Scaffolding Example](#kubebuilder-scaffolding-example)
22+
* [User Stories](#user-stories)
23+
* [Controller Author with controller-runtime](#controller-author-with-controller-runtime)
24+
* [Controller Author with kubebuilder (tbd proposal for kubebuilder)](#controller-author-with-kubebuilder-tbd-proposal-for-kubebuilder)
25+
* [Controller User without modifications to config](#controller-user-without-modifications-to-config)
26+
* [Controller User with modifications to config](#controller-user-with-modifications-to-config)
27+
* [Risks and Mitigations](#risks-and-mitigations)
28+
* [Alternatives](#alternatives)
29+
* [Implementation History](#implementation-history)
30+
31+
<!--te-->
32+
33+
## Summary
34+
35+
Currently controllers that use `controller-runtime` need to configure the `ctrl.Manager` by using flags or hardcoding values into the initialization methods. Core Kubernetes has started to move away from using flags as a mechanism for configuring components and standardized on [`ComponentConfig` or Versioned Component Configuration Files](https://docs.google.com/document/d/1FdaEJUEh091qf5B98HM6_8MS764iXrxxigNIdwHYW9c/edit). This proposal is to bring `ComponentConfig` to `controller-runtime` to allow controller authors to make `go` types backed by `apimachinery` to unmarshal and configure the `ctrl.Manager{}` reducing the flags and allowing code based tools to easily configure controllers instead of requiring them to mutate CLI args.
36+
37+
## Motivation
38+
39+
This change is important because:
40+
- it will help make it easier for controllers to be configured by other machine processes
41+
- it will reduce the required flags required to start a controller
42+
- allow for configuration types which aren't natively supported by flags
43+
- allow using and upgrading older configurations avoiding breaking changes in flags
44+
45+
### Links to Open Issues
46+
47+
- [#518 Provide a ComponentConfig to tweak the Manager](https://github.com/kubernetes-sigs/controller-runtime/issues/518)
48+
- [#207 Reduce command line flag boilerplate](https://github.com/kubernetes-sigs/controller-runtime/issues/207)
49+
- [#722 Implement ComponentConfig by default & stop using (most) flags](https://github.com/kubernetes-sigs/kubebuilder/issues/722)
50+
51+
### Goals
52+
53+
- Provide an interface for pulling configuration data out of exposed `ComponentConfig` types (see below for implementation)
54+
- Provide a new `ctrl.NewFromComponentConfig()` function for initializing a manager
55+
- Provide an embeddable `ControllerConfiguration` type for easily authoring `ComponentConfig` types
56+
- Provide an `DefaultControllerConfig` to make the switching easier for clients
57+
58+
### Non-Goals/Future Work
59+
60+
- `kubebuilder` implementation and design in another PR
61+
- Changing the default `controller-runtime` implementation
62+
- Dynamically reloading `ComponentConfig` object
63+
- Providing `flags` interface and overrides
64+
65+
## Proposal
66+
67+
The `ctrl.Manager` _SHOULD_ support loading configurations from `ComponentConfig` like objects.
68+
An interface for that object with getters for the specific configuration parameters is created to bridge existing patterns.
69+
70+
Without breaking the current `ctrl.NewManager` which uses an exported `ctrl.Options{}` the `manager.go` can expose a new func, `NewFromComponentConfig()` this would be able to loop through the getters to hydrate an internal `ctrl.Options{}` and pass that into `New()`.
71+
72+
```golang
73+
//pkg/manager/manager.go
74+
75+
// ManagerConfiguration defines what the ComponentConfig object for ControllerRuntime needs to support
76+
type ManagerConfiguration interface {
77+
GetSyncPeriod() *time.Duration
78+
79+
GetLeaderElection() bool
80+
GetLeaderElectionNamespace() string
81+
GetLeaderElectionID() string
82+
83+
GetLeaseDuration() *time.Duration
84+
GetRenewDeadline() *time.Duration
85+
GetRetryPeriod() *time.Duration
86+
87+
GetNamespace() string
88+
GetMetricsBindAddress() string
89+
GetHealthProbeBindAddress() string
90+
91+
GetReadinessEndpointName() string
92+
GetLivenessEndpointName() string
93+
94+
GetPort() int
95+
GetHost() string
96+
97+
GetCertDir() string
98+
}
99+
100+
func NewFromComponentConfig(config *rest.Config, scheme *runtime.Scheme, filename string, managerconfig ManagerConfiguration) (Manager, error) {
101+
codecs := serializer.NewCodecFactory(scheme)
102+
if err := decodeComponentConfigFileInto(codecs, filename, componentconfig); err != nil {
103+
104+
}
105+
options := Options{}
106+
107+
if scheme != nil {
108+
options.Scheme = scheme
109+
}
110+
111+
// Loop through getters
112+
if managerconfig.GetLeaderElection() {
113+
managerconfig.GetLeaderElection()
114+
}
115+
// ...
116+
117+
return New(config, options)
118+
}
119+
```
120+
121+
#### ComponentConfig Load Order
122+
123+
![ComponentConfig Load Order](/designs/images/component-config-load.png)
124+
125+
#### Embeddable ComponentConfig Type
126+
127+
To make this easier for Controller authors `controller-runtime` can expose a set of `config.ControllerConfiguration` type that can be embedded similar to the way that `k8s.io/apimachinery/pkg/apis/meta/v1` works for `TypeMeta` and `ObjectMeta` these could live in `pkg/api/config/v1alpha1/types.go`. See the `DefaultComponentConfig` type below for and example implementation.
128+
129+
```golang
130+
// pkg/api/config/v1alpha1/types.go
131+
package v1alpha1
132+
133+
import (
134+
"time"
135+
136+
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
137+
)
138+
139+
// RuntimeConfig defines the embedded RuntimeConfiguration for controller-runtime clients.
140+
type RuntimeConfig struct {
141+
SyncPeriod *time.Duration `json:"syncPeriod,omitempty"`
142+
143+
LeaderElection configv1alpha1.LeaderElectionConfiguration `json:"leaderElection,omitempty"`
144+
145+
MetricsBindAddress string `json:"metricsBindAddress,omitempty"`
146+
147+
Health DefaultControllerConfigurationHealth `json:"health,omitempty"`
148+
149+
Port *int `json:"port,omitempty"`
150+
Host string `json:"host,omitempty"`
151+
152+
CertDir string `json:"certDir,omitempty"`
153+
}
154+
155+
// DefaultControllerConfigurationHealth defines the health configs
156+
type RuntimeConfigHealth struct {
157+
HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"`
158+
159+
ReadinessEndpointName string `json:"readinessEndpointName,omitempty"`
160+
LivenessEndpointName string `json:"livenessEndpointName,omitempty"`
161+
}
162+
```
163+
164+
165+
166+
#### Default ComponentConfig Type
167+
168+
To enable `controller-runtime` to have a default `ComponentConfig` struct which can be used instead of requiring each controller or extension to build it's own `ComponentConfig` type, we can create a `DefaultControllerConfiguration` type which can exist in `pkg/api/config/v1alpha1/types.go`. This will allow the controller authors to use this before needing to implement their own type with additional configs.
169+
170+
```golang
171+
// pkg/api/config/v1alpha1/types.go
172+
package v1alpha1
173+
174+
import (
175+
"time"
176+
177+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
178+
configv1alpha1 "sigs.k8s.io/controller-runtime/pkg/apis/config/v1alpha1"
179+
)
180+
181+
// DefaultControllerConfiguration is the Schema for the DefaultControllerConfigurations API
182+
type DefaultControllerConfiguration struct {
183+
metav1.TypeMeta `json:",inline"`
184+
185+
Spec configv1alpha1.ControllerConfiguration `json:"spec,omitempty"`
186+
}
187+
```
188+
189+
This would allow a controller author to use this struct with any config that supports the json/yaml structure. For example a controller author could define their `Kind` as `FoobarControllerConfiguration` and have it defined as the following.
190+
191+
```yaml
192+
# config.yaml
193+
apiVersion: somedomain.io/v1alpha1
194+
kind: FoobarControllerConfiguration
195+
spec:
196+
port: 9443
197+
metricsBindAddress: ":8080"
198+
leaderElection:
199+
leaderElect: false
200+
```
201+
202+
Given the following config and `DefaultControllerConfiguration` we'd be able to initialize the controller using the following.
203+
204+
205+
```golang
206+
mgr, err := ctrl.NewManagerFromComponentConfig(ctrl.GetConfigOrDie(), scheme, configname, &defaultv1alpha1.DefaultControllerConfiguration{})
207+
if err != nil {
208+
// ...
209+
}
210+
```
211+
212+
The above example uses `configname` which is the name of the file to load the configuration from and uses `scheme` to get the specific serializer, eg `serializer.NewCodecFactory(scheme)`. This will allow the configuration to be unmarshalled into the `runtime.Object` type and passed into the
213+
`ctrl.NewManagerFromComponentConfig()` as a `ManagerConfiguration` interface.
214+
215+
#### Using Flags w/ ComponentConfig
216+
217+
Since this design still requires setting up the initial `ComponentConfig` type and passing in a pointer to `ctrl.NewFromComponentConfig()` if you want to allow for the use of flags, your controller can use any of the different flagging interfaces. eg [`flag`](https://golang.org/pkg/flag/), [`pflag`](https://godoc.org/github.com/spf13/pflag), [`flagnum`](https://godoc.org/github.com/luci/luci-go/common/flag/flagenum) and set values on the `ComponentConfig` type prior to passing the pointer into the `ctrl.NewFromComponentConfig()`, example below.
218+
219+
```golang
220+
leaderElect := true
221+
222+
config := &defaultv1alpha1.DefaultControllerConfiguration{
223+
Spec: configv1alpha1.ControllerConfiguration{
224+
LeaderElection: configv1alpha1.LeaderElectionConfiguration{
225+
LeaderElect: &leaderElect,
226+
},
227+
},
228+
}
229+
mgr, err := ctrl.NewManagerFromComponentConfig(ctrl.GetConfigOrDie(), scheme, configname, config)
230+
if err != nil {
231+
// ...
232+
}
233+
```
234+
235+
#### Kubebuilder Scaffolding Example
236+
237+
Within expanded in a separate design _(link once created)_ this will allow controller authors to generate a type that implements the `ManagerConfiguration` interface. The following is a sample of what this looks like:
238+
239+
```golang
240+
package config
241+
242+
import (
243+
"time"
244+
245+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
246+
configv1alpha1 "sigs.k8s.io/controller-runtime/pkg/apis/config/v1alpha1"
247+
)
248+
249+
type ControllerNameConfigurationSpec struct {
250+
configv1alpha1.ControllerConfiguration `json:",inline"`
251+
}
252+
253+
type ControllerNameConfiguration struct {
254+
metav1.TypeMeta
255+
256+
Spec ControllerNameConfigurationSpec `json:"spec"`
257+
}
258+
```
259+
260+
Usage of this custom `ComponentConfig` type would require then changing the `ctrl.NewFromComponentConfig()` to use the new struct vs the `DefaultControllerConfiguration`.
261+
262+
## User Stories
263+
264+
### Controller Author with `controller-runtime` and default type
265+
266+
- Mount `ConfigMap`
267+
- Initialize `ctrl.Manager` with `NewFromComponentConfig` with config name and `DefaultControllerConfiguration` type
268+
- Build custom controller as usual
269+
270+
### Controller Author with `controller-runtime` and custom type
271+
272+
- Implement `ComponentConfig` type
273+
- Embed `ControllerConfiguration` type
274+
- Mount `ConfigMap`
275+
- Initialize `ctrl.Manager` with `NewFromComponentConfig` with config name and `ComponentConfig` type
276+
- Build custom controller as usual
277+
278+
### Controller Author with `kubebuilder` (tbd proposal for `kubebuilder`)
279+
280+
- Initialize `kubebuilder` project using `--component-config-name=XYZConfiguration`
281+
- Build custom controller as usual
282+
283+
### Controller User without modifications to config
284+
285+
_Provided that the controller provides manifests_
286+
287+
- Apply the controller to the cluster
288+
- Deploy custom resources
289+
290+
### Controller User with modifications to config
291+
292+
- _Following from previous example without changes_
293+
- Create a new `ConfigMap` for changes
294+
- Modify the `controller-runtime` pod to use the new `ConfigMap`
295+
- Apply the controller to the cluster
296+
- Deploy custom resources
297+
298+
299+
## Risks and Mitigations
300+
301+
- Given that this isn't changing the core Manager initialization for `controller-runtime` it's fairly low risk
302+
303+
## Alternatives
304+
305+
* `NewFromComponentConfig()` could load the object from disk based on the file name and hydrate the `ComponentConfig` type.
306+
307+
## Implementation History
308+
309+
- [x] 02/19/2020: Proposed idea in an issue or [community meeting]
310+
- [x] 02/24/2020: Proposal submitted to `controller-runtime`
311+
- [x] 03/02/2020: Updated with default `DefaultControllerConfiguration`
312+
- [x] 03/04/2020: Updated with embeddable `RuntimeConfig`
313+
- [x] 03/10/2020: Updated embeddable name to `ControllerConfiguration`
314+
315+
316+
<!-- Links -->
317+
[community meeting]: https://docs.google.com/document/d/1Ih-2cgg1bUrLwLVTB9tADlPcVdgnuMNBGbUl4D-0TIk
27.8 KB
Loading

0 commit comments

Comments
 (0)