From 938563d28f7b0d210ea369f76ab88f4f0ac0abae Mon Sep 17 00:00:00 2001 From: Mojtaba Date: Thu, 29 Aug 2024 11:58:10 +0330 Subject: [PATCH] Revert "chore!: refactor to use unique static names for instances" This reverts commit c11c80412992fa24fa636a4896cd66ed7b0d3fbd. --- pkg/builder/kaniko/kaniko.go | 2 +- pkg/builder/kaniko/kaniko_test.go | 2 +- pkg/instance/build.go | 2 +- pkg/instance/errors.go | 1 - pkg/instance/execution.go | 84 ++++++++++++------------ pkg/instance/instance.go | 62 +++++++++++------ pkg/instance/network.go | 52 +++++++-------- pkg/instance/proxy.go | 4 +- pkg/instance/resources.go | 14 ++-- pkg/instance/sidecars.go | 16 ++--- pkg/instance/storage.go | 16 ++--- pkg/knuu/instance.go | 4 +- pkg/knuu/knuu.go | 4 +- pkg/preloader/errors.go | 2 +- pkg/preloader/preloader.go | 32 +++++---- pkg/sidecars/netshaper/netshaper.go | 12 ++-- pkg/sidecars/netshaper/netshaper_test.go | 10 ++- pkg/sidecars/observability/obsy.go | 12 ++-- pkg/sidecars/observability/obsy_test.go | 7 +- pkg/sidecars/tshark/tshark.go | 12 ++-- pkg/sidecars/tshark/tshark_test.go | 7 +- pkg/system/dependencies.go | 12 ---- 22 files changed, 179 insertions(+), 190 deletions(-) diff --git a/pkg/builder/kaniko/kaniko.go b/pkg/builder/kaniko/kaniko.go index 026590b4..61a119bd 100644 --- a/pkg/builder/kaniko/kaniko.go +++ b/pkg/builder/kaniko/kaniko.go @@ -30,7 +30,7 @@ const ( ) type Kaniko struct { - *system.SystemDependencies + system.SystemDependencies } var _ builder.Builder = &Kaniko{} diff --git a/pkg/builder/kaniko/kaniko_test.go b/pkg/builder/kaniko/kaniko_test.go index 8b75b378..f440c10b 100644 --- a/pkg/builder/kaniko/kaniko_test.go +++ b/pkg/builder/kaniko/kaniko_test.go @@ -30,7 +30,7 @@ func TestKanikoBuilder(t *testing.T) { k8sClient, err := k8s.NewClientCustom(context.Background(), k8sCS, k8sCS.Discovery(), nil, k8sNamespace, logrus.New()) require.NoError(t, err) kb := &Kaniko{ - SystemDependencies: &system.SystemDependencies{ + SystemDependencies: system.SystemDependencies{ K8sClient: k8sClient, }, } diff --git a/pkg/instance/build.go b/pkg/instance/build.go index d07a4055..82d04f7b 100644 --- a/pkg/instance/build.go +++ b/pkg/instance/build.go @@ -200,7 +200,7 @@ func getImageRegistry(imageName string) (string, error) { // getBuildDir returns the build directory for the instance func (b *build) getBuildDir() string { - return filepath.Join(buildDirBase, b.instance.name) + return filepath.Join(buildDirBase, b.instance.k8sName) } // addFileToBuilder adds a file to the builder diff --git a/pkg/instance/errors.go b/pkg/instance/errors.go index 6ef64615..8f911de3 100644 --- a/pkg/instance/errors.go +++ b/pkg/instance/errors.go @@ -217,5 +217,4 @@ var ( ErrSidecarInstanceIsNil = errors.New("SidecarInstanceIsNil", "sidecar instance is nil for instance '%s'") ErrFailedToDeletePersistentVolumeClaim = errors.New("FailedToDeletePersistentVolumeClaim", "failed to delete persistent volume claim") ErrUpgradingImageNotAllowed = errors.New("UpgradingImageNotAllowed", "upgrading image is only allowed in state 'Started'. Current state is '%s'") - ErrInstanceNameAlreadyExists = errors.New("InstanceNameAlreadyExists", "instance name '%s' already exists") ) diff --git a/pkg/instance/execution.go b/pkg/instance/execution.go index 83f45237..948c505d 100644 --- a/pkg/instance/execution.go +++ b/pkg/instance/execution.go @@ -40,20 +40,20 @@ func (e *execution) ExecuteCommand(ctx context.Context, command ...string) (stri var ( instanceName string eErr *Error - containerName = e.instance.name + containerName = e.instance.k8sName ) if e.instance.sidecars.isSidecar { - instanceName = e.instance.parentInstance.name - eErr = ErrExecutingCommandInSidecar.WithParams(command, e.instance.name, e.instance.parentInstance.name) + instanceName = e.instance.parentInstance.k8sName + eErr = ErrExecutingCommandInSidecar.WithParams(command, e.instance.k8sName, e.instance.parentInstance.k8sName) } else { - instanceName = e.instance.name - eErr = ErrExecutingCommandInInstance.WithParams(command, e.instance.name) + instanceName = e.instance.k8sName + eErr = ErrExecutingCommandInInstance.WithParams(command, e.instance.k8sName) } pod, err := e.instance.K8sClient.GetFirstPodFromReplicaSet(ctx, instanceName) if err != nil { - return "", ErrGettingPodFromReplicaSet.WithParams(e.instance.name).Wrap(err) + return "", ErrGettingPodFromReplicaSet.WithParams(e.instance.k8sName).Wrap(err) } commandWithShell := []string{"/bin/sh", "-c", strings.Join(command, " ")} @@ -73,7 +73,7 @@ func (e *execution) StartWithCallback(ctx context.Context, callback func()) erro go func() { err := e.WaitInstanceIsRunning(ctx) if err != nil { - e.instance.Logger.Errorf("Error waiting for instance '%s' to be running: %s", e.instance.name, err) + e.instance.Logger.Errorf("Error waiting for instance '%s' to be running: %s", e.instance.k8sName, err) return } callback() @@ -85,7 +85,7 @@ func (e *execution) StartWithCallback(ctx context.Context, callback func()) erro // This function can only be called in the state 'Committed' or 'Stopped' func (e *execution) StartAsync(ctx context.Context) error { if !e.instance.IsInState(StateCommitted, StateStopped) { - return ErrStartingNotAllowed.WithParams(e.instance.name, e.instance.state.String()) + return ErrStartingNotAllowed.WithParams(e.instance.k8sName, e.instance.state.String()) } if err := e.instance.sidecars.verifySidecarsStates(); err != nil { @@ -109,17 +109,17 @@ func (e *execution) StartAsync(ctx context.Context) error { if e.instance.state == StateCommitted { if err := e.deployResourcesForCommittedState(ctx); err != nil { - return ErrDeployingResourcesForInstance.WithParams(e.instance.name).Wrap(err) + return ErrDeployingResourcesForInstance.WithParams(e.instance.k8sName).Wrap(err) } } if err := e.deployPod(ctx); err != nil { - return ErrDeployingPodForInstance.WithParams(e.instance.name).Wrap(err) + return ErrDeployingPodForInstance.WithParams(e.instance.k8sName).Wrap(err) } e.instance.state = StateStarted e.instance.sidecars.setStateForSidecars(StateStarted) - e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.name, e.instance.state.String()) + e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.k8sName, e.instance.state.String()) return nil } @@ -132,7 +132,7 @@ func (e *execution) Start(ctx context.Context) error { } if err := e.WaitInstanceIsRunning(ctx); err != nil { - return ErrWaitingForInstanceRunning.WithParams(e.instance.name).Wrap(err) + return ErrWaitingForInstanceRunning.WithParams(e.instance.k8sName).Wrap(err) } return nil } @@ -144,7 +144,7 @@ func (e *execution) IsRunning(ctx context.Context) (bool, error) { return false, ErrCheckingIfInstanceRunningNotAllowed.WithParams(e.instance.state.String()) } - return e.instance.K8sClient.IsReplicaSetRunning(ctx, e.instance.name) + return e.instance.K8sClient.IsReplicaSetRunning(ctx, e.instance.k8sName) } // WaitInstanceIsRunning waits until the instance is running @@ -157,7 +157,7 @@ func (e *execution) WaitInstanceIsRunning(ctx context.Context) error { for { running, err := e.IsRunning(ctx) if err != nil { - return ErrCheckingIfInstanceRunning.WithParams(e.instance.name).Wrap(err) + return ErrCheckingIfInstanceRunning.WithParams(e.instance.k8sName).Wrap(err) } if running { return nil @@ -166,7 +166,7 @@ func (e *execution) WaitInstanceIsRunning(ctx context.Context) error { select { case <-ctx.Done(): return ErrWaitingForInstanceTimeout. - WithParams(e.instance.name).Wrap(ctx.Err()) + WithParams(e.instance.k8sName).Wrap(ctx.Err()) case <-time.After(waitForInstanceRetry): continue } @@ -185,13 +185,13 @@ func (e *execution) WaitInstanceIsStopped(ctx context.Context) error { break } if err != nil { - return ErrCheckingIfInstanceStopped.WithParams(e.instance.name).Wrap(err) + return ErrCheckingIfInstanceStopped.WithParams(e.instance.k8sName).Wrap(err) } select { case <-ctx.Done(): return ErrWaitingForInstanceTimeout. - WithParams(e.instance.name).Wrap(ctx.Err()) + WithParams(e.instance.k8sName).Wrap(ctx.Err()) case <-time.After(waitForInstanceRetry): continue } @@ -210,11 +210,11 @@ func (e *execution) Stop(ctx context.Context) error { } if err := e.destroyPod(ctx); err != nil { - return ErrDestroyingPod.WithParams(e.instance.name).Wrap(err) + return ErrDestroyingPod.WithParams(e.instance.k8sName).Wrap(err) } e.instance.state = StateStopped e.instance.sidecars.setStateForSidecars(StateStopped) - e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.name, e.instance.state.String()) + e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.k8sName, e.instance.state.String()) return nil } @@ -222,12 +222,12 @@ func (e *execution) Stop(ctx context.Context) error { // Labels returns the labels for the instance func (e *execution) Labels() map[string]string { return map[string]string{ - labelAppKey: e.instance.name, + labelAppKey: e.instance.k8sName, labelManagedByKey: labelKnuuValue, labelScopeKey: e.instance.Scope, labelTestStartedKey: e.instance.StartTime, labelNameKey: e.instance.name, - labelK8sNameKey: e.instance.name, + labelK8sNameKey: e.instance.k8sName, labelTypeKey: e.instance.instanceType.String(), } } @@ -244,24 +244,24 @@ func (e *execution) Destroy(ctx context.Context) error { } if err := e.destroyPod(ctx); err != nil { - return ErrDestroyingPod.WithParams(e.instance.name).Wrap(err) + return ErrDestroyingPod.WithParams(e.instance.k8sName).Wrap(err) } if err := e.instance.resources.destroyResources(ctx); err != nil { - return ErrDestroyingResourcesForInstance.WithParams(e.instance.name).Wrap(err) + return ErrDestroyingResourcesForInstance.WithParams(e.instance.k8sName).Wrap(err) } err := e.instance.sidecars.applyFunctionToSidecars( func(sidecar SidecarManager) error { - e.instance.Logger.Debugf("Destroying sidecar resources from '%s'", sidecar.Instance().name) + e.instance.Logger.Debugf("Destroying sidecar resources from '%s'", sidecar.Instance().k8sName) return sidecar.Instance().resources.destroyResources(ctx) }) if err != nil { - return ErrDestroyingResourcesForSidecars.WithParams(e.instance.name).Wrap(err) + return ErrDestroyingResourcesForSidecars.WithParams(e.instance.k8sName).Wrap(err) } e.instance.state = StateDestroyed e.instance.sidecars.setStateForSidecars(StateDestroyed) - e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.name, e.instance.state.String()) + e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.k8sName, e.instance.state.String()) return nil } @@ -299,7 +299,7 @@ func BatchDestroy(ctx context.Context, instances ...*Instance) error { // deployResourcesForCommittedState handles resource deployment for instances in the 'Committed' state func (e *execution) deployResourcesForCommittedState(ctx context.Context) error { if err := e.instance.resources.deployResources(ctx); err != nil { - return ErrDeployingResourcesForInstance.WithParams(e.instance.name).Wrap(err) + return ErrDeployingResourcesForInstance.WithParams(e.instance.k8sName).Wrap(err) } err := e.instance.sidecars.applyFunctionToSidecars(func(sc SidecarManager) error { if err := sc.PreStart(ctx); err != nil { @@ -308,7 +308,7 @@ func (e *execution) deployResourcesForCommittedState(ctx context.Context) error return sc.Instance().resources.deployResources(ctx) }) if err != nil { - return ErrDeployingResourcesForSidecars.WithParams(e.instance.name).Wrap(err) + return ErrDeployingResourcesForSidecars.WithParams(e.instance.k8sName).Wrap(err) } return nil @@ -320,16 +320,16 @@ func (e *execution) deployPod(ctx context.Context) error { labels := e.Labels() // create a service account for the pod - if err := e.instance.K8sClient.CreateServiceAccount(ctx, e.instance.name, labels); err != nil { + if err := e.instance.K8sClient.CreateServiceAccount(ctx, e.instance.k8sName, labels); err != nil { return ErrFailedToCreateServiceAccount.Wrap(err) } // create a role and role binding for the pod if there are policy rules if len(e.instance.security.policyRules) > 0 { - if err := e.instance.K8sClient.CreateRole(ctx, e.instance.name, labels, e.instance.security.policyRules); err != nil { + if err := e.instance.K8sClient.CreateRole(ctx, e.instance.k8sName, labels, e.instance.security.policyRules); err != nil { return ErrFailedToCreateRole.Wrap(err) } - if err := e.instance.K8sClient.CreateRoleBinding(ctx, e.instance.name, labels, e.instance.name, e.instance.name); err != nil { + if err := e.instance.K8sClient.CreateRoleBinding(ctx, e.instance.k8sName, labels, e.instance.k8sName, e.instance.k8sName); err != nil { return ErrFailedToCreateRoleBinding.Wrap(err) } } @@ -344,8 +344,8 @@ func (e *execution) deployPod(ctx context.Context) error { e.instance.kubernetesReplicaSet = replicaSet // Log the deployment of the pod - e.instance.Logger.Debugf("Started statefulSet '%s'", e.instance.name) - e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.name, e.instance.state.String()) + e.instance.Logger.Debugf("Started statefulSet '%s'", e.instance.k8sName) + e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.k8sName, e.instance.state.String()) return nil } @@ -353,13 +353,13 @@ func (e *execution) deployPod(ctx context.Context) error { // destroyPod destroys the pod for the instance (no grace period) // Skips if the pod is already destroyed func (e *execution) destroyPod(ctx context.Context) error { - err := e.instance.K8sClient.DeleteReplicaSetWithGracePeriod(ctx, e.instance.name, nil) + err := e.instance.K8sClient.DeleteReplicaSetWithGracePeriod(ctx, e.instance.k8sName, nil) if err != nil { return ErrFailedToDeletePod.Wrap(err) } // Delete the service account for the pod - if err := e.instance.K8sClient.DeleteServiceAccount(ctx, e.instance.name); err != nil { + if err := e.instance.K8sClient.DeleteServiceAccount(ctx, e.instance.k8sName); err != nil { return ErrFailedToDeleteServiceAccount.Wrap(err) } @@ -368,10 +368,10 @@ func (e *execution) destroyPod(ctx context.Context) error { return nil } - if err := e.instance.K8sClient.DeleteRole(ctx, e.instance.name); err != nil { + if err := e.instance.K8sClient.DeleteRole(ctx, e.instance.k8sName); err != nil { return ErrFailedToDeleteRole.Wrap(err) } - if err := e.instance.K8sClient.DeleteRoleBinding(ctx, e.instance.name); err != nil { + if err := e.instance.K8sClient.DeleteRoleBinding(ctx, e.instance.k8sName); err != nil { return ErrFailedToDeleteRoleBinding.Wrap(err) } @@ -381,7 +381,7 @@ func (e *execution) destroyPod(ctx context.Context) error { // prepareConfig prepares the config for the instance func (e *execution) prepareReplicaSetConfig() k8s.ReplicaSetConfig { containerConfig := k8s.ContainerConfig{ - Name: e.instance.name, + Name: e.instance.k8sName, Image: e.instance.build.imageName, ImagePullPolicy: e.instance.build.imagePullPolicy, Command: e.instance.build.command, @@ -401,7 +401,7 @@ func (e *execution) prepareReplicaSetConfig() k8s.ReplicaSetConfig { sidecarConfigs := make([]k8s.ContainerConfig, 0) for _, sidecar := range e.instance.sidecars.sidecars { sidecarConfigs = append(sidecarConfigs, k8s.ContainerConfig{ - Name: sidecar.Instance().name, + Name: sidecar.Instance().k8sName, Image: sidecar.Instance().build.imageName, Command: sidecar.Instance().build.command, Args: sidecar.Instance().build.args, @@ -420,9 +420,9 @@ func (e *execution) prepareReplicaSetConfig() k8s.ReplicaSetConfig { podConfig := k8s.PodConfig{ Namespace: e.instance.K8sClient.Namespace(), - Name: e.instance.name, + Name: e.instance.k8sName, Labels: e.Labels(), - ServiceAccountName: e.instance.name, + ServiceAccountName: e.instance.k8sName, FsGroup: e.instance.storage.fsGroup, ContainerConfig: containerConfig, SidecarConfigs: sidecarConfigs, @@ -430,7 +430,7 @@ func (e *execution) prepareReplicaSetConfig() k8s.ReplicaSetConfig { return k8s.ReplicaSetConfig{ Namespace: e.instance.K8sClient.Namespace(), - Name: e.instance.name, + Name: e.instance.k8sName, Labels: e.Labels(), Replicas: 1, PodConfig: podConfig, diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index 2a339515..681a76eb 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" "github.com/celestiaorg/knuu/pkg/k8s" + "github.com/celestiaorg/knuu/pkg/names" "github.com/celestiaorg/knuu/pkg/system" ) @@ -23,7 +24,7 @@ const ( // Instance represents a instance type Instance struct { - *system.SystemDependencies + system.SystemDependencies resources *resources network *network @@ -35,6 +36,7 @@ type Instance struct { sidecars *sidecars name string + k8sName string state InstanceState instanceType InstanceType @@ -43,15 +45,16 @@ type Instance struct { parentInstance *Instance } -func New(name string, sysDeps *system.SystemDependencies) (*Instance, error) { - name = k8s.SanitizeName(name) - if sysDeps.HasInstanceName(name) { - return nil, ErrInstanceNameAlreadyExists.WithParams(name) +func New(name string, sysDeps system.SystemDependencies) (*Instance, error) { + k8sName, err := names.NewRandomK8(name) + if err != nil { + return nil, ErrGeneratingK8sName.WithParams(name).Wrap(err) } - sysDeps.AddInstanceName(name) + // Create the instance i := &Instance{ name: name, + k8sName: k8sName, state: StateNone, instanceType: BasicInstance, SystemDependencies: sysDeps, @@ -111,7 +114,7 @@ func (i *Instance) Name() string { } func (i *Instance) K8sName() string { - return i.name + return i.k8sName } func (i *Instance) State() InstanceState { @@ -122,9 +125,23 @@ func (i *Instance) SetInstanceType(instanceType InstanceType) { i.instanceType = instanceType } -// cloneWithSuffix clones the instance with a suffix -func (i *Instance) CloneWithSuffix(suffix string) (*Instance, error) { - return i.CloneWithName(i.name + "-" + suffix) +// Clone creates a clone of the instance +// This function can only be called in the state 'Committed' +// When cloning an instance that is a sidecar, the clone will be not a sidecar +// When cloning an instance with sidecars, the sidecars will be cloned as well +func (i *Instance) Clone() (*Instance, error) { + if !i.IsInState(StateCommitted) { + return nil, ErrCloningNotAllowed.WithParams(i.state.String()) + } + + newK8sName, err := names.NewRandomK8(i.name) + if err != nil { + return nil, ErrGeneratingK8sName.WithParams(i.name).Wrap(err) + } + // Create a new instance with the same attributes as the original instance + ins := i.CloneWithSuffix("") + ins.k8sName = newK8sName + return ins, nil } // CloneWithName creates a clone of the instance with a given name @@ -132,19 +149,26 @@ func (i *Instance) CloneWithSuffix(suffix string) (*Instance, error) { // When cloning an instance that is a sidecar, the clone will be not a sidecar // When cloning an instance with sidecars, the sidecars will be cloned as well func (i *Instance) CloneWithName(name string) (*Instance, error) { - name = k8s.SanitizeName(name) - if i.SystemDependencies.HasInstanceName(name) { - return nil, ErrInstanceNameAlreadyExists.WithParams(name) + if !i.IsInState(StateCommitted) { + return nil, ErrCloningNotAllowedForSidecar.WithParams(i.state.String()) } - i.SystemDependencies.AddInstanceName(name) - clonedSidecars, err := i.sidecars.clone() + newK8sName, err := names.NewRandomK8(name) if err != nil { - return nil, err + return nil, ErrGeneratingK8sNameForSidecar.WithParams(name).Wrap(err) } + // Create a new instance with the same attributes as the original instance + ins := i.CloneWithSuffix("") + ins.name = name + ins.k8sName = newK8sName + return ins, nil +} +// cloneWithSuffix clones the instance with a suffix +func (i *Instance) CloneWithSuffix(suffix string) *Instance { newInstance := &Instance{ - name: name, + name: i.name + "-" + suffix, + k8sName: i.k8sName + "-" + suffix, SystemDependencies: i.SystemDependencies, build: i.build.clone(), @@ -154,7 +178,7 @@ func (i *Instance) CloneWithName(name string) (*Instance, error) { storage: i.storage.clone(), monitoring: i.monitoring.clone(), security: i.security.clone(), - sidecars: clonedSidecars, + sidecars: i.sidecars.cloneWithSuffix(suffix), state: i.state, instanceType: i.instanceType, @@ -170,5 +194,5 @@ func (i *Instance) CloneWithName(name string) (*Instance, error) { newInstance.resources.instance = newInstance newInstance.build.instance = newInstance - return newInstance, nil + return newInstance } diff --git a/pkg/instance/network.go b/pkg/instance/network.go index 27dfab57..91caee19 100644 --- a/pkg/instance/network.go +++ b/pkg/instance/network.go @@ -58,9 +58,9 @@ func (n *network) PortForwardTCP(ctx context.Context, port int) (int, error) { } // Forward the port - pod, err := n.instance.K8sClient.GetFirstPodFromReplicaSet(ctx, n.instance.name) + pod, err := n.instance.K8sClient.GetFirstPodFromReplicaSet(ctx, n.instance.k8sName) if err != nil { - return -1, ErrGettingPodFromReplicaSet.WithParams(n.instance.name).Wrap(err) + return -1, ErrGettingPodFromReplicaSet.WithParams(n.instance.k8sName).Wrap(err) } for attempt := 1; attempt <= maxRetries; attempt++ { @@ -99,7 +99,7 @@ func (n *network) AddPortUDP(port int) error { } n.portsUDP = append(n.portsUDP, port) - n.instance.Logger.Debugf("Added UDP port '%d' to instance '%s'", port, n.instance.name) + n.instance.Logger.Debugf("Added UDP port '%d' to instance '%s'", port, n.instance.k8sName) return nil } @@ -111,22 +111,22 @@ func (n *network) GetIP(ctx context.Context) (string, error) { return n.kubernetesService.Spec.ClusterIP, nil } // If not, proceed with the existing logic to deploy the service and get the IP - svc, err := n.instance.K8sClient.GetService(ctx, n.instance.name) + svc, err := n.instance.K8sClient.GetService(ctx, n.instance.k8sName) if err != nil || svc == nil { // Service does not exist, so we need to deploy it err := n.deployService(ctx, n.portsTCP, n.portsUDP) if err != nil { - return "", ErrDeployingServiceForInstance.WithParams(n.instance.name).Wrap(err) + return "", ErrDeployingServiceForInstance.WithParams(n.instance.k8sName).Wrap(err) } - svc, err = n.instance.K8sClient.GetService(ctx, n.instance.name) + svc, err = n.instance.K8sClient.GetService(ctx, n.instance.k8sName) if err != nil { - return "", ErrGettingServiceForInstance.WithParams(n.instance.name).Wrap(err) + return "", ErrGettingServiceForInstance.WithParams(n.instance.k8sName).Wrap(err) } } ip := svc.Spec.ClusterIP if ip == "" { - return "", ErrGettingServiceIP.WithParams(n.instance.name) + return "", ErrGettingServiceIP.WithParams(n.instance.k8sName) } // Update i.kubernetesService for future reference @@ -138,21 +138,21 @@ func (n *network) GetIP(ctx context.Context) (string, error) { func (n *network) deployService(ctx context.Context, portsTCP, portsUDP []int) error { // a sidecar instance should use the parent instance's service if n.instance.sidecars.IsSidecar() { - return ErrDeployingServiceForSidecar.WithParams(n.instance.name) + return ErrDeployingServiceForSidecar.WithParams(n.instance.k8sName) } var ( - serviceName = n.instance.name + serviceName = n.instance.k8sName labels = n.instance.execution.Labels() labelSelectors = labels ) srv, err := n.instance.K8sClient.CreateService(ctx, serviceName, labels, labelSelectors, portsTCP, portsUDP) if err != nil { - return ErrDeployingService.WithParams(n.instance.name).Wrap(err) + return ErrDeployingService.WithParams(n.instance.k8sName).Wrap(err) } n.kubernetesService = srv - n.instance.Logger.Debugf("Started service '%s'", n.instance.name) + n.instance.Logger.Debugf("Started service '%s'", n.instance.k8sName) return nil } @@ -160,11 +160,11 @@ func (n *network) deployService(ctx context.Context, portsTCP, portsUDP []int) e func (n *network) patchService(ctx context.Context, portsTCP, portsUDP []int) error { // a sidecar instance should use the parent instance's service if n.instance.sidecars.IsSidecar() { - return ErrPatchingServiceForSidecar.WithParams(n.instance.name) + return ErrPatchingServiceForSidecar.WithParams(n.instance.k8sName) } var ( - serviceName = n.instance.name + serviceName = n.instance.k8sName labels = n.instance.execution.Labels() labelSelectors = labels ) @@ -180,7 +180,7 @@ func (n *network) patchService(ctx context.Context, portsTCP, portsUDP []int) er // destroyService destroys the service for the instance func (n *network) destroyService(ctx context.Context) error { - return n.instance.K8sClient.DeleteService(ctx, n.instance.name) + return n.instance.K8sClient.DeleteService(ctx, n.instance.k8sName) } // isTCPPortRegistered returns true if the given port is registered @@ -220,9 +220,9 @@ func (n *network) Disable(ctx context.Context) error { return ErrDisablingNetworkNotAllowed.WithParams(n.instance.state.String()) } - err := n.instance.K8sClient.CreateNetworkPolicy(ctx, n.instance.name, n.instance.execution.Labels(), nil, nil) + err := n.instance.K8sClient.CreateNetworkPolicy(ctx, n.instance.k8sName, n.instance.execution.Labels(), nil, nil) if err != nil { - return ErrDisablingNetwork.WithParams(n.instance.name).Wrap(err) + return ErrDisablingNetwork.WithParams(n.instance.k8sName).Wrap(err) } return nil } @@ -234,9 +234,9 @@ func (n *network) Enable(ctx context.Context) error { return ErrEnablingNetworkNotAllowed.WithParams(n.instance.state.String()) } - err := n.instance.K8sClient.DeleteNetworkPolicy(ctx, n.instance.name) + err := n.instance.K8sClient.DeleteNetworkPolicy(ctx, n.instance.k8sName) if err != nil { - return ErrEnablingNetwork.WithParams(n.instance.name).Wrap(err) + return ErrEnablingNetwork.WithParams(n.instance.k8sName).Wrap(err) } return nil } @@ -248,7 +248,7 @@ func (n *network) IsDisabled(ctx context.Context) (bool, error) { return false, ErrCheckingIfNetworkDisabledNotAllowed.WithParams(n.instance.state.String()) } - return n.instance.K8sClient.NetworkPolicyExists(ctx, n.instance.name), nil + return n.instance.K8sClient.NetworkPolicyExists(ctx, n.instance.k8sName), nil } // deployService deploys the service for the instance @@ -257,17 +257,17 @@ func (n *network) deployOrPatchService(ctx context.Context, portsTCP, portsUDP [ return nil } - n.instance.Logger.Debugf("Ports not empty, deploying service for instance '%s'", n.instance.name) - svc, _ := n.instance.K8sClient.GetService(ctx, n.instance.name) + n.instance.Logger.Debugf("Ports not empty, deploying service for instance '%s'", n.instance.k8sName) + svc, _ := n.instance.K8sClient.GetService(ctx, n.instance.k8sName) if svc == nil { if err := n.deployService(ctx, portsTCP, portsUDP); err != nil { - return ErrDeployingServiceForInstance.WithParams(n.instance.name).Wrap(err) + return ErrDeployingServiceForInstance.WithParams(n.instance.k8sName).Wrap(err) } return nil } if err := n.patchService(ctx, portsTCP, portsUDP); err != nil { - return ErrPatchingServiceForInstance.WithParams(n.instance.name).Wrap(err) + return ErrPatchingServiceForInstance.WithParams(n.instance.k8sName).Wrap(err) } return nil } @@ -276,7 +276,7 @@ func (n *network) enableIfDisabled(ctx context.Context) error { disableNetwork, err := n.IsDisabled(ctx) if err != nil { n.instance.Logger.Errorf("error checking network status for instance") - return ErrCheckingNetworkStatusForInstance.WithParams(n.instance.name).Wrap(err) + return ErrCheckingNetworkStatusForInstance.WithParams(n.instance.k8sName).Wrap(err) } if !disableNetwork { @@ -284,7 +284,7 @@ func (n *network) enableIfDisabled(ctx context.Context) error { } if err := n.Enable(ctx); err != nil { n.instance.Logger.Errorf("error enabling network for instance") - return ErrEnablingNetworkForInstance.WithParams(n.instance.name).Wrap(err) + return ErrEnablingNetworkForInstance.WithParams(n.instance.k8sName).Wrap(err) } return nil } diff --git a/pkg/instance/proxy.go b/pkg/instance/proxy.go index 79a3760d..378b009e 100644 --- a/pkg/instance/proxy.go +++ b/pkg/instance/proxy.go @@ -15,12 +15,12 @@ func (n *network) AddHost(ctx context.Context, port int) (host string, err error return "", ErrProxyNotInitialized } - serviceName := n.instance.name + serviceName := n.instance.k8sName if n.instance.sidecars.IsSidecar() { // The service is created for the main instance and // named after it which will be the parent instance for sidecars, // so we need to use the parent instance's service name. - serviceName = n.instance.parentInstance.name + serviceName = n.instance.parentInstance.k8sName } prefix := fmt.Sprintf("%s-%d", serviceName, port) diff --git a/pkg/instance/resources.go b/pkg/instance/resources.go index 6f4f5eda..1c54aa4a 100644 --- a/pkg/instance/resources.go +++ b/pkg/instance/resources.go @@ -52,7 +52,7 @@ func (r *resources) CreateCustomResource(ctx context.Context, gvr *schema.GroupV return ErrCustomResourceDefinitionDoesNotExist.WithParams(gvr.Resource) } - return r.instance.K8sClient.CreateCustomResource(ctx, r.instance.name, gvr, obj) + return r.instance.K8sClient.CreateCustomResource(ctx, r.instance.k8sName, gvr, obj) } // CustomResourceDefinitionExists checks if the custom resource definition exists @@ -78,7 +78,7 @@ func (r *resources) deployResources(ctx context.Context) error { func (r *resources) deployStorage(ctx context.Context) error { if len(r.instance.storage.volumes) != 0 { if err := r.instance.storage.deployVolume(ctx); err != nil { - return ErrDeployingVolumeForInstance.WithParams(r.instance.name).Wrap(err) + return ErrDeployingVolumeForInstance.WithParams(r.instance.k8sName).Wrap(err) } } if len(r.instance.storage.files) == 0 { @@ -86,7 +86,7 @@ func (r *resources) deployStorage(ctx context.Context) error { } if err := r.instance.storage.deployFiles(ctx); err != nil { - return ErrDeployingFilesForInstance.WithParams(r.instance.name).Wrap(err) + return ErrDeployingFilesForInstance.WithParams(r.instance.k8sName).Wrap(err) } return nil } @@ -110,20 +110,20 @@ func (r *resources) deployService(ctx context.Context) error { func (r *resources) destroyResources(ctx context.Context) error { if len(r.instance.storage.volumes) != 0 { if err := r.instance.storage.destroyVolume(ctx); err != nil { - return ErrDestroyingVolumeForInstance.WithParams(r.instance.name).Wrap(err) + return ErrDestroyingVolumeForInstance.WithParams(r.instance.k8sName).Wrap(err) } } if len(r.instance.storage.files) != 0 { err := r.instance.storage.destroyFiles(ctx) if err != nil { - return ErrDestroyingFilesForInstance.WithParams(r.instance.name).Wrap(err) + return ErrDestroyingFilesForInstance.WithParams(r.instance.k8sName).Wrap(err) } } if r.instance.network.kubernetesService != nil { err := r.instance.network.destroyService(ctx) if err != nil { - return ErrDestroyingServiceForInstance.WithParams(r.instance.name).Wrap(err) + return ErrDestroyingServiceForInstance.WithParams(r.instance.k8sName).Wrap(err) } } @@ -131,7 +131,7 @@ func (r *resources) destroyResources(ctx context.Context) error { if !r.instance.sidecars.IsSidecar() { // enable network when network is disabled if err := r.instance.network.enableIfDisabled(ctx); err != nil { - return ErrEnablingNetworkForInstance.WithParams(r.instance.name).Wrap(err) + return ErrEnablingNetworkForInstance.WithParams(r.instance.k8sName).Wrap(err) } } diff --git a/pkg/instance/sidecars.go b/pkg/instance/sidecars.go index 5aa64125..bdd235aa 100644 --- a/pkg/instance/sidecars.go +++ b/pkg/instance/sidecars.go @@ -7,10 +7,10 @@ import ( ) type SidecarManager interface { - Initialize(ctx context.Context, sysDeps *system.SystemDependencies) error + Initialize(ctx context.Context, sysDeps system.SystemDependencies) error Instance() *Instance PreStart(ctx context.Context) error - Clone() (SidecarManager, error) + CloneWithSuffix(suffix string) SidecarManager } type sidecars struct { @@ -77,7 +77,7 @@ func (s *sidecars) verifySidecarsStates() error { func (s *sidecars) applyFunctionToSidecars(fn func(sc SidecarManager) error) error { for _, i := range s.sidecars { if err := fn(i); err != nil { - return ErrApplyingFunctionToSidecar.WithParams(i.Instance().name).Wrap(err) + return ErrApplyingFunctionToSidecar.WithParams(i.Instance().k8sName).Wrap(err) } } return nil @@ -92,16 +92,12 @@ func (s *sidecars) setStateForSidecars(state InstanceState) { }) } -func (s *sidecars) clone() (*sidecars, error) { +func (s *sidecars) cloneWithSuffix(suffix string) *sidecars { clonedSidecars := make([]SidecarManager, len(s.sidecars)) for i, sc := range s.sidecars { - cloned, err := sc.Clone() - if err != nil { - return nil, err - } - clonedSidecars[i] = cloned + clonedSidecars[i] = sc.CloneWithSuffix(suffix) } return &sidecars{ sidecars: clonedSidecars, - }, nil + } } diff --git a/pkg/instance/storage.go b/pkg/instance/storage.go index 1c7a50cf..89b70062 100644 --- a/pkg/instance/storage.go +++ b/pkg/instance/storage.go @@ -283,19 +283,19 @@ func (s *storage) deployVolume(ctx context.Context) error { for _, volume := range s.volumes { totalSize.Add(volume.Size) } - s.instance.K8sClient.CreatePersistentVolumeClaim(ctx, s.instance.name, s.instance.execution.Labels(), totalSize) - s.instance.Logger.Debugf("Deployed persistent volume '%s'", s.instance.name) + s.instance.K8sClient.CreatePersistentVolumeClaim(ctx, s.instance.k8sName, s.instance.execution.Labels(), totalSize) + s.instance.Logger.Debugf("Deployed persistent volume '%s'", s.instance.k8sName) return nil } // destroyVolume destroys the volume for the instance func (s *storage) destroyVolume(ctx context.Context) error { - err := s.instance.K8sClient.DeletePersistentVolumeClaim(ctx, s.instance.name) + err := s.instance.K8sClient.DeletePersistentVolumeClaim(ctx, s.instance.k8sName) if err != nil { return ErrFailedToDeletePersistentVolumeClaim.Wrap(err) } - s.instance.Logger.Debugf("Destroyed persistent volume '%s'", s.instance.name) + s.instance.Logger.Debugf("Destroyed persistent volume '%s'", s.instance.k8sName) return nil } @@ -325,23 +325,23 @@ func (s *storage) deployFiles(ctx context.Context) error { } // create configmap - _, err := s.instance.K8sClient.CreateConfigMap(ctx, s.instance.name, s.instance.execution.Labels(), data) + _, err := s.instance.K8sClient.CreateConfigMap(ctx, s.instance.k8sName, s.instance.execution.Labels(), data) if err != nil { return ErrFailedToCreateConfigMap.Wrap(err) } - s.instance.Logger.Debugf("Deployed configmap '%s'", s.instance.name) + s.instance.Logger.Debugf("Deployed configmap '%s'", s.instance.k8sName) return nil } // destroyFiles destroys the files for the instance func (s *storage) destroyFiles(ctx context.Context) error { - if err := s.instance.K8sClient.DeleteConfigMap(ctx, s.instance.name); err != nil { + if err := s.instance.K8sClient.DeleteConfigMap(ctx, s.instance.k8sName); err != nil { return ErrFailedToDeleteConfigMap.Wrap(err) } - s.instance.Logger.Debugf("Destroyed configmap '%s'", s.instance.name) + s.instance.Logger.Debugf("Destroyed configmap '%s'", s.instance.k8sName) return nil } diff --git a/pkg/knuu/instance.go b/pkg/knuu/instance.go index 5a086588..c9e6e752 100644 --- a/pkg/knuu/instance.go +++ b/pkg/knuu/instance.go @@ -10,6 +10,6 @@ func (k *Knuu) NewInstance(name string) (*instance.Instance, error) { return instance.New(name, k.SystemDependencies) } -func (k *Knuu) NewPreloader(name string) (*preloader.Preloader, error) { - return preloader.New(name, k.SystemDependencies) +func (k *Knuu) NewPreloader() (*preloader.Preloader, error) { + return preloader.New(k.SystemDependencies) } diff --git a/pkg/knuu/knuu.go b/pkg/knuu/knuu.go index ba9f6d22..06ce179d 100644 --- a/pkg/knuu/knuu.go +++ b/pkg/knuu/knuu.go @@ -33,7 +33,7 @@ const ( ) type Knuu struct { - *system.SystemDependencies + system.SystemDependencies timeout time.Duration } @@ -53,7 +53,7 @@ func New(ctx context.Context, opts Options) (*Knuu, error) { } k := &Knuu{ - SystemDependencies: &system.SystemDependencies{ + SystemDependencies: system.SystemDependencies{ K8sClient: opts.K8sClient, MinioClient: opts.MinioClient, ImageBuilder: opts.ImageBuilder, diff --git a/pkg/preloader/errors.go b/pkg/preloader/errors.go index 0b16a3c4..08beb826 100644 --- a/pkg/preloader/errors.go +++ b/pkg/preloader/errors.go @@ -7,5 +7,5 @@ import ( type Error = errors.Error var ( - ErrPreloaderNameAlreadyExists = errors.New("PreloaderNameAlreadyExists", "preloader name '%s' already exists") + ErrGeneratingK8sNameForPreloader = errors.New("GeneratingK8sNameForPreloader", "error generating k8s name for preloader") ) diff --git a/pkg/preloader/preloader.go b/pkg/preloader/preloader.go index 2b807178..b60f782b 100644 --- a/pkg/preloader/preloader.go +++ b/pkg/preloader/preloader.go @@ -6,7 +6,7 @@ import ( v1 "k8s.io/api/core/v1" - "github.com/celestiaorg/knuu/pkg/k8s" + "github.com/celestiaorg/knuu/pkg/names" "github.com/celestiaorg/knuu/pkg/system" ) @@ -30,21 +30,19 @@ const ( // A preloader makes sure that the images are preloaded before the test suite starts. // Hint: If you use a Preloader per test suite, you can save resources type Preloader struct { - Name string `json:"name"` - Images []string `json:"images"` - *system.SystemDependencies + K8sName string `json:"k8sName"` + Images []string `json:"images"` + system.SystemDependencies } // New creates a new preloader -func New(name string, sysDeps *system.SystemDependencies) (*Preloader, error) { - name = k8s.SanitizeName(name) - if sysDeps.HasInstanceName(name) { - return nil, ErrPreloaderNameAlreadyExists.WithParams(name) +func New(sysDeps system.SystemDependencies) (*Preloader, error) { + k8sName, err := names.NewRandomK8(preloaderName) + if err != nil { + return nil, ErrGeneratingK8sNameForPreloader.Wrap(err) } - sysDeps.AddInstanceName(name) - return &Preloader{ - Name: name, + K8sName: k8sName, Images: []string{}, SystemDependencies: sysDeps, }, nil @@ -89,7 +87,7 @@ func (p *Preloader) EmptyImages(ctx context.Context) error { func (p *Preloader) preloadImages(ctx context.Context) error { // delete the daemonset if no images are preloaded if len(p.Images) == 0 { - return p.K8sClient.DeleteDaemonSet(ctx, p.Name) + return p.K8sClient.DeleteDaemonSet(ctx, p.K8sName) } var initContainers []v1.Container @@ -113,28 +111,28 @@ func (p *Preloader) preloadImages(ctx context.Context) error { }) labels := map[string]string{ - labelApp: p.Name, + labelApp: p.K8sName, labelManagedBy: managedByLabel, labelScope: p.Scope, labelTestStarted: p.StartTime, } - exists, err := p.K8sClient.DaemonSetExists(ctx, p.Name) + exists, err := p.K8sClient.DaemonSetExists(ctx, p.K8sName) if err != nil { return err } // update the daemonset if it already exists if exists { - _, err = p.K8sClient.UpdateDaemonSet(ctx, p.Name, labels, initContainers, containers) + _, err = p.K8sClient.UpdateDaemonSet(ctx, p.K8sName, labels, initContainers, containers) return err } // create the daemonset if it doesn't exist - _, err = p.K8sClient.CreateDaemonSet(ctx, p.Name, labels, initContainers, containers) + _, err = p.K8sClient.CreateDaemonSet(ctx, p.K8sName, labels, initContainers, containers) return err } func (p *Preloader) Cleanup(ctx context.Context) error { - return p.K8sClient.DeleteDaemonSet(ctx, p.Name) + return p.K8sClient.DeleteDaemonSet(ctx, p.K8sName) } diff --git a/pkg/sidecars/netshaper/netshaper.go b/pkg/sidecars/netshaper/netshaper.go index b4d40973..3cb06231 100644 --- a/pkg/sidecars/netshaper/netshaper.go +++ b/pkg/sidecars/netshaper/netshaper.go @@ -42,7 +42,7 @@ func New() *NetShaper { // Initialize initializes the BitTwister sidecar // and it is called once the instance.AddSidecar is called -func (bt *NetShaper) Initialize(ctx context.Context, sysDeps *system.SystemDependencies) error { +func (bt *NetShaper) Initialize(ctx context.Context, sysDeps system.SystemDependencies) error { var err error bt.instance, err = instance.New(instanceName, sysDeps) if err != nil { @@ -100,15 +100,11 @@ func (bt *NetShaper) Instance() *instance.Instance { return bt.instance } -func (bt *NetShaper) Clone() (instance.SidecarManager, error) { - clone, err := bt.instance.CloneWithName(instanceName) - if err != nil { - return nil, err - } +func (bt *NetShaper) CloneWithSuffix(suffix string) instance.SidecarManager { return &NetShaper{ - instance: clone, + instance: bt.instance.CloneWithSuffix(suffix), port: bt.port, image: bt.image, networkInterface: bt.networkInterface, - }, nil + } } diff --git a/pkg/sidecars/netshaper/netshaper_test.go b/pkg/sidecars/netshaper/netshaper_test.go index 1923a537..8182643c 100644 --- a/pkg/sidecars/netshaper/netshaper_test.go +++ b/pkg/sidecars/netshaper/netshaper_test.go @@ -18,14 +18,14 @@ type TestSuite struct { suite.Suite bt *NetShaper ctx context.Context - sysDeps *system.SystemDependencies + sysDeps system.SystemDependencies mockServer *httptest.Server } func (s *TestSuite) SetupTest() { s.bt = New() s.ctx = context.Background() - s.sysDeps = &system.SystemDependencies{ + s.sysDeps = system.SystemDependencies{ Logger: logrus.New(), } @@ -80,8 +80,7 @@ func (s *TestSuite) TestCloneWithSuffix() { s.Require().NoError(err) s.Require().NotNil(s.bt.instance, "Instance should be initialized before cloning") - clone, err := s.bt.Clone() - s.Require().NoError(err) + clone := s.bt.CloneWithSuffix("test") s.Assert().NotNil(clone) clonedBt, ok := clone.(*NetShaper) @@ -108,8 +107,7 @@ func (s *TestSuite) TestCloneWithSuffixWithCustomValues() { s.bt.SetImage("nginx") s.bt.SetNetworkInterface("eth0") - clone, err := s.bt.Clone() - s.Require().NoError(err) + clone := s.bt.CloneWithSuffix("test") s.Assert().NotNil(clone) clonedBt, ok := clone.(*NetShaper) diff --git a/pkg/sidecars/observability/obsy.go b/pkg/sidecars/observability/obsy.go index b9c91a79..f332d338 100644 --- a/pkg/sidecars/observability/obsy.go +++ b/pkg/sidecars/observability/obsy.go @@ -86,7 +86,7 @@ func New() *Obsy { } } -func (o *Obsy) Initialize(ctx context.Context, sysDeps *system.SystemDependencies) error { +func (o *Obsy) Initialize(ctx context.Context, sysDeps system.SystemDependencies) error { var err error o.instance, err = instance.New(otelAgentName, sysDeps) if err != nil { @@ -149,14 +149,10 @@ func (o *Obsy) Instance() *instance.Instance { return o.instance } -func (o *Obsy) Clone() (instance.SidecarManager, error) { - clone, err := o.instance.CloneWithName(otelAgentName) - if err != nil { - return nil, err - } +func (o *Obsy) CloneWithSuffix(suffix string) instance.SidecarManager { conf := *o.obsyConfig return &Obsy{ - instance: clone, + instance: o.instance.CloneWithSuffix(suffix), obsyConfig: &conf, - }, nil + } } diff --git a/pkg/sidecars/observability/obsy_test.go b/pkg/sidecars/observability/obsy_test.go index 4f521ee6..3ec6f4f5 100644 --- a/pkg/sidecars/observability/obsy_test.go +++ b/pkg/sidecars/observability/obsy_test.go @@ -13,7 +13,7 @@ import ( type TestSuite struct { suite.Suite - sysDeps *system.SystemDependencies + sysDeps system.SystemDependencies } func TestObsyTestSuite(t *testing.T) { @@ -30,7 +30,7 @@ func (m *mockK8sCli) Namespace() string { } func (s *TestSuite) SetupTest() { - s.sysDeps = &system.SystemDependencies{ + s.sysDeps = system.SystemDependencies{ K8sClient: &mockK8sCli{ namespace: "test", KubeManager: &k8s.Client{}, @@ -62,8 +62,7 @@ func (s *TestSuite) TestCloneWithSuffix() { err := o.Initialize(context.Background(), s.sysDeps) s.Require().NoError(err) - clone, err := o.Clone() - s.Require().NoError(err) + clone := o.CloneWithSuffix("test") s.Assert().NotNil(clone) clonedObsy, ok := clone.(*Obsy) diff --git a/pkg/sidecars/tshark/tshark.go b/pkg/sidecars/tshark/tshark.go index 792c0afb..c2aa8617 100644 --- a/pkg/sidecars/tshark/tshark.go +++ b/pkg/sidecars/tshark/tshark.go @@ -64,7 +64,7 @@ var ( // Initialize initializes the BitTwister sidecar // and it is called once the instance.AddSidecar is called -func (t *Tshark) Initialize(ctx context.Context, sysDeps *system.SystemDependencies) error { +func (t *Tshark) Initialize(ctx context.Context, sysDeps system.SystemDependencies) error { if err := t.validateConfig(); err != nil { return err } @@ -135,13 +135,9 @@ func (t *Tshark) Instance() *instance.Instance { return t.instance } -func (t *Tshark) Clone() (instance.SidecarManager, error) { - clone, err := t.instance.CloneWithName(tsharkCollectorName) - if err != nil { - return nil, err - } +func (t *Tshark) CloneWithSuffix(suffix string) instance.SidecarManager { return &Tshark{ - instance: clone, + instance: t.instance.CloneWithSuffix(suffix), VolumeSize: t.VolumeSize, S3AccessKey: t.S3AccessKey, S3SecretKey: t.S3SecretKey, @@ -151,5 +147,5 @@ func (t *Tshark) Clone() (instance.SidecarManager, error) { S3KeyPrefix: t.S3KeyPrefix, S3Endpoint: t.S3Endpoint, UploadInterval: t.UploadInterval, - }, nil + } } diff --git a/pkg/sidecars/tshark/tshark_test.go b/pkg/sidecars/tshark/tshark_test.go index 73e5fbfc..94ab1203 100644 --- a/pkg/sidecars/tshark/tshark_test.go +++ b/pkg/sidecars/tshark/tshark_test.go @@ -83,7 +83,7 @@ func TestTsharkInitialize(t *testing.T) { logrus.New(), ) require.NoError(t, err) - sysDeps := &system.SystemDependencies{ + sysDeps := system.SystemDependencies{ K8sClient: k8sClient, Logger: logger, } @@ -169,7 +169,7 @@ func TestTsharkValidateConfig(t *testing.T) { } func TestTsharkCloneWithSuffix(t *testing.T) { - testInstance, err := instance.New("testInstance", &system.SystemDependencies{}) + testInstance, err := instance.New("testInstance", system.SystemDependencies{}) require.NoError(t, err) tshark := &Tshark{ @@ -185,8 +185,7 @@ func TestTsharkCloneWithSuffix(t *testing.T) { instance: testInstance, } - clone, err := tshark.Clone() - require.NoError(t, err) + clone := tshark.CloneWithSuffix("-clone") assert.Equal(t, tshark.VolumeSize, clone.(*Tshark).VolumeSize) assert.Equal(t, tshark.S3AccessKey, clone.(*Tshark).S3AccessKey) diff --git a/pkg/system/dependencies.go b/pkg/system/dependencies.go index 87101e81..524ed521 100644 --- a/pkg/system/dependencies.go +++ b/pkg/system/dependencies.go @@ -1,8 +1,6 @@ package system import ( - "sync" - "github.com/sirupsen/logrus" "github.com/celestiaorg/knuu/pkg/builder" @@ -19,14 +17,4 @@ type SystemDependencies struct { Proxy *traefik.Traefik Scope string StartTime string - instancesMap sync.Map -} - -func (s *SystemDependencies) AddInstanceName(name string) { - s.instancesMap.Store(name, struct{}{}) -} - -func (s *SystemDependencies) HasInstanceName(name string) bool { - _, exists := s.instancesMap.Load(name) - return exists }