Skip to content

Commit

Permalink
Truncate UI service name if over 63 characters (#2311)
Browse files Browse the repository at this point in the history
* Truncate UI service name if over 63 characters

Signed-off-by: Jacob Salway <[email protected]>

* Also truncate ingress name

Signed-off-by: Jacob Salway <[email protected]>

---------

Signed-off-by: Jacob Salway <[email protected]>
  • Loading branch information
jacobsalway authored Nov 18, 2024
1 parent 22e4fb8 commit 43c1888
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
18 changes: 16 additions & 2 deletions 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 @@ -155,12 +156,25 @@ func GetExecutorLocalVolumeMounts(app *v1beta2.SparkApplication) []corev1.Volume
return volumeMounts
}

func generateName(name, suffix string) string {
// Some resource names are used as DNS labels, so must be 63 characters or shorter
preferredName := fmt.Sprintf("%s-%s", name, suffix)
if len(preferredName) <= 63 {
return preferredName
}

// Truncate the name and append a hash to ensure uniqueness while staying below the limit
maxNameLength := 63 - len(suffix) - 10 // 8 for the hash, 2 for the dash
hash := fmt.Sprintf("%x", md5.Sum([]byte(preferredName)))
return fmt.Sprintf("%s-%s-%s", name[:maxNameLength], hash[:8], suffix)
}

func GetDefaultUIServiceName(app *v1beta2.SparkApplication) string {
return fmt.Sprintf("%s-ui-svc", app.Name)
return generateName(app.Name, "ui-svc")
}

func GetDefaultUIIngressName(app *v1beta2.SparkApplication) string {
return fmt.Sprintf("%s-ui-ingress", app.Name)
return generateName(app.Name, "ui-ingress")
}

func GetResourceLabels(app *v1beta2.SparkApplication) map[string]string {
Expand Down
28 changes: 28 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 All @@ -297,6 +311,20 @@ var _ = Describe("GetDefaultUIIngressName", func() {
It("Should return the default UI ingress name", func() {
Expect(util.GetDefaultUIIngressName(app)).To(Equal("test-app-ui-ingress"))
})

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 ingress name is below 63 characters", func() {
serviceName := util.GetDefaultUIIngressName(appWithLongName)
Expect(len(serviceName)).To(BeNumerically("<=", 63))
Expect(serviceName).To(HavePrefix(appWithLongName.Name[:42]))
Expect(serviceName).To(HaveSuffix("-ui-ingress"))
})
})

var _ = Describe("IsDriverTerminated", func() {
Expand Down

0 comments on commit 43c1888

Please sign in to comment.