-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SafeToEvict: Implement Eviction API, add SetEviction cloud product ho…
…ok (#2857) * SafeToEvict feature: Implement the `eviction.safe` API logic Towards #2794: This commit implements the defaulting/validation for the eviction.safe field, under the SafeToEvict feature gate. In particular, it enshrines the defaulting logic for the somewhat complicated backwards compatibility case (safe-to-evict=true is set but SafeToEvict is not). Along the way, refactor a very repetitive unit test table. * Ran gen-api-docs * Implement SafeToEvict enforcement of safe-to-evict annotations/label This commit adds the interpretation of GameServer.Status.Eviction: * Following the pattern in #2840, moves the enforcement of cloudproduct specific things up to the controller, adding a new cloudproduct SetEviction hook to handle it. * As a result, when the SafeToEvict feature flag is on, we disable annotation in gameserver.go and test that we don't set anything (as a change detection test). * Adds a generic and GKE Autopilot version of how to set policy from the eviction.safe status. In Autopilot, we skip using safe-to-evict=false.
- Loading branch information
Showing
9 changed files
with
1,975 additions
and
1,433 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
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,123 @@ | ||
// Copyright 2022 Google LLC All Rights Reserved. | ||
// | ||
// 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 v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"agones.dev/agones/pkg/util/runtime" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestSetEviction(t *testing.T) { | ||
runtime.FeatureTestMutex.Lock() | ||
defer runtime.FeatureTestMutex.Unlock() | ||
|
||
emptyPodAnd := func(f func(*corev1.Pod)) *corev1.Pod { | ||
pod := &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{}, | ||
Labels: map[string]string{}, | ||
}, | ||
} | ||
f(pod) | ||
return pod | ||
} | ||
for desc, tc := range map[string]struct { | ||
featureFlags string | ||
safeToEvict EvictionSafe | ||
pod *corev1.Pod | ||
wantPod *corev1.Pod | ||
}{ | ||
"SafeToEvict feature gate disabled => no change": { | ||
featureFlags: "SafeToEvict=false", | ||
// intentionally leave pod nil, it'll crash if anything's touched. | ||
}, | ||
"SafeToEvict: Always, no incoming labels/annotations": { | ||
featureFlags: "SafeToEvict=true", | ||
safeToEvict: EvictionSafeAlways, | ||
pod: emptyPodAnd(func(*corev1.Pod) {}), | ||
wantPod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = True | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = True | ||
}), | ||
}, | ||
"SafeToEvict: OnUpgrade, no incoming labels/annotations": { | ||
featureFlags: "SafeToEvict=true", | ||
safeToEvict: EvictionSafeOnUpgrade, | ||
pod: emptyPodAnd(func(*corev1.Pod) {}), | ||
wantPod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = False | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = True | ||
}), | ||
}, | ||
"SafeToEvict: Never, no incoming labels/annotations": { | ||
featureFlags: "SafeToEvict=true", | ||
safeToEvict: EvictionSafeNever, | ||
pod: emptyPodAnd(func(*corev1.Pod) {}), | ||
wantPod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = False | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = False | ||
}), | ||
}, | ||
"SafeToEvict: Always, incoming labels/annotations": { | ||
featureFlags: "SafeToEvict=true", | ||
safeToEvict: EvictionSafeAlways, | ||
pod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = "just don't touch, ok?" | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = "seriously, leave it" | ||
}), | ||
wantPod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = "just don't touch, ok?" | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = "seriously, leave it" | ||
}), | ||
}, | ||
"SafeToEvict: OnUpgrade, incoming labels/annotations": { | ||
featureFlags: "SafeToEvict=true", | ||
safeToEvict: EvictionSafeOnUpgrade, | ||
pod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = "better not touch" | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = "not another one" | ||
}), | ||
wantPod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = "better not touch" | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = "not another one" | ||
}), | ||
}, | ||
"SafeToEvict: Never, incoming labels/annotations": { | ||
featureFlags: "SafeToEvict=true", | ||
safeToEvict: EvictionSafeNever, | ||
pod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = "a passthrough" | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = "or is it passthru?" | ||
}), | ||
wantPod: emptyPodAnd(func(pod *corev1.Pod) { | ||
pod.ObjectMeta.Annotations[PodSafeToEvictAnnotation] = "a passthrough" | ||
pod.ObjectMeta.Labels[SafeToEvictLabel] = "or is it passthru?" | ||
}), | ||
}, | ||
} { | ||
t.Run(desc, func(t *testing.T) { | ||
err := runtime.ParseFeatures(tc.featureFlags) | ||
assert.NoError(t, err) | ||
|
||
err = (generic{}).SetEviction(tc.safeToEvict, tc.pod) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.wantPod, tc.pod) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.