diff --git a/pkg/util/sparkapplication.go b/pkg/util/sparkapplication.go index a0aadd93f..7c1594531 100644 --- a/pkg/util/sparkapplication.go +++ b/pkg/util/sparkapplication.go @@ -17,6 +17,7 @@ limitations under the License. package util import ( + "crypto/md5" "fmt" "reflect" "strings" @@ -156,7 +157,16 @@ func GetExecutorLocalVolumeMounts(app *v1beta2.SparkApplication) []corev1.Volume } func GetDefaultUIServiceName(app *v1beta2.SparkApplication) string { - return fmt.Sprintf("%s-ui-svc", app.Name) + preferredName := fmt.Sprintf("%s-ui-svc", app.Name) + + // Service names are used as DNS labels, so must be 63 characters or shorter + if len(preferredName) <= 63 { + return preferredName + } + + // Truncate the name and append a hash to ensure uniqueness while staying below the limit + hash := fmt.Sprintf("%x", md5.Sum([]byte(preferredName))) + return fmt.Sprintf("%s-%s-ui-svc", app.Name[:47], hash[:8]) } func GetDefaultUIIngressName(app *v1beta2.SparkApplication) string { diff --git a/pkg/util/sparkapplication_test.go b/pkg/util/sparkapplication_test.go index 7f0ab4a46..fee80a8e3 100644 --- a/pkg/util/sparkapplication_test.go +++ b/pkg/util/sparkapplication_test.go @@ -284,6 +284,20 @@ var _ = Describe("GetDefaultUIServiceName", func() { It("Should return the default UI service name", func() { Expect(util.GetDefaultUIServiceName(app)).To(Equal("test-app-ui-svc")) }) + + appWithLongName := &v1beta2.SparkApplication{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-app-with-a-long-name-that-would-be-over-63-characters", + Namespace: "test-namespace", + }, + } + + It("Should truncate the app name so the service name is below 63 characters", func() { + serviceName := util.GetDefaultUIServiceName(appWithLongName) + Expect(len(serviceName)).To(BeNumerically("<=", 63)) + Expect(serviceName).To(HavePrefix(appWithLongName.Name[:47])) + Expect(serviceName).To(HaveSuffix("-ui-svc")) + }) }) var _ = Describe("GetDefaultUIIngressName", func() {