Skip to content

Commit

Permalink
fix collaset revision panic: issue#148 (#154)
Browse files Browse the repository at this point in the history
fix(CollaSet): fix collaset revision panic: issue #148
  • Loading branch information
ColdsteelRail committed Feb 5, 2024
1 parent 4e14416 commit eb06583
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 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 @@ -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,9 +19,11 @@ 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/collaset/podcontrol"
"kusionstack.io/operating/pkg/controllers/utils/revision"
)

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

0 comments on commit eb06583

Please sign in to comment.