-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to use feature-gates in the operator (#1619)
* Add ability to use feature-gates in the operator * Add changelog * Apply feedback
- Loading branch information
1 parent
3a334af
commit fce73df
Showing
6 changed files
with
148 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action) | ||
component: operator | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add ability to use feature gates in the operator | ||
|
||
# One or more tracking issues related to the change | ||
issues: [1619] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: |
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
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,33 @@ | ||
// Copyright The OpenTelemetry 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 ( | ||
"flag" | ||
|
||
"go.opentelemetry.io/collector/featuregate" | ||
) | ||
|
||
const ( | ||
featureGatesFlag = "feature-gates" | ||
) | ||
|
||
// Flags creates a new FlagSet that represents the available featuregate flags using the supplied featuregate registry. | ||
func Flags(reg *featuregate.Registry) *flag.FlagSet { | ||
flagSet := new(flag.FlagSet) | ||
flagSet.Var(featuregate.NewFlag(reg), featureGatesFlag, | ||
"Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature.") | ||
return flagSet | ||
} |
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,89 @@ | ||
// Copyright The OpenTelemetry 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 ( | ||
"testing" | ||
|
||
"go.opentelemetry.io/collector/featuregate" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
basicGate = "basic" | ||
advancedGate = "advanced" | ||
falseGate = "false" | ||
) | ||
|
||
func TestSetFlag(t *testing.T) { | ||
featuregate.GlobalRegistry().MustRegister(basicGate, featuregate.StageAlpha) | ||
featuregate.GlobalRegistry().MustRegister(advancedGate, featuregate.StageBeta) | ||
featuregate.GlobalRegistry().MustRegister(falseGate, featuregate.StageStable, featuregate.WithRegisterRemovalVersion("v0.0.1")) | ||
tests := []struct { | ||
name string | ||
args []string | ||
expectedTrue []string | ||
expectedFalse []string | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "simple set", | ||
args: []string{"--feature-gates=basic"}, | ||
expectedTrue: []string{basicGate}, | ||
}, | ||
{ | ||
name: "two flags", | ||
args: []string{"--feature-gates=basic,advanced"}, | ||
expectedTrue: []string{basicGate, advancedGate}, | ||
}, | ||
{ | ||
name: "one true one false", | ||
args: []string{"--feature-gates=basic,-advanced"}, | ||
expectedTrue: []string{basicGate}, | ||
expectedFalse: []string{advancedGate}, | ||
}, | ||
{ | ||
name: "invalid set", | ||
args: []string{"--feature-gates=01"}, | ||
expectedErr: `no such feature gate -01`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
flgs := Flags(featuregate.GlobalRegistry()) | ||
err := flgs.Parse(tt.args) | ||
if tt.expectedErr != "" { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
featuregate.GlobalRegistry().VisitAll(func(gate *featuregate.Gate) { | ||
for _, id := range tt.expectedTrue { | ||
if gate.ID() == id { | ||
assert.True(t, gate.IsEnabled()) | ||
} | ||
} | ||
for _, id := range tt.expectedFalse { | ||
if gate.ID() == id { | ||
assert.False(t, gate.IsEnabled()) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
} |