Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#85 Long service names are now truncated #87

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions pkg/k8s/exposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package k8s

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -46,7 +48,7 @@ func (exp *k8sResourceExposer) Add(app apps.App) (apps.App, error) {
if childFactoryErr != nil {
return apps.App{}, childFactoryErr
}
entityName := fmt.Sprintf("%s-%s", app.Peer, app.Name)
entityName := capName(fmt.Sprintf("%s-%s", app.Peer, app.Name))
for _, managedResource := range exp.managedResources {
addErr := managedResource.Add(k8sResourceMetadata{
entityName: entityName,
Expand All @@ -65,7 +67,7 @@ func (exp *k8sResourceExposer) Withdraw(app apps.App) error {
if clientSetErr != nil {
return clientSetErr
}
entityName := fmt.Sprintf("%s-%s", app.Peer, app.Name)
entityName := capName(fmt.Sprintf("%s-%s", app.Peer, app.Name))
for i := range exp.managedResources {
managedResource := exp.managedResources[len(exp.managedResources)-1-i]
removeErr := managedResource.Remove(entityName, clientset)
Expand Down Expand Up @@ -129,3 +131,31 @@ func extractPortFromAddr(address string) (int, error) {
parts := strings.Split(address, ":")
return strconv.Atoi(parts[1])
}

func capName(name string) string {
if len(name) <= 63 {
return name
}

// Calculate hash of the full name
hasher := sha256.New()
hasher.Write([]byte(name))
hash := hex.EncodeToString(hasher.Sum(nil))[:8] // Take first 8 chars of hash

// We need 9 chars for "-" + hash
// So we can search up to position 54 (63 - 9 = 54)
searchStart := 32
searchEnd := 54

substring := name[searchStart:searchEnd]
hyphenIndex := strings.LastIndex(substring, "-")

if hyphenIndex != -1 {
// Found hyphen in the search range
actualIndex := searchStart + hyphenIndex
return name[:actualIndex] + "-" + hash
}

// No suitable hyphen found, cut at position 54
return name[:54] + "-" + hash
}
12 changes: 12 additions & 0 deletions pkg/k8s/exposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,15 @@ func TestExposerWithdrawAll(t *testing.T) {
assert.Equal(t, 0, rsc2.removeAllCalled)
assert.Equal(t, 1, rsc1.removeAllCalled)
}

func TestCapName(t *testing.T) {
assert.Equal(t, "", capName(""))
assert.Equal(t, "foo-bar-baz", capName("foo-bar-baz"))
assert.Equal(
t,
"alpha-beta-gamma-delta-epsilon-zeta-eta-iota-kappa-76198ab9",
capName(
"alpha-beta-gamma-delta-epsilon-zeta-eta-iota-kappa-lambda-mi-ni-xi-omikron-pi-rho-sigma",
),
)
}
8 changes: 5 additions & 3 deletions pkg/k8s/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ func (m *managedK8sService) Add(metadata k8sResourceMetadata, clientset *kuberne

func (m *managedK8sService) Remove(entityName string, clientset *kubernetes.Clientset) error {
servicesClient := clientset.CoreV1().Services(m.namespace)
deleteErr := servicesClient.Delete(context.Background(), entityName, metav1.DeleteOptions{})
deleteErr := servicesClient.Delete(context.Background(), capName(
entityName,
), metav1.DeleteOptions{})
if deleteErr != nil {
return fmt.Errorf("Could not delete service %s: %v", entityName, deleteErr)
return fmt.Errorf("Could not delete service %s: %v", capName(entityName), deleteErr)
}
logrus.Infof("Deleted service %s", entityName)
logrus.Infof("Deleted service %s", capName(entityName))
return nil
}

Expand Down
Loading