Skip to content

Commit 00a99cb

Browse files
committed
pkg/types/featuregates: improve ergonomics
This commit removes error checking from the GenerateCustomFeatures function. There are two possible errors that are caught in this function, and both are already handled separately by validation. So continually checking for errors throughout the code makes using feature gate checks less ergonomic for no benefit. The two potential errors are: 1. Unknown feature set within FeatureGateFromFeatureSets, which is already validated here: https://github.com/openshift/installer/blob/b36d7c201e0114aa0e9261cf8ae5b8d68c74bf07/pkg/types/validation/installconfig.go#L1051 2. The other is that a custom feature gate was incorrectly entered, which is validated here: https://github.com/openshift/installer/blob/master/pkg/types/validation/installconfig.go#L1103
1 parent 9b07817 commit 00a99cb

File tree

4 files changed

+20
-40
lines changed

4 files changed

+20
-40
lines changed

pkg/asset/manifests/featuregate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ func (f *FeatureGate) Generate(dependencies asset.Parents) error {
6161
return errors.Errorf("custom features can only be used with the CustomNoUpgrade feature set")
6262
}
6363

64-
customFeatures, err := featuregates.GenerateCustomFeatures(installConfig.Config.FeatureGates)
65-
if err != nil {
66-
return errors.Wrapf(err, "failed to generate custom features")
67-
}
64+
customFeatures := featuregates.GenerateCustomFeatures(installConfig.Config.FeatureGates)
6865
f.Config.Spec.CustomNoUpgrade = customFeatures
6966
}
7067

pkg/types/featuregates/featuregates.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package featuregates
33
// source: https://github.com/openshift/cluster-config-operator/blob/636a2dc303037e2561a243ae1ab5c5b953ddad04/pkg/cmd/render/render.go#L153
44

55
import (
6-
"fmt"
76
"strconv"
87
"strings"
98

10-
"github.com/pkg/errors"
119
"k8s.io/apimachinery/pkg/util/sets"
1210

1311
configv1 "github.com/openshift/api/config/v1"
@@ -43,30 +41,24 @@ func completeFeatureGates(knownFeatureSets map[configv1.FeatureSet]*configv1.Fea
4341
}
4442

4543
// FeatureGateFromFeatureSets creates a FeatureGate from the active feature sets.
46-
func FeatureGateFromFeatureSets(knownFeatureSets map[configv1.FeatureSet]*configv1.FeatureGateEnabledDisabled, fs configv1.FeatureSet, customFS *configv1.CustomFeatureGates) (FeatureGate, error) {
44+
func FeatureGateFromFeatureSets(knownFeatureSets map[configv1.FeatureSet]*configv1.FeatureGateEnabledDisabled, fs configv1.FeatureSet, customFS *configv1.CustomFeatureGates) FeatureGate {
4745
if customFS != nil {
4846
completeEnabled, completeDisabled := completeFeatureGates(knownFeatureSets, customFS.Enabled, customFS.Disabled)
49-
return newFeatureGate(completeEnabled, completeDisabled), nil
47+
return newFeatureGate(completeEnabled, completeDisabled)
5048
}
5149

52-
featureSet, ok := knownFeatureSets[fs]
53-
if !ok {
54-
return nil, fmt.Errorf(".spec.featureSet %q not found", featureSet)
55-
}
50+
featureSet := knownFeatureSets[fs]
5651

5752
completeEnabled, completeDisabled := completeFeatureGates(knownFeatureSets, toFeatureGateNames(featureSet.Enabled), toFeatureGateNames(featureSet.Disabled))
58-
return newFeatureGate(completeEnabled, completeDisabled), nil
53+
return newFeatureGate(completeEnabled, completeDisabled)
5954
}
6055

6156
// GenerateCustomFeatures generates the custom feature gates from the install config.
62-
func GenerateCustomFeatures(features []string) (*configv1.CustomFeatureGates, error) {
57+
func GenerateCustomFeatures(features []string) *configv1.CustomFeatureGates {
6358
customFeatures := &configv1.CustomFeatureGates{}
6459

6560
for _, feature := range features {
66-
featureName, enabled, err := parseCustomFeatureGate(feature)
67-
if err != nil {
68-
return nil, errors.Wrapf(err, "failed to parse custom feature %s", feature)
69-
}
61+
featureName, enabled := parseCustomFeatureGate(feature)
7062

7163
if enabled {
7264
customFeatures.Enabled = append(customFeatures.Enabled, featureName)
@@ -75,27 +67,27 @@ func GenerateCustomFeatures(features []string) (*configv1.CustomFeatureGates, er
7567
}
7668
}
7769

78-
return customFeatures, nil
70+
return customFeatures
7971
}
8072

8173
// parseCustomFeatureGates parses the custom feature gate string into the feature name and whether it is enabled.
8274
// The expected format is <FeatureName>=<Enabled>.
83-
func parseCustomFeatureGate(rawFeature string) (configv1.FeatureGateName, bool, error) {
75+
func parseCustomFeatureGate(rawFeature string) (configv1.FeatureGateName, bool) {
8476
var featureName string
8577
var enabled bool
8678

8779
featureParts := strings.Split(rawFeature, "=")
8880
if len(featureParts) != 2 {
89-
return "", false, errors.Errorf("feature not in expected format %s", rawFeature)
81+
return "", false
9082
}
9183

9284
featureName = featureParts[0]
9385

9486
var err error
9587
enabled, err = strconv.ParseBool(featureParts[1])
9688
if err != nil {
97-
return "", false, errors.Wrapf(err, "feature not in expected format %s, could not parse boolean value", rawFeature)
89+
return "", false
9890
}
9991

100-
return configv1.FeatureGateName(featureName), enabled, nil
92+
return configv1.FeatureGateName(featureName), enabled
10193
}

pkg/types/installconfig.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -520,19 +520,14 @@ func (c *InstallConfig) WorkerMachinePool() *MachinePool {
520520

521521
// EnabledFeatureGates returns a FeatureGate that can be checked (using the Enabled function)
522522
// to determine if a feature gate is enabled in the current feature sets.
523-
func (c *InstallConfig) EnabledFeatureGates() (featuregates.FeatureGate, error) {
523+
func (c *InstallConfig) EnabledFeatureGates() featuregates.FeatureGate {
524524
var customFS *configv1.CustomFeatureGates
525-
var err error
525+
526526
if c.FeatureSet == configv1.CustomNoUpgrade {
527-
customFS, err = featuregates.GenerateCustomFeatures(c.FeatureGates)
528-
}
529-
if err != nil {
530-
return nil, fmt.Errorf("error generating feature set from custom features: %w", err)
527+
customFS = featuregates.GenerateCustomFeatures(c.FeatureGates)
531528
}
532529

533-
fg, err := featuregates.FeatureGateFromFeatureSets(configv1.FeatureSets, c.FeatureSet, customFS)
534-
if err != nil {
535-
return nil, fmt.Errorf("error generating feature gates from feature sets: %w", err)
536-
}
537-
return fg, nil
530+
fg := featuregates.FeatureGateFromFeatureSets(configv1.FeatureSets, c.FeatureSet, customFS)
531+
532+
return fg
538533
}

pkg/types/validation/installconfig.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,13 +1124,9 @@ func validateGatedFeatures(c *types.InstallConfig) field.ErrorList {
11241124
gatedFeatures = append(gatedFeatures, vspherevalidation.GatedFeatures(c)...)
11251125
}
11261126

1127-
fg, err := c.EnabledFeatureGates()
1128-
if err != nil {
1129-
errMsg := fmt.Errorf("error getting feature gates: %w", err)
1130-
return append(allErrs, field.InternalError(field.NewPath("featureSets"), errMsg))
1131-
}
1132-
1127+
fg := c.EnabledFeatureGates()
11331128
errMsgTemplate := "this field is protected by the %s feature gate which must be enabled through either the TechPreviewNoUpgrade or CustomNoUpgrade feature set"
1129+
11341130
fgCheck := func(c featuregates.GatedInstallConfigFeature) {
11351131
if !fg.Enabled(c.FeatureGateName) && c.Condition {
11361132
errMsg := fmt.Sprintf(errMsgTemplate, c.FeatureGateName)

0 commit comments

Comments
 (0)