Skip to content

Commit ebc4056

Browse files
committed
--deployment-strategy: add custom tyhpe
1 parent 66f07b4 commit ebc4056

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

CHANGES.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## v5.0.1 (unreleased)
1+
## v5.1.0 (unreleased)
22

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

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

docs/reference.md

+4
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ See examples of koyeb service create --help
283283
--checks-grace-period strings Set healthcheck grace period in seconds.
284284
Use the format <healthcheck>=<seconds>, for example --checks-grace-period 8080=10
285285
286+
--deployment-strategy STRATEGY Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".
286287
--docker string Docker image
287288
--docker-args strings Set arguments to the docker command. To provide multiple arguments, use the --docker-args flag multiple times.
288289
--docker-command string Set the docker CMD explicitly. To provide arguments to the command, use the --docker-args flag.
@@ -574,6 +575,7 @@ koyeb deploy <path> <app>/<service> [flags]
574575
--checks-grace-period strings Set healthcheck grace period in seconds.
575576
Use the format <healthcheck>=<seconds>, for example --checks-grace-period 8080=10
576577
578+
--deployment-strategy STRATEGY Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".
577579
--env strings Update service environment variables using the format KEY=VALUE, for example --env FOO=bar
578580
To use the value of a secret as an environment variable, specify the secret name preceded by @, for example --env FOO=@bar
579581
To delete an environment variable, prefix its name with '!', for example --env '!FOO'
@@ -1373,6 +1375,7 @@ $> koyeb service create myservice --app myapp --docker nginx --port 80:tcp
13731375
--checks-grace-period strings Set healthcheck grace period in seconds.
13741376
Use the format <healthcheck>=<seconds>, for example --checks-grace-period 8080=10
13751377
1378+
--deployment-strategy STRATEGY Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".
13761379
--docker string Docker image
13771380
--docker-args strings Set arguments to the docker command. To provide multiple arguments, use the --docker-args flag multiple times.
13781381
--docker-command string Set the docker CMD explicitly. To provide arguments to the command, use the --docker-args flag.
@@ -1792,6 +1795,7 @@ $> koyeb service update myapp/myservice --port 80:tcp --route '!/'
17921795
--checks-grace-period strings Set healthcheck grace period in seconds.
17931796
Use the format <healthcheck>=<seconds>, for example --checks-grace-period 8080=10
17941797
1798+
--deployment-strategy STRATEGY Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".
17951799
--docker string Docker image
17961800
--docker-args strings Set arguments to the docker command. To provide multiple arguments, use the --docker-args flag multiple times.
17971801
--docker-command string Set the docker CMD explicitly. To provide arguments to the command, use the --docker-args flag.

pkg/koyeb/services.go

+36-38
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,10 @@ func (h *ServiceHandler) addServiceDefinitionFlagsForAllSources(flags *pflag.Fla
318318
"To delete an environment variable, prefix its name with '!', for example --env '!FOO'\n",
319319
)
320320
flags.String("instance-type", "nano", "Instance type")
321-
flags.String("deployment-strategy", "rolling", "Deployment strategy")
321+
322+
var strategy DeploymentStrategy
323+
flags.Var(&strategy, "deployment-strategy", `Deployment strategy, either "rolling" (default), "canary", "blue-green" or "immediate".`)
324+
322325
flags.Int64("scale", 1, "Set both min-scale and max-scale")
323326
flags.Int64("min-scale", 1, "Min scale")
324327
flags.Int64("max-scale", 1, "Max scale")
@@ -594,45 +597,13 @@ func (h *ServiceHandler) parseInstanceType(flags *pflag.FlagSet, currentInstance
594597
// Parse --deployment-strategy
595598
func (h *ServiceHandler) parseDeploymentStrategy(flags *pflag.FlagSet, currentStrategy koyeb.DeploymentStrategy) (koyeb.DeploymentStrategy, error) {
596599
if !flags.Lookup("deployment-strategy").Changed {
597-
// New service: return the default value
598-
if currentStrategy.Type == nil {
599-
value := flags.Lookup("deployment-strategy").DefValue
600-
enum := fmt.Sprintf("DEPLOYMENT_STRATEGY_TYPE_%s", strings.ToUpper(strings.ReplaceAll(value, "-", "_")))
601-
kind, err := koyeb.NewDeploymentStrategyTypeFromValue(enum)
602-
if err != nil {
603-
panic(err)
604-
}
605-
ret := koyeb.DeploymentStrategy{
606-
Type: kind,
607-
}
608-
return ret, nil
609-
}
610-
// Existing service: return the strategy currently configured
611600
return currentStrategy, nil
612601
}
613-
614-
value, _ := flags.GetString("deployment-strategy")
615-
enum := fmt.Sprintf("DEPLOYMENT_STRATEGY_TYPE_%s", strings.ToUpper(strings.ReplaceAll(value, "-", "_")))
616-
kind, err := koyeb.NewDeploymentStrategyTypeFromValue(enum)
617-
if err != nil {
618-
return koyeb.DeploymentStrategy{}, &errors.CLIError{
619-
What: "Error while updating the service",
620-
Why: "the --deployment-strategy flag is not valid",
621-
Additional: []string{
622-
"The --deployment-strategy flag must be one of the following:",
623-
" - rolling",
624-
" - canary",
625-
" - blue-green",
626-
" - immediate",
627-
},
628-
Orig: nil,
629-
Solution: "Fix the --deployment-strategy flag and try again",
630-
}
631-
}
632-
ret := koyeb.DeploymentStrategy{
633-
Type: kind,
634-
}
635-
return ret, nil
602+
flagValue := flags.Lookup("deployment-strategy").Value.(*DeploymentStrategy)
603+
strategy := koyeb.DeploymentStrategyType(*flagValue)
604+
return koyeb.DeploymentStrategy{
605+
Type: &strategy,
606+
}, nil
636607
}
637608

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

17541725
return parseListFlags("volumes", flags_list.GetNewVolumeListFromFlags(wrappedResolveVolumeId), flags, currentVolumes)
17551726
}
1727+
1728+
// DeploymentStrategy is a type alias for koyeb.DeploymentStrategyType which implements the pflag.Value interface.
1729+
type DeploymentStrategy koyeb.DeploymentStrategyType
1730+
1731+
func (s *DeploymentStrategy) String() string {
1732+
return string(*s)
1733+
}
1734+
1735+
func (s *DeploymentStrategy) Set(value string) error {
1736+
switch value {
1737+
case "rolling":
1738+
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_ROLLING)
1739+
case "canary":
1740+
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_CANARY)
1741+
case "blue-green":
1742+
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_BLUE_GREEN)
1743+
case "immediate":
1744+
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_IMMEDIATE)
1745+
default:
1746+
return fmt.Errorf("invalid deployment strategy: %s. Valid values are: rolling, canary, blue-green, immediate.", value)
1747+
}
1748+
return nil
1749+
}
1750+
1751+
func (s *DeploymentStrategy) Type() string {
1752+
return "STRATEGY"
1753+
}

0 commit comments

Comments
 (0)