forked from LaconicNetwork/kompose
Support deploy update_config (#1232)
This commit is contained in:
+79
-9
@@ -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
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user