diff --git a/pkg/builder/kaniko/kaniko.go b/pkg/builder/kaniko/kaniko.go index b187da77..94afb331 100644 --- a/pkg/builder/kaniko/kaniko.go +++ b/pkg/builder/kaniko/kaniko.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + "time" batchv1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" @@ -13,7 +14,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/celestiaorg/knuu/pkg/builder" - "github.com/celestiaorg/knuu/pkg/names" "github.com/celestiaorg/knuu/pkg/system" ) @@ -133,10 +133,7 @@ func (k *Kaniko) containerLogs(ctx context.Context, pod *v1.Pod) (string, error) } func (k *Kaniko) prepareJob(ctx context.Context, b *builder.BuilderOptions) (*batchv1.Job, error) { - jobName, err := names.NewRandomK8(kanikoJobNamePrefix) - if err != nil { - return nil, ErrGeneratingUUID.Wrap(err) - } + jobName := fmt.Sprintf("%s-%d", kanikoJobNamePrefix, time.Now().UnixNano()) ephemeralStorage, err := resource.ParseQuantity(EphemeralStorage) if err != nil { diff --git a/pkg/instance/storage.go b/pkg/instance/storage.go index 4287a4df..0e2cbfed 100644 --- a/pkg/instance/storage.go +++ b/pkg/instance/storage.go @@ -8,12 +8,12 @@ import ( "path/filepath" "strconv" "strings" + "time" "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/api/resource" "github.com/celestiaorg/knuu/pkg/k8s" - "github.com/celestiaorg/knuu/pkg/names" ) type storage struct { @@ -101,7 +101,6 @@ func (s *storage) AddFolder(src string, dest string, chown string) error { // copy file to destination path return s.AddFile(path, filepath.Join(dest, relPath), chown) }) - if err != nil { return ErrCopyingFolderToInstance.WithParams(src, s.instance.name).Wrap(err) } @@ -377,10 +376,7 @@ func (s *storage) readFileFromImage(ctx context.Context, filePath string) ([]byt // extract the file from them, but it seems hacky and will run on the user's machine. // Therefore, we will use the tmp instance to get the file from the image - tmpName, err := names.NewRandomK8("tmp-dl") - if err != nil { - return nil, err - } + tmpName := fmt.Sprintf("tmp-%d", time.Now().UnixNano()) ti, err := New(tmpName, s.instance.SystemDependencies) if err != nil { diff --git a/pkg/names/names.go b/pkg/names/names.go deleted file mode 100644 index 7b63f7df..00000000 --- a/pkg/names/names.go +++ /dev/null @@ -1,16 +0,0 @@ -package names - -import ( - "fmt" - - "github.com/google/uuid" -) - -// NewRandomK8 returns a random k8s compatible name with the given prefix. -func NewRandomK8(prefix string) (string, error) { - uuid, err := uuid.NewRandom() - if err != nil { - return "", err - } - return fmt.Sprintf("%s-%s", prefix, uuid.String()[:8]), nil -} diff --git a/pkg/traefik/traefik.go b/pkg/traefik/traefik.go index e31456a9..c8d7ed45 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 ( @@ -52,21 +51,15 @@ func (t *Traefik) Deploy(ctx context.Context) error { } // Create a dedicated service account for Traefik - serviceAccountName, err := names.NewRandomK8("traefik-service-account") - if err != nil { - return err - } + serviceAccountName := fmt.Sprintf("traefik-service-account-%d", time.Now().UnixNano()) 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 := fmt.Sprintf("%s-%d", roleName, time.Now().UnixNano()) // 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"}, @@ -87,7 +80,6 @@ func (t *Traefik) Deploy(ctx context.Context) error { Verbs: []string{"get", "list", "watch"}, }, }) - if err != nil { return ErrTraefikRoleCreationFailed.Wrap(err) } @@ -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 := fmt.Sprintf("middleware-%d", time.Now().UnixNano()) // middleware is required to strip the prefix from the service name if err := t.createMiddleware(ctx, prefix, middlewareName); err != nil { @@ -303,10 +292,7 @@ func (t *Traefik) createIngressRoute( Resource: "ingressroutes", } - ingressRouteName, err := names.NewRandomK8("ing-route-" + prefix) - if err != nil { - return ErrTraefikIngressRouteCreationFailed.Wrap(err) - } + ingressRouteName := fmt.Sprintf("ing-route-%d", time.Now().UnixNano()) ingressRoute := &unstructured.Unstructured{ Object: map[string]interface{}{ @@ -339,7 +325,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)