Skip to content

Commit

Permalink
chore!: adjust stopped status actions for the update image and others
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Sep 13, 2024
1 parent 60ca33c commit 3e17222
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 66 deletions.
32 changes: 4 additions & 28 deletions pkg/instance/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"path/filepath"
"sync"
"time"

"github.com/google/uuid"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -47,7 +46,7 @@ func (b *build) SetImagePullPolicy(pullPolicy v1.PullPolicy) {
// SetImage sets the image of the instance.
// It is only allowed in the 'None' and 'Preparing' states.
func (b *build) SetImage(ctx context.Context, image string) error {
if !b.instance.IsInState(StateNone, StatePreparing) {
if !b.instance.IsInState(StateNone, StatePreparing, StateStopped) {
if b.instance.sidecars.IsSidecar() {
return ErrSettingImageNotAllowedForSidecarsStarted
}
Expand Down Expand Up @@ -95,7 +94,7 @@ func (b *build) SetGitRepo(ctx context.Context, gitContext builder.GitContext) e
// SetStartCommand sets the command to run in the instance
// This function can only be called when the instance is in state 'Preparing' or 'Committed'
func (b *build) SetStartCommand(command ...string) error {
if !b.instance.IsInState(StatePreparing, StateCommitted) {
if !b.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrSettingCommand.WithParams(b.instance.state.String())
}
b.command = command
Expand All @@ -105,7 +104,7 @@ func (b *build) SetStartCommand(command ...string) error {
// SetArgs sets the arguments passed to the instance
// This function can only be called in the states 'Preparing' or 'Committed'
func (b *build) SetArgs(args ...string) error {
if !b.instance.IsInState(StatePreparing, StateCommitted) {
if !b.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrSettingArgsNotAllowed.WithParams(b.instance.state.String())
}
b.args = args
Expand Down Expand Up @@ -228,7 +227,7 @@ func (b *build) addFileToBuilder(src, dest, chown string) {
// SetEnvironmentVariable sets the given environment variable in the instance
// This function can only be called in the states 'Preparing' and 'Committed'
func (b *build) SetEnvironmentVariable(key, value string) error {
if !b.instance.IsInState(StatePreparing, StateCommitted) {
if !b.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrSettingEnvNotAllowed.WithParams(b.instance.state.String())
}
b.instance.Logger.WithFields(logrus.Fields{
Expand All @@ -245,29 +244,6 @@ func (b *build) SetEnvironmentVariable(key, value string) error {
return nil
}

// setImageWithGracePeriod sets the image of the instance with a grace period
func (b *build) setImageWithGracePeriod(ctx context.Context, imageName string, gracePeriod time.Duration) error {
b.imageName = imageName

var gracePeriodInSecondsPtr *int64
if gracePeriod != 0 {
gpInSeconds := int64(gracePeriod.Seconds())
gracePeriodInSecondsPtr = &gpInSeconds
}
_, err := b.instance.K8sClient.ReplaceReplicaSetWithGracePeriod(ctx, b.instance.execution.prepareReplicaSetConfig(), gracePeriodInSecondsPtr)
if err != nil {
return ErrReplacingPod.Wrap(err)
}

if err := b.instance.execution.WaitInstanceIsRunning(ctx); err != nil {
return ErrWaitingInstanceIsRunning.Wrap(err)
}

return nil
}

// imageCache maps image hash values to image names

// checkImageHashInCache checks if the given image hash exists in the cache.
func (b *build) checkImageHashInCache(imageHash string) (string, bool) {
value, exists := b.imageCache.Load(imageHash)
Expand Down
1 change: 1 addition & 0 deletions pkg/instance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,5 @@ 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'")
ErrAddingHostToProxyNotAllowed = errors.New("AddingHostToProxyNotAllowed", "adding host to proxy is only allowed in state 'Started'. Current state is '%s'")
)
16 changes: 4 additions & 12 deletions pkg/instance/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func (e *execution) Stop(ctx context.Context) error {
return nil
}

func (b *execution) SetImage(ctx context.Context, image string) error {
return b.instance.build.SetImage(ctx, image)
}

// Labels returns the labels for the instance
func (e *execution) Labels() map[string]string {
return map[string]string{
Expand Down Expand Up @@ -264,18 +268,6 @@ func (e *execution) Destroy(ctx context.Context) error {
return nil
}

func (e *execution) UpgradeImage(ctx context.Context, image string) error {
return e.UpgradeImageWithGracePeriod(ctx, image, 0)
}

func (e *execution) UpgradeImageWithGracePeriod(ctx context.Context, image string, gracePeriod time.Duration) error {
if !e.instance.IsInState(StateStarted) {
return ErrUpgradingImageNotAllowed.WithParams(e.instance.state.String())
}

return e.instance.build.setImageWithGracePeriod(ctx, image, gracePeriod)
}

// BatchDestroy destroys a list of instances.
func BatchDestroy(ctx context.Context, instances ...*Instance) error {
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/instance/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (m *monitoring) SetStartupProbe(startupProbe *v1.Probe) error {

// checkStateForProbe checks if the current state is allowed for setting a probe
func (m *monitoring) checkStateForProbe() error {
if !m.instance.IsInState(StatePreparing, StateCommitted) {
if !m.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrSettingProbeNotAllowed.WithParams(m.instance.state.String())
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/instance/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func (i *Instance) Network() *network {
}

// AddPortTCP adds a TCP port to the instance
// This function can be called in the states 'Preparing' and 'Committed'
// This function can be called in the states 'Preparing', 'Committed' and 'Stopped'
func (n *network) AddPortTCP(port int) error {
if !n.instance.IsInState(StatePreparing, StateCommitted) {
if !n.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingPortNotAllowed.WithParams(n.instance.state.String())
}

Expand Down Expand Up @@ -97,9 +97,9 @@ func (n *network) PortForwardTCP(ctx context.Context, port int) (int, error) {
}

// AddPortUDP adds a UDP port to the instance
// This function can be called in the states 'Preparing' and 'Committed'
// This function can be called in the states 'Preparing', 'Committed' and 'Stopped'
func (n *network) AddPortUDP(port int) error {
if !n.instance.IsInState(StatePreparing, StateCommitted) {
if !n.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingPortNotAllowed.WithParams(n.instance.state.String())
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/instance/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const (
)

func (n *network) AddHost(ctx context.Context, port int) (host string, err error) {
if n.instance.state != StateStarted {
return "", ErrAddingHostToProxyNotAllowed.WithParams(n.instance.state.String())
}

if n.instance.Proxy == nil {
return "", ErrProxyNotInitialized
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/instance/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func (i *Instance) Resources() *resources {
}

// SetMemory sets the memory of the instance
// This function can only be called in the states 'Preparing' and 'Committed'
// This function can only be called in the states 'Preparing', 'Committed' and 'Stopped'
func (r *resources) SetMemory(request, limit resource.Quantity) error {
if !r.instance.IsInState(StatePreparing, StateCommitted) {
if !r.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrSettingMemoryNotAllowed.WithParams(r.instance.state.String())
}
r.memoryRequest = request
Expand All @@ -36,9 +36,9 @@ func (r *resources) SetMemory(request, limit resource.Quantity) error {
}

// SetCPU sets the CPU of the instance
// This function can only be called in the states 'Preparing' and 'Committed'
// This function can only be called in the states 'Preparing', 'Committed' and 'Stopped'
func (r *resources) SetCPU(request resource.Quantity) error {
if !r.instance.IsInState(StatePreparing, StateCommitted) {
if !r.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrSettingCPUNotAllowed.WithParams(r.instance.state.String())
}
r.cpuRequest = request
Expand Down
16 changes: 8 additions & 8 deletions pkg/instance/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ func (i *Instance) Security() *security {
}

// AddPolicyRule adds a policy rule to the instance
// This function can only be called in the states 'Preparing' and 'Committed'
// This function can only be called in the states 'Preparing', 'Committed' and 'Stopped'
func (s *security) AddPolicyRule(rule rbacv1.PolicyRule) error {
if !s.instance.IsInState(StatePreparing, StateCommitted) {
if !s.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingPolicyRuleNotAllowed.WithParams(s.instance.state.String())
}
s.policyRules = append(s.policyRules, rule)
return nil
}

// SetPrivileged sets the privileged status for the instance
// This function can only be called in the state 'Preparing' or 'Committed'
// This function can only be called in the state 'Preparing', 'Committed' or 'Stopped'
func (s *security) SetPrivileged(privileged bool) error {
if !s.instance.IsInState(StatePreparing, StateCommitted) {
if !s.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrSettingPrivilegedNotAllowed.WithParams(s.instance.state.String())
}
s.privileged = privileged
Expand All @@ -51,9 +51,9 @@ func (s *security) SetPrivileged(privileged bool) error {
}

// AddKubernetesCapability adds a Kubernetes capability to the instance
// This function can only be called in the state 'Preparing' or 'Committed'
// This function can only be called in the state 'Preparing', 'Committed' or 'Stopped'
func (s *security) AddKubernetesCapability(capability string) error {
if !s.instance.IsInState(StatePreparing, StateCommitted) {
if !s.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingCapabilityNotAllowed.WithParams(s.instance.state.String())
}
s.capabilitiesAdd = append(s.capabilitiesAdd, capability)
Expand All @@ -65,9 +65,9 @@ func (s *security) AddKubernetesCapability(capability string) error {
}

// AddKubernetesCapabilities adds multiple Kubernetes capabilities to the instance
// This function can only be called in the state 'Preparing' or 'Committed'
// This function can only be called in the state 'Preparing', 'Committed' or 'Stopped'
func (s *security) AddKubernetesCapabilities(capabilities []string) error {
if !s.instance.IsInState(StatePreparing, StateCommitted) {
if !s.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingCapabilitiesNotAllowed.WithParams(s.instance.state.String())
}
s.capabilitiesAdd = append(s.capabilitiesAdd, capabilities...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/instance/sidecars.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func (s *sidecars) IsSidecar() bool {
}

// Add adds a sidecar to the instance
// This function can only be called in the state 'Preparing' or 'Committed'
// This function can only be called in the state 'Preparing', 'Committed' or 'Stopped'
func (s *sidecars) Add(ctx context.Context, sc SidecarManager) error {
if sc == nil {
return ErrSidecarIsNil
}
if !s.instance.IsInState(StatePreparing, StateCommitted) {
if !s.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingSidecarNotAllowed.WithParams(s.instance.state.String())
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/instance/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *storage) AddFile(src string, dest string, chown string) error {
case StatePreparing:
s.instance.build.addFileToBuilder(src, dest, chown)
return nil
case StateCommitted:
case StateCommitted, StateStopped:
return s.addFileToInstance(dstPath, dest, chown)
}

Expand All @@ -64,9 +64,9 @@ func (s *storage) AddFile(src string, dest string, chown string) error {
}

// AddFolder adds a folder to the instance
// This function can only be called in the state 'Preparing' or 'Committed'
// This function can only be called in the state 'Preparing', 'Committed' or 'Stopped'
func (s *storage) AddFolder(src string, dest string, chown string) error {
if !s.instance.IsInState(StatePreparing, StateCommitted) {
if !s.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingFolderNotAllowed.WithParams(s.instance.state.String())
}

Expand Down Expand Up @@ -141,7 +141,7 @@ func (s *storage) AddFileBytes(bytes []byte, dest string, chown string) error {

// AddVolume adds a volume to the instance
// The owner of the volume is set to 0, if you want to set a custom owner use AddVolumeWithOwner
// This function can only be called in the states 'Preparing' and 'Committed'
// This function can only be called in the states 'Preparing', 'Committed' and 'Stopped'
func (s *storage) AddVolume(path string, size resource.Quantity) error {
// temporary feat, we will remove it once we can add multiple volumes
if len(s.volumes) > 0 {
Expand All @@ -155,9 +155,9 @@ func (s *storage) AddVolume(path string, size resource.Quantity) error {
}

// AddVolumeWithOwner adds a volume to the instance with the given owner
// This function can only be called in the states 'Preparing' and 'Committed'
// This function can only be called in the states 'Preparing', 'Committed' and 'Stopped'
func (s *storage) AddVolumeWithOwner(path string, size resource.Quantity, owner int64) error {
if !s.instance.IsInState(StatePreparing, StateCommitted) {
if !s.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingVolumeNotAllowed.WithParams(s.instance.state.String())
}
// temporary feat, we will remove it once we can add multiple volumes
Expand Down Expand Up @@ -295,7 +295,7 @@ func (s *storage) addFileToInstance(dstPath, dest, chown string) error {

// checkStateForAddingFile checks if the current state allows adding a file
func (s *storage) checkStateForAddingFile() error {
if !s.instance.IsInState(StatePreparing, StateCommitted) {
if !s.instance.IsInState(StatePreparing, StateCommitted, StateStopped) {
return ErrAddingFileNotAllowed.WithParams(s.instance.state.String())
}
return nil
Expand Down

0 comments on commit 3e17222

Please sign in to comment.