diff --git a/pkg/traefik/traefik.go b/pkg/traefik/traefik.go index 381dfe1..e273bdc 100644 --- a/pkg/traefik/traefik.go +++ b/pkg/traefik/traefik.go @@ -17,7 +17,6 @@ import ( "k8s.io/utils/ptr" "github.com/celestiaorg/knuu/pkg/k8s" - "github.com/celestiaorg/knuu/pkg/names" ) const ( @@ -26,6 +25,7 @@ const ( Port = 80 PortSecure = 443 deploymentName = "traefik-deployment" + serviceAccountName = "traefik-service-account" roleName = "traefik-role" containerName = "traefik" image = "traefik:v3.0" @@ -51,22 +51,14 @@ func (t *Traefik) Deploy(ctx context.Context) error { return ErrTraefikClientNotInitialized } - // Create a dedicated service account for Traefik - serviceAccountName, err := names.NewRandomK8("traefik-service-account") - if err != nil { - return err - } if err := t.K8sClient.CreateServiceAccount(ctx, serviceAccountName, nil); err != nil { return ErrFailedToCreateServiceAccount.Wrap(err) } - clusterRoleName, err := names.NewRandomK8(roleName) - if err != nil { - return err - } + clusterRoleName := k8s.SanitizeName(t.K8sClient.Namespace() + "-" + roleName) // Define and create a ClusterRole for Traefik - err = t.K8sClient.CreateClusterRole(ctx, clusterRoleName, nil, []rbacv1.PolicyRule{ + err := t.K8sClient.CreateClusterRole(ctx, clusterRoleName, nil, []rbacv1.PolicyRule{ { APIGroups: []string{""}, // Core group Resources: []string{"pods", "endpoints", "secrets", "services"}, @@ -209,10 +201,7 @@ func (t *Traefik) Endpoint(ctx context.Context) (string, error) { } func (t *Traefik) AddHost(ctx context.Context, serviceName, prefix string, portTCP int) error { - middlewareName, err := names.NewRandomK8("strip-" + prefix) - if err != nil { - return ErrGeneratingRandomK8sName.Wrap(err) - } + middlewareName := k8s.SanitizeName(prefix + "-strip") // middleware is required to strip the prefix from the service name if err := t.createMiddleware(ctx, prefix, middlewareName); err != nil { @@ -303,11 +292,7 @@ func (t *Traefik) createIngressRoute( Resource: "ingressroutes", } - ingressRouteName, err := names.NewRandomK8("ing-route-" + prefix) - if err != nil { - return ErrTraefikIngressRouteCreationFailed.Wrap(err) - } - + ingressRouteName := k8s.SanitizeName(prefix + "-ing-route") ingressRoute := &unstructured.Unstructured{ Object: map[string]interface{}{ "apiVersion": "traefik.io/v1alpha1", @@ -341,7 +326,7 @@ func (t *Traefik) createIngressRoute( }, } - _, err = t.K8sClient.DynamicClient().Resource(ingressRouteGVR).Namespace(t.K8sClient.Namespace()). + _, err := t.K8sClient.DynamicClient().Resource(ingressRouteGVR).Namespace(t.K8sClient.Namespace()). Create(ctx, ingressRoute, metav1.CreateOptions{}) if err != nil { return ErrTraefikIngressRouteCreationFailed.Wrap(err)