Skip to content

Commit

Permalink
Add structure for feature flags
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
  • Loading branch information
ArthurSens committed May 16, 2024
1 parent f4b82d7 commit 6b1caac
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Documentation/operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Usage of ./operator:
Namespaces not to scope the interaction of the Prometheus Operator (deny list). This is mutually exclusive with --namespaces.
-enable-config-reloader-probes
Enable liveness and readiness for the config-reloader container. Default: false
-feature-gates value
Feature gates are a set of key=value pairs that describe Prometheus-Operator features. At the moment there are no feature gates available.
-key-file string
- NOT RECOMMENDED FOR PRODUCTION - Path to private TLS certificate file.
-kubelet-node-address-priority value
Expand Down
18 changes: 18 additions & 0 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
k8sflag "k8s.io/component-base/cli/flag"
"k8s.io/utils/ptr"

logging "github.com/prometheus-operator/prometheus-operator/internal/log"
"github.com/prometheus-operator/prometheus-operator/pkg/admission"
Expand Down Expand Up @@ -114,6 +116,8 @@ var (
kubeletObject string
kubeletSelector operator.LabelSelector
nodeAddressPriority operator.NodeAddressPriority

featureGates *k8sflag.MapStringBool
)

func parseFlags(fs *flag.FlagSet) {
Expand Down Expand Up @@ -166,6 +170,11 @@ func parseFlags(fs *flag.FlagSet) {
fs.Var(&cfg.ThanosRulerSelector, "thanos-ruler-instance-selector", "Label selector to filter ThanosRuler Custom Resources to watch.")
fs.Var(&cfg.SecretListWatchSelector, "secret-field-selector", "Field selector to filter Secrets to watch")

featureGates = k8sflag.NewMapStringBool(ptr.To(make(map[string]bool)))
fs.Var(featureGates, "feature-gates", "Feature gates are a set of key=value pairs that describe Prometheus-Operator features. At the moment there are no feature gates available.")
// Once the first feature gate is added, the line below should be uncommented and the line above deleted.
//fs.Var(featureGates, "feature-gates", fmt.Sprintf("Feature gates are a set of key=value pairs that describe Prometheus-Operator features. Available features: %q.", operator.AvailableFeatureGates()))

logging.RegisterFlags(fs, &logConfig)
versionutil.RegisterFlags(fs)

Expand Down Expand Up @@ -193,8 +202,17 @@ func run(fs *flag.FlagSet) int {
level.Warn(logger).Log("msg", "Failed to set GOMAXPROCS automatically", "err", err)
}

gates, err := operator.ValidateFeatureGates(featureGates)
if err != nil {
level.Error(logger).Log(
"msg", "error validating feature gates",
"error", err)
return 1
}

level.Info(logger).Log("msg", "Starting Prometheus Operator", "version", version.Info())
level.Info(logger).Log("build_context", version.BuildContext())
level.Info(logger).Log("feature_gates", gates)

if len(cfg.Namespaces.AllowList) > 0 && len(cfg.Namespaces.DenyList) > 0 {
level.Error(logger).Log(
Expand Down
63 changes: 63 additions & 0 deletions pkg/operator/feature_gates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 The prometheus-operator 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 operator

import (
"fmt"
"slices"
"strings"

k8sflag "k8s.io/component-base/cli/flag"
)

// At the moment, the are no feature gates available.
var defaultFeatureGates = map[string]bool{}

// ValidateFeatureGates merges the feature gate default values with
// the values provided by the user.
func ValidateFeatureGates(flags *k8sflag.MapStringBool) (string, error) {
gates := defaultFeatureGates
if flags.Empty() {
return mapToString(gates), nil
}

imgs := *flags.Map
for k, v := range imgs {
if _, ok := gates[k]; !ok {
return "", fmt.Errorf("feature gate %v is unknown", k)
}
gates[k] = v
}
return mapToString(gates), nil
}

func AvailableFeatureGates() []string {
i := 0
gates := make([]string, len(defaultFeatureGates))
for k := range defaultFeatureGates {
gates[i] = k
i++
}
slices.Sort(gates)
return gates
}

func mapToString(m map[string]bool) string {
var s []string
for k, v := range m {
s = append(s, fmt.Sprintf("%s=%t", k, v))
}
return strings.Join(s, ",")
}

0 comments on commit 6b1caac

Please sign in to comment.