|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: 'Introducing Feature Gates to Client-Go: Enhancing Flexibility and Control' |
| 4 | +date: 2024-08-12 |
| 5 | +slug: feature-gates-in-client-go |
| 6 | +author: > |
| 7 | + Ben Luddy (Red Hat), |
| 8 | + Lukasz Szaszkiewicz (Red Hat) |
| 9 | +--- |
| 10 | + |
| 11 | +Kubernetes components use on-off switches called _feature gates_ to manage the risk of adding a new feature. |
| 12 | +The feature gate mechanism is what enables incremental graduation of a feature through the stages Alpha, Beta, and GA. |
| 13 | + |
| 14 | +Kubernetes components, such as kube-controller-manager and kube-scheduler, use the client-go library to interact with the API. |
| 15 | +The same library is used across the Kubernetes ecosystem to build controllers, tools, webhooks, and more. client-go now includes |
| 16 | +its own feature gating mechanism, giving developers and cluster administrators more control over how they adopt client features. |
| 17 | + |
| 18 | +To learn more about feature gates in Kubernetes, visit [Feature Gates](/docs/reference/command-line-tools-reference/feature-gates/). |
| 19 | + |
| 20 | +## Motivation |
| 21 | + |
| 22 | +In the absence of client-go feature gates, each new feature separated feature availability from enablement in its own way, if at all. |
| 23 | +Some features were enabled by updating to a newer version of client-go. Others needed to be actively configured in each program that used them. |
| 24 | +A few were configurable at runtime using environment variables. Consuming a feature-gated functionality exposed by the kube-apiserver sometimes |
| 25 | +required a client-side fallback mechanism to remain compatible with servers that don’t support the functionality due to their age or configuration. |
| 26 | +In cases where issues were discovered in these fallback mechanisms, mitigation required updating to a fixed version of client-go or rolling back. |
| 27 | + |
| 28 | +None of these approaches offer good support for enabling a feature by default in some, but not all, programs that consume client-go. |
| 29 | +Instead of enabling a new feature at first only for a single component, a change in the default setting immediately affects the default |
| 30 | +for all Kubernetes components, which broadens the blast radius significantly. |
| 31 | + |
| 32 | +## Feature gates in client-go |
| 33 | + |
| 34 | +To address these challenges, substantial client-go features will be phased in using the new feature gate mechanism. |
| 35 | +It will allow developers and users to enable or disable features in a way that will be familiar to anyone who has experience |
| 36 | +with feature gates in the Kubernetes components. |
| 37 | + |
| 38 | +Out of the box, simply by using a recent version of client-go, this offers several benefits. |
| 39 | + |
| 40 | +For people who use software built with client-go: |
| 41 | + |
| 42 | + |
| 43 | +* Early adopters can enable a default-off client-go feature on a per-process basis. |
| 44 | +* Misbehaving features can be disabled without building a new binary. |
| 45 | +* The state of all known client-go feature gates is logged, allowing users to inspect it. |
| 46 | + |
| 47 | +For people who develop software built with client-go: |
| 48 | + |
| 49 | +* By default, client-go feature gate overrides are read from environment variables. |
| 50 | + If a bug is found in a client-go feature, users will be able to disable it without waiting for a new release. |
| 51 | +* Developers can replace the default environment-variable-based overrides in a program to change defaults, |
| 52 | + read overrides from another source, or disable runtime overrides completely. |
| 53 | + The Kubernetes components use this customizability to integrate client-go feature gates with |
| 54 | + the existing `--feature-gates` command-line flag, feature enablement metrics, and logging. |
| 55 | + |
| 56 | +## Overriding client-go feature gates |
| 57 | + |
| 58 | +**Note**: This describes the default method for overriding client-go feature gates at runtime. |
| 59 | +It can be disabled or customized by the developer of a particular program. |
| 60 | +In Kubernetes components, client-go feature gate overrides are controlled by the `--feature-gates` flag. |
| 61 | + |
| 62 | +Features of client-go can be enabled or disabled by setting environment variables prefixed with `KUBE_FEATURE`. |
| 63 | +For example, to enable a feature named `MyFeature`, set the environment variable as follows: |
| 64 | + |
| 65 | +``` |
| 66 | + KUBE_FEATURE_MyFeature=true |
| 67 | +``` |
| 68 | + |
| 69 | +To disable the feature, set the environment variable to `false`: |
| 70 | + |
| 71 | +``` |
| 72 | + KUBE_FEATURE_MyFeature=false |
| 73 | +``` |
| 74 | + |
| 75 | +**Note**: Environment variables are case-sensitive on some operating systems. |
| 76 | +Therefore, `KUBE_FEATURE_MyFeature` and `KUBE_FEATURE_MYFEATURE` would be considered two different variables. |
| 77 | + |
| 78 | +## Customizing client-go feature gates |
| 79 | + |
| 80 | +The default environment-variable based mechanism for feature gate overrides can be sufficient for many programs in the Kubernetes ecosystem, |
| 81 | +and requires no special integration. Programs that require different behavior can replace it with their own custom feature gate provider. |
| 82 | +This allows a program to do things like force-disable a feature that is known to work poorly, |
| 83 | +read feature gates directly from a remote configuration service, or accept feature gate overrides through command-line options. |
| 84 | + |
| 85 | +The Kubernetes components replace client-go’s default feature gate provider with a shim to the existing Kubernetes feature gate provider. |
| 86 | +For all practical purposes, client-go feature gates are treated the same as other Kubernetes |
| 87 | +feature gates: they are wired to the `--feature-gates` command-line flag, included in feature enablement metrics, and logged on startup. |
| 88 | + |
| 89 | +To replace the default feature gate provider, implement the Gates interface and call ReplaceFeatureGates |
| 90 | +at package initialization time, as in this simple example: |
| 91 | + |
| 92 | +```go |
| 93 | +import ( |
| 94 | + “k8s.io/client-go/features” |
| 95 | +) |
| 96 | + |
| 97 | +type AlwaysEnabledGates struct{} |
| 98 | + |
| 99 | +func (AlwaysEnabledGates) Enabled(features.Feature) bool { |
| 100 | + return true |
| 101 | +} |
| 102 | + |
| 103 | +func init() { |
| 104 | + features.ReplaceFeatureGates(AlwaysEnabledGates{}) |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +Implementations that need the complete list of defined client-go features can get it by implementing the Registry interface |
| 109 | +and calling `AddFeaturesToExistingFeatureGates`. |
| 110 | +For a complete example, refer to [the usage within Kubernetes](https://github.com/kubernetes/kubernetes/blob/64ba17c605a41700f7f4c4e27dca3684b593b2b9/pkg/features/kube_features.go#L990-L997). |
| 111 | + |
| 112 | +## Summary |
| 113 | + |
| 114 | +With the introduction of feature gates in client-go v1.30, rolling out a new client-go feature has become safer and easier. |
| 115 | +Users and developers can control the pace of their own adoption of client-go features. |
| 116 | +The work of Kubernetes contributors is streamlined by having a common mechanism for graduating features that span both sides of the Kubernetes API boundary. |
| 117 | + |
| 118 | +Special shoutout to [@sttts](https://github.com/sttts) and [@deads2k](https://github.com/deads2k) for their help in shaping this feature. |
0 commit comments