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

Automated cherry pick of #395: default service name in controller #412

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
4 changes: 0 additions & 4 deletions api/jobset/v1alpha2/jobset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func (js *JobSet) Default() {
if js.Spec.Network.EnableDNSHostnames == nil {
js.Spec.Network.EnableDNSHostnames = ptr.To(true)
}
// Subdomain defaults to the JobSet name
if js.Spec.Network.Subdomain == "" {
js.Spec.Network.Subdomain = js.Name
}

// Default pod restart policy to OnFailure.
if js.Spec.ReplicatedJobs[i].Template.Spec.Template.Spec.RestartPolicy == "" {
Expand Down
43 changes: 0 additions & 43 deletions api/jobset/v1alpha2/jobset_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,49 +138,6 @@ func TestJobSetDefaulting(t *testing.T) {
},
},
},
{
name: "subdomain defaults to jobset name",
js: &JobSet{
ObjectMeta: metav1.ObjectMeta{
Name: "custom-jobset",
},
Spec: JobSetSpec{
SuccessPolicy: defaultSuccessPolicy,
ReplicatedJobs: []ReplicatedJob{
{
Template: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: TestPodTemplate,
CompletionMode: completionModePtr(batchv1.IndexedCompletion),
},
},
},
},
},
},
want: &JobSet{
ObjectMeta: metav1.ObjectMeta{
Name: "custom-jobset",
},
Spec: JobSetSpec{
SuccessPolicy: defaultSuccessPolicy,
Network: &Network{
EnableDNSHostnames: ptr.To(true),
Subdomain: "custom-jobset",
},
ReplicatedJobs: []ReplicatedJob{
{
Template: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: TestPodTemplate,
CompletionMode: completionModePtr(batchv1.IndexedCompletion),
},
},
},
},
},
},
},
{
name: "enableDNSHostnames is false",
js: &JobSet{
Expand Down
13 changes: 8 additions & 5 deletions pkg/controllers/jobset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func (r *JobSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
ctx = ctrl.LoggerInto(ctx, log)
log.V(2).Info("Reconciling JobSet")

// If enableDNSHostnames is set, and subdomain is unset, default the subdomain to be the JobSet name.
// This must be done in the controller rather than in the request-time defaulting, since if a JobSet
// uses generateName rather than setting the name explicitly, the JobSet name will still be an empty
// string at that time.
if dnsHostnamesEnabled(&js) && js.Spec.Network.Subdomain == "" {
js.Spec.Network.Subdomain = js.Name
}

// Get Jobs owned by JobSet.
ownedJobs, err := r.getChildJobs(ctx, &js)
if err != nil {
Expand Down Expand Up @@ -363,11 +371,6 @@ func (r *JobSetReconciler) createJobs(ctx context.Context, js *jobset.JobSet, ow

// If pod DNS hostnames are enabled, create a headless service for the JobSet
if dnsHostnamesEnabled(js) {

// Don't try to create a headless service without a subdomain
if js.Spec.Network.Subdomain == "" {
return fmt.Errorf("a subdomain should be set if dns hostnames are enabled")
}
if err := r.createHeadlessSvcIfNotExist(ctx, js); err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/testing/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func (j *JobSetWrapper) SetAnnotations(annotations map[string]string) *JobSetWra
return j
}

// GenerateName sets the JobSet name.
func (j *JobSetWrapper) SetGenerateName(namePrefix string) *JobSetWrapper {
// Name and GenerateName are mutually exclusive, so we must unset the Name field.
j.Name = ""
j.GenerateName = namePrefix
return j
}

// Obj returns the inner JobSet.
func (j *JobSetWrapper) Obj() *jobset.JobSet {
return &j.JobSet
Expand Down
15 changes: 14 additions & 1 deletion test/integration/controller/jobset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,20 @@ var _ = ginkgo.Describe("JobSet controller", func() {
},
},
}),
ginkgo.Entry("jobset using generateName with enableDNSHostnames should have headless service name set to the jobset name", &testCase{
makeJobSet: func(ns *corev1.Namespace) *testing.JobSetWrapper {
return testJobSet(ns).SetGenerateName("name-prefix").EnableDNSHostnames(true)
},
updates: []*update{
{
checkJobSetState: func(js *jobset.JobSet) {
gomega.Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: js.Name, Namespace: js.Namespace}, &corev1.Service{})
}, timeout, interval).Should(gomega.Succeed())
},
},
},
}),
) // end of DescribeTable
}) // end of Describe

Expand Down Expand Up @@ -837,7 +851,6 @@ func testJobSet(ns *corev1.Namespace) *testing.JobSetWrapper {
return testing.MakeJobSet(jobSetName, ns.Name).
SuccessPolicy(&jobset.SuccessPolicy{Operator: jobset.OperatorAll, TargetReplicatedJobs: []string{}}).
EnableDNSHostnames(true).
NetworkSubdomain(jobSetName).
ReplicatedJob(testing.MakeReplicatedJob("replicated-job-a").
Job(testing.MakeJobTemplate("test-job-A", ns.Name).PodSpec(testing.TestPodSpec).Obj()).
Replicas(1).
Expand Down