Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of deployment-strategy in the deployment definition #248

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions pkg/koyeb/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +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")

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 @@ -387,6 +391,8 @@ func (h *ServiceHandler) addServiceDefinitionFlagsForAllSources(flags *pflag.Fla
"health-checks-graee": "checks-grace-period",
"health-checks-grace-period": "checks-grace-period",

"strategy": "deployment-strategy",

"route": "routes",
"volume": "volumes",
"region": "regions",
Expand Down Expand Up @@ -466,6 +472,12 @@ func (h *ServiceHandler) parseServiceDefinitionFlags(ctx *CLIContext, flags *pfl
}
definition.SetType(type_)

strategy, err := h.parseDeploymentStrategy(flags, definition.GetStrategy())
if err != nil {
return err
}
definition.SetStrategy(strategy)

skipCache, _ := flags.GetBool("skip-cache")
definition.SetSkipCache(skipCache)

Expand Down Expand Up @@ -582,6 +594,18 @@ func (h *ServiceHandler) parseInstanceType(flags *pflag.FlagSet, currentInstance
return []koyeb.DeploymentInstanceType{*ret}
}

// Parse --deployment-strategy
func (h *ServiceHandler) parseDeploymentStrategy(flags *pflag.FlagSet, currentStrategy koyeb.DeploymentStrategy) (koyeb.DeploymentStrategy, error) {
if !flags.Lookup("deployment-strategy").Changed {
return currentStrategy, 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
// It gets the arguments given from the command line for the given flag, then
// builds a list of flags_list.Flag entries, and update the service
Expand Down Expand Up @@ -1700,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"
}
Loading