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

fix collaset revision panic: issue#148 #153

Closed
wants to merge 1 commit into from
Closed
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 @@ -70,7 +70,7 @@ func NewReconciler(mgr ctrl.Manager) reconcile.Reconciler {

return &CollaSetReconciler{
ReconcilerMixin: mixin,
revisionManager: revision.NewRevisionManager(mixin.Client, mixin.Scheme, &revisionOwnerAdapter{}),
revisionManager: revision.NewRevisionManager(mixin.Client, mixin.Scheme, NewRevisionOwnerAdapter(podcontrol.NewRealPodControl(mixin.Client, mixin.Scheme))),
syncControl: synccontrol.NewRealSyncControl(mixin.Client, mixin.Logger, podcontrol.NewRealPodControl(mixin.Client, mixin.Scheme), mixin.Recorder),
}
}
Expand Down
27 changes: 25 additions & 2 deletions pkg/controllers/collaset/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package collaset
import (
"encoding/json"

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

appsv1alpha1 "kusionstack.io/operating/apis/apps/v1alpha1"
"kusionstack.io/operating/pkg/controllers/utils/revision"
"kusionstack.io/operating/pkg/controllers/collaset/podcontrol"
)

func getCollaSetPatch(cls *appsv1alpha1.CollaSet) ([]byte, error) {
Expand Down Expand Up @@ -53,9 +55,14 @@ func getCollaSetPatch(cls *appsv1alpha1.CollaSet) ([]byte, error) {
return patch, err
}

var _ revision.OwnerAdapter = &revisionOwnerAdapter{}
func NewRevisionOwnerAdapter(podControl podcontrol.Interface) revision.OwnerAdapter {
return &revisionOwnerAdapter{
podControl: podControl,
}
}

type revisionOwnerAdapter struct {
podControl podcontrol.Interface
}

func (roa *revisionOwnerAdapter) GetSelector(obj metav1.Object) *metav1.LabelSelector {
Expand Down Expand Up @@ -83,6 +90,22 @@ func (roa *revisionOwnerAdapter) GetCurrentRevision(obj metav1.Object) string {
return ips.Status.CurrentRevision
}

func (roa *revisionOwnerAdapter) IsInUsed(_ metav1.Object, _ string) bool {
func (roa *revisionOwnerAdapter) IsInUsed(obj metav1.Object, revision string) bool {
ips, _ := obj.(*appsv1alpha1.CollaSet)

if ips.Status.UpdatedRevision == revision || ips.Status.CurrentRevision == revision {
return true
}

pods, _ := roa.podControl.GetFilteredPods(ips.Spec.Selector, ips)
for _, pod := range pods {
if pod.Labels != nil {
currentRevisionName, exist := pod.Labels[appsv1.ControllerRevisionHashLabelKey]
if exist && currentRevisionName == revision {
return true
}
}
}

return false
}
9 changes: 3 additions & 6 deletions pkg/controllers/utils/revision/revision_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,10 @@ func (rm *RevisionManager) cleanExpiredRevision(set metav1.Object, sortedRevisio
return sortedRevisions, nil
}

var cleanedRevisions []*apps.ControllerRevision
for _, revision := range *sortedRevisions {
if exceedNum == 0 {
break
}

if rm.ownerGetter.IsInUsed(set, revision.Name) {
if exceedNum == 0 || rm.ownerGetter.IsInUsed(set, revision.Name) {
cleanedRevisions = append(cleanedRevisions, revision)
continue
}

Expand All @@ -209,7 +207,6 @@ func (rm *RevisionManager) cleanExpiredRevision(set metav1.Object, sortedRevisio

exceedNum--
}
cleanedRevisions := (*sortedRevisions)[exceedNum:]

return &cleanedRevisions, nil
}
Expand Down
Loading