Skip to content

Commit 43c1888

Browse files
authored
Truncate UI service name if over 63 characters (#2311)
* 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]>
1 parent 22e4fb8 commit 43c1888

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

pkg/util/sparkapplication.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"crypto/md5"
2021
"fmt"
2122
"reflect"
2223
"strings"
@@ -155,12 +156,25 @@ func GetExecutorLocalVolumeMounts(app *v1beta2.SparkApplication) []corev1.Volume
155156
return volumeMounts
156157
}
157158

159+
func generateName(name, suffix string) string {
160+
// Some resource names are used as DNS labels, so must be 63 characters or shorter
161+
preferredName := fmt.Sprintf("%s-%s", name, suffix)
162+
if len(preferredName) <= 63 {
163+
return preferredName
164+
}
165+
166+
// Truncate the name and append a hash to ensure uniqueness while staying below the limit
167+
maxNameLength := 63 - len(suffix) - 10 // 8 for the hash, 2 for the dash
168+
hash := fmt.Sprintf("%x", md5.Sum([]byte(preferredName)))
169+
return fmt.Sprintf("%s-%s-%s", name[:maxNameLength], hash[:8], suffix)
170+
}
171+
158172
func GetDefaultUIServiceName(app *v1beta2.SparkApplication) string {
159-
return fmt.Sprintf("%s-ui-svc", app.Name)
173+
return generateName(app.Name, "ui-svc")
160174
}
161175

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

166180
func GetResourceLabels(app *v1beta2.SparkApplication) map[string]string {

pkg/util/sparkapplication_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ var _ = Describe("GetDefaultUIServiceName", func() {
284284
It("Should return the default UI service name", func() {
285285
Expect(util.GetDefaultUIServiceName(app)).To(Equal("test-app-ui-svc"))
286286
})
287+
288+
appWithLongName := &v1beta2.SparkApplication{
289+
ObjectMeta: metav1.ObjectMeta{
290+
Name: "test-app-with-a-long-name-that-would-be-over-63-characters",
291+
Namespace: "test-namespace",
292+
},
293+
}
294+
295+
It("Should truncate the app name so the service name is below 63 characters", func() {
296+
serviceName := util.GetDefaultUIServiceName(appWithLongName)
297+
Expect(len(serviceName)).To(BeNumerically("<=", 63))
298+
Expect(serviceName).To(HavePrefix(appWithLongName.Name[:47]))
299+
Expect(serviceName).To(HaveSuffix("-ui-svc"))
300+
})
287301
})
288302

289303
var _ = Describe("GetDefaultUIIngressName", func() {
@@ -297,6 +311,20 @@ var _ = Describe("GetDefaultUIIngressName", func() {
297311
It("Should return the default UI ingress name", func() {
298312
Expect(util.GetDefaultUIIngressName(app)).To(Equal("test-app-ui-ingress"))
299313
})
314+
315+
appWithLongName := &v1beta2.SparkApplication{
316+
ObjectMeta: metav1.ObjectMeta{
317+
Name: "test-app-with-a-long-name-that-would-be-over-63-characters",
318+
Namespace: "test-namespace",
319+
},
320+
}
321+
322+
It("Should truncate the app name so the ingress name is below 63 characters", func() {
323+
serviceName := util.GetDefaultUIIngressName(appWithLongName)
324+
Expect(len(serviceName)).To(BeNumerically("<=", 63))
325+
Expect(serviceName).To(HavePrefix(appWithLongName.Name[:42]))
326+
Expect(serviceName).To(HaveSuffix("-ui-ingress"))
327+
})
300328
})
301329

302330
var _ = Describe("IsDriverTerminated", func() {

0 commit comments

Comments
 (0)