Skip to content
This repository was archived by the owner on Sep 12, 2023. It is now read-only.

Commit 9309adf

Browse files
authored
Support for PodGroup updates (#207)
* Support for PodGroup updates Signed-off-by: Yuki Iwai <[email protected]> * Add the logic to check PodGroup changes Signed-off-by: Yuki Iwai <[email protected]> * make podgroup pointer Signed-off-by: Yuki Iwai <[email protected]> Signed-off-by: Yuki Iwai <[email protected]>
1 parent 4d97a27 commit 9309adf

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.19
44

55
require (
66
github.com/go-logr/logr v1.2.3
7+
github.com/google/go-cmp v0.5.8
78
github.com/prometheus/client_golang v1.12.2
89
github.com/sirupsen/logrus v1.8.1
910
github.com/stretchr/testify v1.8.0
@@ -37,7 +38,6 @@ require (
3738
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3839
github.com/golang/protobuf v1.5.2 // indirect
3940
github.com/google/gnostic v0.5.7-v3refs // indirect
40-
github.com/google/go-cmp v0.5.8 // indirect
4141
github.com/google/gofuzz v1.1.0 // indirect
4242
github.com/google/uuid v1.3.0 // indirect
4343
github.com/imdario/mergo v0.3.12 // indirect

pkg/controller.v1/common/scheduling.go

+27-16
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,52 @@ import (
2323

2424
apiv1 "github.com/kubeflow/common/pkg/apis/common/v1"
2525

26+
"github.com/google/go-cmp/cmp"
2627
log "github.com/sirupsen/logrus"
2728
policyapi "k8s.io/api/policy/v1beta1"
2829
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031
"k8s.io/apimachinery/pkg/util/intstr"
32+
"k8s.io/klog/v2"
3133
"sigs.k8s.io/controller-runtime/pkg/client"
3234
)
3335

3436
type FillPodGroupSpecFunc func(object metav1.Object) error
3537

3638
func (jc *JobController) SyncPodGroup(job metav1.Object, specFunc FillPodGroupSpecFunc) (metav1.Object, error) {
3739
pgctl := jc.PodGroupControl
40+
3841
// Check whether podGroup exists or not
3942
podGroup, err := pgctl.GetPodGroup(job.GetNamespace(), job.GetName())
4043
if err == nil {
44+
// update podGroup for gang scheduling
45+
oldPodGroup := &podGroup
46+
if err = specFunc(podGroup); err != nil {
47+
return nil, fmt.Errorf("unable to fill the spec of PodGroup, '%v': %v", klog.KObj(podGroup), err)
48+
}
49+
if diff := cmp.Diff(oldPodGroup, podGroup); len(diff) != 0 {
50+
return podGroup, pgctl.UpdatePodGroup(podGroup.(client.Object))
51+
}
4152
return podGroup, nil
4253
} else if client.IgnoreNotFound(err) != nil {
43-
return nil, fmt.Errorf("unable to get PodGroup: %v", err)
44-
}
45-
46-
// create podGroup for gang scheduling
47-
toCreatePodGroup := pgctl.NewEmptyPodGroup()
48-
toCreatePodGroup.SetName(job.GetName())
49-
toCreatePodGroup.SetNamespace(job.GetNamespace())
50-
toCreatePodGroup.SetAnnotations(job.GetAnnotations())
51-
toCreatePodGroup.SetOwnerReferences([]metav1.OwnerReference{*jc.GenOwnerReference(job)})
52-
if err = specFunc(toCreatePodGroup); err != nil {
53-
return nil, fmt.Errorf("unable to fill the spec of PodGroup: %v", err)
54-
}
54+
return nil, fmt.Errorf("unable to get a PodGroup: %v", err)
55+
} else {
56+
// create podGroup for gang scheduling
57+
newPodGroup := pgctl.NewEmptyPodGroup()
58+
newPodGroup.SetName(job.GetName())
59+
newPodGroup.SetNamespace(job.GetNamespace())
60+
newPodGroup.SetAnnotations(job.GetAnnotations())
61+
newPodGroup.SetOwnerReferences([]metav1.OwnerReference{*jc.GenOwnerReference(job)})
62+
if err = specFunc(newPodGroup); err != nil {
63+
return nil, fmt.Errorf("unable to fill the spec of PodGroup, '%v': %v", klog.KObj(newPodGroup), err)
64+
}
5565

56-
err = pgctl.CreatePodGroup(toCreatePodGroup)
57-
if err != nil {
58-
return podGroup, fmt.Errorf("unable to create PodGroup: %v", err)
66+
err = pgctl.CreatePodGroup(newPodGroup)
67+
if err != nil {
68+
return podGroup, fmt.Errorf("unable to create PodGroup: %v", err)
69+
}
70+
createdPodGroupsCount.Inc()
5971
}
60-
createdPodGroupsCount.Inc()
6172

6273
createdPodGroup, err := pgctl.GetPodGroup(job.GetNamespace(), job.GetName())
6374
if err != nil {

pkg/controller.v1/control/podgroup_control.go

+31-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
corev1 "k8s.io/api/core/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/types"
26+
"k8s.io/klog/v2"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
schedulerpluginsv1alpha1 "sigs.k8s.io/scheduler-plugins/apis/scheduling/v1alpha1"
2829
volcanobatchv1alpha1 "volcano.sh/apis/pkg/apis/batch/v1alpha1"
@@ -33,20 +34,22 @@ import (
3334
// PodGroupControlInterface is an interface that knows how to add or delete PodGroups
3435
// created as an interface to allow testing.
3536
type PodGroupControlInterface interface {
36-
// NewEmptyPodGroup returns an empty PodGroup
37+
// NewEmptyPodGroup returns an empty PodGroup.
3738
NewEmptyPodGroup() client.Object
38-
// GetPodGroup gets the PodGroup identified by namespace and name
39+
// GetPodGroup gets the PodGroup identified by namespace and name.
3940
GetPodGroup(namespace string, name string) (metav1.Object, error)
4041
// DeletePodGroup deletes the PodGroup identified by namespace and name.
4142
DeletePodGroup(namespace string, name string) error
43+
// UpdatePodGroup updates a PodGroup.
44+
UpdatePodGroup(podGroup client.Object) error
4245
// CreatePodGroup creates a new PodGroup with PodGroup spec fill function.
4346
CreatePodGroup(podGroup client.Object) error
4447
// DelayPodCreationDueToPodGroup determines whether it should delay Pod Creation.
4548
DelayPodCreationDueToPodGroup(pg metav1.Object) bool
4649
// DecoratePodTemplateSpec decorates PodTemplateSpec.
47-
// If the PodTemplateSpec has SchedulerName set, this method will Not override
50+
// If the PodTemplateSpec has SchedulerName set, this method will Not override.
4851
DecoratePodTemplateSpec(pts *corev1.PodTemplateSpec, job metav1.Object, rtype string)
49-
// GetSchedulerName returns the name of the gang scheduler
52+
// GetSchedulerName returns the name of the gang scheduler.
5053
GetSchedulerName() string
5154
}
5255

@@ -99,6 +102,15 @@ func (v *VolcanoControl) DeletePodGroup(namespace string, name string) error {
99102
return v.Client.SchedulingV1beta1().PodGroups(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
100103
}
101104

105+
func (v *VolcanoControl) UpdatePodGroup(podGroup client.Object) error {
106+
pg := podGroup.(*volcanov1beta1.PodGroup)
107+
_, err := v.Client.SchedulingV1beta1().PodGroups(pg.GetNamespace()).Update(context.TODO(), pg, metav1.UpdateOptions{})
108+
if err != nil {
109+
return fmt.Errorf("unable to update a PodGroup, '%v': %v", klog.KObj(pg), err)
110+
}
111+
return nil
112+
}
113+
102114
func (v *VolcanoControl) CreatePodGroup(podGroup client.Object) error {
103115
pg := podGroup.(*volcanov1beta1.PodGroup)
104116
createPodGroup, err := v.Client.SchedulingV1beta1().PodGroups(pg.GetNamespace()).Create(context.TODO(), pg, metav1.CreateOptions{})
@@ -162,9 +174,22 @@ func (s *SchedulerPluginsControl) DeletePodGroup(namespace, name string) error {
162174
return s.Client.Delete(ctx, pg)
163175
}
164176

177+
func (s *SchedulerPluginsControl) UpdatePodGroup(podGroup client.Object) error {
178+
pg := podGroup.(*schedulerpluginsv1alpha1.PodGroup)
179+
err := s.Client.Update(context.TODO(), pg, &client.UpdateOptions{})
180+
if err != nil {
181+
return fmt.Errorf("unable to update a PodGroup, '%v': %v", klog.KObj(pg), err)
182+
}
183+
return nil
184+
}
185+
165186
func (s *SchedulerPluginsControl) CreatePodGroup(podGroup client.Object) error {
166-
ctx := context.TODO()
167-
return s.Client.Create(ctx, podGroup)
187+
pg := podGroup.(*schedulerpluginsv1alpha1.PodGroup)
188+
err := s.Client.Create(context.TODO(), pg, &client.CreateOptions{})
189+
if err != nil {
190+
return fmt.Errorf("unable to create a PodGroup, '%v': %v", klog.KObj(pg), err)
191+
}
192+
return nil
168193
}
169194

170195
var _ PodGroupControlInterface = &SchedulerPluginsControl{}

0 commit comments

Comments
 (0)