forked from LaconicNetwork/kompose
Support deploy labels (#1231)
This commit is contained in:
parent
05e9bf4fc8
commit
4d864a9aab
@ -35,7 +35,7 @@ __Glossary:__
|
|||||||
| deploy: update_config | - | - | n | | |
|
| deploy: update_config | - | - | n | | |
|
||||||
| deploy: resources | - | - | ✓ | Containers.Resources.Limits.Memory / Containers.Resources.Limits.CPU | Support for memory as well as cpu |
|
| deploy: resources | - | - | ✓ | Containers.Resources.Limits.Memory / Containers.Resources.Limits.CPU | Support for memory as well as cpu |
|
||||||
| deploy: restart_policy | - | - | ✓ | Pod generation | This generated a Pod, see the [user guide on restart](http://kompose.io/user-guide/#restart) |
|
| deploy: restart_policy | - | - | ✓ | Pod generation | This generated a Pod, see the [user guide on restart](http://kompose.io/user-guide/#restart) |
|
||||||
| deploy: labels | - | - | n | | |
|
| deploy: labels | - | - | ✓ | Workload.Metadata.Labels | Only applied to workload resource | |
|
||||||
| devices | x | x | x | | Not supported within Kubernetes, See issue https://github.com/kubernetes/kubernetes/issues/5607 |
|
| devices | x | x | x | | Not supported within Kubernetes, See issue https://github.com/kubernetes/kubernetes/issues/5607 |
|
||||||
| depends_on | x | x | x | | |
|
| depends_on | x | x | x | | |
|
||||||
| dns | x | x | x | | Not used within Kubernetes. Kubernetes uses a managed DNS server |
|
| dns | x | x | x | | Not used within Kubernetes. Kubernetes uses a managed DNS server |
|
||||||
|
|||||||
@ -113,14 +113,16 @@ type ServiceConfig struct {
|
|||||||
MemLimit yaml.MemStringorInt `compose:"mem_limit"`
|
MemLimit yaml.MemStringorInt `compose:"mem_limit"`
|
||||||
MemReservation yaml.MemStringorInt `compose:""`
|
MemReservation yaml.MemStringorInt `compose:""`
|
||||||
DeployMode string `compose:""`
|
DeployMode string `compose:""`
|
||||||
TmpFs []string `compose:"tmpfs"`
|
// DeployLabels mapping to kubernetes labels
|
||||||
Dockerfile string `compose:"dockerfile"`
|
DeployLabels map[string]string `compose:""`
|
||||||
Replicas int `compose:"replicas"`
|
TmpFs []string `compose:"tmpfs"`
|
||||||
GroupAdd []int64 `compose:"group_add"`
|
Dockerfile string `compose:"dockerfile"`
|
||||||
Volumes []Volumes `compose:""`
|
Replicas int `compose:"replicas"`
|
||||||
Secrets []dockerCliTypes.ServiceSecretConfig
|
GroupAdd []int64 `compose:"group_add"`
|
||||||
HealthChecks HealthCheck `compose:""`
|
Volumes []Volumes `compose:""`
|
||||||
Placement map[string]string `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)
|
//This is for long LONG SYNTAX link(https://docs.docker.com/compose/compose-file/#long-syntax)
|
||||||
Configs []dockerCliTypes.ServiceConfigObjConfig `compose:""`
|
Configs []dockerCliTypes.ServiceConfigObjConfig `compose:""`
|
||||||
//This is for SHORT SYNTAX link(https://docs.docker.com/compose/compose-file/#configs)
|
//This is for SHORT SYNTAX link(https://docs.docker.com/compose/compose-file/#configs)
|
||||||
|
|||||||
@ -311,30 +311,17 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
|||||||
serviceConfig.DomainName = composeServiceConfig.DomainName
|
serviceConfig.DomainName = composeServiceConfig.DomainName
|
||||||
serviceConfig.Secrets = composeServiceConfig.Secrets
|
serviceConfig.Secrets = composeServiceConfig.Secrets
|
||||||
|
|
||||||
//Adding network key related info
|
parseV3Network(&composeServiceConfig, &serviceConfig, composeObject)
|
||||||
if len(composeServiceConfig.Networks) == 0 {
|
|
||||||
if defaultNetwork, ok := composeObject.Networks["default"]; ok {
|
|
||||||
serviceConfig.Network = append(serviceConfig.Network, defaultNetwork.Name)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
var alias = ""
|
|
||||||
for key := range composeServiceConfig.Networks {
|
|
||||||
alias = key
|
|
||||||
netName := composeObject.Networks[alias].Name
|
|
||||||
// if Network Name Field is empty in the docker-compose definition
|
|
||||||
// we will use the alias name defined in service config file
|
|
||||||
if netName == "" {
|
|
||||||
netName = alias
|
|
||||||
}
|
|
||||||
serviceConfig.Network = append(serviceConfig.Network, netName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// Deploy keys
|
|
||||||
//
|
|
||||||
|
|
||||||
|
if err := parseV3Resources(&composeServiceConfig, &serviceConfig); err != nil {
|
||||||
|
return kobject.KomposeObject{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deploy keys
|
||||||
// mode:
|
// mode:
|
||||||
serviceConfig.DeployMode = composeServiceConfig.Deploy.Mode
|
serviceConfig.DeployMode = composeServiceConfig.Deploy.Mode
|
||||||
|
// labels
|
||||||
|
serviceConfig.DeployLabels = composeServiceConfig.Deploy.Labels
|
||||||
|
|
||||||
// HealthCheck
|
// HealthCheck
|
||||||
if composeServiceConfig.HealthCheck != nil && !composeServiceConfig.HealthCheck.Disable {
|
if composeServiceConfig.HealthCheck != nil && !composeServiceConfig.HealthCheck.Disable {
|
||||||
@ -345,40 +332,6 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (composeServiceConfig.Deploy.Resources != types.Resources{}) {
|
|
||||||
|
|
||||||
// memory:
|
|
||||||
// TODO: Refactor yaml.MemStringorInt in kobject.go to int64
|
|
||||||
// cpu:
|
|
||||||
// convert to k8s format, for example: 0.5 = 500m
|
|
||||||
// See: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
|
|
||||||
// "The expression 0.1 is equivalent to the expression 100m, which can be read as “one hundred millicpu”."
|
|
||||||
|
|
||||||
// Since Deploy.Resources.Limits does not initialize, we must check type Resources before continuing
|
|
||||||
if composeServiceConfig.Deploy.Resources.Limits != nil {
|
|
||||||
serviceConfig.MemLimit = libcomposeyaml.MemStringorInt(composeServiceConfig.Deploy.Resources.Limits.MemoryBytes)
|
|
||||||
|
|
||||||
if composeServiceConfig.Deploy.Resources.Limits.NanoCPUs != "" {
|
|
||||||
cpuLimit, err := strconv.ParseFloat(composeServiceConfig.Deploy.Resources.Limits.NanoCPUs, 64)
|
|
||||||
if err != nil {
|
|
||||||
return kobject.KomposeObject{}, errors.Wrap(err, "Unable to convert cpu limits resources value")
|
|
||||||
}
|
|
||||||
serviceConfig.CPULimit = int64(cpuLimit * 1000)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if composeServiceConfig.Deploy.Resources.Reservations != nil {
|
|
||||||
serviceConfig.MemReservation = libcomposeyaml.MemStringorInt(composeServiceConfig.Deploy.Resources.Reservations.MemoryBytes)
|
|
||||||
|
|
||||||
if composeServiceConfig.Deploy.Resources.Reservations.NanoCPUs != "" {
|
|
||||||
cpuReservation, err := strconv.ParseFloat(composeServiceConfig.Deploy.Resources.Reservations.NanoCPUs, 64)
|
|
||||||
if err != nil {
|
|
||||||
return kobject.KomposeObject{}, errors.Wrap(err, "Unable to convert cpu limits reservation value")
|
|
||||||
}
|
|
||||||
serviceConfig.CPUReservation = int64(cpuReservation * 1000)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// restart-policy: deploy.restart_policy.condition will rewrite restart option
|
// restart-policy: deploy.restart_policy.condition will rewrite restart option
|
||||||
// see: https://docs.docker.com/compose/compose-file/#restart_policy
|
// see: https://docs.docker.com/compose/compose-file/#restart_policy
|
||||||
serviceConfig.Restart = composeServiceConfig.Restart
|
serviceConfig.Restart = composeServiceConfig.Restart
|
||||||
@ -447,6 +400,64 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
|||||||
return komposeObject, nil
|
return komposeObject, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseV3Network(composeServiceConfig *types.ServiceConfig, serviceConfig *kobject.ServiceConfig, composeObject *types.Config) {
|
||||||
|
if len(composeServiceConfig.Networks) == 0 {
|
||||||
|
if defaultNetwork, ok := composeObject.Networks["default"]; ok {
|
||||||
|
serviceConfig.Network = append(serviceConfig.Network, defaultNetwork.Name)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var alias = ""
|
||||||
|
for key := range composeServiceConfig.Networks {
|
||||||
|
alias = key
|
||||||
|
netName := composeObject.Networks[alias].Name
|
||||||
|
// if Network Name Field is empty in the docker-compose definition
|
||||||
|
// we will use the alias name defined in service config file
|
||||||
|
if netName == "" {
|
||||||
|
netName = alias
|
||||||
|
}
|
||||||
|
serviceConfig.Network = append(serviceConfig.Network, netName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseV3Resources(composeServiceConfig *types.ServiceConfig, serviceConfig *kobject.ServiceConfig) error {
|
||||||
|
if (composeServiceConfig.Deploy.Resources != types.Resources{}) {
|
||||||
|
|
||||||
|
// memory:
|
||||||
|
// TODO: Refactor yaml.MemStringorInt in kobject.go to int64
|
||||||
|
// cpu:
|
||||||
|
// convert to k8s format, for example: 0.5 = 500m
|
||||||
|
// See: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
|
||||||
|
// "The expression 0.1 is equivalent to the expression 100m, which can be read as “one hundred millicpu”."
|
||||||
|
|
||||||
|
// Since Deploy.Resources.Limits does not initialize, we must check type Resources before continuing
|
||||||
|
if composeServiceConfig.Deploy.Resources.Limits != nil {
|
||||||
|
serviceConfig.MemLimit = libcomposeyaml.MemStringorInt(composeServiceConfig.Deploy.Resources.Limits.MemoryBytes)
|
||||||
|
|
||||||
|
if composeServiceConfig.Deploy.Resources.Limits.NanoCPUs != "" {
|
||||||
|
cpuLimit, err := strconv.ParseFloat(composeServiceConfig.Deploy.Resources.Limits.NanoCPUs, 64)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to convert cpu limits resources value")
|
||||||
|
}
|
||||||
|
serviceConfig.CPULimit = int64(cpuLimit * 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if composeServiceConfig.Deploy.Resources.Reservations != nil {
|
||||||
|
serviceConfig.MemReservation = libcomposeyaml.MemStringorInt(composeServiceConfig.Deploy.Resources.Reservations.MemoryBytes)
|
||||||
|
|
||||||
|
if composeServiceConfig.Deploy.Resources.Reservations.NanoCPUs != "" {
|
||||||
|
cpuReservation, err := strconv.ParseFloat(composeServiceConfig.Deploy.Resources.Reservations.NanoCPUs, 64)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to convert cpu limits reservation value")
|
||||||
|
}
|
||||||
|
serviceConfig.CPUReservation = int64(cpuReservation * 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func parseV3Environment(composeServiceConfig *types.ServiceConfig, serviceConfig *kobject.ServiceConfig) {
|
func parseV3Environment(composeServiceConfig *types.ServiceConfig, serviceConfig *kobject.ServiceConfig) {
|
||||||
// Gather the environment values
|
// Gather the environment values
|
||||||
// DockerCompose uses map[string]*string while we use []string
|
// DockerCompose uses map[string]*string while we use []string
|
||||||
|
|||||||
@ -393,7 +393,7 @@ func (k *Kubernetes) InitD(name string, service kobject.ServiceConfig, replicas
|
|||||||
},
|
},
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
Name: name,
|
Name: name,
|
||||||
Labels: transformer.ConfigLabels(name),
|
Labels: transformer.ConfigAllLabels(name, &service),
|
||||||
},
|
},
|
||||||
Spec: extensions.DeploymentSpec{
|
Spec: extensions.DeploymentSpec{
|
||||||
Replicas: int32(replicas),
|
Replicas: int32(replicas),
|
||||||
@ -423,7 +423,7 @@ func (k *Kubernetes) InitDS(name string, service kobject.ServiceConfig) *extensi
|
|||||||
},
|
},
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
Name: name,
|
Name: name,
|
||||||
Labels: transformer.ConfigLabels(name),
|
Labels: transformer.ConfigAllLabels(name, &service),
|
||||||
},
|
},
|
||||||
Spec: extensions.DaemonSetSpec{
|
Spec: extensions.DaemonSetSpec{
|
||||||
Template: api.PodTemplateSpec{
|
Template: api.PodTemplateSpec{
|
||||||
|
|||||||
@ -125,6 +125,18 @@ func ConfigLabelsWithNetwork(name string, net []string) map[string]string {
|
|||||||
//return map[string]string{Selector: name, "Network": net}
|
//return map[string]string{Selector: name, "Network": net}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConfigAllLabels creates labels with service nam and deploy labels
|
||||||
|
func ConfigAllLabels(name string, service *kobject.ServiceConfig) map[string]string {
|
||||||
|
base := ConfigLabels(name)
|
||||||
|
if service.DeployLabels != nil {
|
||||||
|
for k, v := range service.DeployLabels {
|
||||||
|
base[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return base
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// ConfigAnnotations configures annotations
|
// ConfigAnnotations configures annotations
|
||||||
func ConfigAnnotations(service kobject.ServiceConfig) map[string]string {
|
func ConfigAnnotations(service kobject.ServiceConfig) map[string]string {
|
||||||
|
|
||||||
|
|||||||
@ -7,4 +7,6 @@ services:
|
|||||||
deploy:
|
deploy:
|
||||||
mode: global
|
mode: global
|
||||||
replicas: 6
|
replicas: 6
|
||||||
|
labels:
|
||||||
|
com.example.description: "This label will appear on the web service"
|
||||||
image: redis
|
image: redis
|
||||||
|
|||||||
@ -46,6 +46,7 @@
|
|||||||
},
|
},
|
||||||
"creationTimestamp": null,
|
"creationTimestamp": null,
|
||||||
"labels": {
|
"labels": {
|
||||||
|
"com.example.description": "This label will appear on the web service",
|
||||||
"io.kompose.service": "foo"
|
"io.kompose.service": "foo"
|
||||||
},
|
},
|
||||||
"name": "foo"
|
"name": "foo"
|
||||||
|
|||||||
@ -46,6 +46,7 @@
|
|||||||
},
|
},
|
||||||
"creationTimestamp": null,
|
"creationTimestamp": null,
|
||||||
"labels": {
|
"labels": {
|
||||||
|
"com.example.description": "This label will appear on the web service",
|
||||||
"io.kompose.service": "foo"
|
"io.kompose.service": "foo"
|
||||||
},
|
},
|
||||||
"name": "foo"
|
"name": "foo"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user