Skip to content

Commit

Permalink
chore: remove random names from knuu
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Nov 15, 2024
1 parent 23bffd9 commit 6ee0d8a
Showing 1 changed file with 6 additions and 21 deletions.
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

0 comments on commit 6ee0d8a

Please sign in to comment.