Skip to content

Commit

Permalink
--deployment-strategy: add custom tyhpe
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Sep 2, 2024
1 parent 0cde4d0 commit 07cfbcc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
5 changes: 3 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v5.0.1 (unreleased)
## v5.1.0 (unreleased)

* ...
* Add `--deployment-strategy` which can have the value `rolling`, `canary`, `blue-green` or `immediate` to service.
- https://github.com/koyeb/koyeb-cli/pull/248/files

## v5.0.0 (2024-08-20)

Expand Down
4 changes: 4 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ See examples of koyeb service create --help
--checks-grace-period strings Set healthcheck grace period in seconds.
Use the format <healthcheck>=<seconds>, for example --checks-grace-period 8080=10
--deployment-strategy STRATEGY Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".
--docker string Docker image
--docker-args strings Set arguments to the docker command. To provide multiple arguments, use the --docker-args flag multiple times.
--docker-command string Set the docker CMD explicitly. To provide arguments to the command, use the --docker-args flag.
Expand Down Expand Up @@ -574,6 +575,7 @@ koyeb deploy <path> <app>/<service> [flags]
--checks-grace-period strings Set healthcheck grace period in seconds.
Use the format <healthcheck>=<seconds>, for example --checks-grace-period 8080=10
--deployment-strategy STRATEGY Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".
--env strings Update service environment variables using the format KEY=VALUE, for example --env FOO=bar
To use the value of a secret as an environment variable, specify the secret name preceded by @, for example --env FOO=@bar
To delete an environment variable, prefix its name with '!', for example --env '!FOO'
Expand Down Expand Up @@ -1373,6 +1375,7 @@ $> koyeb service create myservice --app myapp --docker nginx --port 80:tcp
--checks-grace-period strings Set healthcheck grace period in seconds.
Use the format <healthcheck>=<seconds>, for example --checks-grace-period 8080=10
--deployment-strategy STRATEGY Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".
--docker string Docker image
--docker-args strings Set arguments to the docker command. To provide multiple arguments, use the --docker-args flag multiple times.
--docker-command string Set the docker CMD explicitly. To provide arguments to the command, use the --docker-args flag.
Expand Down Expand Up @@ -1792,6 +1795,7 @@ $> koyeb service update myapp/myservice --port 80:tcp --route '!/'
--checks-grace-period strings Set healthcheck grace period in seconds.
Use the format <healthcheck>=<seconds>, for example --checks-grace-period 8080=10
--deployment-strategy STRATEGY Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".
--docker string Docker image
--docker-args strings Set arguments to the docker command. To provide multiple arguments, use the --docker-args flag multiple times.
--docker-command string Set the docker CMD explicitly. To provide arguments to the command, use the --docker-args flag.
Expand Down
74 changes: 36 additions & 38 deletions pkg/koyeb/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,10 @@ func (h *ServiceHandler) addServiceDefinitionFlagsForAllSources(flags *pflag.Fla
"To delete an environment variable, prefix its name with '!', for example --env '!FOO'\n",
)
flags.String("instance-type", "nano", "Instance type")
flags.String("deployment-strategy", "rolling", "Deployment strategy")

var strategy DeploymentStrategy
flags.Var(&strategy, "deployment-strategy", `Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".`)

flags.Int64("scale", 1, "Set both min-scale and max-scale")
flags.Int64("min-scale", 1, "Min scale")
flags.Int64("max-scale", 1, "Max scale")
Expand Down Expand Up @@ -594,45 +597,13 @@ func (h *ServiceHandler) parseInstanceType(flags *pflag.FlagSet, currentInstance
// Parse --deployment-strategy
func (h *ServiceHandler) parseDeploymentStrategy(flags *pflag.FlagSet, currentStrategy koyeb.DeploymentStrategy) (koyeb.DeploymentStrategy, error) {
if !flags.Lookup("deployment-strategy").Changed {
// New service: return the default value
if currentStrategy.Type == nil {
value := flags.Lookup("deployment-strategy").DefValue
enum := fmt.Sprintf("DEPLOYMENT_STRATEGY_TYPE_%s", strings.ToUpper(strings.ReplaceAll(value, "-", "_")))
kind, err := koyeb.NewDeploymentStrategyTypeFromValue(enum)
if err != nil {
panic(err)
}
ret := koyeb.DeploymentStrategy{
Type: kind,
}
return ret, nil
}
// Existing service: return the strategy currently configured
return currentStrategy, nil
}

value, _ := flags.GetString("deployment-strategy")
enum := fmt.Sprintf("DEPLOYMENT_STRATEGY_TYPE_%s", strings.ToUpper(strings.ReplaceAll(value, "-", "_")))
kind, err := koyeb.NewDeploymentStrategyTypeFromValue(enum)
if err != nil {
return koyeb.DeploymentStrategy{}, &errors.CLIError{
What: "Error while updating the service",
Why: "the --deployment-strategy flag is not valid",
Additional: []string{
"The --deployment-strategy flag must be one of the following:",
" - rolling",
" - canary",
" - blue-green",
" - immediate",
},
Orig: nil,
Solution: "Fix the --deployment-strategy flag and try again",
}
}
ret := koyeb.DeploymentStrategy{
Type: kind,
}
return ret, nil
flagValue := flags.Lookup("deployment-strategy").Value.(*DeploymentStrategy)
strategy := koyeb.DeploymentStrategyType(*flagValue)
return koyeb.DeploymentStrategy{
Type: &strategy,
}, nil
}

// parseListFlags is the generic function parsing --env, --port, --routes, --checks, --regions and --volumes
Expand Down Expand Up @@ -1753,3 +1724,30 @@ func (h *ServiceHandler) parseVolumes(ctx *CLIContext, flags *pflag.FlagSet, cur

return parseListFlags("volumes", flags_list.GetNewVolumeListFromFlags(wrappedResolveVolumeId), flags, currentVolumes)
}

// DeploymentStrategy is a type alias for koyeb.DeploymentStrategyType which implements the pflag.Value interface.
type DeploymentStrategy koyeb.DeploymentStrategyType

func (s *DeploymentStrategy) String() string {
return string(*s)
}

func (s *DeploymentStrategy) Set(value string) error {
switch value {
case "rolling":
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_ROLLING)
case "canary":
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_CANARY)
case "blue-green":
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_BLUE_GREEN)
case "immediate":
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_IMMEDIATE)
default:
return fmt.Errorf("invalid deployment strategy: %s. Valid values are: rolling, canary, blue-green, immediate.", value)
}
return nil
}

func (s *DeploymentStrategy) Type() string {
return "STRATEGY"
}

0 comments on commit 07cfbcc

Please sign in to comment.