diff --git a/pkg/control/pubcontrol/api.go b/pkg/control/pubcontrol/api.go index 1a75109831..33c67de3f0 100644 --- a/pkg/control/pubcontrol/api.go +++ b/pkg/control/pubcontrol/api.go @@ -24,9 +24,6 @@ import ( ) type PubControl interface { - // Common - // get PodUnavailableBudget - GetPodUnavailableBudget() *policyv1alpha1.PodUnavailableBudget // IsPodReady indicates whether pod is fully ready // 1. pod.Status.Phase == v1.PodRunning // 2. pod.condition PodReady == true @@ -37,13 +34,16 @@ type PubControl interface { // return two parameters // 1. podList // 2. expectedCount, the default is workload.Replicas - GetPodsForPub() ([]*corev1.Pod, int32, error) + GetPodsForPub(pub *policyv1alpha1.PodUnavailableBudget) ([]*corev1.Pod, int32, error) // webhook // determine if this change to pod might cause unavailability IsPodUnavailableChanged(oldPod, newPod *corev1.Pod) bool + // get pub for pod + GetPubForPod(pod *corev1.Pod) (*policyv1alpha1.PodUnavailableBudget, error) } -func NewPubControl(pub *policyv1alpha1.PodUnavailableBudget, controllerFinder *controllerfinder.ControllerFinder, client client.Client) PubControl { - return &commonControl{PodUnavailableBudget: pub, controllerFinder: controllerFinder, Client: client} +func NewPubControl(client client.Client) PubControl { + controllerFinder := controllerfinder.NewControllerFinder(client) + return &commonControl{controllerFinder: controllerFinder, Client: client} } diff --git a/pkg/control/pubcontrol/pub_control.go b/pkg/control/pubcontrol/pub_control.go index 3effba9c5a..f79f175250 100644 --- a/pkg/control/pubcontrol/pub_control.go +++ b/pkg/control/pubcontrol/pub_control.go @@ -24,9 +24,11 @@ import ( policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1" "github.com/openkruise/kruise/pkg/control/sidecarcontrol" "github.com/openkruise/kruise/pkg/util" + utilclient "github.com/openkruise/kruise/pkg/util/client" "github.com/openkruise/kruise/pkg/util/controllerfinder" "github.com/openkruise/kruise/pkg/util/inplaceupdate" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/klog/v2" kubecontroller "k8s.io/kubernetes/pkg/controller" @@ -35,14 +37,9 @@ import ( type commonControl struct { client.Client - *policyv1alpha1.PodUnavailableBudget controllerFinder *controllerfinder.ControllerFinder } -func (c *commonControl) GetPodUnavailableBudget() *policyv1alpha1.PodUnavailableBudget { - return c.PodUnavailableBudget -} - func (c *commonControl) IsPodReady(pod *corev1.Pod) bool { // 1. pod.Status.Phase == v1.PodRunning // 2. pod.condition PodReady == true @@ -56,7 +53,7 @@ func (c *commonControl) IsPodUnavailableChanged(oldPod, newPod *corev1.Pod) bool } // If pod.spec changed, pod will be in unavailable condition if !reflect.DeepEqual(oldPod.Spec, newPod.Spec) { - klog.V(3).Infof("pod(%s.%s) specification changed, and maybe cause unavailability", newPod.Namespace, newPod.Name) + klog.V(3).Infof("pod(%s/%s) specification changed, and maybe cause unavailability", newPod.Namespace, newPod.Name) return true } // pod other changes will not cause unavailability situation, then return false @@ -67,8 +64,7 @@ func (c *commonControl) IsPodUnavailableChanged(oldPod, newPod *corev1.Pod) bool // return two parameters // 1. podList // 2. expectedCount, the default is workload.Replicas -func (c *commonControl) GetPodsForPub() ([]*corev1.Pod, int32, error) { - pub := c.GetPodUnavailableBudget() +func (c *commonControl) GetPodsForPub(pub *policyv1alpha1.PodUnavailableBudget) ([]*corev1.Pod, int32, error) { // if targetReference isn't nil, priority to take effect var listOptions *client.ListOptions if pub.Spec.TargetReference != nil { @@ -87,7 +83,7 @@ func (c *commonControl) GetPodsForPub() ([]*corev1.Pod, int32, error) { } listOptions = &client.ListOptions{Namespace: pub.Namespace, LabelSelector: labelSelector} podList := &corev1.PodList{} - if err := c.List(context.TODO(), podList, listOptions); err != nil { + if err = c.List(context.TODO(), podList, listOptions, utilclient.DisableDeepCopy); err != nil { return nil, 0, err } @@ -102,7 +98,6 @@ func (c *commonControl) GetPodsForPub() ([]*corev1.Pod, int32, error) { if err != nil { return nil, 0, err } - return matchedPods, expectedCount, nil } @@ -119,7 +114,7 @@ func (c *commonControl) IsPodStateConsistent(pod *corev1.Pod) bool { } if !util.IsPodContainerDigestEqual(sets.NewString(container.Name), pod) { - klog.V(5).Infof("pod(%s.%s) container(%s) image is inconsistent", pod.Namespace, pod.Name, container.Name) + klog.V(5).Infof("pod(%s/%s) container(%s) image is inconsistent", pod.Namespace, pod.Name, container.Name) return false } } @@ -138,13 +133,30 @@ func (c *commonControl) IsPodStateConsistent(pod *corev1.Pod) bool { // whether other containers is consistent if err := inplaceupdate.DefaultCheckInPlaceUpdateCompleted(pod); err != nil { - klog.V(5).Infof("check pod(%s.%s) InPlaceUpdate failed: %s", pod.Namespace, pod.Name, err.Error()) + klog.V(5).Infof("check pod(%s/%s) InPlaceUpdate failed: %s", pod.Namespace, pod.Name, err.Error()) return false } return true } +func (c *commonControl) GetPubForPod(pod *corev1.Pod) (*policyv1alpha1.PodUnavailableBudget, error) { + if len(pod.Annotations) == 0 || pod.Annotations[PodRelatedPubAnnotation] == "" { + return nil, nil + } + pubName := pod.Annotations[PodRelatedPubAnnotation] + pub := &policyv1alpha1.PodUnavailableBudget{} + err := c.Get(context.TODO(), client.ObjectKey{Namespace: pod.Namespace, Name: pubName}, pub) + if err != nil { + if errors.IsNotFound(err) { + klog.Warningf("pod(%s/%s) pub(%s) Is NotFound", pod.Namespace, pod.Name, pubName) + return nil, nil + } + return nil, err + } + return pub, nil +} + func getSidecarSetsInPod(pod *corev1.Pod) (sidecarSets, containers sets.String) { containers = sets.NewString() sidecarSets = sets.NewString() diff --git a/pkg/control/pubcontrol/utils.go b/pkg/control/pubcontrol/utils.go index 42f3d70c13..3b1e4aef45 100644 --- a/pkg/control/pubcontrol/utils.go +++ b/pkg/control/pubcontrol/utils.go @@ -24,14 +24,10 @@ import ( policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1" kubeClient "github.com/openkruise/kruise/pkg/client" "github.com/openkruise/kruise/pkg/util" - "github.com/openkruise/kruise/pkg/util/controllerfinder" - corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/util/retry" @@ -59,21 +55,23 @@ const ( // Marked pods will not be pub-protected, solving the scenario of force pod deletion PodPubNoProtectionAnnotation = "pub.kruise.io/no-protect" + + // related-pub annotation in pod + PodRelatedPubAnnotation = "kruise.io/related-pub" ) // parameters: // 1. allowed(bool) indicates whether to allow this update operation // 2. err(error) -func PodUnavailableBudgetValidatePod(client client.Client, pod *corev1.Pod, control PubControl, operation Operation, dryRun bool) (allowed bool, reason string, err error) { - pub := control.GetPodUnavailableBudget() +func PodUnavailableBudgetValidatePod(client client.Client, control PubControl, pub *policyv1alpha1.PodUnavailableBudget, pod *corev1.Pod, operation Operation, dryRun bool) (allowed bool, reason string, err error) { // If the pod is not ready, it doesn't count towards healthy and we should not decrement if !control.IsPodReady(pod) { - klog.V(3).Infof("pod(%s.%s) is not ready, then don't need check pub", pod.Namespace, pod.Name) + klog.V(3).Infof("pod(%s/%s) is not ready, then don't need check pub", pod.Namespace, pod.Name) return true, "", nil } // pod is in pub.Status.DisruptedPods or pub.Status.UnavailablePods, then don't need check it if isPodRecordedInPub(pod.Name, pub) { - klog.V(5).Infof("pod(%s.%s) already is recorded in pub(%s.%s)", pod.Namespace, pod.Name, pub.Namespace, pub.Name) + klog.V(5).Infof("pod(%s/%s) already is recorded in pub(%s/%s)", pod.Namespace, pod.Name, pub.Namespace, pub.Name) return true, "", nil } @@ -129,10 +127,10 @@ func PodUnavailableBudgetValidatePod(client client.Client, pod *corev1.Pod, cont // If this is a dry-run, we don't need to go any further than that. if dryRun { - klog.V(5).Infof("pod(%s) operation for pub(%s.%s) is a dry run", pod.Name, pubClone.Namespace, pubClone.Name) + klog.V(5).Infof("pod(%s) operation for pub(%s/%s) is a dry run", pod.Name, pubClone.Namespace, pubClone.Name) return nil } - klog.V(3).Infof("pub(%s.%s) update status(disruptedPods:%d, unavailablePods:%d, expectedCount:%d, desiredAvailable:%d, currentAvailable:%d, unavailableAllowed:%d)", + klog.V(3).Infof("pub(%s/%s) update status(disruptedPods:%d, unavailablePods:%d, expectedCount:%d, desiredAvailable:%d, currentAvailable:%d, unavailableAllowed:%d)", pubClone.Namespace, pubClone.Name, len(pubClone.Status.DisruptedPods), len(pubClone.Status.UnavailablePods), pubClone.Status.TotalReplicas, pubClone.Status.DesiredAvailable, pubClone.Status.CurrentAvailable, pubClone.Status.UnavailableAllowed) start = time.Now() @@ -152,15 +150,15 @@ func PodUnavailableBudgetValidatePod(client client.Client, pod *corev1.Pod, cont klog.V(3).Infof("Webhook cost of pub(%s/%s): conflict times %v, cost of Get %v, cost of Update %v", pub.Namespace, pub.Name, conflictTimes, costOfGet, costOfUpdate) if err != nil && err != wait.ErrWaitTimeout { - klog.V(3).Infof("pod(%s.%s) operation(%s) for pub(%s.%s) failed: %s", pod.Namespace, pod.Name, operation, pub.Namespace, pub.Name, err.Error()) + klog.V(3).Infof("pod(%s/%s) operation(%s) for pub(%s/%s) failed: %s", pod.Namespace, pod.Name, operation, pub.Namespace, pub.Name, err.Error()) return false, err.Error(), nil } else if err == wait.ErrWaitTimeout { err = errors.NewTimeoutError(fmt.Sprintf("couldn't update PodUnavailableBudget %s due to conflicts", pub.Name), 10) - klog.Errorf("pod(%s.%s) operation(%s) failed: %s", pod.Namespace, pod.Name, operation, err.Error()) + klog.Errorf("pod(%s/%s) operation(%s) failed: %s", pod.Namespace, pod.Name, operation, err.Error()) return false, err.Error(), nil } - klog.V(3).Infof("admit pod(%s.%s) operation(%s) for pub(%s.%s)", pod.Namespace, pod.Name, operation, pub.Namespace, pub.Name) + klog.V(3).Infof("admit pod(%s/%s) operation(%s) for pub(%s/%s)", pod.Namespace, pod.Name, operation, pub.Namespace, pub.Name) return true, "", nil } @@ -183,10 +181,10 @@ func checkAndDecrement(podName string, pub *policyv1alpha1.PodUnavailableBudget, if operation == UpdateOperation { pub.Status.UnavailablePods[podName] = metav1.Time{Time: time.Now()} - klog.V(3).Infof("pod(%s) is recorded in pub(%s.%s) UnavailablePods", podName, pub.Namespace, pub.Name) + klog.V(3).Infof("pod(%s) is recorded in pub(%s/%s) UnavailablePods", podName, pub.Namespace, pub.Name) } else { pub.Status.DisruptedPods[podName] = metav1.Time{Time: time.Now()} - klog.V(3).Infof("pod(%s) is recorded in pub(%s.%s) DisruptedPods", podName, pub.Namespace, pub.Name) + klog.V(3).Infof("pod(%s) is recorded in pub(%s/%s) DisruptedPods", podName, pub.Namespace, pub.Name) } return nil } @@ -200,79 +198,3 @@ func isPodRecordedInPub(podName string, pub *policyv1alpha1.PodUnavailableBudget } return false } - -func GetPodUnavailableBudgetForPod(kClient client.Client, finders *controllerfinder.ControllerFinder, pod *corev1.Pod) (*policyv1alpha1.PodUnavailableBudget, error) { - var err error - if len(pod.Labels) == 0 { - return nil, nil - } - - pubList := &policyv1alpha1.PodUnavailableBudgetList{} - if err = kClient.List(context.TODO(), pubList, &client.ListOptions{Namespace: pod.Namespace}); err != nil { - return nil, err - } - - var matchedPubs []policyv1alpha1.PodUnavailableBudget - for _, pub := range pubList.Items { - // if targetReference isn't nil, priority to take effect - if pub.Spec.TargetReference != nil { - targetRef := pub.Spec.TargetReference - // check whether APIVersion, Kind, Name is equal - ref := metav1.GetControllerOf(pod) - if ref == nil { - continue - } - // recursive fetch pod reference, e.g. ref.Kind=Replicas, return podRef.Kind=Deployment - podRef, err := finders.GetScaleAndSelectorForRef(ref.APIVersion, ref.Kind, pod.Namespace, ref.Name, ref.UID) - if err != nil { - return nil, err - } - pubRef, err := finders.GetScaleAndSelectorForRef(targetRef.APIVersion, targetRef.Kind, pub.Namespace, targetRef.Name, "") - if err != nil { - return nil, err - } - if podRef == nil || pubRef == nil { - continue - } - // belongs the same workload - if isReferenceEqual(podRef, pubRef) { - matchedPubs = append(matchedPubs, pub) - } - } else { - // This error is irreversible, so continue - labelSelector, err := util.GetFastLabelSelector(pub.Spec.Selector) - if err != nil { - continue - } - // If a PUB with a nil or empty selector creeps in, it should match nothing, not everything. - if labelSelector.Empty() || !labelSelector.Matches(labels.Set(pod.Labels)) { - continue - } - matchedPubs = append(matchedPubs, pub) - } - } - - if len(matchedPubs) == 0 { - klog.V(6).Infof("could not find PodUnavailableBudget for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) - return nil, nil - } - if len(matchedPubs) > 1 { - klog.Warningf("Pod %q/%q matches multiple PodUnavailableBudgets. Choose %q arbitrarily.", pod.Namespace, pod.Name, matchedPubs[0].Name) - } - - return &matchedPubs[0], nil -} - -// check APIVersion, Kind, Name -func isReferenceEqual(ref1, ref2 *controllerfinder.ScaleAndSelector) bool { - gv1, err := schema.ParseGroupVersion(ref1.APIVersion) - if err != nil { - return false - } - gv2, err := schema.ParseGroupVersion(ref2.APIVersion) - if err != nil { - return false - } - return gv1.Group == gv2.Group && ref1.Kind == ref2.Kind && - ref1.Name == ref2.Name && ref1.UID == ref2.UID -} diff --git a/pkg/control/pubcontrol/utils_test.go b/pkg/control/pubcontrol/utils_test.go index 65b7b708e6..e18f295d3c 100644 --- a/pkg/control/pubcontrol/utils_test.go +++ b/pkg/control/pubcontrol/utils_test.go @@ -20,7 +20,6 @@ import ( "testing" policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1" - "github.com/openkruise/kruise/pkg/util/controllerfinder" apps "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -173,9 +172,10 @@ func TestGetPodUnavailableBudgetForPod(t *testing.T) { matchedPub bool }{ { - name: "matched pub targetRef deployment", + name: "matched pub", getPod: func() *corev1.Pod { pod := podDemo.DeepCopy() + pod.Annotations[PodRelatedPubAnnotation] = pubDemo.Name return pod }, getDeployment: func() *apps.Deployment { @@ -198,36 +198,11 @@ func TestGetPodUnavailableBudgetForPod(t *testing.T) { }, matchedPub: true, }, - { - name: "no matched pub targetRef deployment, for unequal name", - getPod: func() *corev1.Pod { - pod := podDemo.DeepCopy() - return pod - }, - getDeployment: func() *apps.Deployment { - dep := deploymentDemo.DeepCopy() - return dep - }, - getReplicaSet: func() *apps.ReplicaSet { - rep := replicaSetDemo.DeepCopy() - return rep - }, - getPub: func() *policyv1alpha1.PodUnavailableBudget { - pub := pubDemo.DeepCopy() - pub.Spec.Selector = nil - pub.Spec.TargetReference = &policyv1alpha1.TargetReference{ - Name: "no-deployment", - Kind: deploymentDemo.Kind, - APIVersion: deploymentDemo.APIVersion, - } - return pub - }, - matchedPub: false, - }, { name: "no matched pub targetRef deployment, for unequal ns", getPod: func() *corev1.Pod { pod := podDemo.DeepCopy() + pod.Annotations[PodRelatedPubAnnotation] = pubDemo.Name return pod }, getDeployment: func() *apps.Deployment { @@ -252,29 +227,10 @@ func TestGetPodUnavailableBudgetForPod(t *testing.T) { matchedPub: false, }, { - name: "matched pub selector", - getPod: func() *corev1.Pod { - pod := podDemo.DeepCopy() - return pod - }, - getDeployment: func() *apps.Deployment { - dep := deploymentDemo.DeepCopy() - return dep - }, - getReplicaSet: func() *apps.ReplicaSet { - rep := replicaSetDemo.DeepCopy() - return rep - }, - getPub: func() *policyv1alpha1.PodUnavailableBudget { - pub := pubDemo.DeepCopy() - return pub - }, - matchedPub: true, - }, - { - name: "no match pub selector", + name: "no match, pub not found", getPod: func() *corev1.Pod { pod := podDemo.DeepCopy() + pod.Annotations[PodRelatedPubAnnotation] = "o-pub" return pod }, getDeployment: func() *apps.Deployment { @@ -301,17 +257,17 @@ func TestGetPodUnavailableBudgetForPod(t *testing.T) { for _, cs := range cases { t.Run(cs.name, func(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cs.getDeployment(), cs.getReplicaSet(), cs.getPub()).Build() - controllerFinder := controllerfinder.NewControllerFinder(fakeClient) + control := NewPubControl(fakeClient) pod := cs.getPod() - pub, err := GetPodUnavailableBudgetForPod(fakeClient, controllerFinder, pod) + pub, err := control.GetPubForPod(pod) if err != nil { - t.Fatalf("GetPodUnavailableBudgetForPod failed: %s", err.Error()) + t.Fatalf("GetPubForPod failed: %s", err.Error()) } if cs.matchedPub && pub == nil { - t.Fatalf("GetPodUnavailableBudgetForPod failed") + t.Fatalf("GetPubForPod failed") } if !cs.matchedPub && pub != nil { - t.Fatalf("GetPodUnavailableBudgetForPod failed") + t.Fatalf("GetPubForPod failed") } }) } diff --git a/pkg/control/sidecarcontrol/sidecarset_control.go b/pkg/control/sidecarcontrol/sidecarset_control.go index 926b24736f..db0d8eaf40 100644 --- a/pkg/control/sidecarcontrol/sidecarset_control.go +++ b/pkg/control/sidecarcontrol/sidecarset_control.go @@ -84,7 +84,7 @@ func (c *commonControl) IsPodReady(pod *v1.Pod) bool { for _, container := range pod.Spec.Containers { // If container is empty container, then its image must be empty image if emptyImage := emptyContainers[container.Name]; emptyImage != "" && container.Image != emptyImage { - klog.V(5).Infof("pod(%s.%s) sidecar empty container(%s) Image(%s) isn't Empty Image(%s)", + klog.V(5).Infof("pod(%s/%s) sidecar empty container(%s) Image(%s) isn't Empty Image(%s)", pod.Namespace, pod.Name, container.Name, container.Image, emptyImage) return false } @@ -107,7 +107,7 @@ func (c *commonControl) UpdatePodAnnotationsInUpgrade(changedContainers []string sidecarUpdateStates := make(map[string]*pub.InPlaceUpdateState) if stateStr, _ := pod.Annotations[SidecarsetInplaceUpdateStateKey]; len(stateStr) > 0 { if err := json.Unmarshal([]byte(stateStr), &sidecarUpdateStates); err != nil { - klog.Errorf("parse pod(%s.%s) annotations[%s] value(%s) failed: %s", + klog.Errorf("parse pod(%s/%s) annotations[%s] value(%s) failed: %s", pod.Namespace, pod.Name, SidecarsetInplaceUpdateStateKey, stateStr, err.Error()) } } @@ -227,7 +227,7 @@ func IsSidecarContainerUpdateCompleted(pod *v1.Pod, sidecarSets, containers sets return true // this won't happen in practice, unless someone manually edit pod annotations } else if err := json.Unmarshal([]byte(stateStr), &sidecarUpdateStates); err != nil { - klog.V(5).Infof("parse pod(%s.%s) annotations[%s] value(%s) failed: %s", + klog.V(5).Infof("parse pod(%s/%s) annotations[%s] value(%s) failed: %s", pod.Namespace, pod.Name, SidecarsetInplaceUpdateStateKey, stateStr, err.Error()) return false } @@ -259,7 +259,7 @@ func IsSidecarContainerUpdateCompleted(pod *v1.Pod, sidecarSets, containers sets // we assume that users should not update workload template with new image // which actually has the same imageID as the old image if oldStatus.ImageID == cs.ImageID && containerImages[cs.Name] != cs.Image { - klog.V(5).Infof("pod(%s.%s) container %s status imageID not changed, then inconsistent", pod.Namespace, pod.Name, cs.Name) + klog.V(5).Infof("pod(%s/%s) container %s status imageID not changed, then inconsistent", pod.Namespace, pod.Name, cs.Name) return false } } diff --git a/pkg/control/sidecarcontrol/util.go b/pkg/control/sidecarcontrol/util.go index 0857dba7ad..4250432b05 100644 --- a/pkg/control/sidecarcontrol/util.go +++ b/pkg/control/sidecarcontrol/util.go @@ -126,7 +126,7 @@ func GetPodSidecarSetUpgradeSpecInAnnotations(sidecarSetName, annotationKey stri sidecarSetHash := make(map[string]SidecarSetUpgradeSpec) if err := json.Unmarshal([]byte(annotations[hashKey]), &sidecarSetHash); err != nil { - klog.Errorf("parse pod(%s.%s) annotations[%s] value(%s) failed: %s", pod.GetNamespace(), pod.GetName(), hashKey, + klog.Errorf("parse pod(%s/%s) annotations[%s] value(%s) failed: %s", pod.GetNamespace(), pod.GetName(), hashKey, annotations[hashKey], err.Error()) // to be compatible with older sidecarSet hash struct, map[string]string olderSidecarSetHash := make(map[string]string) @@ -157,7 +157,7 @@ func updatePodSidecarSetHash(pod *corev1.Pod, sidecarSet *appsv1alpha1.SidecarSe hashKey := SidecarSetHashAnnotation sidecarSetHash := make(map[string]SidecarSetUpgradeSpec) if err := json.Unmarshal([]byte(pod.Annotations[hashKey]), &sidecarSetHash); err != nil { - klog.Errorf("unmarshal pod(%s.%s) annotations[%s] failed: %s", pod.Namespace, pod.Name, hashKey, err.Error()) + klog.Errorf("unmarshal pod(%s/%s) annotations[%s] failed: %s", pod.Namespace, pod.Name, hashKey, err.Error()) // to be compatible with older sidecarSet hash struct, map[string]string olderSidecarSetHash := make(map[string]string) @@ -289,7 +289,7 @@ func GetInjectedVolumeMountsAndEnvs(control SidecarControl, sidecarContainer *ap // get envVar in container eVar := util.GetContainerEnvVar(&appContainer, envName) if eVar == nil { - klog.Warningf("pod(%s.%s) container(%s) get env(%s) is nil", pod.Namespace, pod.Name, appContainer.Name, envName) + klog.Warningf("pod(%s/%s) container(%s) get env(%s) is nil", pod.Namespace, pod.Name, appContainer.Name, envName) continue } injectedEnvs = append(injectedEnvs, *eVar) @@ -324,7 +324,7 @@ func GetSidecarTransferEnvs(sidecarContainer *appsv1alpha1.SidecarContainer, pod if tEnv.SourceContainerNameFrom != nil && tEnv.SourceContainerNameFrom.FieldRef != nil { containerName, err := ExtractContainerNameFromFieldPath(tEnv.SourceContainerNameFrom.FieldRef, pod) if err != nil { - klog.Errorf("get containerName from pod(%s.%s) annotations or labels[%s] failed: %s", pod.Namespace, pod.Name, tEnv.SourceContainerNameFrom.FieldRef, err.Error()) + klog.Errorf("get containerName from pod(%s/%s) annotations or labels[%s] failed: %s", pod.Namespace, pod.Name, tEnv.SourceContainerNameFrom.FieldRef, err.Error()) continue } sourceContainerName = containerName diff --git a/pkg/control/sidecarcontrol/util_hotupgrade.go b/pkg/control/sidecarcontrol/util_hotupgrade.go index e3ef49665d..f94cbceb32 100644 --- a/pkg/control/sidecarcontrol/util_hotupgrade.go +++ b/pkg/control/sidecarcontrol/util_hotupgrade.go @@ -70,11 +70,11 @@ func GetPodHotUpgradeInfoInAnnotations(pod *corev1.Pod) map[string]string { hotUpgradeWorkContainer := make(map[string]string) currentStr, ok := pod.Annotations[SidecarSetWorkingHotUpgradeContainer] if !ok { - klog.V(6).Infof("Pod(%s.%s) annotations(%s) Not Found", pod.Namespace, pod.Name, SidecarSetWorkingHotUpgradeContainer) + klog.V(6).Infof("Pod(%s/%s) annotations(%s) Not Found", pod.Namespace, pod.Name, SidecarSetWorkingHotUpgradeContainer) return hotUpgradeWorkContainer } if err := json.Unmarshal([]byte(currentStr), &hotUpgradeWorkContainer); err != nil { - klog.Errorf("Parse Pod(%s.%s) annotations(%s) Value(%s) failed: %s", pod.Namespace, pod.Name, + klog.Errorf("Parse Pod(%s/%s) annotations(%s) Value(%s) failed: %s", pod.Namespace, pod.Name, SidecarSetWorkingHotUpgradeContainer, currentStr, err.Error()) return hotUpgradeWorkContainer } @@ -120,7 +120,7 @@ func findContainerToHotUpgrade(sidecarContainer *appsv1alpha1.SidecarContainer, // Second, Not ready sidecar container will be upgraded c1Ready := podutil.GetExistingContainerStatus(pod.Status.ContainerStatuses, c1.Name).Ready && control.IsPodStateConsistent(pod, sets.NewString(c1.Name)) c2Ready := podutil.GetExistingContainerStatus(pod.Status.ContainerStatuses, c2.Name).Ready && control.IsPodStateConsistent(pod, sets.NewString(c2.Name)) - klog.V(3).Infof("pod(%s.%s) container(%s) ready(%v) container(%s) ready(%v)", pod.Namespace, pod.Name, c1.Name, c1Ready, c2.Name, c2Ready) + klog.V(3).Infof("pod(%s/%s) container(%s) ready(%v) container(%s) ready(%v)", pod.Namespace, pod.Name, c1.Name, c1Ready, c2.Name, c2Ready) if c1Ready && !c2Ready { return c2.Name, c1.Name } else if !c1Ready && c2Ready { diff --git a/pkg/controller/cloneset/sync/api.go b/pkg/controller/cloneset/sync/api.go index 17a4128a8f..2a9e683529 100644 --- a/pkg/controller/cloneset/sync/api.go +++ b/pkg/controller/cloneset/sync/api.go @@ -18,6 +18,7 @@ package sync import ( appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" + "github.com/openkruise/kruise/pkg/control/pubcontrol" clonesetutils "github.com/openkruise/kruise/pkg/controller/cloneset/utils" "github.com/openkruise/kruise/pkg/util/controllerfinder" "github.com/openkruise/kruise/pkg/util/inplaceupdate" @@ -48,6 +49,7 @@ type realControl struct { inplaceControl inplaceupdate.Interface recorder record.EventRecorder controllerFinder *controllerfinder.ControllerFinder + pubControl pubcontrol.PubControl } func New(c client.Client, recorder record.EventRecorder) Interface { @@ -57,5 +59,6 @@ func New(c client.Client, recorder record.EventRecorder) Interface { lifecycleControl: lifecycle.New(c), recorder: recorder, controllerFinder: controllerfinder.NewControllerFinder(c), + pubControl: pubcontrol.NewPubControl(c), } } diff --git a/pkg/controller/cloneset/sync/cloneset_update.go b/pkg/controller/cloneset/sync/cloneset_update.go index 79f444b575..109b8aa6ac 100644 --- a/pkg/controller/cloneset/sync/cloneset_update.go +++ b/pkg/controller/cloneset/sync/cloneset_update.go @@ -133,7 +133,7 @@ func (c *realControl) Update(cs *appsv1alpha1.CloneSet, var pub *policyv1alpha1.PodUnavailableBudget var err error if utilfeature.DefaultFeatureGate.Enabled(features.PodUnavailableBudgetUpdateGate) && len(waitUpdateIndexes) > 0 { - pub, err = pubcontrol.GetPodUnavailableBudgetForPod(c.Client, c.controllerFinder, pods[waitUpdateIndexes[0]]) + pub, err = c.pubControl.GetPubForPod(pods[waitUpdateIndexes[0]]) if err != nil { return err } @@ -143,7 +143,7 @@ func (c *realControl) Update(cs *appsv1alpha1.CloneSet, pod := pods[idx] // Determine the pub before updating the pod if pub != nil { - allowed, _, err := pubcontrol.PodUnavailableBudgetValidatePod(c.Client, pod, pubcontrol.NewPubControl(pub, c.controllerFinder, c.Client), pubcontrol.UpdateOperation, false) + allowed, _, err := pubcontrol.PodUnavailableBudgetValidatePod(c.Client, c.pubControl, pub, pod, pubcontrol.UpdateOperation, false) if err != nil { return err // pub check does not pass, try again in seconds diff --git a/pkg/controller/cloneset/sync/cloneset_update_test.go b/pkg/controller/cloneset/sync/cloneset_update_test.go index 8ef8e19478..f65ae72069 100644 --- a/pkg/controller/cloneset/sync/cloneset_update_test.go +++ b/pkg/controller/cloneset/sync/cloneset_update_test.go @@ -26,6 +26,7 @@ import ( "github.com/openkruise/kruise/apis" appspub "github.com/openkruise/kruise/apis/apps/pub" appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" + "github.com/openkruise/kruise/pkg/control/pubcontrol" clonesetcore "github.com/openkruise/kruise/pkg/controller/cloneset/core" clonesetutils "github.com/openkruise/kruise/pkg/controller/cloneset/utils" "github.com/openkruise/kruise/pkg/features" @@ -588,6 +589,7 @@ func TestUpdate(t *testing.T) { inplaceupdate.New(fakeClient, clonesetutils.RevisionAdapterImpl), record.NewFakeRecorder(10), controllerfinder.NewControllerFinder(fakeClient), + pubcontrol.NewPubControl(fakeClient), } currentRevision := mc.updateRevision if len(mc.revisions) > 0 { diff --git a/pkg/controller/podunavailablebudget/podunavailablebudget_controller.go b/pkg/controller/podunavailablebudget/podunavailablebudget_controller.go index a5de71229a..c7ad3033cd 100644 --- a/pkg/controller/podunavailablebudget/podunavailablebudget_controller.go +++ b/pkg/controller/podunavailablebudget/podunavailablebudget_controller.go @@ -20,6 +20,7 @@ import ( "context" "flag" "fmt" + "strings" "time" kruiseappsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" @@ -29,6 +30,7 @@ import ( "github.com/openkruise/kruise/pkg/control/pubcontrol" "github.com/openkruise/kruise/pkg/features" "github.com/openkruise/kruise/pkg/util" + utilclient "github.com/openkruise/kruise/pkg/util/client" "github.com/openkruise/kruise/pkg/util/controllerfinder" utildiscovery "github.com/openkruise/kruise/pkg/util/discovery" utilfeature "github.com/openkruise/kruise/pkg/util/feature" @@ -38,6 +40,7 @@ import ( apiequality "k8s.io/apimachinery/pkg/api/equality" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" @@ -70,6 +73,9 @@ var ( const ( DeletionTimeout = 20 * time.Second UpdatedDelayCheckTime = 10 * time.Second + + // patch related-pub annotation Reconcile prefix + PatchPubAnnotationReconcilePrefix = "patch#" ) var ConflictRetry = wait.Backoff{ @@ -104,6 +110,7 @@ func newReconciler(mgr manager.Manager) reconcile.Reconciler { Scheme: mgr.GetScheme(), recorder: mgr.GetEventRecorderFor("podunavailablebudget-controller"), controllerFinder: controllerfinder.NewControllerFinder(mgr.GetClient()), + pubControl: pubcontrol.NewPubControl(mgr.GetClient()), } } @@ -124,8 +131,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { } // Watch for changes to Pod - if err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &enqueueRequestForPod{client: mgr.GetClient(), - controllerFinder: controllerfinder.NewControllerFinder(mgr.GetClient())}); err != nil { + if err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, newEnqueueRequestForPod(mgr.GetClient())); err != nil { return err } @@ -203,6 +209,7 @@ type ReconcilePodUnavailableBudget struct { Scheme *runtime.Scheme recorder record.EventRecorder controllerFinder *controllerfinder.ControllerFinder + pubControl pubcontrol.PubControl } // +kubebuilder:rbac:groups=policy.kruise.io,resources=podunavailablebudgets,verbs=get;list;watch;create;update;patch;delete @@ -210,14 +217,23 @@ type ReconcilePodUnavailableBudget struct { // pkg/controller/cloneset/cloneset_controller.go Watch for changes to CloneSet func (r *ReconcilePodUnavailableBudget) Reconcile(_ context.Context, req ctrl.Request) (ctrl.Result, error) { + // patch related-pub annotation in pods + if strings.Contains(req.Name, PatchPubAnnotationReconcilePrefix) { + if err := r.patchRelatedPubAnnotationInPod(req); err != nil { + klog.Errorf("PodUnavailableBudget patch related-pub annotation in workload(%s/%s)'s pods failed: %s", req.Namespace, req.Name, err.Error()) + return ctrl.Result{}, err + } + return ctrl.Result{}, nil + } + // Fetch the PodUnavailableBudget instance pub := &policyv1alpha1.PodUnavailableBudget{} err := r.Get(context.TODO(), req.NamespacedName, pub) if (err != nil && errors.IsNotFound(err)) || (err == nil && !pub.DeletionTimestamp.IsZero()) { - klog.V(3).Infof("pub(%s.%s) is Deletion in this time", req.Namespace, req.Name) + klog.V(3).Infof("pub(%s/%s) is Deletion in this time", req.Namespace, req.Name) if cacheErr := util.GlobalCache.Delete(&policyv1alpha1.PodUnavailableBudget{ TypeMeta: metav1.TypeMeta{ - APIVersion: "policy.kruise.io/v1alpha1", + APIVersion: policyv1alpha1.GroupVersion.String(), Kind: "PodUnavailableBudget", }, ObjectMeta: metav1.ObjectMeta{ @@ -235,7 +251,7 @@ func (r *ReconcilePodUnavailableBudget) Reconcile(_ context.Context, req ctrl.Re return reconcile.Result{}, err } - klog.V(3).Infof("begin to process podUnavailableBudget(%s.%s)", pub.Namespace, pub.Name) + klog.V(3).Infof("begin to process podUnavailableBudget(%s/%s)", pub.Namespace, pub.Name) recheckTime, err := r.syncPodUnavailableBudget(pub) if err != nil { return ctrl.Result{}, err @@ -247,8 +263,8 @@ func (r *ReconcilePodUnavailableBudget) Reconcile(_ context.Context, req ctrl.Re } func (r *ReconcilePodUnavailableBudget) syncPodUnavailableBudget(pub *policyv1alpha1.PodUnavailableBudget) (*time.Time, error) { - control := pubcontrol.NewPubControl(pub, r.controllerFinder, r.Client) - pods, expectedCount, err := control.GetPodsForPub() + currentTime := time.Now() + pods, expectedCount, err := r.pubControl.GetPodsForPub(pub) if err != nil { return nil, err } @@ -256,7 +272,7 @@ func (r *ReconcilePodUnavailableBudget) syncPodUnavailableBudget(pub *policyv1al r.recorder.Eventf(pub, corev1.EventTypeNormal, "NoPods", "No matching pods found") } - klog.V(3).Infof("pub(%s.%s) controller pods(%d) expectedCount(%d)", pub.Namespace, pub.Name, len(pods), expectedCount) + klog.V(3).Infof("pub(%s/%s) controller pods(%d) expectedCount(%d)", pub.Namespace, pub.Name, len(pods), expectedCount) desiredAvailable, err := r.getDesiredAvailableForPub(pub, expectedCount) if err != nil { r.recorder.Eventf(pub, corev1.EventTypeWarning, "CalculateExpectedPodCountFailed", "Failed to calculate the number of expected pods: %v", err) @@ -266,8 +282,6 @@ func (r *ReconcilePodUnavailableBudget) syncPodUnavailableBudget(pub *policyv1al // for debug var conflictTimes int var costOfGet, costOfUpdate time.Duration - - currentTime := time.Now() var pubClone *policyv1alpha1.PodUnavailableBudget refresh := false var recheckTime *time.Time @@ -313,7 +327,7 @@ func (r *ReconcilePodUnavailableBudget) syncPodUnavailableBudget(pub *policyv1al // unavailablePods contains information about pods whose specification changed(in-place update), in case of informer cache latency, after 5 seconds to remove it. var disruptedPods, unavailablePods map[string]metav1.Time disruptedPods, unavailablePods, recheckTime = r.buildDisruptedAndUnavailablePods(pods, pubClone, currentTime) - currentAvailable := countAvailablePods(pods, disruptedPods, unavailablePods, control) + currentAvailable := countAvailablePods(pods, disruptedPods, unavailablePods, r.pubControl) start = time.Now() updateErr := r.updatePubStatus(pubClone, currentAvailable, desiredAvailable, expectedCount, disruptedPods, unavailablePods) @@ -329,11 +343,60 @@ func (r *ReconcilePodUnavailableBudget) syncPodUnavailableBudget(pub *policyv1al klog.V(3).Infof("Controller cost of pub(%s/%s): conflict times %v, cost of Get %v, cost of Update %v", pub.Namespace, pub.Name, conflictTimes, costOfGet, costOfUpdate) if err != nil { - klog.Errorf("update pub(%s.%s) status failed: %s", pub.Namespace, pub.Name, err.Error()) + klog.Errorf("update pub(%s/%s) status failed: %s", pub.Namespace, pub.Name, err.Error()) } return recheckTime, err } +// patch related-pub annotation in all pods of workload +func (r *ReconcilePodUnavailableBudget) patchRelatedPubAnnotationInPod(req ctrl.Request) error { + // req.Name Format = patch#{apiVersion}#{workload.Kind}#{workload.Name} + // example for patch#apps/v1#Deployment#echoserver + arr := strings.Split(req.Name, "#") + if len(arr) != 4 { + klog.Warningf("Reconcile PodUnavailableBudget workload(%s) is invalid", req.Name) + return nil + } + // fetch workload + apiVersion, kind, ns, name := arr[1], arr[2], req.Namespace, arr[3] + workload, err := r.controllerFinder.GetScaleAndSelectorForRef(apiVersion, kind, ns, name, "") + if err != nil { + return err + } else if workload == nil { + klog.Warningf("Reconcile PodUnavailableBudget workload(%s) is Not Found", req.Name) + return nil + } + + // fetch pub for workload + pub, err := r.getPubForWorkload(workload) + if err != nil || pub == nil { + return err + } + pods, _, err := r.controllerFinder.GetPodsForRef(workload.APIVersion, workload.Kind, workload.Name, workload.Metadata.Namespace, true) + if err != nil { + return err + } + updatedPods := make([]*corev1.Pod, 0, len(pods)) + for i := range pods { + if pods[i].Annotations[pubcontrol.PodRelatedPubAnnotation] == "" { + updatedPods = append(updatedPods, pods[i].DeepCopy()) + } + } + if len(updatedPods) == 0 { + return nil + } + + // update related-pub annotation in pods + for _, pod := range updatedPods { + body := fmt.Sprintf(`{"metadata":{"annotations":{"%s":"%s"}}}`, pubcontrol.PodRelatedPubAnnotation, pub.Name) + if err = r.Patch(context.TODO(), pod, client.RawPatch(types.StrategicMergePatchType, []byte(body))); err != nil { + return err + } + } + klog.V(3).Infof("patch pub(%s/%s) old pods(%d) related-pub annotation success", pub.Namespace, pub.Name, len(updatedPods)) + return nil +} + func countAvailablePods(pods []*corev1.Pod, disruptedPods, unavailablePods map[string]metav1.Time, control pubcontrol.PubControl) (currentAvailable int32) { recordPods := sets.String{} for pName := range disruptedPods { @@ -432,7 +495,7 @@ func (r *ReconcilePodUnavailableBudget) getExpectedScale(pub *policyv1alpha1.Pod for _, finder := range r.controllerFinder.Finders() { scaleNSelector, err := finder(ref, pub.Namespace) if err != nil { - klog.Errorf("podUnavailableBudget(%s.%s) handle TargetReference failed: %s", pub.Namespace, pub.Name, err.Error()) + klog.Errorf("podUnavailableBudget(%s/%s) handle TargetReference failed: %s", pub.Namespace, pub.Name, err.Error()) return 0, err } if scaleNSelector != nil && scaleNSelector.Metadata.DeletionTimestamp.IsZero() { @@ -563,7 +626,41 @@ func (r *ReconcilePodUnavailableBudget) updatePubStatus(pub *policyv1alpha1.PodU if err = util.GlobalCache.Add(pub); err != nil { klog.Errorf("Add cache failed for PodUnavailableBudget(%s/%s): %s", pub.Namespace, pub.Name, err.Error()) } - klog.V(3).Infof("pub(%s.%s) update status(disruptedPods:%d, unavailablePods:%d, expectedCount:%d, desiredAvailable:%d, currentAvailable:%d, unavailableAllowed:%d)", + klog.V(3).Infof("pub(%s/%s) update status(disruptedPods:%d, unavailablePods:%d, expectedCount:%d, desiredAvailable:%d, currentAvailable:%d, unavailableAllowed:%d)", pub.Namespace, pub.Name, len(disruptedPods), len(unavailablePods), expectedCount, desiredAvailable, currentAvailable, unavailableAllowed) return nil } + +func (r *ReconcilePodUnavailableBudget) getPubForWorkload(workload *controllerfinder.ScaleAndSelector) (*policyv1alpha1.PodUnavailableBudget, error) { + pubList := &policyv1alpha1.PodUnavailableBudgetList{} + if err := r.List(context.TODO(), pubList, &client.ListOptions{Namespace: workload.Metadata.Namespace}, utilclient.DisableDeepCopy); err != nil { + return nil, err + } + for i := range pubList.Items { + pub := &pubList.Items[i] + // if targetReference isn't nil, priority to take effect + if pub.Spec.TargetReference != nil { + // belongs the same workload + if isReferenceEqual(&policyv1alpha1.TargetReference{ + APIVersion: workload.APIVersion, + Kind: workload.Kind, + Name: workload.Name, + }, pub.Spec.TargetReference) { + return pub, nil + } + } else { + // This error is irreversible, so continue + labelSelector, err := util.GetFastLabelSelector(pub.Spec.Selector) + if err != nil { + continue + } + // If a PUB with a nil or empty selector creeps in, it should match nothing, not everything. + if labelSelector.Empty() || !labelSelector.Matches(labels.Set(workload.TempLabels)) { + continue + } + return pub, nil + } + } + klog.V(6).Infof("could not find PodUnavailableBudget for workload %s in namespace %s with labels: %v", workload.Name, workload.Metadata.Namespace, workload.TempLabels) + return nil, nil +} diff --git a/pkg/controller/podunavailablebudget/pub_controller_test.go b/pkg/controller/podunavailablebudget/pub_controller_test.go index a64d5c6507..d2bfad9d55 100644 --- a/pkg/controller/podunavailablebudget/pub_controller_test.go +++ b/pkg/controller/podunavailablebudget/pub_controller_test.go @@ -24,6 +24,7 @@ import ( "time" policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1" + "github.com/openkruise/kruise/pkg/control/pubcontrol" "github.com/openkruise/kruise/pkg/util" "github.com/openkruise/kruise/pkg/util/controllerfinder" apps "k8s.io/api/apps/v1" @@ -770,6 +771,7 @@ func TestPubReconcile(t *testing.T) { Client: fakeClient, recorder: record.NewFakeRecorder(10), controllerFinder: controllerfinder.NewControllerFinder(fakeClient), + pubControl: pubcontrol.NewPubControl(fakeClient), } _, err := reconciler.syncPodUnavailableBudget(pub) diff --git a/pkg/controller/podunavailablebudget/pub_pod_event_handler.go b/pkg/controller/podunavailablebudget/pub_pod_event_handler.go index 9d451d0d95..78e44d5b15 100644 --- a/pkg/controller/podunavailablebudget/pub_pod_event_handler.go +++ b/pkg/controller/podunavailablebudget/pub_pod_event_handler.go @@ -18,7 +18,7 @@ package podunavailablebudget import ( "context" - "reflect" + "fmt" "time" appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" @@ -29,6 +29,7 @@ import ( "github.com/openkruise/kruise/pkg/util/controllerfinder" apps "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -45,9 +46,17 @@ import ( var _ handler.EventHandler = &enqueueRequestForPod{} +func newEnqueueRequestForPod(c client.Client) handler.EventHandler { + e := &enqueueRequestForPod{client: c} + e.controllerFinder = controllerfinder.NewControllerFinder(c) + e.pubControl = pubcontrol.NewPubControl(c) + return e +} + type enqueueRequestForPod struct { client client.Client controllerFinder *controllerfinder.ControllerFinder + pubControl pubcontrol.PubControl } func (p *enqueueRequestForPod) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) { @@ -68,13 +77,18 @@ func (p *enqueueRequestForPod) addPod(q workqueue.RateLimitingInterface, obj run if !ok { return } + // add related-pub annotation in pod + if pod.Annotations[pubcontrol.PodRelatedPubAnnotation] == "" { + p.enqueuePatchPubAnnotationRequest(q, pod) + return + } - pub, _ := pubcontrol.GetPodUnavailableBudgetForPod(p.client, p.controllerFinder, pod) + // reconcile pub + pub, _ := p.pubControl.GetPubForPod(pod) if pub == nil { return } - - klog.V(3).Infof("add pod(%s.%s) reconcile pub(%s.%s)", pod.Namespace, pod.Name, pub.Namespace, pub.Name) + klog.V(3).Infof("add pod(%s/%s) reconcile pub(%s/%s)", pod.Namespace, pod.Name, pub.Namespace, pub.Name) q.Add(reconcile.Request{ NamespacedName: types.NamespacedName{ Name: pub.Name, @@ -90,50 +104,11 @@ func (p *enqueueRequestForPod) updatePod(q workqueue.RateLimitingInterface, old, return } - //labels changed, and reconcile union pubs - if !reflect.DeepEqual(newPod.Labels, oldPod.Labels) { - oldPub, _ := pubcontrol.GetPodUnavailableBudgetForPod(p.client, p.controllerFinder, oldPod) - newPub, _ := pubcontrol.GetPodUnavailableBudgetForPod(p.client, p.controllerFinder, newPod) - if oldPub != nil && newPub != nil && oldPub.Name == newPub.Name { - control := pubcontrol.NewPubControl(newPub, p.controllerFinder, p.client) - if isReconcile, enqueueDelayTime := isPodAvailableChanged(oldPod, newPod, newPub, control); isReconcile { - q.AddAfter(reconcile.Request{ - NamespacedName: types.NamespacedName{ - Name: newPub.Name, - Namespace: newPub.Namespace, - }, - }, enqueueDelayTime) - } - return - } - if oldPub != nil { - klog.V(3).Infof("pod(%s.%s) labels changed, and reconcile pub(%s.%s)", oldPod.Namespace, oldPod.Name, oldPub.Namespace, oldPub.Name) - q.Add(reconcile.Request{ - NamespacedName: types.NamespacedName{ - Name: oldPub.Name, - Namespace: oldPub.Namespace, - }, - }) - } - if newPub != nil { - klog.V(3).Infof("pod(%s.%s) labels changed, and reconcile pub(%s.%s)", newPod.Namespace, newPod.Name, newPub.Namespace, newPub.Name) - q.Add(reconcile.Request{ - NamespacedName: types.NamespacedName{ - Name: newPub.Name, - Namespace: newPub.Namespace, - }, - }) - } - - return - } - - pub, _ := pubcontrol.GetPodUnavailableBudgetForPod(p.client, p.controllerFinder, newPod) + pub, _ := p.pubControl.GetPubForPod(newPod) if pub == nil { return } - control := pubcontrol.NewPubControl(pub, p.controllerFinder, p.client) - if isReconcile, enqueueDelayTime := isPodAvailableChanged(oldPod, newPod, pub, control); isReconcile { + if isReconcile, enqueueDelayTime := isPodAvailableChanged(oldPod, newPod, pub, p.pubControl); isReconcile { q.AddAfter(reconcile.Request{ NamespacedName: types.NamespacedName{ Name: pub.Name, @@ -144,12 +119,30 @@ func (p *enqueueRequestForPod) updatePod(q workqueue.RateLimitingInterface, old, } +func (p *enqueueRequestForPod) enqueuePatchPubAnnotationRequest(q workqueue.RateLimitingInterface, pod *corev1.Pod) { + ref := metav1.GetControllerOf(pod) + if ref == nil { + return + } + if !controllerfinder.IsValidGroupVersionKind(ref.APIVersion, ref.Kind) { + return + } + + // name = patch#apps/v1#kind#name + name := fmt.Sprintf("%s%s#%s#%s", PatchPubAnnotationReconcilePrefix, ref.APIVersion, ref.Kind, ref.Name) + q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ + Namespace: pod.Namespace, + Name: name, + }}) + klog.V(6).Infof("reconcile patch workload(%s) related-pub annotation", name) +} + func isPodAvailableChanged(oldPod, newPod *corev1.Pod, pub *policyv1alpha1.PodUnavailableBudget, control pubcontrol.PubControl) (bool, time.Duration) { var enqueueDelayTime time.Duration // If the pod's deletion timestamp is set, remove endpoint from ready address. if oldPod.DeletionTimestamp.IsZero() && !newPod.DeletionTimestamp.IsZero() { enqueueDelayTime = time.Second * 5 - klog.V(3).Infof("pod(%s.%s) DeletionTimestamp changed, and reconcile pub(%s.%s) delayTime(5s)", newPod.Namespace, newPod.Name, pub.Namespace, pub.Name) + klog.V(3).Infof("pod(%s/%s) DeletionTimestamp changed, and reconcile pub(%s/%s) delayTime(5s)", newPod.Namespace, newPod.Name, pub.Namespace, pub.Name) return true, enqueueDelayTime // oldPod Deletion is set, then no reconcile } else if !oldPod.DeletionTimestamp.IsZero() { @@ -163,7 +156,7 @@ func isPodAvailableChanged(oldPod, newPod *corev1.Pod, pub *policyv1alpha1.PodUn oldReady := control.IsPodReady(oldPod) && control.IsPodStateConsistent(oldPod) newReady := control.IsPodReady(newPod) && control.IsPodStateConsistent(newPod) if oldReady != newReady { - klog.V(3).Infof("pod(%s.%s) ConsistentAndReady changed(from %v to %v), and reconcile pub(%s.%s)", + klog.V(3).Infof("pod(%s/%s) ConsistentAndReady changed(from %v to %v), and reconcile pub(%s/%s)", newPod.Namespace, newPod.Name, oldReady, newReady, pub.Namespace, pub.Name) return true, enqueueDelayTime } diff --git a/pkg/controller/podunavailablebudget/pub_pod_event_handler_test.go b/pkg/controller/podunavailablebudget/pub_pod_event_handler_test.go index 03f7de8c88..a62bfd4117 100644 --- a/pkg/controller/podunavailablebudget/pub_pod_event_handler_test.go +++ b/pkg/controller/podunavailablebudget/pub_pod_event_handler_test.go @@ -22,6 +22,7 @@ import ( "testing" "time" + "github.com/openkruise/kruise/pkg/control/pubcontrol" corev1 "k8s.io/api/core/v1" "k8s.io/client-go/util/workqueue" podutil "k8s.io/kubernetes/pkg/api/v1/pod" @@ -31,7 +32,7 @@ import ( func TestPodEventHandler(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() - handler := enqueueRequestForPod{client: fakeClient} + handler := newEnqueueRequestForPod(fakeClient) err := fakeClient.Create(context.TODO(), pubDemo.DeepCopy()) if nil != err { @@ -43,6 +44,7 @@ func TestPodEventHandler(t *testing.T) { createEvt := event.CreateEvent{ Object: podDemo.DeepCopy(), } + createEvt.Object.SetAnnotations(map[string]string{pubcontrol.PodRelatedPubAnnotation: pubDemo.Name}) handler.Create(createEvt, createQ) if createQ.Len() != 1 { t.Errorf("unexpected create event handle queue size, expected 1 actual %d", createQ.Len()) @@ -58,6 +60,8 @@ func TestPodEventHandler(t *testing.T) { ObjectOld: podDemo, ObjectNew: newPod, } + updateEvent.ObjectOld.SetAnnotations(map[string]string{pubcontrol.PodRelatedPubAnnotation: pubDemo.Name}) + updateEvent.ObjectNew.SetAnnotations(map[string]string{pubcontrol.PodRelatedPubAnnotation: pubDemo.Name}) handler.Update(updateEvent, updateQ) if updateQ.Len() != 1 { t.Errorf("unexpected update event handle queue size, expected 1 actual %d", updateQ.Len()) @@ -72,6 +76,8 @@ func TestPodEventHandler(t *testing.T) { ObjectOld: podDemo, ObjectNew: newPod, } + updateEvent.ObjectOld.SetAnnotations(map[string]string{pubcontrol.PodRelatedPubAnnotation: pubDemo.Name}) + updateEvent.ObjectNew.SetAnnotations(map[string]string{pubcontrol.PodRelatedPubAnnotation: pubDemo.Name}) handler.Update(updateEvent, updateQ) if updateQ.Len() != 0 { t.Errorf("unexpected update event handle queue size, expected 0 actual %d", updateQ.Len()) diff --git a/pkg/controller/resourcedistribution/resourcedistribution_controller_test.go b/pkg/controller/resourcedistribution/resourcedistribution_controller_test.go index 8c2a003209..b5b8fd1067 100644 --- a/pkg/controller/resourcedistribution/resourcedistribution_controller_test.go +++ b/pkg/controller/resourcedistribution/resourcedistribution_controller_test.go @@ -76,7 +76,7 @@ func TestDoReconcile(t *testing.T) { resource := &corev1.Secret{} // check whether resource exists if err := reconcileHandler.Client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: "test-secret-1"}, resource); err != nil { - t.Fatalf("failed to get resource(%s.%s) from fake client, err %v", namespace, "test-secret-1", err) + t.Fatalf("failed to get resource(%s/%s) from fake client, err %v", namespace, "test-secret-1", err) } // check resource source and version if !isControlledByDistributor(resource, distributor) { diff --git a/pkg/controller/sidecarset/sidecarset_hotupgrade.go b/pkg/controller/sidecarset/sidecarset_hotupgrade.go index 4dd620e6b9..56f10778ca 100644 --- a/pkg/controller/sidecarset/sidecarset_hotupgrade.go +++ b/pkg/controller/sidecarset/sidecarset_hotupgrade.go @@ -58,7 +58,7 @@ func (p *Processor) flipPodSidecarContainer(control sidecarcontrol.SidecarContro Name: podClone.Name, } if err := p.Client.Get(context.TODO(), key, podClone); err != nil { - klog.Errorf("error getting updated pod(%s.%s) from client", podClone.Namespace, podClone.Name) + klog.Errorf("error getting updated pod(%s/%s) from client", podClone.Namespace, podClone.Name) } return updateErr }) diff --git a/pkg/controller/sidecarset/sidecarset_pod_event_handler.go b/pkg/controller/sidecarset/sidecarset_pod_event_handler.go index 009d9cc494..9c218eeeaa 100644 --- a/pkg/controller/sidecarset/sidecarset_pod_event_handler.go +++ b/pkg/controller/sidecarset/sidecarset_pod_event_handler.go @@ -56,7 +56,7 @@ func (p *enqueueRequestForPod) addPod(q workqueue.RateLimitingInterface, obj run } for _, sidecarSet := range sidecarSets { - klog.V(3).Infof("Create pod(%s.%s) and reconcile sidecarSet(%s)", pod.Namespace, pod.Name, sidecarSet.Name) + klog.V(3).Infof("Create pod(%s/%s) and reconcile sidecarSet(%s)", pod.Namespace, pod.Name, sidecarSet.Name) q.Add(reconcile.Request{ NamespacedName: types.NamespacedName{ Name: sidecarSet.Name, diff --git a/pkg/util/controllerfinder/controller_finder.go b/pkg/util/controllerfinder/controller_finder.go index 2e78c1bb5a..efd23335ce 100644 --- a/pkg/util/controllerfinder/controller_finder.go +++ b/pkg/util/controllerfinder/controller_finder.go @@ -19,6 +19,8 @@ package controllerfinder import ( "context" + appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" + appsv1beta1 "github.com/openkruise/kruise/apis/apps/v1beta1" apps "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -26,9 +28,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" - - appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" - appsv1beta1 "github.com/openkruise/kruise/apis/apps/v1beta1" ) // ScaleAndSelector is used to return (controller, scale, selector) fields from the @@ -41,6 +40,8 @@ type ScaleAndSelector struct { Selector *metav1.LabelSelector // metadata Metadata metav1.ObjectMeta + // template labels + TempLabels map[string]string } type ControllerReference struct { @@ -123,6 +124,18 @@ func (r *ControllerFinder) Finders() []PodControllerFinder { r.getPodStatefulSet, r.getPodKruiseCloneSet, r.getPodKruiseStatefulSet} } +func IsValidGroupVersionKind(apiVersion, kind string) bool { + for _, gvk := range validWorkloadList { + valid, err := verifyGroupKind(apiVersion, kind, gvk) + if err != nil { + return false + } else if valid { + return true + } + } + return false +} + var ( ControllerKindRS = apps.SchemeGroupVersion.WithKind("ReplicaSet") ControllerKindSS = apps.SchemeGroupVersion.WithKind("StatefulSet") @@ -130,12 +143,14 @@ var ( ControllerKindDep = apps.SchemeGroupVersion.WithKind("Deployment") ControllerKruiseKindCS = appsv1alpha1.SchemeGroupVersion.WithKind("CloneSet") ControllerKruiseKindSS = appsv1beta1.SchemeGroupVersion.WithKind("StatefulSet") + + validWorkloadList = []schema.GroupVersionKind{ControllerKindRS, ControllerKindSS, ControllerKindRC, ControllerKindDep, ControllerKruiseKindCS, ControllerKruiseKindSS} ) // getPodReplicaSet finds a replicaset which has no matching deployments. func (r *ControllerFinder) getPodReplicaSet(ref ControllerReference, namespace string) (*ScaleAndSelector, error) { // This error is irreversible, so there is no need to return error - ok, _ := verifyGroupKind(ref, ControllerKindRS.Kind, []string{ControllerKindRS.Group}) + ok, _ := verifyGroupKind(ref.APIVersion, ref.Kind, ControllerKindRS) if !ok { return nil, nil } @@ -165,14 +180,15 @@ func (r *ControllerFinder) getPodReplicaSet(ref ControllerReference, namespace s Name: replicaSet.Name, UID: replicaSet.UID, }, - Metadata: replicaSet.ObjectMeta, + Metadata: replicaSet.ObjectMeta, + TempLabels: replicaSet.Spec.Template.Labels, }, nil } // getPodReplicaSet finds a replicaset which has no matching deployments. func (r *ControllerFinder) getReplicaSet(ref ControllerReference, namespace string) (*apps.ReplicaSet, error) { // This error is irreversible, so there is no need to return error - ok, _ := verifyGroupKind(ref, ControllerKindRS.Kind, []string{ControllerKindRS.Group}) + ok, _ := verifyGroupKind(ref.APIVersion, ref.Kind, ControllerKindRS) if !ok { return nil, nil } @@ -194,7 +210,7 @@ func (r *ControllerFinder) getReplicaSet(ref ControllerReference, namespace stri // getPodStatefulSet returns the statefulset referenced by the provided controllerRef. func (r *ControllerFinder) getPodStatefulSet(ref ControllerReference, namespace string) (*ScaleAndSelector, error) { // This error is irreversible, so there is no need to return error - ok, _ := verifyGroupKind(ref, ControllerKindSS.Kind, []string{ControllerKindSS.Group}) + ok, _ := verifyGroupKind(ref.APIVersion, ref.Kind, ControllerKindSS) if !ok { return nil, nil } @@ -220,14 +236,15 @@ func (r *ControllerFinder) getPodStatefulSet(ref ControllerReference, namespace Name: statefulSet.Name, UID: statefulSet.UID, }, - Metadata: statefulSet.ObjectMeta, + Metadata: statefulSet.ObjectMeta, + TempLabels: statefulSet.Spec.Template.Labels, }, nil } // getPodDeployments finds deployments for any replicasets which are being managed by deployments. func (r *ControllerFinder) getPodDeployment(ref ControllerReference, namespace string) (*ScaleAndSelector, error) { // This error is irreversible, so there is no need to return error - ok, _ := verifyGroupKind(ref, ControllerKindDep.Kind, []string{ControllerKindDep.Group}) + ok, _ := verifyGroupKind(ref.APIVersion, ref.Kind, ControllerKindDep) if !ok { return nil, nil } @@ -252,13 +269,14 @@ func (r *ControllerFinder) getPodDeployment(ref ControllerReference, namespace s Name: deployment.Name, UID: deployment.UID, }, - Metadata: deployment.ObjectMeta, + Metadata: deployment.ObjectMeta, + TempLabels: deployment.Spec.Template.Labels, }, nil } func (r *ControllerFinder) getPodReplicationController(ref ControllerReference, namespace string) (*ScaleAndSelector, error) { // This error is irreversible, so there is no need to return error - ok, _ := verifyGroupKind(ref, ControllerKindRC.Kind, []string{ControllerKindRC.Group}) + ok, _ := verifyGroupKind(ref.APIVersion, ref.Kind, ControllerKindRC) if !ok { return nil, nil } @@ -283,14 +301,15 @@ func (r *ControllerFinder) getPodReplicationController(ref ControllerReference, Name: rc.Name, UID: rc.UID, }, - Metadata: rc.ObjectMeta, + Metadata: rc.ObjectMeta, + TempLabels: rc.Spec.Template.Labels, }, nil } // getPodStatefulSet returns the kruise cloneSet referenced by the provided controllerRef. func (r *ControllerFinder) getPodKruiseCloneSet(ref ControllerReference, namespace string) (*ScaleAndSelector, error) { // This error is irreversible, so there is no need to return error - ok, _ := verifyGroupKind(ref, ControllerKruiseKindCS.Kind, []string{ControllerKruiseKindCS.Group}) + ok, _ := verifyGroupKind(ref.APIVersion, ref.Kind, ControllerKruiseKindCS) if !ok { return nil, nil } @@ -316,14 +335,15 @@ func (r *ControllerFinder) getPodKruiseCloneSet(ref ControllerReference, namespa Name: cloneSet.Name, UID: cloneSet.UID, }, - Metadata: cloneSet.ObjectMeta, + Metadata: cloneSet.ObjectMeta, + TempLabels: cloneSet.Spec.Template.Labels, }, nil } // getPodStatefulSet returns the kruise statefulset referenced by the provided controllerRef. func (r *ControllerFinder) getPodKruiseStatefulSet(ref ControllerReference, namespace string) (*ScaleAndSelector, error) { // This error is irreversible, so there is no need to return error - ok, _ := verifyGroupKind(ref, ControllerKruiseKindSS.Kind, []string{ControllerKruiseKindSS.Group}) + ok, _ := verifyGroupKind(ref.APIVersion, ref.Kind, ControllerKruiseKindSS) if !ok { return nil, nil } @@ -349,25 +369,15 @@ func (r *ControllerFinder) getPodKruiseStatefulSet(ref ControllerReference, name Name: ss.Name, UID: ss.UID, }, - Metadata: ss.ObjectMeta, + Metadata: ss.ObjectMeta, + TempLabels: ss.Spec.Template.Labels, }, nil } -func verifyGroupKind(ref ControllerReference, expectedKind string, expectedGroups []string) (bool, error) { - gv, err := schema.ParseGroupVersion(ref.APIVersion) +func verifyGroupKind(apiVersion, kind string, gvk schema.GroupVersionKind) (bool, error) { + gv, err := schema.ParseGroupVersion(apiVersion) if err != nil { return false, err } - - if ref.Kind != expectedKind { - return false, nil - } - - for _, group := range expectedGroups { - if group == gv.Group { - return true, nil - } - } - - return false, nil + return gv.Group == gvk.Group && kind == gvk.Kind, nil } diff --git a/pkg/webhook/pod/mutating/pod_create_update_handler.go b/pkg/webhook/pod/mutating/pod_create_update_handler.go index 201b0ce003..d282cbeb6c 100644 --- a/pkg/webhook/pod/mutating/pod_create_update_handler.go +++ b/pkg/webhook/pod/mutating/pod_create_update_handler.go @@ -23,8 +23,8 @@ import ( "reflect" "github.com/openkruise/kruise/pkg/features" + "github.com/openkruise/kruise/pkg/util/controllerfinder" utilfeature "github.com/openkruise/kruise/pkg/util/feature" - corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/runtime/inject" @@ -41,6 +41,8 @@ type PodCreateHandler struct { // Decoder decodes objects Decoder *admission.Decoder + + finder *controllerfinder.ControllerFinder } var _ admission.Handler = &PodCreateHandler{} @@ -94,6 +96,7 @@ var _ inject.Client = &PodCreateHandler{} // InjectClient injects the client into the PodCreateHandler func (h *PodCreateHandler) InjectClient(c client.Client) error { h.Client = c + h.finder = controllerfinder.NewControllerFinder(c) return nil } diff --git a/pkg/webhook/pod/mutating/sidecarset.go b/pkg/webhook/pod/mutating/sidecarset.go index fcce25ec8f..78e092dd75 100644 --- a/pkg/webhook/pod/mutating/sidecarset.go +++ b/pkg/webhook/pod/mutating/sidecarset.go @@ -94,7 +94,7 @@ func (h *PodCreateHandler) sidecarsetMutatingPod(ctx context.Context, req admiss // check pod if isUpdated { if !matchedSidecarSets[0].IsPodAvailabilityChanged(pod, oldPod) { - klog.V(3).Infof("pod(%s.%s) availability unchanged for sidecarSet, and ignore", pod.Namespace, pod.Name) + klog.V(3).Infof("pod(%s/%s) availability unchanged for sidecarSet, and ignore", pod.Namespace, pod.Name) return nil } } @@ -106,12 +106,12 @@ func (h *PodCreateHandler) sidecarsetMutatingPod(ctx context.Context, req admiss if err != nil { return err } else if len(sidecarContainers) == 0 && len(sidecarInitContainers) == 0 { - klog.V(3).Infof("[sidecar inject] pod(%s.%s) don't have injected containers", pod.Namespace, pod.Name) + klog.V(3).Infof("[sidecar inject] pod(%s/%s) don't have injected containers", pod.Namespace, pod.Name) return nil } klog.V(3).Infof("[sidecar inject] begin inject sidecarContainers(%v) sidecarInitContainers(%v) sidecarSecrets(%v), volumes(%s)"+ - "annotations(%v) into pod(%s.%s)", sidecarContainers, sidecarInitContainers, sidecarSecrets, volumesInSidecar, injectedAnnotations, + "annotations(%v) into pod(%s/%s)", sidecarContainers, sidecarInitContainers, sidecarSecrets, volumesInSidecar, injectedAnnotations, pod.Namespace, pod.Name) klog.V(4).Infof("[sidecar inject] before mutating: %v", util.DumpJSON(pod)) // apply sidecar set info into pod @@ -203,7 +203,7 @@ func buildSidecars(isUpdated bool, pod *corev1.Pod, oldPod *corev1.Pod, matchedS olderSidecarSetHash := make(map[string]string) if err = json.Unmarshal([]byte(oldHashStr), &olderSidecarSetHash); err != nil { return nil, nil, nil, nil, nil, - fmt.Errorf("pod(%s.%s) invalid annotations[%s] value %v, unmarshal failed: %v", pod.Namespace, pod.Name, sidecarcontrol.SidecarSetHashAnnotation, oldHashStr, err) + fmt.Errorf("pod(%s/%s) invalid annotations[%s] value %v, unmarshal failed: %v", pod.Namespace, pod.Name, sidecarcontrol.SidecarSetHashAnnotation, oldHashStr, err) } for k, v := range olderSidecarSetHash { sidecarSetHash[k] = sidecarcontrol.SidecarSetUpgradeSpec{ @@ -219,7 +219,7 @@ func buildSidecars(isUpdated bool, pod *corev1.Pod, oldPod *corev1.Pod, matchedS olderSidecarSetHash := make(map[string]string) if err = json.Unmarshal([]byte(oldHashStr), &olderSidecarSetHash); err != nil { return nil, nil, nil, nil, nil, - fmt.Errorf("pod(%s.%s) invalid annotations[%s] value %v, unmarshal failed: %v", pod.Namespace, pod.Name, sidecarcontrol.SidecarSetHashWithoutImageAnnotation, oldHashStr, err) + fmt.Errorf("pod(%s/%s) invalid annotations[%s] value %v, unmarshal failed: %v", pod.Namespace, pod.Name, sidecarcontrol.SidecarSetHashWithoutImageAnnotation, oldHashStr, err) } for k, v := range olderSidecarSetHash { sidecarSetHashWithoutImage[k] = sidecarcontrol.SidecarSetUpgradeSpec{ @@ -234,7 +234,7 @@ func buildSidecars(isUpdated bool, pod *corev1.Pod, oldPod *corev1.Pod, matchedS sidecarSetNames := make([]string, 0) for _, control := range matchedSidecarSets { sidecarSet := control.GetSidecarset() - klog.V(3).Infof("build pod(%s.%s) sidecar containers for sidecarSet(%s)", pod.Namespace, pod.Name, sidecarSet.Name) + klog.V(3).Infof("build pod(%s/%s) sidecar containers for sidecarSet(%s)", pod.Namespace, pod.Name, sidecarSet.Name) // sidecarSet List sidecarSetNames = append(sidecarSetNames, sidecarSet.Name) // pre-process volumes only in sidecar diff --git a/pkg/webhook/pod/validating/pod_create_update_handler.go b/pkg/webhook/pod/validating/pod_create_update_handler.go index 37a0f824ff..1fd914c676 100644 --- a/pkg/webhook/pod/validating/pod_create_update_handler.go +++ b/pkg/webhook/pod/validating/pod_create_update_handler.go @@ -20,10 +20,10 @@ import ( "context" "net/http" + "github.com/openkruise/kruise/pkg/control/pubcontrol" "github.com/openkruise/kruise/pkg/features" "github.com/openkruise/kruise/pkg/util/controllerfinder" utilfeature "github.com/openkruise/kruise/pkg/util/feature" - admissionv1 "k8s.io/api/admission/v1" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" @@ -42,7 +42,8 @@ type PodCreateHandler struct { // Decoder decodes objects Decoder *admission.Decoder - finders *controllerfinder.ControllerFinder + finders *controllerfinder.ControllerFinder + pubControl pubcontrol.PubControl } func (h *PodCreateHandler) validatingPodFn(ctx context.Context, req admission.Request) (allowed bool, reason string, err error) { @@ -90,6 +91,7 @@ var _ inject.Client = &PodCreateHandler{} func (h *PodCreateHandler) InjectClient(c client.Client) error { h.Client = c h.finders = controllerfinder.NewControllerFinder(c) + h.pubControl = pubcontrol.NewPubControl(c) return nil } diff --git a/pkg/webhook/pod/validating/pod_unavailable_budget.go b/pkg/webhook/pod/validating/pod_unavailable_budget.go index 5990384bad..3ef521f968 100644 --- a/pkg/webhook/pod/validating/pod_unavailable_budget.go +++ b/pkg/webhook/pod/validating/pod_unavailable_budget.go @@ -80,7 +80,7 @@ func (p *PodCreateHandler) podUnavailableBudgetValidatingPod(ctx context.Context // filter out invalid Delete operation, only validate delete pods resources case admissionv1.Delete: if req.AdmissionRequest.SubResource != "" { - klog.V(6).Infof("pod(%s.%s) AdmissionRequest operation(DELETE) subResource(%s), then admit", req.Namespace, req.Name, req.SubResource) + klog.V(6).Infof("pod(%s/%s) AdmissionRequest operation(DELETE) subResource(%s), then admit", req.Namespace, req.Name, req.SubResource) return true, "", nil } if err := p.Decoder.DecodeRaw(req.OldObject, newPod); err != nil { @@ -98,7 +98,7 @@ func (p *PodCreateHandler) podUnavailableBudgetValidatingPod(ctx context.Context case admissionv1.Create: // ignore create operation other than subresource eviction if req.AdmissionRequest.SubResource != "eviction" { - klog.V(6).Infof("pod(%s.%s) AdmissionRequest operation(CREATE) Resource(%s) subResource(%s), then admit", req.Namespace, req.Name, req.Resource, req.SubResource) + klog.V(6).Infof("pod(%s/%s) AdmissionRequest operation(CREATE) Resource(%s) subResource(%s), then admit", req.Namespace, req.Name, req.Resource, req.SubResource) return true, "", nil } eviction := &policy.Eviction{} @@ -133,11 +133,11 @@ func (p *PodCreateHandler) podUnavailableBudgetValidatingPod(ctx context.Context // returns true for pod conditions that allow the operation for pod without checking PUB. if newPod.Status.Phase == corev1.PodSucceeded || newPod.Status.Phase == corev1.PodFailed || newPod.Status.Phase == corev1.PodPending || newPod.Status.Phase == "" || !newPod.ObjectMeta.DeletionTimestamp.IsZero() { - klog.V(3).Infof("pod(%s.%s) Status(%s) Deletion(%v), then admit", newPod.Namespace, newPod.Name, newPod.Status.Phase, !newPod.ObjectMeta.DeletionTimestamp.IsZero()) + klog.V(3).Infof("pod(%s/%s) Status(%s) Deletion(%v), then admit", newPod.Namespace, newPod.Name, newPod.Status.Phase, !newPod.ObjectMeta.DeletionTimestamp.IsZero()) return true, "", nil } - pub, err := pubcontrol.GetPodUnavailableBudgetForPod(p.Client, p.finders, newPod) + pub, err := p.pubControl.GetPubForPod(newPod) if err != nil { return false, "", err } @@ -145,21 +145,20 @@ func (p *PodCreateHandler) podUnavailableBudgetValidatingPod(ctx context.Context if pub == nil { return true, "", nil } - control := pubcontrol.NewPubControl(pub, p.finders, p.Client) - klog.V(3).Infof("validating pod(%s.%s) operation(%s) for pub(%s.%s)", newPod.Namespace, newPod.Name, req.Operation, pub.Namespace, pub.Name) + klog.V(3).Infof("validating pod(%s/%s) operation(%s) for pub(%s/%s)", newPod.Namespace, newPod.Name, req.Operation, pub.Namespace, pub.Name) // pods that contain annotations[pod.kruise.io/pub-no-protect]="true" will be ignore // and will no longer check the pub quota if newPod.Annotations[pubcontrol.PodPubNoProtectionAnnotation] == "true" { - klog.V(3).Infof("pod(%s.%s) contains annotations[%s], then don't need check pub", newPod.Namespace, newPod.Name, pubcontrol.PodPubNoProtectionAnnotation) + klog.V(3).Infof("pod(%s/%s) contains annotations[%s], then don't need check pub", newPod.Namespace, newPod.Name, pubcontrol.PodPubNoProtectionAnnotation) return true, "", nil } // the change will not cause pod unavailability, then pass - if !control.IsPodUnavailableChanged(oldPod, newPod) { - klog.V(3).Infof("validate pod(%s.%s) changed cannot cause unavailability, then don't need check pub", newPod.Namespace, newPod.Name) + if !p.pubControl.IsPodUnavailableChanged(oldPod, newPod) { + klog.V(3).Infof("validate pod(%s/%s) changed cannot cause unavailability, then don't need check pub", newPod.Namespace, newPod.Name) return true, "", nil } - return pubcontrol.PodUnavailableBudgetValidatePod(p.Client, newPod, control, pubcontrol.Operation(req.Operation), dryRun) + return pubcontrol.PodUnavailableBudgetValidatePod(p.Client, p.pubControl, pub, newPod, pubcontrol.Operation(req.Operation), dryRun) } diff --git a/pkg/webhook/pod/validating/pod_unavailable_budget_test.go b/pkg/webhook/pod/validating/pod_unavailable_budget_test.go index c8a556290a..4290b21ca7 100644 --- a/pkg/webhook/pod/validating/pod_unavailable_budget_test.go +++ b/pkg/webhook/pod/validating/pod_unavailable_budget_test.go @@ -24,9 +24,9 @@ import ( appspub "github.com/openkruise/kruise/apis/apps/pub" policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1" + "github.com/openkruise/kruise/pkg/control/pubcontrol" "github.com/openkruise/kruise/pkg/control/sidecarcontrol" "github.com/openkruise/kruise/pkg/util" - admissionv1 "k8s.io/api/admission/v1" apps "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -86,10 +86,12 @@ var ( podDemo = &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: "test-pod-0", - Namespace: "default", - Labels: map[string]string{"app": "pub-controller"}, - Annotations: map[string]string{}, + Name: "test-pod-0", + Namespace: "default", + Labels: map[string]string{"app": "pub-controller"}, + Annotations: map[string]string{ + pubcontrol.PodRelatedPubAnnotation: pubDemo.Name, + }, }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ @@ -352,11 +354,13 @@ func TestValidateUpdatePodForPub(t *testing.T) { name: "valid update pod, no matched pub, allow", oldPod: func() *corev1.Pod { pod := podDemo.DeepCopy() + delete(pod.Annotations, pubcontrol.PodRelatedPubAnnotation) return pod }, newPod: func() *corev1.Pod { pod := podDemo.DeepCopy() pod.Labels["app"] = "no-pub" + delete(pod.Annotations, pubcontrol.PodRelatedPubAnnotation) pod.Spec.Containers[0].Image = "nginx:1.18" return pod }, @@ -424,8 +428,9 @@ func TestValidateUpdatePodForPub(t *testing.T) { decoder, _ := admission.NewDecoder(scheme) fClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cs.pub()).Build() podHandler := PodCreateHandler{ - Client: fClient, - Decoder: decoder, + Client: fClient, + Decoder: decoder, + pubControl: pubcontrol.NewPubControl(fClient), } oldPodRaw := runtime.RawExtension{ Raw: []byte(util.DumpJSON(cs.oldPod())), @@ -581,8 +586,9 @@ func TestValidateEvictPodForPub(t *testing.T) { decoder, _ := admission.NewDecoder(scheme) fClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cs.pub(), cs.newPod()).Build() podHandler := PodCreateHandler{ - Client: fClient, - Decoder: decoder, + Client: fClient, + Decoder: decoder, + pubControl: pubcontrol.NewPubControl(fClient), } evictionRaw := runtime.RawExtension{ Raw: []byte(util.DumpJSON(cs.eviction())), @@ -715,8 +721,9 @@ func TestValidateDeletePodForPub(t *testing.T) { decoder, _ := admission.NewDecoder(scheme) fClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cs.pub(), cs.newPod()).Build() podHandler := PodCreateHandler{ - Client: fClient, - Decoder: decoder, + Client: fClient, + Decoder: decoder, + pubControl: pubcontrol.NewPubControl(fClient), } deletionRaw := runtime.RawExtension{ Raw: []byte(util.DumpJSON(cs.deletion())), diff --git a/test/e2e/apps/daemonset.go b/test/e2e/apps/daemonset.go index 4cd20729c7..f06b673a00 100644 --- a/test/e2e/apps/daemonset.go +++ b/test/e2e/apps/daemonset.go @@ -44,7 +44,7 @@ var _ = SIGDescribe("DaemonSet", func() { if ginkgo.CurrentGinkgoTestDescription().Failed { framework.DumpDebugInfo(c, ns) } - framework.Logf("Deleting DaemonSet %s.%s in cluster", ns, dsName) + framework.Logf("Deleting DaemonSet %s/%s in cluster", ns, dsName) tester.DeleteDaemonSet(ns, dsName) }) diff --git a/test/e2e/apps/resourcedistribution.go b/test/e2e/apps/resourcedistribution.go index a19bbeb69d..bcfb25066a 100644 --- a/test/e2e/apps/resourcedistribution.go +++ b/test/e2e/apps/resourcedistribution.go @@ -274,7 +274,7 @@ var _ = SIGDescribe("ResourceDistribution", func() { consistentVersion := hex.EncodeToString(md5Hash[:]) for namespace := range matchedNamespaces { object, err := tester.GetSecret(namespace, secretName, true) - ginkgo.By(fmt.Sprintf("checking distributed secret(%s.%s).", namespace, secretName)) + ginkgo.By(fmt.Sprintf("checking distributed secret(%s/%s).", namespace, secretName)) gomega.Expect(err).NotTo(gomega.HaveOccurred()) gomega.Expect(object.GetAnnotations()).ShouldNot(gomega.BeNil()) version := object.Annotations[utils.ResourceHashCodeAnnotation] @@ -329,7 +329,7 @@ var _ = SIGDescribe("ResourceDistribution", func() { ginkgo.By("checking created secrets...") for namespace := range matchedNamespaces { object, err := tester.GetSecret(namespace, secretName, true) - ginkgo.By(fmt.Sprintf("checking distributed secret(%s.%s).", namespace, secretName)) + ginkgo.By(fmt.Sprintf("checking distributed secret(%s/%s).", namespace, secretName)) gomega.Expect(err).NotTo(gomega.HaveOccurred()) gomega.Expect(object).ShouldNot(gomega.BeNil()) version := object.Annotations[utils.ResourceHashCodeAnnotation] diff --git a/test/e2e/apps/sidecarset.go b/test/e2e/apps/sidecarset.go index f6b119ba4c..6a01796d00 100644 --- a/test/e2e/apps/sidecarset.go +++ b/test/e2e/apps/sidecarset.go @@ -77,7 +77,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deployment := tester.NewBaseDeployment(ns) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deployment.Namespace, deployment.Name)) tester.CreateDeployment(deployment) // get pods @@ -134,7 +134,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deployment := tester.NewBaseDeployment(ns) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deployment.Namespace, deployment.Name)) tester.CreateDeployment(deployment) // get pods @@ -216,7 +216,7 @@ var _ = SIGDescribe("SidecarSet", func() { time.Sleep(time.Second) deploymentIn := cs.getDeployment() - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // get pods pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) @@ -319,7 +319,7 @@ var _ = SIGDescribe("SidecarSet", func() { time.Sleep(time.Second) deploymentIn := cs.getDeployment() - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // get pods pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) @@ -392,7 +392,7 @@ var _ = SIGDescribe("SidecarSet", func() { Value: "127.0.0.1", }, } - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // get pods pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) @@ -467,7 +467,7 @@ var _ = SIGDescribe("SidecarSet", func() { Value: "127.0.0.1", }, } - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // get pods pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) @@ -542,7 +542,7 @@ var _ = SIGDescribe("SidecarSet", func() { Value: "127.0.0.1", }, } - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // get pods pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) @@ -595,7 +595,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(2) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // update sidecarSet sidecar container sidecarSetIn.Spec.Containers[0].Image = BusyboxImage @@ -635,7 +635,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(2) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) sidecarSetIn, err := kc.AppsV1alpha1().SidecarSets().Get(context.TODO(), sidecarSetIn.Name, metav1.GetOptions{}) @@ -725,7 +725,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(2) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // update sidecarSet sidecar container sidecarSetIn.Spec.Containers[0].Image = BusyboxImage @@ -769,7 +769,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(2) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // update pod[0] labels[canary.release] = true pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) @@ -842,7 +842,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(2) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) except := &appsv1alpha1.SidecarSetStatus{ @@ -897,7 +897,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(4) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // update sidecarSet sidecar container @@ -945,7 +945,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(1) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // check sidecarSet diff --git a/test/e2e/apps/sidecarset_hotupgrade.go b/test/e2e/apps/sidecarset_hotupgrade.go index 7707dcda70..87a0026ae9 100644 --- a/test/e2e/apps/sidecarset_hotupgrade.go +++ b/test/e2e/apps/sidecarset_hotupgrade.go @@ -72,7 +72,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // get pods pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) @@ -116,7 +116,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(1) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // check pod image and annotations pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) @@ -227,7 +227,7 @@ var _ = SIGDescribe("SidecarSet", func() { // create deployment deploymentIn := tester.NewBaseDeployment(ns) deploymentIn.Spec.Replicas = utilpointer.Int32Ptr(2) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deploymentIn.Namespace, deploymentIn.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deploymentIn.Namespace, deploymentIn.Name)) tester.CreateDeployment(deploymentIn) // check pod image and annotations pods, err := tester.GetSelectorPods(deploymentIn.Namespace, deploymentIn.Spec.Selector) diff --git a/test/e2e/framework/daemonset_util.go b/test/e2e/framework/daemonset_util.go index ec4f5ec2ab..9df14fc5b5 100644 --- a/test/e2e/framework/daemonset_util.go +++ b/test/e2e/framework/daemonset_util.go @@ -108,7 +108,7 @@ func (t *DaemonSetTester) UpdateDaemonSet(name string, fn func(ds *appsv1alpha1. func (t *DaemonSetTester) DeleteDaemonSet(namespace, name string) { err := t.kc.AppsV1alpha1().DaemonSets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{}) if err != nil { - Logf("delete daemonset(%s.%s) failed: %s", t.ns, name, err.Error()) + Logf("delete daemonset(%s/%s) failed: %s", t.ns, name, err.Error()) return } } diff --git a/test/e2e/framework/ephemeraljob_utils.go b/test/e2e/framework/ephemeraljob_utils.go index a2136c183f..7ef41216ee 100644 --- a/test/e2e/framework/ephemeraljob_utils.go +++ b/test/e2e/framework/ephemeraljob_utils.go @@ -151,7 +151,7 @@ func (t *EphemeralJobTester) CreateTestDeployment(randStr string, replicas int32 } var err error - Logf("create deployment(%s.%s)", deployment.Namespace, deployment.Name) + Logf("create deployment(%s/%s)", deployment.Namespace, deployment.Name) _, err = t.c.AppsV1().Deployments(deployment.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) t.WaitForDeploymentRunning(deployment) @@ -179,7 +179,7 @@ func (t *EphemeralJobTester) CreateTestEphemeralJob(randStr string, replicas, Pa }, } - Logf("create ephemeral job(%s.%s)", job.Namespace, job.Name) + Logf("create ephemeral job(%s/%s)", job.Namespace, job.Name) job, _ = t.kc.AppsV1alpha1().EphemeralJobs(t.ns).Create(context.TODO(), job, metav1.CreateOptions{}) t.WaitForEphemeralJobCreated(job) @@ -189,7 +189,7 @@ func (t *EphemeralJobTester) CreateTestEphemeralJob(randStr string, replicas, Pa func (t *EphemeralJobTester) CreateEphemeralJob(job *appsv1alpha1.EphemeralJob) *appsv1alpha1.EphemeralJob { job.Namespace = t.ns - Logf("create ephemeral job(%s.%s)", job.Namespace, job.Name) + Logf("create ephemeral job(%s/%s)", job.Namespace, job.Name) job, _ = t.kc.AppsV1alpha1().EphemeralJobs(t.ns).Create(context.TODO(), job, metav1.CreateOptions{}) t.WaitForEphemeralJobCreated(job) return job @@ -232,7 +232,7 @@ func (s *EphemeralJobTester) DeleteDeployments(namespace string) { func (s *EphemeralJobTester) DeleteDeployment(deployment *apps.Deployment) { err := s.c.AppsV1().Deployments(deployment.Namespace).Delete(context.TODO(), deployment.Name, metav1.DeleteOptions{}) if err != nil { - Logf("delete deployment(%s.%s) failed: %s", deployment.Namespace, deployment.Name, err.Error()) + Logf("delete deployment(%s/%s) failed: %s", deployment.Namespace, deployment.Name, err.Error()) return } s.WaitForDeploymentDeleted(deployment) diff --git a/test/e2e/framework/podunavailablebudget_util.go b/test/e2e/framework/podunavailablebudget_util.go index 33c01573ef..5c3095f83f 100644 --- a/test/e2e/framework/podunavailablebudget_util.go +++ b/test/e2e/framework/podunavailablebudget_util.go @@ -101,8 +101,9 @@ func (s *PodUnavailableBudgetTester) NewBaseDeployment(namespace string) *apps.D Spec: corev1.PodSpec{ Containers: []corev1.Container{ { - Name: "main", - Image: imageutils.GetE2EImage(imageutils.Httpd), + Name: "main", + Image: imageutils.GetE2EImage(imageutils.Httpd), + ImagePullPolicy: corev1.PullIfNotPresent, }, }, }, @@ -152,8 +153,9 @@ func (s *PodUnavailableBudgetTester) NewBaseCloneSet(namespace string) *appsv1al Spec: corev1.PodSpec{ Containers: []corev1.Container{ { - Name: "main", - Image: imageutils.GetE2EImage(imageutils.Httpd), + Name: "main", + Image: imageutils.GetE2EImage(imageutils.Httpd), + ImagePullPolicy: corev1.PullIfNotPresent, }, }, }, @@ -174,7 +176,7 @@ func (s *PodUnavailableBudgetTester) NewBaseCloneSet(namespace string) *appsv1al } func (t *PodUnavailableBudgetTester) CreatePub(pub *policyv1alpha1.PodUnavailableBudget) *policyv1alpha1.PodUnavailableBudget { - Logf("create PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name) + Logf("create PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name) _, err := t.kc.PolicyV1alpha1().PodUnavailableBudgets(pub.Namespace).Create(context.TODO(), pub, metav1.CreateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) t.WaitForPubCreated(pub) @@ -183,19 +185,19 @@ func (t *PodUnavailableBudgetTester) CreatePub(pub *policyv1alpha1.PodUnavailabl } func (t *PodUnavailableBudgetTester) CreateDeployment(deployment *apps.Deployment) { - Logf("create deployment(%s.%s)", deployment.Namespace, deployment.Name) + Logf("create deployment(%s/%s)", deployment.Namespace, deployment.Name) _, err := t.c.AppsV1().Deployments(deployment.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) t.WaitForDeploymentRunning(deployment) - Logf("create deployment(%s.%s) done", deployment.Namespace, deployment.Name) + Logf("create deployment(%s/%s) done", deployment.Namespace, deployment.Name) } func (t *PodUnavailableBudgetTester) CreateCloneSet(cloneset *appsv1alpha1.CloneSet) *appsv1alpha1.CloneSet { - Logf("create CloneSet(%s.%s)", cloneset.Namespace, cloneset.Name) + Logf("create CloneSet(%s/%s)", cloneset.Namespace, cloneset.Name) _, err := t.kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Create(context.TODO(), cloneset, metav1.CreateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) t.WaitForCloneSetRunning(cloneset) - Logf("create cloneset(%s.%s) done", cloneset.Namespace, cloneset.Name) + Logf("create cloneset(%s/%s) done", cloneset.Namespace, cloneset.Name) cloneset, _ = t.kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Get(context.TODO(), cloneset.Name, metav1.GetOptions{}) return cloneset } @@ -319,7 +321,7 @@ func (t *PodUnavailableBudgetTester) DeletePubs(namespace string) { for _, pub := range pubList.Items { err := t.kc.PolicyV1alpha1().PodUnavailableBudgets(namespace).Delete(context.TODO(), pub.Name, metav1.DeleteOptions{}) if err != nil { - Logf("delete PodUnavailableBudget(%s.%s) failed: %s", pub.Namespace, pub.Name, err.Error()) + Logf("delete PodUnavailableBudget(%s/%s) failed: %s", pub.Namespace, pub.Name, err.Error()) } } } @@ -334,7 +336,7 @@ func (t *PodUnavailableBudgetTester) DeleteDeployments(namespace string) { for _, deployment := range deploymentList.Items { err := t.c.AppsV1().Deployments(namespace).Delete(context.TODO(), deployment.Name, metav1.DeleteOptions{}) if err != nil { - Logf("delete Deployment(%s.%s) failed: %s", deployment.Namespace, deployment.Name, err.Error()) + Logf("delete Deployment(%s/%s) failed: %s", deployment.Namespace, deployment.Name, err.Error()) continue } t.WaitForDeploymentDeleted(&deployment) @@ -351,7 +353,7 @@ func (t *PodUnavailableBudgetTester) DeleteCloneSets(namespace string) { for _, object := range objectList.Items { err := t.kc.AppsV1alpha1().CloneSets(namespace).Delete(context.TODO(), object.Name, metav1.DeleteOptions{}) if err != nil { - Logf("delete CloneSet(%s.%s) failed: %s", object.Namespace, object.Name, err.Error()) + Logf("delete CloneSet(%s/%s) failed: %s", object.Namespace, object.Name, err.Error()) continue } t.WaitForCloneSetDeleted(&object) diff --git a/test/e2e/framework/resourcedistribution_utils.go b/test/e2e/framework/resourcedistribution_utils.go index b79fdb4209..f5dc6d7382 100644 --- a/test/e2e/framework/resourcedistribution_utils.go +++ b/test/e2e/framework/resourcedistribution_utils.go @@ -172,7 +172,7 @@ func (s *ResourceDistributionTester) CreateNamespaces(namespaces ...*corev1.Name func (s *ResourceDistributionTester) CreateSecretResources(secrets ...*corev1.Secret) { for _, secret := range secrets { - Logf("create secrets(%s.%s)", secret.Namespace, secret.Name) + Logf("create secrets(%s/%s)", secret.Namespace, secret.Name) err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { _, err := s.c.CoreV1().Secrets(secret.Namespace).Create(context.TODO(), secret, metav1.CreateOptions{}) return err @@ -322,7 +322,7 @@ func (s *ResourceDistributionTester) WaitForSecretCreated(namespace, name string return true, nil }) if pollErr != nil { - Failf("Failed waiting for secret(%s.%s) to enter created: %v", namespace, name, pollErr) + Failf("Failed waiting for secret(%s/%s) to enter created: %v", namespace, name, pollErr) } } diff --git a/test/e2e/framework/sidecarset_utils.go b/test/e2e/framework/sidecarset_utils.go index 7dc13c195d..07c2eac913 100644 --- a/test/e2e/framework/sidecarset_utils.go +++ b/test/e2e/framework/sidecarset_utils.go @@ -73,9 +73,10 @@ func (s *SidecarSetTester) NewBaseSidecarSet(ns string) *appsv1alpha1.SidecarSet Containers: []appsv1alpha1.SidecarContainer{ { Container: corev1.Container{ - Name: "nginx-sidecar", - Image: imageutils.GetE2EImage(imageutils.Nginx), - Command: []string{"tail", "-f", "/dev/null"}, + Name: "nginx-sidecar", + Image: imageutils.GetE2EImage(imageutils.Nginx), + ImagePullPolicy: corev1.PullIfNotPresent, + Command: []string{"tail", "-f", "/dev/null"}, }, PodInjectPolicy: appsv1alpha1.BeforeAppContainerType, ShareVolumePolicy: appsv1alpha1.ShareVolumePolicy{ @@ -163,7 +164,7 @@ func (s *SidecarSetTester) UpdateSidecarSet(sidecarSet *appsv1alpha1.SidecarSet) } func (s *SidecarSetTester) UpdatePod(pod *corev1.Pod) { - Logf("update pod(%s.%s)", pod.Namespace, pod.Name) + Logf("update pod(%s/%s)", pod.Namespace, pod.Name) podClone := pod.DeepCopy() err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { podClone.Annotations = pod.Annotations @@ -201,7 +202,7 @@ func (s *SidecarSetTester) WaitForSidecarSetUpgradeComplete(sidecarSet *appsv1al } func (s *SidecarSetTester) CreateDeployment(deployment *apps.Deployment) { - Logf("create deployment(%s.%s)", deployment.Namespace, deployment.Name) + Logf("create deployment(%s/%s)", deployment.Namespace, deployment.Name) _, err := s.c.AppsV1().Deployments(deployment.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) s.WaitForDeploymentRunning(deployment) @@ -242,7 +243,7 @@ func (s *SidecarSetTester) DeleteDeployments(namespace string) { func (s *SidecarSetTester) DeleteDeployment(deployment *apps.Deployment) { err := s.c.AppsV1().Deployments(deployment.Namespace).Delete(context.TODO(), deployment.Name, metav1.DeleteOptions{}) if err != nil { - Logf("delete deployment(%s.%s) failed: %s", deployment.Namespace, deployment.Name, err.Error()) + Logf("delete deployment(%s/%s) failed: %s", deployment.Namespace, deployment.Name, err.Error()) return } s.WaitForDeploymentDeleted(deployment) @@ -381,11 +382,11 @@ func (s *SidecarSetTester) NewBaseCloneSet(namespace string) *appsv1alpha1.Clone } func (t *SidecarSetTester) CreateCloneSet(cloneset *appsv1alpha1.CloneSet) *appsv1alpha1.CloneSet { - Logf("create CloneSet(%s.%s)", cloneset.Namespace, cloneset.Name) + Logf("create CloneSet(%s/%s)", cloneset.Namespace, cloneset.Name) _, err := t.kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Create(context.TODO(), cloneset, metav1.CreateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) t.WaitForCloneSetRunning(cloneset) - Logf("create cloneset(%s.%s) done", cloneset.Namespace, cloneset.Name) + Logf("create cloneset(%s/%s) done", cloneset.Namespace, cloneset.Name) cloneset, _ = t.kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Get(context.TODO(), cloneset.Name, metav1.GetOptions{}) return cloneset } diff --git a/test/e2e/policy/podunavailablebudget.go b/test/e2e/policy/podunavailablebudget.go index 3bcb7d9b7a..031689afcf 100644 --- a/test/e2e/policy/podunavailablebudget.go +++ b/test/e2e/policy/podunavailablebudget.go @@ -67,19 +67,20 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { }) ginkgo.It("PodUnavailableBudget selector no matched pods", func() { - // create pub - pub := tester.NewBasePub(ns) - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) - tester.CreatePub(pub) - // create deployment deployment := tester.NewBaseDeployment(ns) deployment.Spec.Selector.MatchLabels["pub-controller"] = "false" deployment.Spec.Template.Labels["pub-controller"] = "false" - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deployment.Namespace, deployment.Name)) tester.CreateDeployment(deployment) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + // create pub + pub := tester.NewBasePub(ns) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) + tester.CreatePub(pub) + time.Sleep(time.Second * 5) + + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 0, DesiredAvailable: 0, @@ -105,16 +106,16 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { Type: intstr.Int, IntVal: 0, } - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create deployment deployment := tester.NewBaseDeployment(ns) deployment.Spec.Replicas = utilpointer.Int32Ptr(1) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deployment.Namespace, deployment.Name)) tester.CreateDeployment(deployment) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 0, DesiredAvailable: 1, @@ -163,11 +164,11 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { time.Sleep(time.Second * 5) // delete deployment - ginkgo.By(fmt.Sprintf("Deleting Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("Deleting Deployment(%s/%s)", deployment.Namespace, deployment.Name)) err = c.AppsV1().Deployments(deployment.Namespace).Delete(context.TODO(), deployment.Name, metav1.DeleteOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 0, DesiredAvailable: 0, @@ -193,16 +194,16 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { ginkgo.It("PodUnavailableBudget selector pods and scale down deployment ignore", func() { // create pub pub := tester.NewBasePub(ns) - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create deployment deployment := tester.NewBaseDeployment(ns) deployment.Spec.Replicas = utilpointer.Int32Ptr(4) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deployment.Namespace, deployment.Name)) tester.CreateDeployment(deployment) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 1, DesiredAvailable: 3, @@ -220,12 +221,12 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { }, 30*time.Second, time.Second).Should(gomega.Equal(expectStatus)) // scale down deployment - ginkgo.By(fmt.Sprintf("scale down Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("scale down Deployment(%s/%s)", deployment.Namespace, deployment.Name)) deployment.Spec.Replicas = utilpointer.Int32Ptr(0) _, err = c.AppsV1().Deployments(deployment.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ DesiredAvailable: 0, TotalReplicas: 0, @@ -259,15 +260,15 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { Kind: "Deployment", Name: "webserver", } - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create deployment deployment := tester.NewBaseDeployment(ns) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deployment.Namespace, deployment.Name)) tester.CreateDeployment(deployment) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 1, DesiredAvailable: 1, @@ -285,12 +286,12 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { }, 30*time.Second, time.Second).Should(gomega.Equal(expectStatus)) // update failed image - ginkgo.By(fmt.Sprintf("update Deployment(%s.%s) failed image", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("update Deployment(%s/%s) failed image", deployment.Namespace, deployment.Name)) deployment.Spec.Template.Spec.Containers[0].Image = InvalidImage _, err = c.AppsV1().Deployments(deployment.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 0, DesiredAvailable: 1, @@ -319,12 +320,12 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { gomega.Expect(noUpdatePods).To(gomega.HaveLen(1)) // update success image - ginkgo.By(fmt.Sprintf("update Deployment(%s.%s) success image", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("update Deployment(%s/%s) success image", deployment.Namespace, deployment.Name)) deployment.Spec.Template.Spec.Containers[0].Image = NewWebserverImage _, err = c.AppsV1().Deployments(deployment.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 1, DesiredAvailable: 1, @@ -362,7 +363,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { Type: intstr.String, StrVal: "50%", } - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create deployment1 @@ -370,15 +371,15 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { deployment.Spec.Replicas = utilpointer.Int32Ptr(5) deploymentIn1 := deployment.DeepCopy() deploymentIn1.Name = fmt.Sprintf("%s-1", deploymentIn1.Name) - ginkgo.By(fmt.Sprintf("Creating Deployment1(%s.%s)", deploymentIn1.Namespace, deploymentIn1.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment1(%s/%s)", deploymentIn1.Namespace, deploymentIn1.Name)) tester.CreateDeployment(deploymentIn1) // create deployment2 deploymentIn2 := deployment.DeepCopy() deploymentIn2.Name = fmt.Sprintf("%s-2", deploymentIn1.Name) - ginkgo.By(fmt.Sprintf("Creating Deployment2(%s.%s)", deploymentIn2.Namespace, deploymentIn2.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment2(%s/%s)", deploymentIn2.Namespace, deploymentIn2.Name)) tester.CreateDeployment(deploymentIn2) - ginkgo.By(fmt.Sprintf("PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 5, DesiredAvailable: 5, @@ -442,7 +443,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { ginkgo.It("PodUnavailableBudget selector SidecarSet, inject sidecar container, update failed sidecar image, block", func() { // create pub pub := tester.NewBasePub(ns) - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create sidecarset @@ -476,7 +477,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { // create deployment deployment := tester.NewBaseDeployment(ns) deployment.Spec.Replicas = utilpointer.Int32Ptr(5) - ginkgo.By(fmt.Sprintf("Creating Deployment(%s.%s)", deployment.Namespace, deployment.Name)) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deployment.Namespace, deployment.Name)) tester.CreateDeployment(deployment) time.Sleep(time.Second) @@ -581,16 +582,16 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { ginkgo.It("PodUnavailableBudget selector cloneSet, strategy.type=recreate, update failed image and block", func() { // create pub pub := tester.NewBasePub(ns) - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create cloneset cloneset := tester.NewBaseCloneSet(ns) - ginkgo.By(fmt.Sprintf("Creating CloneSet(%s.%s)", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("Creating CloneSet(%s/%s)", cloneset.Namespace, cloneset.Name)) cloneset = tester.CreateCloneSet(cloneset) // wait 10 seconds - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 1, DesiredAvailable: 1, @@ -608,13 +609,13 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { }, 30*time.Second, time.Second).Should(gomega.Equal(expectStatus)) // update failed image - ginkgo.By(fmt.Sprintf("update CloneSet(%s.%s) with failed image", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("update CloneSet(%s/%s) with failed image", cloneset.Namespace, cloneset.Name)) cloneset.Spec.Template.Spec.Containers[0].Image = InvalidImage _, err = kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Update(context.TODO(), cloneset, metav1.UpdateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) //wait 20 seconds - ginkgo.By(fmt.Sprintf("waiting 20 seconds, and check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("waiting 20 seconds, and check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 0, DesiredAvailable: 1, @@ -643,7 +644,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { gomega.Expect(noUpdatePods).To(gomega.HaveLen(1)) // update success image - ginkgo.By(fmt.Sprintf("update CloneSet(%s.%s) success image", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("update CloneSet(%s/%s) success image", cloneset.Namespace, cloneset.Name)) cloneset, _ = kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Get(context.TODO(), cloneset.Name, metav1.GetOptions{}) cloneset.Spec.Template.Spec.Containers[0].Image = NewWebserverImage cloneset, err = kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Update(context.TODO(), cloneset, metav1.UpdateOptions{}) @@ -651,7 +652,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { tester.WaitForCloneSetMinReadyAndRunning([]*appsv1alpha1.CloneSet{cloneset}, 1) // check pub status - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 1, DesiredAvailable: 1, @@ -684,16 +685,16 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { ginkgo.It("PodUnavailableBudget selector cloneSet, strategy.type=in-place, update failed image and block", func() { // create pub pub := tester.NewBasePub(ns) - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create cloneset cloneset := tester.NewBaseCloneSet(ns) cloneset.Spec.UpdateStrategy.Type = appsv1alpha1.InPlaceOnlyCloneSetUpdateStrategyType - ginkgo.By(fmt.Sprintf("Creating CloneSet(%s.%s)", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("Creating CloneSet(%s/%s)", cloneset.Namespace, cloneset.Name)) cloneset = tester.CreateCloneSet(cloneset) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 1, DesiredAvailable: 1, @@ -711,13 +712,13 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { }, 30*time.Second, time.Second).Should(gomega.Equal(expectStatus)) // update failed image - ginkgo.By(fmt.Sprintf("update CloneSet(%s.%s) with failed image", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("update CloneSet(%s/%s) with failed image", cloneset.Namespace, cloneset.Name)) cloneset.Spec.Template.Spec.Containers[0].Image = InvalidImage _, err = kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Update(context.TODO(), cloneset, metav1.UpdateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) //wait 20 seconds - ginkgo.By(fmt.Sprintf("waiting 20 seconds, and check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("waiting 20 seconds, and check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 0, DesiredAvailable: 1, @@ -746,7 +747,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { gomega.Expect(noUpdatePods).To(gomega.HaveLen(1)) // update success image - ginkgo.By(fmt.Sprintf("update CloneSet(%s.%s) success image", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("update CloneSet(%s/%s) success image", cloneset.Namespace, cloneset.Name)) err = retry.RetryOnConflict(retry.DefaultRetry, func() error { cloneset, err = kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Get(context.TODO(), cloneset.Name, metav1.GetOptions{}) if err != nil { @@ -760,7 +761,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { tester.WaitForCloneSetMinReadyAndRunning([]*appsv1alpha1.CloneSet{cloneset}, 1) //wait 20 seconds - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 1, DesiredAvailable: 1, @@ -797,7 +798,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { Type: intstr.String, StrVal: "20%", } - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create cloneset1 @@ -806,16 +807,16 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { cloneset.Spec.UpdateStrategy.Type = appsv1alpha1.InPlaceIfPossibleCloneSetUpdateStrategyType clonesetIn1 := cloneset.DeepCopy() clonesetIn1.Name = fmt.Sprintf("%s-1", clonesetIn1.Name) - ginkgo.By(fmt.Sprintf("Creating CloneSet1(%s.%s)", clonesetIn1.Namespace, clonesetIn1.Name)) + ginkgo.By(fmt.Sprintf("Creating CloneSet1(%s/%s)", clonesetIn1.Namespace, clonesetIn1.Name)) clonesetIn1 = tester.CreateCloneSet(clonesetIn1) //create cloneSet2 clonesetIn2 := cloneset.DeepCopy() clonesetIn2.Name = fmt.Sprintf("%s-2", clonesetIn2.Name) - ginkgo.By(fmt.Sprintf("Creating CloneSet2(%s.%s)", clonesetIn2.Namespace, clonesetIn2.Name)) + ginkgo.By(fmt.Sprintf("Creating CloneSet2(%s/%s)", clonesetIn2.Namespace, clonesetIn2.Name)) clonesetIn2 = tester.CreateCloneSet(clonesetIn2) // wait 10 seconds - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 2, DesiredAvailable: 8, @@ -833,7 +834,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { }, 30*time.Second, time.Second).Should(gomega.Equal(expectStatus)) // update failed image - ginkgo.By(fmt.Sprintf("update CloneSet(%s.%s) with failed image", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("update CloneSet(%s/%s) with failed image", cloneset.Namespace, cloneset.Name)) clonesetIn1.Spec.Template.Spec.Containers[0].Image = InvalidImage _, err = kc.AppsV1alpha1().CloneSets(clonesetIn1.Namespace).Update(context.TODO(), clonesetIn1, metav1.UpdateOptions{}) gomega.Expect(err).NotTo(gomega.HaveOccurred()) @@ -842,7 +843,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { gomega.Expect(err).NotTo(gomega.HaveOccurred()) //wait 20 seconds - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 0, DesiredAvailable: 8, @@ -871,7 +872,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { gomega.Expect(noUpdatePods).To(gomega.HaveLen(8)) // update success image - ginkgo.By(fmt.Sprintf("update CloneSet(%s.%s) success image", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("update CloneSet(%s/%s) success image", cloneset.Namespace, cloneset.Name)) err = retry.RetryOnConflict(retry.DefaultRetry, func() error { clonesetIn1, err = kc.AppsV1alpha1().CloneSets(clonesetIn1.Namespace).Get(context.TODO(), clonesetIn1.Name, metav1.GetOptions{}) if err != nil { @@ -896,7 +897,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { tester.WaitForCloneSetMinReadyAndRunning([]*appsv1alpha1.CloneSet{clonesetIn1, clonesetIn2}, 7) // check pub status - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 2, DesiredAvailable: 8, @@ -932,7 +933,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { Type: intstr.String, StrVal: "20%", } - ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s.%s)", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("Creating PodUnavailableBudget(%s/%s)", pub.Namespace, pub.Name)) tester.CreatePub(pub) // create sidecarSet @@ -966,7 +967,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { cloneset := tester.NewBaseCloneSet(ns) cloneset.Spec.UpdateStrategy.Type = appsv1alpha1.InPlaceOnlyCloneSetUpdateStrategyType cloneset.Spec.Replicas = utilpointer.Int32Ptr(10) - ginkgo.By(fmt.Sprintf("Creating CloneSet(%s.%s)", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("Creating CloneSet(%s/%s)", cloneset.Namespace, cloneset.Name)) cloneset = tester.CreateCloneSet(cloneset) time.Sleep(time.Second) @@ -977,7 +978,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { pod := pods[0] gomega.Expect(pod.Spec.Containers).To(gomega.HaveLen(len(cloneset.Spec.Template.Spec.Containers) + len(sidecarSet.Spec.Containers))) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus := &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 2, DesiredAvailable: 8, @@ -994,7 +995,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { }, 30*time.Second, time.Second).Should(gomega.Equal(expectStatus)) // update success image - ginkgo.By(fmt.Sprintf("update CloneSet(%s.%s) success image", cloneset.Namespace, cloneset.Name)) + ginkgo.By(fmt.Sprintf("update CloneSet(%s/%s) success image", cloneset.Namespace, cloneset.Name)) cloneset, _ = kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Get(context.TODO(), cloneset.Name, metav1.GetOptions{}) cloneset.Spec.Template.Spec.Containers[0].Image = NewWebserverImage cloneset, err = kc.AppsV1alpha1().CloneSets(cloneset.Namespace).Update(context.TODO(), cloneset, metav1.UpdateOptions{}) @@ -1013,7 +1014,7 @@ var _ = SIGDescribe("PodUnavailableBudget", func() { } sidecarTester.WaitForSidecarSetMinReadyAndUpgrade(sidecarSet, exceptSidecarSetStatus, 2) - ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s.%s) Status", pub.Namespace, pub.Name)) + ginkgo.By(fmt.Sprintf("check PodUnavailableBudget(%s/%s) Status", pub.Namespace, pub.Name)) expectStatus = &policyv1alpha1.PodUnavailableBudgetStatus{ UnavailableAllowed: 2, DesiredAvailable: 8,