Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Add feature flag support #339

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/controller/app/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func buildControllerContext(opts *options.ControllerOptions) (*controllers.Conte
KubeSharedInformerFactory: kubeSharedInformerFactory,
SharedInformerFactory: sharedInformerFactory,
Namespace: opts.Namespace,
Features: opts.Features,
}, kubeCfg, nil
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/controller/app/options/options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package options

import (
"strings"
"time"

"github.com/spf13/pflag"

"github.com/jetstack/navigator/pkg/util/features"
)

type ControllerOptions struct {
Expand All @@ -15,6 +18,9 @@ type ControllerOptions struct {
LeaderElectionLeaseDuration time.Duration
LeaderElectionRenewDeadline time.Duration
LeaderElectionRetryPeriod time.Duration

FeatureGatesString string
Features map[string]bool
}

const (
Expand All @@ -26,6 +32,8 @@ const (
defaultLeaderElectionLeaseDuration = 15 * time.Second
defaultLeaderElectionRenewDeadline = 10 * time.Second
defaultLeaderElectionRetryPeriod = 2 * time.Second

defaultFeatureGatesString = ""
)

func NewControllerOptions() *ControllerOptions {
Expand All @@ -37,6 +45,8 @@ func NewControllerOptions() *ControllerOptions {
LeaderElectionLeaseDuration: defaultLeaderElectionLeaseDuration,
LeaderElectionRenewDeadline: defaultLeaderElectionRenewDeadline,
LeaderElectionRetryPeriod: defaultLeaderElectionRetryPeriod,
FeatureGatesString: defaultFeatureGatesString,
Features: map[string]bool{},
}
}

Expand Down Expand Up @@ -66,6 +76,8 @@ func (s *ControllerOptions) AddFlags(fs *pflag.FlagSet) {
fs.DurationVar(&s.LeaderElectionRetryPeriod, "leader-election-retry-period", defaultLeaderElectionRetryPeriod, ""+
"The duration the clients should wait between attempting acquisition and renewal "+
"of a leadership. This is only applicable if leader election is enabled.")
fs.StringVar(&s.FeatureGatesString, "feature-gates", defaultFeatureGatesString, "A set of key=value pairs that describe feature gates for various features. "+
"Options are:\n"+strings.Join(features.KnownFeatures(&features.InitFeatureGates), "\n"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comma or space separated?

}

func (o *ControllerOptions) Validate() error {
Expand Down
6 changes: 6 additions & 0 deletions cmd/controller/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jetstack/navigator/cmd/controller/app/options"
_ "github.com/jetstack/navigator/pkg/controllers/cassandra"
_ "github.com/jetstack/navigator/pkg/controllers/elasticsearch"
"github.com/jetstack/navigator/pkg/util/features"
)

type NavigatorControllerOptions struct {
Expand Down Expand Up @@ -64,6 +65,11 @@ to renew certificates at an appropriate time before expiry.`,
func (o NavigatorControllerOptions) Validate(args []string) error {
errors := []error{}
errors = append(errors, o.ControllerOptions.Validate())

f, err := features.NewFeatureGate(&features.InitFeatureGates, o.ControllerOptions.FeatureGatesString)
errors = append(errors, err)
o.ControllerOptions.Features = f

return utilerrors.NewAggregate(errors)
}

Expand Down
3 changes: 3 additions & 0 deletions contrib/charts/navigator/templates/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ spec:
- navigator-controller
{{- if .Values.controller.namespace }}
- --namespace={{ .Values.controller.namespace }}
{{- end }}
{{- if .Values.featureGates }}
- --feature-gates={{ .Values.featureGates }}
{{- end }}
- --leader-election-namespace={{ .Release.Namespace }}
- --v={{ .Values.controller.logLevel }}
Expand Down
3 changes: 3 additions & 0 deletions contrib/charts/navigator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ createAPIService: true
rbac:
enabled: true

# A set of key=value pairs that describe feature gates for various features. Use --help to see available flags.
featureGates: ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comma or space separated?


apiserver:
## Set to true to skip deploying the apiserver components RBAC policies,
## which require cluster admin access to deploy.
Expand Down
9 changes: 9 additions & 0 deletions pkg/controllers/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/jetstack/navigator/pkg/controllers/cassandra/seedlabeller"
"github.com/jetstack/navigator/pkg/controllers/cassandra/service"
"github.com/jetstack/navigator/pkg/controllers/cassandra/serviceaccount"
"github.com/jetstack/navigator/pkg/util/features"
)

// NewCassandra returns a new CassandraController that can be used
Expand All @@ -55,6 +56,7 @@ type CassandraController struct {
roleBindingsListerSynced cache.InformerSynced
queue workqueue.RateLimitingInterface
recorder record.EventRecorder
features map[string]bool
}

func NewCassandra(
Expand All @@ -69,6 +71,7 @@ func NewCassandra(
roles rbacinformers.RoleInformer,
roleBindings rbacinformers.RoleBindingInformer,
recorder record.EventRecorder,
features map[string]bool,
) *CassandraController {
queue := workqueue.NewNamedRateLimitingQueue(
workqueue.DefaultControllerRateLimiter(),
Expand All @@ -79,6 +82,7 @@ func NewCassandra(
navigatorClient: naviClient,
queue: queue,
recorder: recorder,
features: features,
}
cassClusters.Informer().AddEventHandler(
&controllers.QueuingEventHandler{Queue: queue},
Expand Down Expand Up @@ -166,6 +170,10 @@ func NewCassandra(
func (e *CassandraController) Run(workers int, stopCh <-chan struct{}) error {
glog.Infof("Starting Cassandra controller")

if features.Enabled(e.features, features.Example) {
glog.Infof("Example feature flag enabled")
}

if !cache.WaitForCacheSync(
stopCh,
e.cassListerSynced,
Expand Down Expand Up @@ -333,6 +341,7 @@ func CassandraControllerFromContext(ctx *controllers.Context) *CassandraControll
ctx.KubeSharedInformerFactory.Rbac().V1beta1().Roles(),
ctx.KubeSharedInformerFactory.Rbac().V1beta1().RoleBindings(),
ctx.Recorder,
ctx.Features,
)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ type Context struct {
KubeSharedInformerFactory kubeinformers.SharedInformerFactory
SharedInformerFactory intinformers.SharedInformerFactory

Features map[string]bool

Namespace string
}
5 changes: 5 additions & 0 deletions pkg/controllers/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type ElasticsearchController struct {
queue workqueue.RateLimitingInterface
elasticsearchClusterControl ControlInterface
recorder record.EventRecorder

features map[string]bool
}

// NewElasticsearch returns a new ElasticsearchController that can be used
Expand All @@ -89,6 +91,7 @@ func NewElasticsearch(
cl kubernetes.Interface,
navigatorCl clientset.Interface,
recorder record.EventRecorder,
features map[string]bool,
) *ElasticsearchController {
queue := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "elasticsearchCluster")
// create a new ElasticsearchController to manage ElasticsearchCluster resources
Expand All @@ -97,6 +100,7 @@ func NewElasticsearch(
navigatorClient: navigatorCl,
queue: queue,
recorder: recorder,
features: features,
}

// add an event handler to the ElasticsearchCluster informer
Expand Down Expand Up @@ -352,6 +356,7 @@ func init() {
ctx.Client,
ctx.NavigatorClient,
ctx.Recorder,
ctx.Features,
)

return e.Run
Expand Down
Loading