Skip to content

Commit

Permalink
Truncate UI service name if over 63 characters
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Salway <[email protected]>
  • Loading branch information
jacobsalway committed Nov 5, 2024
1 parent 763682d commit af3400d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/util/sparkapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package util

import (
"crypto/md5"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/sparkapplication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit af3400d

Please sign in to comment.