Skip to content

Commit

Permalink
Revert "chore!: refactor to use unique static names for instances"
Browse files Browse the repository at this point in the history
This reverts commit c11c804.
  • Loading branch information
mojtaba-esk committed Aug 29, 2024
1 parent a581de0 commit 938563d
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 190 deletions.
2 changes: 1 addition & 1 deletion pkg/builder/kaniko/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
)

type Kaniko struct {
*system.SystemDependencies
system.SystemDependencies
}

var _ builder.Builder = &Kaniko{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/kaniko/kaniko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/instance/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/instance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
84 changes: 42 additions & 42 deletions pkg/instance/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")}
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -210,24 +210,24 @@ 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
}

// 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(),
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
}
Expand All @@ -344,22 +344,22 @@ 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
}

// 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)
}

Expand All @@ -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)
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -420,17 +420,17 @@ 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,
}

return k8s.ReplicaSetConfig{
Namespace: e.instance.K8sClient.Namespace(),
Name: e.instance.name,
Name: e.instance.k8sName,
Labels: e.Labels(),
Replicas: 1,
PodConfig: podConfig,
Expand Down
Loading

0 comments on commit 938563d

Please sign in to comment.