Skip to content

Commit d74b31d

Browse files
adding defaultcontrollercomponentconfig design
Signed-off-by: Chris Hein <[email protected]>
1 parent 4ec6da5 commit d74b31d

File tree

1 file changed

+122
-44
lines changed

1 file changed

+122
-44
lines changed

designs/component-config.md

Lines changed: 122 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ComponentConfig Controller Runtime Support
2-
32
Author: @christopherhein
4-
Last Updated on: 02/24/2020
3+
4+
Last Updated on: 03/02/2020
55

66
## Table of Contents
77

@@ -15,46 +15,51 @@ Last Updated on: 02/24/2020
1515
* [Non-Goals/Future Work](#non-goalsfuture-work)
1616
* [Proposal](#proposal)
1717
* [ComponentConfig Load Order](#componentconfig-load-order)
18-
* [User Stories](#user-stories)
19-
* [Controller Author with controller-runtime](#controller-author-with-controller-runtime)
20-
* [Controller Author with kubebuilder (tbd proposal for kubebuilder)](#controller-author-with-kubebuilder-tbd-proposal-for-kubebuilder)
21-
* [Controller User without modifications to config](#controller-user-without-modifications-to-config)
22-
* [Controller User with modifications to config](#controller-user-with-modifications-to-config)
23-
* [Risks and Mitigations](#risks-and-mitigations)
18+
* [Default ComponentConfig Type](#default-componentconfig-type)
19+
* [Caveats](#caveats)
20+
* [Kubebuilder Scaffolding Example](#kubebuilder-scaffolding-example)
21+
* [User Stories](#user-stories)
22+
* [Controller Author with controller-runtime](#controller-author-with-controller-runtime)
23+
* [Controller Author with kubebuilder (tbd proposal for kubebuilder)](#controller-author-with-kubebuilder-tbd-proposal-for-kubebuilder)
24+
* [Controller User without modifications to config](#controller-user-without-modifications-to-config)
25+
* [Controller User with modifications to config](#controller-user-with-modifications-to-config)
26+
* [Risks and Mitigations](#risks-and-mitigations)
2427
* [Alternatives](#alternatives)
2528
* [Implementation History](#implementation-history)
2629

2730
<!--te-->
2831

2932
## Summary
3033

31-
Currently controllers that use `controller-runtime` need to configure the `ctrl.Manager` by using flags or hard coding values into the initialization methods. Core Kubernetes has started to move away from using flags as a mechanism for configuring components and standardized along the pattern of [`ComponentConfig` or Versioned Component Configuration Files](https://docs.google.com/document/d/1FdaEJUEh091qf5B98HM6_8MS764iXrxxigNIdwHYW9c/edit). This proposal is to bring `ComponentConfig` patterns into `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.
34+
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 along the pattern of [`ComponentConfig` or Versioned Component Configuration Files](https://docs.google.com/document/d/1FdaEJUEh091qf5B98HM6_8MS764iXrxxigNIdwHYW9c/edit). This proposal is to bring `ComponentConfig` patterns into `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.
3235

3336

3437
## Motivation
3538

3639
This change is important because:
3740
- it will help make it easier for controllers to be configured by other machine processes
38-
- it will reduce the upfront flags required to start a controller
41+
- it will reduce the required flags required to start a controller
3942
- allow for more configuration types which flags don't natively support
4043
- allow using and upgrading older configurations avoiding breaking changes in flags
4144

4245
### Links to Open Issues
4346

44-
- [Provide a ComponentConfig to tweak the Manager](https://github.com/kubernetes-sigs/controller-runtime/issues/518)
45-
- [Reduce command line flag boilerplate](https://github.com/kubernetes-sigs/controller-runtime/issues/207)
46-
- [Implement ComponentConfig by default & stop using (most) flags](https://github.com/kubernetes-sigs/kubebuilder/issues/722)
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)
4750

4851
### Goals
4952

5053
- Provide an interface for pulling configuration data out of exposed `ComponentConfig` types (see below for implementation)
5154
- Provide a new `ctrl.NewFromComponentConfig()` function for initializing a manager
55+
- Provide a `DefaultControllerConfig` to make the switch easier
5256

5357
### Non-Goals/Future Work
5458

5559
- `kubebuilder` implementation and design in another PR
5660
- Changing the default `controller-runtime` implementation
5761
- Dynamically reloading ComponentConfig object
62+
- Providing `flags` interface and overrides
5863

5964
## Proposal
6065

@@ -90,18 +95,22 @@ type ManagerConfiguration interface {
9095
GetCertDir() string
9196
}
9297

93-
func NewFromComponentConfig(config *rest.Config, scheme *runtime.Scheme, managerconfig ManagerConfiguration) (Manager, error) {
94-
options := Options{}
98+
func NewFromComponentConfig(config *rest.Config, scheme *runtime.Scheme,filename string, managerconfig ManagerConfiguration) (Manager, error) {
99+
codecs := serializer.NewCodecFactory(scheme)
100+
if err := decodeComponentConfigFileInto(codecs, filename, componentconfig); err != nil {
101+
102+
}
103+
options := Options{}
95104

96-
if scheme != nil {
97-
options.Scheme = scheme
98-
}
105+
if scheme != nil {
106+
options.Scheme = scheme
107+
}
99108

100-
// Loop through getters
101-
if managerconfig.GetLeaderElection() {
102-
managerconfig.GetLeaderElection()
103-
}
104-
// ...
109+
// Loop through getters
110+
if managerconfig.GetLeaderElection() {
111+
managerconfig.GetLeaderElection()
112+
}
113+
// ...
105114

106115
return New(config, options)
107116
}
@@ -111,6 +120,84 @@ func NewFromComponentConfig(config *rest.Config, scheme *runtime.Scheme, manager
111120

112121
![ComponentConfig Load Order](/designs/images/component-config-load.png)
113122

123+
#### Default ComponentConfig Type
124+
125+
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/v1alpha1/types.go`. This will allow the controller authors to use this before needing to implement their own type with additional configs.
126+
127+
```golang
128+
package v1alpha1
129+
130+
import (
131+
"time"
132+
133+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
134+
configv1alpha1 "sigs.k8s.io/component-base/config/v1alpha1"
135+
)
136+
137+
// DefaultControllerConfigurationSpec defines the desired state of DefaultControllerConfiguration
138+
type DefaultControllerConfigurationSpec struct {
139+
SyncPeriod *time.Duration `json:"syncPeriod,omitempty"`
140+
141+
LeaderElection configv1alpha1.LeaderElectionConfiguration `json:"leaderElection,omitempty"`
142+
143+
MetricsBindAddress string `json:"metricsBindAddress,omitempty"`
144+
145+
Health DefaultControllerConfigurationHealth `json:"health,omitempty"`
146+
147+
Port *int `json:"port,omitempty"`
148+
Host string `json:"host,omitempty"`
149+
150+
CertDir string `json:"certDir,omitempty"`
151+
}
152+
153+
// DefaultControllerConfigurationHealth defines the health configs
154+
type DefaultControllerConfigurationHealth struct {
155+
HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"`
156+
157+
ReadinessEndpointName string `json:"readinessEndpointName,omitempty"`
158+
LivenessEndpointName string `json:"livenessEndpointName,omitempty"`
159+
}
160+
161+
// DefaultControllerConfiguration is the Schema for the DefaultControllerConfigurations API
162+
type DefaultControllerConfiguration struct {
163+
metav1.TypeMeta `json:",inline"`
164+
165+
Spec DefaultControllerConfigurationSpec `json:"spec,omitempty"`
166+
}
167+
```
168+
169+
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.
170+
171+
```yaml
172+
# config.yaml
173+
apiVersion: somedomain.io/v1alpha1
174+
kind: FoobarControllerConfiguration
175+
spec:
176+
port: 9443
177+
metricsBindAddress: ":8080"
178+
leaderElection:
179+
leaderElect: false
180+
```
181+
182+
Given the following config and `DefaultControllerConfiguration` we'd be able to initialize the controller using the following.
183+
184+
185+
```golang
186+
mgr, err := ctrl.NewManagerFromComponentConfig(ctrl.GetConfigOrDie(), scheme, configname, &defaultv1alpha1.DefaultControllerConfiguration{})
187+
if err != nil {
188+
// ...
189+
}
190+
```
191+
192+
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
193+
`ctrl.NewManagerFromComponentConfig()` as a `ManagerConfiguration` interface.
194+
195+
##### Caveats
196+
197+
> ⚠️ Using `DecodeComponentConfigFileInto` does not support the overrides for flags, this is something that is left up to the controller author since they could be using many 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 `controller-runtime` should be agnostic to the CLI implementation.
198+
199+
#### Kubebuilder Scaffolding Example
200+
114201
Within a separate design (link once created) this will require controller authors to generate a type that implements the `ManagerConfiguration` interface. The following is a sample of what this looks like:
115202

116203
```golang
@@ -120,16 +207,13 @@ import (
120207
"time"
121208
122209
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
210+
configv1alpha1 "sigs.k8s.io/component-base/config/v1alpha1"
123211
)
124212
125213
type ControllerNameConfigurationSpec struct {
126-
LeaderElection ControllerNameConfigurationLeaderElection `json:"leaderElection,omitempty"`
127-
128-
LeaseDuration *time.Duration `json:"leaseDuration,omitempty"`
129-
RenewDeadline *time.Duration `json:"renewDeadline,omitempty"`
130-
RetryPeriod *time.Duration`json:"retryPeriod,omitempty"`
131-
132-
Namespace string `json:"namespace,omitempty"`
214+
SyncPeriod *time.Duration `json:"syncPeriod,omitempty"`
215+
216+
LeaderElection configv1alpha1.LeaderElectionConfiguration `json:"leaderElection,omitempty"`
133217

134218
MetricsBindAddress string `json:"metricsBindAddress,omitempty"`
135219

@@ -141,12 +225,6 @@ type ControllerNameConfigurationSpec struct {
141225
CertDir string `json:"certDir,omitempty"`
142226
}
143227

144-
type ControllerNameConfigurationLeaderElection struct {
145-
Enabled bool `json:"enabled,omitempty"`
146-
Namespace string `json:"namespace,omitempty"`
147-
ID string `json:"id,omitempty"`
148-
}
149-
150228
type ControllerNameConfigurationHealth struct {
151229
HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"`
152230

@@ -194,29 +272,29 @@ func (in *ControllerNameConfiguration) GetCertDir() string {}
194272

195273
Besides the implementation of the `ComponentConfig` The controller author as it stands would also need to implement the unmarshalling of the `ConfigMap` into the `ComponentConfig`, for this `controller-runtime` could expose helper methods to load a file from disk, unmarshal to the struct and pass the pointer into the `NewFromComponentConfig()` to return the `ctrl.Manager`
196274

197-
### User Stories
275+
## User Stories
198276

199-
#### Controller Author with `controller-runtime`
277+
### Controller Author with `controller-runtime`
200278

201279
- Implement `ComponentConfig` type
202280
- Implement `ManagerConfiguration` interface for `ComponentConfig` object
203281
- Set up `ConfigMap` unmarshalling into `ComponentConfig` type
204282
- Initialize `ctrl.Manager` with `NewFromComponentConfig`
205283
- Build custom controller as usual
206284

207-
#### Controller Author with `kubebuilder` (tbd proposal for `kubebuilder`)
285+
### Controller Author with `kubebuilder` (tbd proposal for `kubebuilder`)
208286

209287
- Initialize `kubebuilder` project using `--component-config-name=XYZConfiguration`
210288
- Build custom controller as usual
211289

212-
#### Controller User without modifications to config
290+
### Controller User without modifications to config
213291

214292
_Provided that the controller provides manifests_
215293

216294
- Apply the controller to the cluster
217295
- Deploy custom resources
218296

219-
#### Controller User with modifications to config
297+
### Controller User with modifications to config
220298

221299
- _Following from previous example without changes_
222300
- Create a new `ConfigMap` for changes
@@ -225,19 +303,19 @@ _Provided that the controller provides manifests_
225303
- Deploy custom resources
226304

227305

228-
### Risks and Mitigations
306+
## Risks and Mitigations
229307

230308
- Given that this isn't changing the core Manager initialization for `controller-runtime` it's fairly low risk
231-
- If the underlaying `ctrl.Options{}`
232309

233310
## Alternatives
234311

235-
* `NewFromComponentConfig()` could load the object from disk and hydrate the `ComponentConfig` type
312+
* `NewFromComponentConfig()` could load the object from disk based on the file name and hydrate the `ComponentConfig` type.
236313

237314
## Implementation History
238315

239316
- [x] 02/19/2020: Proposed idea in an issue or [community meeting]
240317
- [x] 02/24/2020: Proposal submitted to `controller-runtime`
318+
- [x] 03/02/2020: Updated with default `DefaultControllerConfiguration`
241319

242320

243321
<!-- Links -->

0 commit comments

Comments
 (0)