Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Collaset respects PodOpsLifecycle label podopslifecycle.kusionstack.io/control #42

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
Comment on lines +40 to +49
Copy link
Collaborator

@Eikykun Eikykun Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ControlledByPodOpsLifecycle and ControlByPodOpsLifecycle path pkg/utils/common.go -> pkg/controllers/utils/podopslifecycle/utils.go may be more appropriat

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ControlledByPodOpsLifecycle and ControlByPodOpsLifecycle path pkg/utils/common.go -> pkg/controllers/utils/podopslifecycle/utils.go may be more appropriat

can't do it, because ControlledByPodOpsLifecycle is also be imported in pkg/webhook

Loading