From 433fc57882dab892b4c06addfd4f6b9c8656dd61 Mon Sep 17 00:00:00 2001 From: Konstanty Karagiorgis Date: Wed, 4 Dec 2024 23:44:33 +0100 Subject: [PATCH] #85 Long service names are now truncated --- pkg/k8s/exposer.go | 34 ++++++++++++++++++++++++++++++++-- pkg/k8s/exposer_test.go | 12 ++++++++++++ pkg/k8s/services.go | 8 +++++--- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/pkg/k8s/exposer.go b/pkg/k8s/exposer.go index 4ff04db..fc705c3 100644 --- a/pkg/k8s/exposer.go +++ b/pkg/k8s/exposer.go @@ -2,6 +2,8 @@ package k8s import ( + "crypto/sha256" + "encoding/hex" "fmt" "strconv" "strings" @@ -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, @@ -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) @@ -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 +} diff --git a/pkg/k8s/exposer_test.go b/pkg/k8s/exposer_test.go index a017c5e..e9141cd 100644 --- a/pkg/k8s/exposer_test.go +++ b/pkg/k8s/exposer_test.go @@ -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", + ), + ) +} diff --git a/pkg/k8s/services.go b/pkg/k8s/services.go index 7eab548..012870f 100644 --- a/pkg/k8s/services.go +++ b/pkg/k8s/services.go @@ -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 }