Support deploy update_config (#1232)

This commit is contained in:
Hang Yan
2020-01-05 19:30:41 +08:00
committed by GitHub
parent 4d864a9aab
commit e7f05588bf
17 changed files with 602 additions and 538 deletions
+79 -9
View File
@@ -19,9 +19,14 @@ package kobject
import (
dockerCliTypes "github.com/docker/cli/cli/compose/types"
"github.com/docker/libcompose/yaml"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/pkg/errors"
"github.com/spf13/cast"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/util/intstr"
"path/filepath"
"time"
)
// KomposeObject holds the generic struct of Kompose transformation
@@ -114,15 +119,16 @@ type ServiceConfig struct {
MemReservation yaml.MemStringorInt `compose:""`
DeployMode string `compose:""`
// DeployLabels mapping to kubernetes labels
DeployLabels map[string]string `compose:""`
TmpFs []string `compose:"tmpfs"`
Dockerfile string `compose:"dockerfile"`
Replicas int `compose:"replicas"`
GroupAdd []int64 `compose:"group_add"`
Volumes []Volumes `compose:""`
Secrets []dockerCliTypes.ServiceSecretConfig
HealthChecks HealthCheck `compose:""`
Placement map[string]string `compose:""`
DeployLabels map[string]string `compose:""`
DeployUpdateConfig dockerCliTypes.UpdateConfig `compose:""`
TmpFs []string `compose:"tmpfs"`
Dockerfile string `compose:"dockerfile"`
Replicas int `compose:"replicas"`
GroupAdd []int64 `compose:"group_add"`
Volumes []Volumes `compose:""`
Secrets []dockerCliTypes.ServiceSecretConfig
HealthChecks HealthCheck `compose:""`
Placement map[string]string `compose:""`
//This is for long LONG SYNTAX link(https://docs.docker.com/compose/compose-file/#long-syntax)
Configs []dockerCliTypes.ServiceConfigObjConfig `compose:""`
//This is for SHORT SYNTAX link(https://docs.docker.com/compose/compose-file/#configs)
@@ -188,3 +194,67 @@ func (s *ServiceConfig) GetConfigMapKeyFromMeta(name string) (string, error) {
return filepath.Base(config.File), nil
}
// GetUpdateStrategy from compose update_config
// 1. only apply to Deployment, but the check is not happened here
// 2. only support `parallelism` and `order`
// return nil if not support
func (s *ServiceConfig) GetKubernetesUpdateStrategy() *extensions.RollingUpdateDeployment {
config := s.DeployUpdateConfig
r := extensions.RollingUpdateDeployment{}
if config.Order == "stop-first" {
if config.Parallelism != nil {
r.MaxUnavailable = intstr.FromInt(cast.ToInt(*config.Parallelism))
}
r.MaxSurge = intstr.FromInt(0)
return &r
}
if config.Order == "start-first" {
if config.Parallelism != nil {
r.MaxSurge = intstr.FromInt(cast.ToInt(*config.Parallelism))
}
r.MaxUnavailable = intstr.FromInt(0)
return &r
}
return nil
}
func (s *ServiceConfig) GetOCUpdateStrategy() *deployapi.RollingDeploymentStrategyParams {
config := s.DeployUpdateConfig
r := deployapi.RollingDeploymentStrategyParams{}
delay := time.Second * 1
if config.Delay != 0 {
delay = config.Delay
}
interval := cast.ToInt64(delay.Seconds())
if config.Order == "stop-first" {
if config.Parallelism != nil {
r.MaxUnavailable = intstr.FromInt(cast.ToInt(*config.Parallelism))
}
r.MaxSurge = intstr.FromInt(0)
r.UpdatePeriodSeconds = &interval
return &r
}
if config.Order == "start-first" {
if config.Parallelism != nil {
r.MaxSurge = intstr.FromInt(cast.ToInt(*config.Parallelism))
}
r.MaxUnavailable = intstr.FromInt(0)
r.UpdatePeriodSeconds = &interval
return &r
}
if cast.ToInt64(config.Delay) != 0 {
r.UpdatePeriodSeconds = &interval
return &r
}
return nil
}
+4
View File
@@ -351,6 +351,10 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
// placement:
serviceConfig.Placement = loadV3Placement(composeServiceConfig.Deploy.Placement.Constraints)
if composeServiceConfig.Deploy.UpdateConfig != nil {
serviceConfig.DeployUpdateConfig = *composeServiceConfig.Deploy.UpdateConfig
}
// TODO: Build is not yet supported, see:
// https://github.com/docker/cli/blob/master/cli/compose/types/types.go#L9
// We will have to *manually* add this / parse.
+9
View File
@@ -411,6 +411,15 @@ func (k *Kubernetes) InitD(name string, service kobject.ServiceConfig, replicas
}
dc.Spec.Template.Labels = transformer.ConfigLabels(name)
update := service.GetKubernetesUpdateStrategy()
if update != nil {
dc.Spec.Strategy = extensions.DeploymentStrategy{
Type: extensions.RollingUpdateDeploymentStrategyType,
RollingUpdate: update,
}
log.Debugf("Set deployment '%s' rolling update: MaxSurge: %s, MaxUnavailable: %s", name, update.MaxSurge.String(), update.MaxUnavailable.String())
}
return dc
}
+10
View File
@@ -232,6 +232,16 @@ func (o *OpenShift) initDeploymentConfig(name string, service kobject.ServiceCon
},
},
}
update := service.GetOCUpdateStrategy()
if update != nil {
dc.Spec.Strategy = deployapi.DeploymentStrategy{
Type: deployapi.DeploymentStrategyTypeRolling,
RollingParams: update,
}
log.Debugf("Set deployment '%s' rolling update: MaxSurge: %s, MaxUnavailable: %s", name, update.MaxSurge.String(), update.MaxUnavailable.String())
}
return dc
}