Skip to content

Commit

Permalink
fix(controller): add statefulset gc for podgroup.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuqw committed Jul 17, 2024
1 parent 33a9eb5 commit 78e452a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions installer/helm/chart/volcano/templates/controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ rules:
resources: ["networkpolicies"]
verbs: ["get", "create", "delete"]
- apiGroups: ["apps"]
resources: ["daemonsets", "statefulsets"]
resources: ["daemonsets"]
verbs: ["get"]
- apiGroups: ["apps"]
resources: ["replicasets"]
resources: ["replicasets", "statefulsets"]
verbs: ["get", "list", "watch"]
- apiGroups: ["batch"]
resources: ["jobs"]
Expand Down
4 changes: 2 additions & 2 deletions installer/volcano-development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4304,10 +4304,10 @@ rules:
resources: ["networkpolicies"]
verbs: ["get", "create", "delete"]
- apiGroups: ["apps"]
resources: ["daemonsets", "statefulsets"]
resources: ["daemonsets"]
verbs: ["get"]
- apiGroups: ["apps"]
resources: ["replicasets"]
resources: ["replicasets", "statefulsets"]
verbs: ["get", "list", "watch"]
- apiGroups: ["batch"]
resources: ["jobs"]
Expand Down
11 changes: 10 additions & 1 deletion pkg/controllers/podgroup/pg_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type pgcontroller struct {
podInformer coreinformers.PodInformer
pgInformer schedulinginformer.PodGroupInformer
rsInformer appinformers.ReplicaSetInformer
stsInformer appinformers.StatefulSetInformer

informerFactory informers.SharedInformerFactory
vcInformerFactory vcinformer.SharedInformerFactory
Expand All @@ -64,7 +65,8 @@ type pgcontroller struct {
pgSynced func() bool

// A store of replicaset
rsSynced func() bool
rsSynced func() bool
stsSynced func() bool

queue workqueue.RateLimitingInterface

Expand Down Expand Up @@ -112,6 +114,13 @@ func (pg *pgcontroller) Initialize(opt *framework.ControllerOption) error {
AddFunc: pg.addReplicaSet,
UpdateFunc: pg.updateReplicaSet,
})

pg.stsInformer = pg.informerFactory.Apps().V1().StatefulSets()
pg.stsSynced = pg.stsInformer.Informer().HasSynced
pg.stsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: pg.addStatefulSet,
UpdateFunc: pg.updateStatefulSet,
})
}
return nil
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/controllers/podgroup/pg_controller_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ func (pg *pgcontroller) updateReplicaSet(oldObj, newObj interface{}) {
pg.addReplicaSet(newObj)
}

func (pg *pgcontroller) addStatefulSet(obj interface{}) {
sts, ok := obj.(*appsv1.StatefulSet)
if !ok {
klog.Errorf("Failed to convert %v to appsv1.StatefulSet", obj)
return
}

if *sts.Spec.Replicas == 0 {
pgName := batchv1alpha1.PodgroupNamePrefix + string(sts.UID)
err := pg.vcClient.SchedulingV1beta1().PodGroups(sts.Namespace).Delete(context.TODO(), pgName, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("Failed to delete PodGroup <%s/%s>: %v", sts.Namespace, pgName, err)
}
}
}

func (pg *pgcontroller) updateStatefulSet(oldObj, newObj interface{}) {
pg.addStatefulSet(newObj)
}

func (pg *pgcontroller) updatePodAnnotations(pod *v1.Pod, pgName string) error {
if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
Expand Down

0 comments on commit 78e452a

Please sign in to comment.