forked from prometheus/node_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement: add feature gate support
Allow APIs to be gated conditionally. Signed-off-by: Pranshu Srivastava <rexagod@gmail.com>
- Loading branch information
Showing
6 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2024 The Prometheus 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 featuregate | ||
|
||
import ( | ||
"fmt" | ||
"github.com/alecthomas/kingpin/v2" | ||
"github.com/go-kit/log" | ||
"golang.org/x/mod/semver" | ||
"io" | ||
"os" | ||
) | ||
|
||
const versionFilePath = "../VERSION" | ||
|
||
var ( | ||
enabledDefaultFeatureGates = map[string]bool{} | ||
gotFeatureGates = kingpin.Flag("feature-gates", "A set of key=value pairs that describe enabled feature gates for experimental features.").Default("").StringMap() | ||
) | ||
|
||
type ( | ||
FeatureGate struct { | ||
Name string | ||
Desc string | ||
InitialAddingVersion string | ||
FinalRetiringVersion string | ||
logger log.Logger | ||
} | ||
) | ||
|
||
// NewFeatureGate creates a new feature gate, but errors if the feature gate is invalid. | ||
func NewFeatureGate(name, desc, initialAddingVersion, finalRetiringVersion string) *FeatureGate { | ||
return &FeatureGate{ | ||
Name: name, | ||
Desc: desc, | ||
InitialAddingVersion: semver.Canonical(initialAddingVersion), | ||
FinalRetiringVersion: semver.Canonical(finalRetiringVersion), | ||
logger: log.NewLogfmtLogger(os.Stdout), | ||
} | ||
} | ||
|
||
func (fg *FeatureGate) String() string { | ||
return fg.Name | ||
} | ||
|
||
// IsEnabled returns true if the feature gate is enabled, false if it is disabled. | ||
func (fg *FeatureGate) IsEnabled() (bool, error) { | ||
_, err := fg.isValid() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
v, err := currentVersion() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// Check if the feature gate is within the specified version range. | ||
// Note: Do not return immediately to allow overriding the specified range enablement. | ||
isFeatureGateWithinSpecifiedVersionRange := semver.Compare(fg.InitialAddingVersion, v) != 1 && | ||
semver.Compare(fg.FinalRetiringVersion, v) != -1 | ||
|
||
// Check if the feature gate is enabled or disabled by default. | ||
var isFeatureGateEnabledOrDisabledByDefault *bool | ||
_, isFeatureGateInDefaultFeatureGates := enabledDefaultFeatureGates[fg.Name] | ||
if isFeatureGateInDefaultFeatureGates { | ||
isFeatureGateEnabledOrDisabledByDefault = ptrTo(enabledDefaultFeatureGates[fg.Name]) | ||
} | ||
|
||
// Check if the feature gate is enabled or disabled by the user. | ||
var isFeatureGateEnabledOrDisabledByFlag *bool | ||
_, isFeatureGateInFlag := (*gotFeatureGates)[fg.Name] | ||
if isFeatureGateInFlag { | ||
t := (*gotFeatureGates)[fg.Name] | ||
// Check for exact values since none of these is the default value (nil). | ||
if t == "true" { | ||
isFeatureGateEnabledOrDisabledByFlag = ptrTo(true) | ||
} else if t == "false" { | ||
isFeatureGateEnabledOrDisabledByFlag = ptrTo(false) | ||
} | ||
} | ||
|
||
// Check for overrides by the user. This taken precedence over the default states and version range. | ||
featureGateState := isFeatureGateWithinSpecifiedVersionRange | ||
if isFeatureGateEnabledOrDisabledByDefault != nil { | ||
featureGateState = *isFeatureGateEnabledOrDisabledByDefault | ||
} | ||
if isFeatureGateEnabledOrDisabledByFlag != nil { | ||
if isFeatureGateEnabledOrDisabledByDefault != nil && | ||
*isFeatureGateEnabledOrDisabledByFlag != *isFeatureGateEnabledOrDisabledByDefault { | ||
fg.logger.Log(fg.Name, "default feature gate state overridden by user, this is not recommended") | ||
} | ||
featureGateState = *isFeatureGateEnabledOrDisabledByFlag | ||
} | ||
return featureGateState, nil | ||
} | ||
|
||
// isValid checks if the feature gate is valid. | ||
// Eventually, the current version will surpass the final retiring version, so we don't check for that. | ||
func (fg *FeatureGate) isValid() (*bool, error) { | ||
v, err := currentVersion() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !semver.IsValid(fg.InitialAddingVersion) { | ||
return ptrTo(false), fmt.Errorf("invalid adding version %q", fg.InitialAddingVersion) | ||
} | ||
if !semver.IsValid(fg.FinalRetiringVersion) { | ||
return ptrTo(false), fmt.Errorf("invalid retiring version %q", fg.FinalRetiringVersion) | ||
} | ||
if semver.Compare(fg.InitialAddingVersion, fg.FinalRetiringVersion) != -1 { | ||
return ptrTo(false), fmt.Errorf("adding version %q is not before the retiring version %q", fg.InitialAddingVersion, fg.FinalRetiringVersion) | ||
} | ||
if semver.Compare(fg.InitialAddingVersion, v) == 1 { | ||
return ptrTo(false), fmt.Errorf("adding version %q is greater than the current version %q", fg.InitialAddingVersion, v) | ||
} | ||
return ptrTo(true), nil | ||
} | ||
|
||
// currentVersion reads the current version from the VERSION file. | ||
func currentVersion() (string, error) { | ||
file, err := os.Open(versionFilePath) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer file.Close() | ||
version, err := io.ReadAll(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
return "v" + string(version[:len(version)-1]), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2024 The Prometheus 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 featuregate | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/mod/semver" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestFeatureGate(t *testing.T) { | ||
// Set a valid version range. | ||
cleanupFn, err := changeVersionTo("v1.8.1") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
err = cleanupFn() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
// Default enabled or disabled feature gates. | ||
enabledDefaultFeatureGates = map[string]bool{ | ||
"foo": true, | ||
"bar": false, | ||
"baz": false, | ||
} | ||
|
||
// User enabled or disabled feature gates. | ||
gotFeatureGates = &map[string]string{ | ||
"foo": "false", | ||
"bar": "true", | ||
"que": "false", | ||
} | ||
|
||
// Check feature gate enablement under various parameters. | ||
testcases := []struct { | ||
name string | ||
fg *FeatureGate | ||
enabled bool | ||
shouldError bool | ||
}{ | ||
{ | ||
name: "feature gate 'foo', should be disabled due to user override", | ||
fg: NewFeatureGate("TestFoo", "", "v1.8.0", "v1.8.2"), | ||
}, | ||
{ | ||
name: "feature gate 'bar', should be enabled due to user override", | ||
fg: NewFeatureGate("TestBar", "", "v1.8.0", "v1.8.2"), | ||
shouldError: true, | ||
}, | ||
{ | ||
name: "feature gate 'baz', should be enabled by default", | ||
fg: NewFeatureGate("TestBaz", "", "v1.8.0", "v1.8.2"), | ||
shouldError: true, | ||
}, | ||
{ | ||
name: "feature gate 'que', should be disabled due to user override", | ||
fg: NewFeatureGate("TestQue", "", "v1.8.0", "v1.8.2"), | ||
}, | ||
{ | ||
name: "feature gate 'qux', should be enabled due to valid version range", | ||
fg: NewFeatureGate("TestQux", "", "v1.8.0", "v1.8.2"), | ||
enabled: true, | ||
}, | ||
{ | ||
name: "feature gate 'qux', should be disabled due to invalid version range", | ||
fg: NewFeatureGate("TestQux", "", "v1", "v1.8.0"), | ||
shouldError: true, | ||
}, | ||
{ | ||
name: "feature gate 'qux', should be disabled due to invalid version range", | ||
fg: NewFeatureGate("TestQux", "", "v1.8.2", "v2"), | ||
shouldError: true, | ||
}, | ||
{ | ||
name: "feature gate 'qux', should be disabled due to invalid version range", | ||
fg: NewFeatureGate("TestQux", "", "v1.8.2", "v1.8.0"), | ||
shouldError: true, | ||
}, | ||
{ | ||
name: "feature gate 'qux', should be disabled due to invalid version range", | ||
fg: NewFeatureGate("TestQux", "", "1.8.0", "v1.8.2"), | ||
shouldError: true, | ||
}, | ||
{ | ||
name: "feature gate 'qux', should be disabled due to invalid version range", | ||
fg: NewFeatureGate("TestQux", "", "v1.8.0", "1.8.2"), | ||
shouldError: true, | ||
}, | ||
} | ||
for _, testcase := range testcases { | ||
t.Run(testcase.name, func(t *testing.T) { | ||
got, err := testcase.fg.IsEnabled() | ||
if err != nil && !testcase.shouldError { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if got != testcase.enabled { | ||
t.Fatalf("expected %v, got %v", testcase.enabled, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func changeVersionTo(version string) (func() error, error) { | ||
if isValid := semver.IsValid(version); !isValid { | ||
return nil, fmt.Errorf("invalid version: %v", version) | ||
} | ||
v, err := currentVersion() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if isValid := semver.IsValid(v); !isValid { | ||
return nil, fmt.Errorf("invalid version: %v", v) | ||
} | ||
cleanupFn := func() error { | ||
err := os.WriteFile(versionFilePath, []byte(v + "\n")[1:], 0644) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
err = os.WriteFile(versionFilePath, []byte(version + "\n")[1:], 0644) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cleanupFn, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package featuregate | ||
|
||
func ptrTo[T any](v T) *T { | ||
return &v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters