Skip to content

Commit

Permalink
Merge pull request #395 from danielvegamyhre/subdomain-fix
Browse files Browse the repository at this point in the history
Default service name in JobSet controller
  • Loading branch information
k8s-ci-robot authored Feb 6, 2024
2 parents 45429d5 + 12de5fe commit 17f87c4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 53 deletions.
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 @@ -368,11 +376,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 @@ -697,6 +697,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 @@ -970,7 +984,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

0 comments on commit 17f87c4

Please sign in to comment.