Skip to content

Commit d79f404

Browse files
zmbergfurykerry
authored andcommitted
support pub pub.kruise.io/disable-fetch-replicas-from-workload=true
Signed-off-by: liheng.zms <[email protected]>
1 parent 450dc5e commit d79f404

File tree

5 files changed

+130
-15
lines changed

5 files changed

+130
-15
lines changed

apis/policy/v1alpha1/podunavailablebudget_types.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ const (
3535
PubUpdateOperation PubOperation = "UPDATE"
3636
PubDeleteOperation PubOperation = "DELETE"
3737
PubEvictOperation PubOperation = "EVICT"
38-
// PubProtectTotalReplicas indicates the pub protected total replicas, rather than workload.spec.replicas.
39-
// and must be used with pub.spec.selector.
40-
PubProtectTotalReplicas = "pub.kruise.io/protect-total-replicas"
38+
// PubProtectTotalReplicasAnnotation is the target replicas.
39+
// By default, PUB will get the target replicas through workload.spec.replicas. but there are some scenarios that may workload doesn't
40+
// implement scale subresources or Pod doesn't have workload management. In this scenario, you can set pub.kruise.io/protect-total-replicas
41+
// in pub annotations to get the target replicas to realize the same effect of protection ability.
42+
PubProtectTotalReplicasAnnotation = "pub.kruise.io/protect-total-replicas"
4143
// Marked the pod will not be pub-protected, solving the scenario of force pod deletion
4244
PodPubNoProtectionAnnotation = "pub.kruise.io/no-protect"
4345
)

pkg/control/pubcontrol/pub_control.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func (c *commonControl) GetPodsForPub(pub *policyv1alpha1.PodUnavailableBudget)
7878
if pub.Spec.TargetReference != nil {
7979
ref := pub.Spec.TargetReference
8080
matchedPods, expectedCount, err := c.controllerFinder.GetPodsForRef(ref.APIVersion, ref.Kind, pub.Namespace, ref.Name, true)
81+
if value, _ := pub.Annotations[policyv1alpha1.PubProtectTotalReplicasAnnotation]; value != "" {
82+
count, _ := strconv.ParseInt(value, 10, 32)
83+
expectedCount = int32(count)
84+
}
8185
return matchedPods, expectedCount, err
8286
} else if pub.Spec.Selector == nil {
8387
klog.InfoS("Pub spec.Selector could not be empty", "pub", klog.KObj(pub))
@@ -101,14 +105,14 @@ func (c *commonControl) GetPodsForPub(pub *policyv1alpha1.PodUnavailableBudget)
101105
matchedPods = append(matchedPods, pod)
102106
}
103107
}
108+
if value, _ := pub.Annotations[policyv1alpha1.PubProtectTotalReplicasAnnotation]; value != "" {
109+
expectedCount, _ := strconv.ParseInt(value, 10, 32)
110+
return matchedPods, int32(expectedCount), nil
111+
}
104112
expectedCount, err := c.controllerFinder.GetExpectedScaleForPods(matchedPods)
105113
if err != nil {
106114
return nil, 0, err
107115
}
108-
if expectedCount == 0 && pub.Annotations[policyv1alpha1.PubProtectTotalReplicas] != "" {
109-
expectedCount, _ := strconv.ParseInt(pub.Annotations[policyv1alpha1.PubProtectTotalReplicas], 10, 32)
110-
return matchedPods, int32(expectedCount), nil
111-
}
112116
return matchedPods, expectedCount, nil
113117
}
114118

pkg/controller/podunavailablebudget/pub_controller_test.go

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,67 @@ func TestPubReconcile(t *testing.T) {
246246
}
247247
},
248248
},
249+
{
250+
name: "select matched pub.annotations[pub.kruise.io/protect-total-replicas]=15 and selector, selector and maxUnavailable 30%",
251+
getPods: func(rs ...*apps.ReplicaSet) []*corev1.Pod {
252+
var matchedPods []*corev1.Pod
253+
for i := 0; int32(i) < 5; i++ {
254+
pod := podDemo.DeepCopy()
255+
pod.OwnerReferences = []metav1.OwnerReference{
256+
{
257+
APIVersion: "apps/v1",
258+
Kind: "ReplicaSet",
259+
Name: rs[0].Name,
260+
UID: rs[0].UID,
261+
Controller: utilpointer.BoolPtr(true),
262+
},
263+
}
264+
pod.Name = fmt.Sprintf("%s-%d", pod.Name, i)
265+
matchedPods = append(matchedPods, pod)
266+
}
267+
for i := 5; int32(i) < 10; i++ {
268+
pod := podDemo.DeepCopy()
269+
pod.OwnerReferences = []metav1.OwnerReference{
270+
{
271+
APIVersion: "apps/v1",
272+
Kind: "ReplicaSet",
273+
Name: rs[1].Name,
274+
UID: rs[1].UID,
275+
Controller: utilpointer.BoolPtr(true),
276+
},
277+
}
278+
pod.Name = fmt.Sprintf("%s-%d", pod.Name, i)
279+
matchedPods = append(matchedPods, pod)
280+
}
281+
return matchedPods
282+
},
283+
getDeployment: func() *apps.Deployment {
284+
obj := deploymentDemo.DeepCopy()
285+
obj.Spec.Replicas = utilpointer.Int32(0)
286+
return obj
287+
},
288+
getReplicaSet: func() []*apps.ReplicaSet {
289+
obj1 := replicaSetDemo.DeepCopy()
290+
obj1.Name = "nginx-rs-1"
291+
obj2 := replicaSetDemo.DeepCopy()
292+
obj2.Name = "nginx-rs-2"
293+
obj2.UID = "a34b0453-3426-4685-a79c-752e7062a523"
294+
return []*apps.ReplicaSet{obj1, obj2}
295+
},
296+
getPub: func() *policyv1alpha1.PodUnavailableBudget {
297+
pub := pubDemo.DeepCopy()
298+
pub.Annotations[policyv1alpha1.PubProtectTotalReplicasAnnotation] = "15"
299+
return pub
300+
},
301+
expectPubStatus: func() policyv1alpha1.PodUnavailableBudgetStatus {
302+
return policyv1alpha1.PodUnavailableBudgetStatus{
303+
UnavailableAllowed: 0,
304+
CurrentAvailable: 10,
305+
DesiredAvailable: 10,
306+
TotalReplicas: 15,
307+
}
308+
},
309+
},
249310
{
250311
name: "select matched deployment(replicas=10,maxSurge=30%,maxUnavailable=0), and pub(selector,maxUnavailable=30%)",
251312
getPods: func(rs ...*apps.ReplicaSet) []*corev1.Pod {
@@ -466,6 +527,56 @@ func TestPubReconcile(t *testing.T) {
466527
}
467528
},
468529
},
530+
{
531+
name: "select matched deployment(replicas=1,maxSurge=0,maxUnavailable=30%), pub.kruise.io/protect-total-replicas=15 and pub(targetRef,maxUnavailable=30%)",
532+
getPods: func(rs ...*apps.ReplicaSet) []*corev1.Pod {
533+
var matchedPods []*corev1.Pod
534+
for i := 0; int32(i) < 10; i++ {
535+
pod := podDemo.DeepCopy()
536+
pod.OwnerReferences = []metav1.OwnerReference{
537+
{
538+
APIVersion: "apps/v1",
539+
Kind: "ReplicaSet",
540+
Name: rs[0].Name,
541+
UID: rs[0].UID,
542+
Controller: utilpointer.BoolPtr(true),
543+
},
544+
}
545+
pod.Name = fmt.Sprintf("%s-%d", pod.Name, i)
546+
matchedPods = append(matchedPods, pod)
547+
}
548+
return matchedPods
549+
},
550+
getDeployment: func() *apps.Deployment {
551+
obj := deploymentDemo.DeepCopy()
552+
obj.Spec.Replicas = utilpointer.Int32(1)
553+
return obj
554+
},
555+
getReplicaSet: func() []*apps.ReplicaSet {
556+
obj1 := replicaSetDemo.DeepCopy()
557+
obj1.Name = "nginx-rs-1"
558+
return []*apps.ReplicaSet{obj1}
559+
},
560+
getPub: func() *policyv1alpha1.PodUnavailableBudget {
561+
pub := pubDemo.DeepCopy()
562+
pub.Spec.Selector = nil
563+
pub.Spec.TargetReference = &policyv1alpha1.TargetReference{
564+
APIVersion: "apps/v1",
565+
Kind: "Deployment",
566+
Name: "nginx",
567+
}
568+
pub.Annotations[policyv1alpha1.PubProtectTotalReplicasAnnotation] = "15"
569+
return pub
570+
},
571+
expectPubStatus: func() policyv1alpha1.PodUnavailableBudgetStatus {
572+
return policyv1alpha1.PodUnavailableBudgetStatus{
573+
UnavailableAllowed: 0,
574+
CurrentAvailable: 10,
575+
DesiredAvailable: 10,
576+
TotalReplicas: 15,
577+
}
578+
},
579+
},
469580
{
470581
name: "select matched deployment(replicas=1,maxSurge=0,maxUnavailable=30%), and pub(targetRef,maxUnavailable=30%)",
471582
getPods: func(rs ...*apps.ReplicaSet) []*corev1.Pod {
@@ -1114,7 +1225,7 @@ func TestPubReconcile(t *testing.T) {
11141225
getPub: func() *policyv1alpha1.PodUnavailableBudget {
11151226
pub := pubDemo.DeepCopy()
11161227

1117-
pub.Annotations[policyv1alpha1.PubProtectTotalReplicas] = "50"
1228+
pub.Annotations[policyv1alpha1.PubProtectTotalReplicasAnnotation] = "50"
11181229
for i := 0; i < 10; i++ {
11191230
if i >= 0 && i < 5 {
11201231
pub.Status.UnavailablePods[fmt.Sprintf("test-pod-%d", i)] = metav1.Time{Time: time.Now().Add(-10 * time.Second)}

pkg/webhook/podunavailablebudget/validating/pub_create_update_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ func (h *PodUnavailableBudgetCreateUpdateHandler) validatingPodUnavailableBudget
9494
}
9595
}
9696
}
97-
if replicasValue, ok := obj.Annotations[policyv1alpha1.PubProtectTotalReplicas]; ok {
97+
if replicasValue, ok := obj.Annotations[policyv1alpha1.PubProtectTotalReplicasAnnotation]; ok {
9898
if _, err := strconv.ParseInt(replicasValue, 10, 32); err != nil {
99-
allErrs = append(allErrs, field.InternalError(field.NewPath("metadata"), fmt.Errorf("annotation[%s] is invalid", policyv1alpha1.PubProtectTotalReplicas)))
99+
allErrs = append(allErrs, field.InternalError(field.NewPath("metadata"), fmt.Errorf("annotation[%s] is invalid", policyv1alpha1.PubProtectTotalReplicasAnnotation)))
100100
}
101101
}
102102

pkg/webhook/podunavailablebudget/validating/pub_validating_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ import (
2020
"context"
2121
"testing"
2222

23-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
24-
2523
policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1"
26-
2724
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2825
"k8s.io/apimachinery/pkg/runtime"
2926
"k8s.io/apimachinery/pkg/util/intstr"
27+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3028
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3129
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3230
)
@@ -166,7 +164,7 @@ func TestValidatingPub(t *testing.T) {
166164
pub := pubDemo.DeepCopy()
167165
pub.Spec.Selector = nil
168166
pub.Spec.MinAvailable = nil
169-
pub.Annotations[policyv1alpha1.PubProtectTotalReplicas] = "%%"
167+
pub.Annotations[policyv1alpha1.PubProtectTotalReplicasAnnotation] = "%%"
170168
return pub
171169
},
172170
expectErrList: 1,
@@ -177,7 +175,7 @@ func TestValidatingPub(t *testing.T) {
177175
pub := pubDemo.DeepCopy()
178176
pub.Spec.Selector = nil
179177
pub.Spec.MinAvailable = nil
180-
pub.Annotations[policyv1alpha1.PubProtectTotalReplicas] = "1000"
178+
pub.Annotations[policyv1alpha1.PubProtectTotalReplicasAnnotation] = "1000"
181179
return pub
182180
},
183181
expectErrList: 0,

0 commit comments

Comments
 (0)