Skip to content

Commit

Permalink
Use Feature Gates to enable or disable features (#847)
Browse files Browse the repository at this point in the history
There are a number of features that are being developed and likely to be
disabled by default in their early stage.

Instead of adding a temporary config for each feature and maintaining
them separately, this patch introduces Feature Gates to toggle the
features. It will be easier to choose code branch based on FeatureGates'
"Enabled" method and to promote features to beta and GA.
  • Loading branch information
tnqn authored Jun 21, 2020
1 parent 2634d0d commit ff265f1
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 20 deletions.
12 changes: 9 additions & 3 deletions build/yamls/antrea-eks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ metadata:
apiVersion: v1
data:
antrea-agent.conf: |
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:
# Name of the OpenVSwitch bridge antrea-agent will create and use.
# Make sure it doesn't conflict with your existing OpenVSwitch bridges.
#ovsBridge: br-int
Expand Down Expand Up @@ -413,6 +416,9 @@ data:
]
}
antrea-controller.conf: |
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:
# The port for the antrea-controller APIServer to serve on.
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
Expand All @@ -432,7 +438,7 @@ metadata:
annotations: {}
labels:
app: antrea
name: antrea-config-hmd2mdhg89
name: antrea-config-2k49hdb86m
namespace: kube-system
---
apiVersion: v1
Expand Down Expand Up @@ -537,7 +543,7 @@ spec:
key: node-role.kubernetes.io/master
volumes:
- configMap:
name: antrea-config-hmd2mdhg89
name: antrea-config-2k49hdb86m
name: antrea-config
- name: antrea-controller-tls
secret:
Expand Down Expand Up @@ -751,7 +757,7 @@ spec:
operator: Exists
volumes:
- configMap:
name: antrea-config-hmd2mdhg89
name: antrea-config-2k49hdb86m
name: antrea-config
- hostPath:
path: /etc/cni/net.d
Expand Down
12 changes: 9 additions & 3 deletions build/yamls/antrea-gke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ metadata:
apiVersion: v1
data:
antrea-agent.conf: |
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:
# Name of the OpenVSwitch bridge antrea-agent will create and use.
# Make sure it doesn't conflict with your existing OpenVSwitch bridges.
#ovsBridge: br-int
Expand Down Expand Up @@ -413,6 +416,9 @@ data:
]
}
antrea-controller.conf: |
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:
# The port for the antrea-controller APIServer to serve on.
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
Expand All @@ -432,7 +438,7 @@ metadata:
annotations: {}
labels:
app: antrea
name: antrea-config-ff5ff2btgc
name: antrea-config-cfhgb2tt48
namespace: kube-system
---
apiVersion: v1
Expand Down Expand Up @@ -537,7 +543,7 @@ spec:
key: node-role.kubernetes.io/master
volumes:
- configMap:
name: antrea-config-ff5ff2btgc
name: antrea-config-cfhgb2tt48
name: antrea-config
- name: antrea-controller-tls
secret:
Expand Down Expand Up @@ -749,7 +755,7 @@ spec:
operator: Exists
volumes:
- configMap:
name: antrea-config-ff5ff2btgc
name: antrea-config-cfhgb2tt48
name: antrea-config
- hostPath:
path: /etc/cni/net.d
Expand Down
12 changes: 9 additions & 3 deletions build/yamls/antrea-ipsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ metadata:
apiVersion: v1
data:
antrea-agent.conf: |
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:
# Name of the OpenVSwitch bridge antrea-agent will create and use.
# Make sure it doesn't conflict with your existing OpenVSwitch bridges.
#ovsBridge: br-int
Expand Down Expand Up @@ -413,6 +416,9 @@ data:
]
}
antrea-controller.conf: |
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:
# The port for the antrea-controller APIServer to serve on.
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
Expand All @@ -432,7 +438,7 @@ metadata:
annotations: {}
labels:
app: antrea
name: antrea-config-fggkd66d2h
name: antrea-config-57kg4gbmk6
namespace: kube-system
---
apiVersion: v1
Expand Down Expand Up @@ -546,7 +552,7 @@ spec:
key: node-role.kubernetes.io/master
volumes:
- configMap:
name: antrea-config-fggkd66d2h
name: antrea-config-57kg4gbmk6
name: antrea-config
- name: antrea-controller-tls
secret:
Expand Down Expand Up @@ -793,7 +799,7 @@ spec:
operator: Exists
volumes:
- configMap:
name: antrea-config-fggkd66d2h
name: antrea-config-57kg4gbmk6
name: antrea-config
- hostPath:
path: /etc/cni/net.d
Expand Down
12 changes: 9 additions & 3 deletions build/yamls/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ metadata:
apiVersion: v1
data:
antrea-agent.conf: |
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:
# Name of the OpenVSwitch bridge antrea-agent will create and use.
# Make sure it doesn't conflict with your existing OpenVSwitch bridges.
#ovsBridge: br-int
Expand Down Expand Up @@ -413,6 +416,9 @@ data:
]
}
antrea-controller.conf: |
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:
# The port for the antrea-controller APIServer to serve on.
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
Expand All @@ -432,7 +438,7 @@ metadata:
annotations: {}
labels:
app: antrea
name: antrea-config-mf4t8c67c8
name: antrea-config-cd7tt4t2f8
namespace: kube-system
---
apiVersion: v1
Expand Down Expand Up @@ -537,7 +543,7 @@ spec:
key: node-role.kubernetes.io/master
volumes:
- configMap:
name: antrea-config-mf4t8c67c8
name: antrea-config-cd7tt4t2f8
name: antrea-config
- name: antrea-controller-tls
secret:
Expand Down Expand Up @@ -749,7 +755,7 @@ spec:
operator: Exists
volumes:
- configMap:
name: antrea-config-mf4t8c67c8
name: antrea-config-cd7tt4t2f8
name: antrea-config
- hostPath:
path: /etc/cni/net.d
Expand Down
3 changes: 3 additions & 0 deletions build/yamls/base/conf/antrea-agent.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:

# Name of the OpenVSwitch bridge antrea-agent will create and use.
# Make sure it doesn't conflict with your existing OpenVSwitch bridges.
#ovsBridge: br-int
Expand Down
3 changes: 3 additions & 0 deletions build/yamls/base/conf/antrea-controller.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# FeatureGates is a map of feature names to bools that enable or disable experimental features.
#featureGates:

# The port for the antrea-controller APIServer to serve on.
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
Expand Down
3 changes: 3 additions & 0 deletions cmd/antrea-agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
)

type AgentConfig struct {
// featureGates is a map of feature names to bools that enable or disable experimental features.
FeatureGates map[string]bool `yaml:"featureGates,omitempty"`

CNISocket string `yaml:"cniSocket,omitempty"`
// clientConnection specifies the kubeconfig file and client connection settings for the agent
// to communicate with the apiserver.
Expand Down
3 changes: 2 additions & 1 deletion cmd/antrea-agent/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/vmware-tanzu/antrea/pkg/agent/config"
"github.com/vmware-tanzu/antrea/pkg/apis"
"github.com/vmware-tanzu/antrea/pkg/cni"
"github.com/vmware-tanzu/antrea/pkg/features"
"github.com/vmware-tanzu/antrea/pkg/ovs/ovsconfig"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func (o *Options) complete(args []string) error {
o.config = c
}
o.setDefaults()
return nil
return features.DefaultMutableFeatureGate.SetFromMap(o.config.FeatureGates)
}

// validate validates all the required options. It must be called after complete.
Expand Down
6 changes: 2 additions & 4 deletions cmd/antrea-controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
)

type ControllerConfig struct {
// FeatureGates is a map of feature names to bools that enable or disable experimental features.
FeatureGates map[string]bool `yaml:"featureGates,omitempty"`
// clientConnection specifies the kubeconfig file and client connection settings for the
// antrea-controller to communicate with the Kubernetes apiserver.
ClientConnection componentbaseconfig.ClientConnectionConfiguration `yaml:"clientConnection"`
Expand All @@ -35,8 +37,4 @@ type ControllerConfig struct {
// tls.key: <TLS private key>
// Defaults to true.
SelfSignedCert bool `yaml:"selfSignedCert,omitempty"`
// Enable controller to watch for ClusterNetworkPolicy CRDs. Temporary config option
// to be removed once CRDs are considered stable.
// Defaults to false.
EnableSecurityCRDs bool `yaml:"enableSecurityCRDs,omitempty"`
}
5 changes: 3 additions & 2 deletions cmd/antrea-controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/vmware-tanzu/antrea/pkg/controller/networkpolicy"
"github.com/vmware-tanzu/antrea/pkg/controller/networkpolicy/store"
"github.com/vmware-tanzu/antrea/pkg/controller/querier"
"github.com/vmware-tanzu/antrea/pkg/features"
"github.com/vmware-tanzu/antrea/pkg/k8s"
"github.com/vmware-tanzu/antrea/pkg/monitor"
"github.com/vmware-tanzu/antrea/pkg/signals"
Expand Down Expand Up @@ -111,8 +112,8 @@ func run(o *Options) error {
stopCh := signals.RegisterSignalHandlers()

informerFactory.Start(stopCh)
// Only start watching Security CRDs when config option is set to true.
if o.config.EnableSecurityCRDs {
// Only start watching Security CRDs when ClusterNetworkPolicy is enabled.
if features.DefaultFeatureGate.Enabled(features.ClusterNetworkPolicy) {
crdInformerFactory.Start(stopCh)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/antrea-controller/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"gopkg.in/yaml.v2"

"github.com/vmware-tanzu/antrea/pkg/apis"
"github.com/vmware-tanzu/antrea/pkg/features"
)

type Options struct {
Expand Down Expand Up @@ -52,7 +53,7 @@ func (o *Options) complete(args []string) error {
o.config = c
}
o.setDefaults()
return nil
return features.DefaultMutableFeatureGate.SetFromMap(o.config.FeatureGates)
}

// validate validates all the required options.
Expand Down
52 changes: 52 additions & 0 deletions pkg/features/antrea_features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2020 Antrea 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 features

import (
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/component-base/featuregate"
)

const (
// Every feature gate should add constant here following this template:
//
// alpha: vX.Y
// beta: vX.Y
// MyFeature featuregate.Feature = "MyFeature"

// alpha: v0.8
// Allows to apply cluster-wide NetworkPolicies.
ClusterNetworkPolicy featuregate.Feature = "ClusterNetworkPolicy"
)

var (
// DefaultMutableFeatureGate is a mutable version of DefaultFeatureGate.
DefaultMutableFeatureGate featuregate.MutableFeatureGate = featuregate.NewFeatureGate()

// DefaultFeatureGate is a shared global FeatureGate.
// The feature gate should be modified via DefaultMutableFeatureGate.
DefaultFeatureGate featuregate.FeatureGate = DefaultMutableFeatureGate

// defaultAntreaFeatureGates consists of all known Antrea-specific feature keys.
// To add a new feature, define a key for it above and add it here. The features will be
// available throughout Antrea binaries.
defaultAntreaFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
ClusterNetworkPolicy: {Default: false, PreRelease: featuregate.Alpha},
}
)

func init() {
runtime.Must(DefaultMutableFeatureGate.Add(defaultAntreaFeatureGates))
}

0 comments on commit ff265f1

Please sign in to comment.