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

chore: remove random names from knuu #581

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 6 additions & 21 deletions pkg/traefik/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"k8s.io/utils/ptr"

"github.com/celestiaorg/knuu/pkg/k8s"
"github.com/celestiaorg/knuu/pkg/names"
)

const (
Expand All @@ -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"
Expand All @@ -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"},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -339,7 +324,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)
Expand Down
Loading