Skip to content

Commit

Permalink
Collaset respects PodOpsLifecycle label podopslifecycle.kusionstack.i…
Browse files Browse the repository at this point in the history
…o/control (#42)

* enhancement (CollaSet): Respect PodOpsLifecycle label podopslifecycle.kusionstack.io/control
  • Loading branch information
wu8685 committed Aug 21, 2023
1 parent f9cb738 commit 9bf0be1
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/collaset/collaset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func AddToMgr(mgr ctrl.Manager, r reconcile.Reconciler) error {
err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &appsv1alpha1.CollaSet{},
})
}, &PodPredicate{})
if err != nil {
return err
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/controllers/collaset/predicts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2023 The KusionStack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package collaset

import (
"sigs.k8s.io/controller-runtime/pkg/event"

"kusionstack.io/kafed/pkg/utils"
)

type PodPredicate struct {
}

// Create returns true if the Create event should be processed
func (p *PodPredicate) Create(e event.CreateEvent) bool {
return utils.ControlledByPodOpsLifecycle(e.Object)
}

// Delete returns true if the Delete event should be processed
func (p *PodPredicate) Delete(e event.DeleteEvent) bool {
return utils.ControlledByPodOpsLifecycle(e.Object)
}

// Update returns true if the Update event should be processed
func (p *PodPredicate) Update(e event.UpdateEvent) bool {
return utils.ControlledByPodOpsLifecycle(e.ObjectNew) || utils.ControlledByPodOpsLifecycle(e.ObjectOld)
}

// Generic returns true if the Generic event should be processed
func (p *PodPredicate) Generic(e event.GenericEvent) bool {
return utils.ControlledByPodOpsLifecycle(e.Object)
}
2 changes: 1 addition & 1 deletion pkg/controllers/collaset/synccontrol/sync_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (sc *RealSyncControl) Scale(set *appsv1alpha1.CollaSet, podWrappers []*coll

// scale out new Pods with updatedRevision
// TODO use cache
pod, err := controllerutils.NewPodFrom(set, metav1.NewControllerRef(set, appsv1alpha1.GroupVersion.WithKind("CollaSet")), revision)
pod, err := collasetutils.NewPodFrom(set, metav1.NewControllerRef(set, appsv1alpha1.GroupVersion.WithKind("CollaSet")), revision)
if err != nil {
return fmt.Errorf("fail to new Pod from revision %s: %s", revision.Name, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/collaset/synccontrol/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ func (u *InPlaceIfPossibleUpdater) AnalyseAndGetUpdatedPod(cls *appsv1alpha1.Col
// 1. build pod from current and updated revision
ownerRef := metav1.NewControllerRef(cls, appsv1alpha1.GroupVersion.WithKind("CollaSet"))
// TODO: use cache
currentPod, err := controllerutils.NewPodFrom(cls, ownerRef, podUpdateInfo.CurrentRevision)
currentPod, err := collasetutils.NewPodFrom(cls, ownerRef, podUpdateInfo.CurrentRevision)
if err != nil {
return false, false, nil, fmt.Errorf("fail to build Pod from current revision %s: %s", podUpdateInfo.CurrentRevision.Name, err)
}

// TODO: use cache
updatedPod, err = controllerutils.NewPodFrom(cls, ownerRef, updatedRevision)
updatedPod, err = collasetutils.NewPodFrom(cls, ownerRef, updatedRevision)
if err != nil {
return false, false, nil, fmt.Errorf("fail to build Pod from updated revision %s: %s", updatedRevision.Name, err)
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/controllers/collaset/utils/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ package utils

import (
"fmt"
corev1 "k8s.io/api/core/v1"
"strconv"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

appsv1alpha1 "kusionstack.io/kafed/apis/apps/v1alpha1"
controllerutils "kusionstack.io/kafed/pkg/controllers/utils"
"kusionstack.io/kafed/pkg/utils"
)

type PodWrapper struct {
Expand Down Expand Up @@ -57,3 +62,13 @@ func GetPodInstanceID(pod *corev1.Pod) (int, error) {

return int(id), nil
}

func NewPodFrom(owner metav1.Object, ownerRef *metav1.OwnerReference, revision *appsv1.ControllerRevision) (*corev1.Pod, error) {
pod, err := controllerutils.NewPodFrom(owner, ownerRef, revision)
if err != nil {
return nil, err
}

utils.ControlByPodOpsLifecycle(pod)
return pod, nil
}
10 changes: 10 additions & 0 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ func ControlledByPodOpsLifecycle(obj client.Object) bool {
v, ok := obj.GetLabels()[v1alpha1.ControlledByPodOpsLifecycle]
return ok && v == "true"
}

func ControlByPodOpsLifecycle(obj client.Object) {
if obj.GetLabels() == nil {
obj.SetLabels(map[string]string{})
}

if v, ok := obj.GetLabels()[v1alpha1.ControlledByPodOpsLifecycle]; !ok || v != "true" {
obj.GetLabels()[v1alpha1.ControlledByPodOpsLifecycle] = "true"
}
}

0 comments on commit 9bf0be1

Please sign in to comment.