From 99b672bcd2abc1afb18dafc88798f2e11631a473 Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Mon, 26 Dec 2016 10:30:32 -0500 Subject: [PATCH] Featureflag implementation --- pkg/featureflag/featureflag.go | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 pkg/featureflag/featureflag.go diff --git a/pkg/featureflag/featureflag.go b/pkg/featureflag/featureflag.go new file mode 100644 index 0000000000000..2cff14fbb34a6 --- /dev/null +++ b/pkg/featureflag/featureflag.go @@ -0,0 +1,66 @@ +/* +Copyright 2016 The Kubernetes 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 featureflag + +import ( + "os" + "strings" + "sync" +) + +var PreviewPrivateDNS = New("PreviewPrivateDNS") + +var flags = make(map[string]*FeatureFlag) +var flagsMutex sync.Mutex + +var initFlags sync.Once + +type FeatureFlag struct { + Key string + Enabled bool +} + +func readFlags() { + f := os.Getenv("KOPS_FEATURE_FLAGS") + f = strings.TrimSpace(f) + for _, s := range strings.Split(f, ",") { + s = strings.TrimSpace(s) + if s == "" { + continue + } + ff := New(s) + ff.Enabled = true + } + +} + +func New(key string) *FeatureFlag { + initFlags.Do(readFlags) + + flagsMutex.Lock() + defer flagsMutex.Unlock() + + f := flags[key] + if f == nil { + f = &FeatureFlag{ + Key: key, + } + flags[key] = f + } + + return f +}