Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use separate default feature gate #6

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions apis/apps/v1/petset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -67,14 +66,14 @@ func (obj *PetSet) Default() {
if obj.Spec.UpdateStrategy.RollingUpdate.Partition == nil {
obj.Spec.UpdateStrategy.RollingUpdate.Partition = ptr.To[int32](0)
}
if utilfeature.DefaultFeatureGate.Enabled(features.MaxUnavailablePetSet) {
if features.DefaultFeatureGate.Enabled(features.MaxUnavailablePetSet) {
if obj.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable == nil {
obj.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable = ptr.To(intstr.FromInt32(1))
}
}
}

if utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if obj.Spec.PersistentVolumeClaimRetentionPolicy == nil {
obj.Spec.PersistentVolumeClaimRetentionPolicy = &appsv1.StatefulSetPersistentVolumeClaimRetentionPolicy{}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
gomodules.xyz/pointer v0.1.0
gomodules.xyz/x v0.0.17
k8s.io/api v0.30.1
k8s.io/apiextensions-apiserver v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/apiserver v0.30.1
k8s.io/client-go v0.30.1
Expand Down Expand Up @@ -176,6 +175,7 @@ require (
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.30.1 // indirect
k8s.io/cli-runtime v0.30.1 // indirect
k8s.io/cloud-provider v0.30.1 // indirect
k8s.io/component-helpers v0.30.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmds/server/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
apiinformers "kubeops.dev/petset/client/informers/externalversions"
"kubeops.dev/petset/pkg/controller"
"kubeops.dev/petset/pkg/controller/petset"
"kubeops.dev/petset/pkg/features"

"github.com/spf13/pflag"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -64,6 +65,7 @@ func (s *OperatorOptions) AddFlags(fs *pflag.FlagSet) {
pfs := flag.NewFlagSet("extra-flags", flag.ExitOnError)
s.AddGoFlags(pfs)
fs.AddGoFlagSet(pfs)
features.DefaultMutableFeatureGate.AddFlag(fs)
}

func (s *OperatorOptions) ApplyTo(cfg *controller.OperatorConfig) error {
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -850,7 +849,7 @@ func (s ActivePodsWithRanks) Less(i, j int) bool {
}

// 4. lower pod-deletion-cost < higher pod-deletion cost
if utilfeature.DefaultFeatureGate.Enabled(features.PodDeletionCost) {
if features.DefaultFeatureGate.Enabled(features.PodDeletionCost) {
pi, _ := helper.GetDeletionCostFromPodAnnotations(s.Pods[i].Annotations)
pj, _ := helper.GetDeletionCostFromPodAnnotations(s.Pods[j].Annotations)
if pi != pj {
Expand All @@ -873,7 +872,7 @@ func (s ActivePodsWithRanks) Less(i, j int) bool {
readyTime1 := podReadyTime(s.Pods[i])
readyTime2 := podReadyTime(s.Pods[j])
if !readyTime1.Equal(readyTime2) {
if !utilfeature.DefaultFeatureGate.Enabled(features.LogarithmicScaleDown) {
if !features.DefaultFeatureGate.Enabled(features.LogarithmicScaleDown) {
return afterOrZero(readyTime1, readyTime2)
} else {
if s.Now.IsZero() || readyTime1.IsZero() || readyTime2.IsZero() {
Expand All @@ -893,7 +892,7 @@ func (s ActivePodsWithRanks) Less(i, j int) bool {
}
// 8. Empty creation time pods < newer pods < older pods
if !s.Pods[i].CreationTimestamp.Equal(&s.Pods[j].CreationTimestamp) {
if !utilfeature.DefaultFeatureGate.Enabled(features.LogarithmicScaleDown) {
if !features.DefaultFeatureGate.Enabled(features.LogarithmicScaleDown) {
return afterOrZero(&s.Pods[i].CreationTimestamp, &s.Pods[j].CreationTimestamp)
} else {
if s.Now.IsZero() || s.Pods[i].CreationTimestamp.IsZero() || s.Pods[j].CreationTimestamp.IsZero() {
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/petset/pet_pod_control.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check failure on line 1 in pkg/controller/petset/pet_pod_control.go

View workflow job for this annotation

GitHub Actions / Build

: # kubeops.dev/petset/pkg/controller/petset [kubeops.dev/petset/pkg/controller/petset.test]
Copyright 2016 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -31,7 +31,6 @@
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
errorutils "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
utilfeature "k8s.io/apiserver/pkg/util/feature"
clientset "k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -141,7 +140,7 @@
if apierrors.IsAlreadyExists(err) {
return err
}
if utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
// Set PVC policy as much as is possible at this point.
if err := spc.UpdatePodClaimForRetentionPolicy(ctx, set, pod); err != nil {
spc.recordPodEvent("update", set, pod, err)
Expand Down Expand Up @@ -172,7 +171,7 @@
return err
}
}
if utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
// if the Pod's PVCs are not consistent with the PetSet's PVC deletion policy, update the PVC
// and dirty the pod.
if match, err := spc.ClaimsMatchRetentionPolicy(ctx, set, pod); err != nil {
Expand Down Expand Up @@ -340,7 +339,7 @@
return err
}

if utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
// Set PVC policy as much as is possible at this point.
if err := spc.UpdatePodClaimForRetentionPolicy(ctx, set, pod); err != nil {
spc.recordPodEvent("update", set, pod, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/petset/pet_pod_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
func TestStatefulPodControlCreatesPods(t *testing.T) {
recorder := record.NewFakeRecorder(10)
set := newPetSet(3)
pod := newPetSetPod(set, 0)

Check failure on line 51 in pkg/controller/petset/pet_pod_control_test.go

View workflow job for this annotation

GitHub Actions / Build

not enough arguments in call to newPetSetPod
fakeClient := &fake.Clientset{}
claimIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
claimLister := corelisters.NewPersistentVolumeClaimLister(claimIndexer)
control := NewStatefulPodControl(fakeClient, nil, claimLister, recorder)

Check failure on line 55 in pkg/controller/petset/pet_pod_control_test.go

View workflow job for this annotation

GitHub Actions / Build

not enough arguments in call to NewStatefulPodControl
fakeClient.AddReactor("get", "persistentvolumeclaims", func(action core.Action) (bool, runtime.Object, error) {
return true, nil, apierrors.NewNotFound(action.GetResource().GroupResource(), action.GetResource().Resource)
})
Expand Down Expand Up @@ -82,7 +82,7 @@
func TestStatefulPodControlCreatePodExists(t *testing.T) {
recorder := record.NewFakeRecorder(10)
set := newPetSet(3)
pod := newPetSetPod(set, 0)

Check failure on line 85 in pkg/controller/petset/pet_pod_control_test.go

View workflow job for this annotation

GitHub Actions / Build

not enough arguments in call to newPetSetPod
fakeClient := &fake.Clientset{}
pvcs := getPersistentVolumeClaims(set, pod)
pvcIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
Expand Down Expand Up @@ -567,7 +567,7 @@
if err := control.UpdatePodClaimForRetentionPolicy(ctx, set, pod); err != nil {
t.Errorf("Unexpected error for UpdatePodClaimForRetentionPolicy (retain): %v", err)
}
expectRef := utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC)
expectRef := features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC)
for k := range claims {
claim, err := claimLister.PersistentVolumeClaims(claims[k].Namespace).Get(claims[k].Name)
if err != nil {
Expand Down Expand Up @@ -725,7 +725,7 @@
}
}
events := collectEvents(recorder.Events)
if utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if eventCount := len(events); eventCount != 1 {
t.Errorf("delete failed: got %d events, but want 1", eventCount)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/controller/petset/pet_set_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/integer"
Expand Down Expand Up @@ -424,7 +423,7 @@ func (ssc *defaultPetSetControl) processReplica(
}
// If we find a Pod that has not been created we create the Pod
if !isCreated(replicas[i]) {
if utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if isStale, err := ssc.podControl.PodClaimIsStale(set, replicas[i]); err != nil {
return true, err
} else if isStale {
Expand Down Expand Up @@ -479,7 +478,7 @@ func (ssc *defaultPetSetControl) processReplica(

// Enforce the PetSet invariants
retentionMatch := true
if utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
var err error
retentionMatch, err = ssc.podControl.ClaimsMatchRetentionPolicy(ctx, updateSet, replicas[i])
// An error is expected if the pod is not yet fully updated, and so return is treated as matching.
Expand Down Expand Up @@ -665,7 +664,7 @@ func (ssc *defaultPetSetControl) updatePetSet(
}

// Fix pod claims for condemned pods, if necessary.
if utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
fixPodClaim := func(i int) (bool, error) {
if matchPolicy, err := ssc.podControl.ClaimsMatchRetentionPolicy(ctx, updateSet, condemned[i]); err != nil {
return true, err
Expand Down Expand Up @@ -703,7 +702,7 @@ func (ssc *defaultPetSetControl) updatePetSet(
return &status, nil
}

if utilfeature.DefaultFeatureGate.Enabled(features.MaxUnavailablePetSet) {
if features.DefaultFeatureGate.Enabled(features.MaxUnavailablePetSet) {
return updatePetSetAfterInvariantEstablished(ctx,
ssc,
set,
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/petset/pet_set_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
t.Error("Failed to set UpdatedReplicas correctly")
}
// Check all pods have correct pod index label.
if utilfeature.DefaultFeatureGate.Enabled(features.PodIndexLabel) {
if features.DefaultFeatureGate.Enabled(features.PodIndexLabel) {
selector, err := metav1.LabelSelectorAsSelector(set.Spec.Selector)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -2713,7 +2713,7 @@
return om.podsLister.Pods(set.Namespace).List(selector)
}

var _ StatefulPodControlObjectManager = &fakeObjectManager{}

Check failure on line 2716 in pkg/controller/petset/pet_set_control_test.go

View workflow job for this annotation

GitHub Actions / Build

cannot use &fakeObjectManager{} (value of type *fakeObjectManager) as StatefulPodControlObjectManager value in variable declaration: *fakeObjectManager does not implement StatefulPodControlObjectManager (missing method GetPlacementPolicy)

type fakeStatefulSetStatusUpdater struct {
setsLister apilisters.PetSetLister
Expand Down Expand Up @@ -2871,7 +2871,7 @@
WhenScaled: apps.RetainPersistentVolumeClaimRetentionPolicyType,
WhenDeleted: apps.RetainPersistentVolumeClaimRetentionPolicyType,
}
if set.Spec.PersistentVolumeClaimRetentionPolicy != nil && utilfeature.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
if set.Spec.PersistentVolumeClaimRetentionPolicy != nil && features.DefaultFeatureGate.Enabled(features.PetSetAutoDeletePVC) {
policy = *set.Spec.PersistentVolumeClaimRetentionPolicy
}
claimShouldBeRetained := policy.WhenScaled == apps.RetainPersistentVolumeClaimRetentionPolicyType
Expand Down
5 changes: 2 additions & 3 deletions pkg/controller/petset/pet_set_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/strategicpatch"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -91,7 +90,7 @@ func getOrdinal(pod *v1.Pod) int {
// getStartOrdinal gets the first possible ordinal (inclusive).
// Returns spec.ordinals.start if spec.ordinals is set, otherwise returns 0.
func getStartOrdinal(set *api.PetSet) int {
if utilfeature.DefaultFeatureGate.Enabled(features.PetSetStartOrdinal) {
if features.DefaultFeatureGate.Enabled(features.PetSetStartOrdinal) {
if set.Spec.Ordinals != nil {
return int(set.Spec.Ordinals.Start)
}
Expand Down Expand Up @@ -399,7 +398,7 @@ func updateIdentity(set *api.PetSet, pod *v1.Pod) {
pod.Labels = make(map[string]string)
}
pod.Labels[apps.StatefulSetPodNameLabel] = pod.Name
if utilfeature.DefaultFeatureGate.Enabled(features.PodIndexLabel) {
if features.DefaultFeatureGate.Enabled(features.PodIndexLabel) {
pod.Labels[apps.PodIndexLabel] = strconv.Itoa(ordinal)
}
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/features/feature_gate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright AppsCode Inc. and Contributors.

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 features

import (
"k8s.io/component-base/featuregate"
)

var (
// DefaultMutableFeatureGate is a mutable version of DefaultFeatureGate.
// Only top-level commands/options setup and the k8s.io/component-base/featuregate/testing package should make use of this.
// Tests that need to modify feature gates for the duration of their test should use:
// defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)()
DefaultMutableFeatureGate featuregate.MutableFeatureGate = featuregate.NewFeatureGate()

// DefaultFeatureGate is a shared global FeatureGate.
// Top-level commands/options setup that needs to modify this feature gate should use DefaultMutableFeatureGate.
DefaultFeatureGate featuregate.FeatureGate = DefaultMutableFeatureGate
)
Loading
Loading