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

Create service function in kubernetes utils #108

Merged
merged 1 commit into from
Aug 18, 2016
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
70 changes: 70 additions & 0 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,73 @@ func PortsExist(name string, service kobject.ServiceConfig) bool {
return true
}
}

// create a kubernetes Service
func CreateService(name string, service kobject.ServiceConfig, objects []runtime.Object) *api.Service {

svc := InitSvc(name, service)

// Configure the environment variables.
envs := ConfigEnvs(name, service)

// Configure the container command.
cmds := transformer.ConfigCommands(service)

// Configure the container volumes.
volumesMount, volumes := ConfigVolumes(service)

// Configure the container ports.
ports := ConfigPorts(name, service)

// Configure the service ports.
servicePorts := ConfigServicePorts(name, service)
svc.Spec.Ports = servicePorts

// Configure annotations
annotations := transformer.ConfigAnnotations(service)
svc.ObjectMeta.Annotations = annotations

// fillTemplate fills the pod template with the value calculated from config
fillTemplate := func(template *api.PodTemplateSpec) {
if len(service.ContainerName) > 0 {
template.Spec.Containers[0].Name = service.ContainerName
}
template.Spec.Containers[0].Env = envs
template.Spec.Containers[0].Command = cmds
template.Spec.Containers[0].Args = service.Args
template.Spec.Containers[0].WorkingDir = service.WorkingDir
template.Spec.Containers[0].VolumeMounts = volumesMount
template.Spec.Volumes = volumes
// Configure the container privileged mode
if service.Privileged == true {
template.Spec.Containers[0].SecurityContext = &api.SecurityContext{
Privileged: &service.Privileged,
}
}
template.Spec.Containers[0].Ports = ports
template.ObjectMeta.Labels = transformer.ConfigLabels(name)
// Configure the container restart policy.
switch service.Restart {
case "", "always":
template.Spec.RestartPolicy = api.RestartPolicyAlways
case "no":
template.Spec.RestartPolicy = api.RestartPolicyNever
case "on-failure":
template.Spec.RestartPolicy = api.RestartPolicyOnFailure
default:
logrus.Fatalf("Unknown restart policy %s for service %s", service.Restart, name)
}
}

// fillObjectMeta fills the metadata with the value calculated from config
fillObjectMeta := func(meta *api.ObjectMeta) {
meta.Annotations = annotations
}

// update supported controller
for _, obj := range objects {
UpdateController(obj, fillTemplate, fillObjectMeta)
}

return svc
}
65 changes: 1 addition & 64 deletions pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
var objects []runtime.Object
svcnames = append(svcnames, name)

svc := InitSvc(name, service)

if opt.CreateD {
objects = append(objects, InitD(name, service, opt.Replicas))
}
Expand All @@ -237,70 +235,9 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
objects = append(objects, InitRC(name, service, opt.Replicas))
}

// Configure the environment variables.
envs := ConfigEnvs(name, service)

// Configure the container command.
cmds := transformer.ConfigCommands(service)

// Configure the container volumes.
volumesMount, volumes := ConfigVolumes(service)

// Configure the container ports.
ports := ConfigPorts(name, service)

// Configure the service ports.
servicePorts := ConfigServicePorts(name, service)
svc.Spec.Ports = servicePorts

// Configure annotations
annotations := transformer.ConfigAnnotations(service)
svc.ObjectMeta.Annotations = annotations

// fillTemplate fills the pod template with the value calculated from config
fillTemplate := func(template *api.PodTemplateSpec) {
if len(service.ContainerName) > 0 {
template.Spec.Containers[0].Name = service.ContainerName
}
template.Spec.Containers[0].Env = envs
template.Spec.Containers[0].Command = cmds
template.Spec.Containers[0].Args = service.Args
template.Spec.Containers[0].WorkingDir = service.WorkingDir
template.Spec.Containers[0].VolumeMounts = volumesMount
template.Spec.Volumes = volumes
// Configure the container privileged mode
if service.Privileged == true {
template.Spec.Containers[0].SecurityContext = &api.SecurityContext{
Privileged: &service.Privileged,
}
}
template.Spec.Containers[0].Ports = ports
template.ObjectMeta.Labels = transformer.ConfigLabels(name)
// Configure the container restart policy.
switch service.Restart {
case "", "always":
template.Spec.RestartPolicy = api.RestartPolicyAlways
case "no":
template.Spec.RestartPolicy = api.RestartPolicyNever
case "on-failure":
template.Spec.RestartPolicy = api.RestartPolicyOnFailure
default:
logrus.Fatalf("Unknown restart policy %s for service %s", service.Restart, name)
}
}

// fillObjectMeta fills the metadata with the value calculated from config
fillObjectMeta := func(meta *api.ObjectMeta) {
meta.Annotations = annotations
}

// update supported controller
for _, obj := range objects {
UpdateController(obj, fillTemplate, fillObjectMeta)
}

// If ports not provided in configuration we will not make service
if PortsExist(name, service) {
svc := CreateService(name, service, objects)
objects = append(objects, svc)
}

Expand Down
68 changes: 1 addition & 67 deletions pkg/transformer/openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ limitations under the License.
package openshift

import (
"github.com/Sirupsen/logrus"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/skippbox/kompose/pkg/kobject"
"github.com/skippbox/kompose/pkg/transformer"
"github.com/skippbox/kompose/pkg/transformer/kubernetes"

"k8s.io/kubernetes/pkg/api"
Expand Down Expand Up @@ -74,8 +72,6 @@ func (k *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
var objects []runtime.Object
svcnames = append(svcnames, name)

svc := kubernetes.InitSvc(name, service)

if opt.CreateD {
objects = append(objects, kubernetes.InitD(name, service, opt.Replicas))
}
Expand All @@ -89,71 +85,9 @@ func (k *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
objects = append(objects, initDeploymentConfig(name, service, opt.Replicas)) // OpenShift DeploymentConfigs
}

// Configure the environment variables.
envs := kubernetes.ConfigEnvs(name, service)

// Configure the container command.
cmds := transformer.ConfigCommands(service)

// Configure the container volumes.
volumesMount, volumes := kubernetes.ConfigVolumes(service)

// Configure the container ports.
ports := kubernetes.ConfigPorts(name, service)

// Configure the service ports.
servicePorts := kubernetes.ConfigServicePorts(name, service)
svc.Spec.Ports = servicePorts

// Configure label
labels := transformer.ConfigLabels(name)
svc.ObjectMeta.Labels = labels

// Configure annotations
annotations := transformer.ConfigAnnotations(service)
svc.ObjectMeta.Annotations = annotations

// fillTemplate fills the pod template with the value calculated from config
fillTemplate := func(template *api.PodTemplateSpec) {
template.Spec.Containers[0].Env = envs
template.Spec.Containers[0].Command = cmds
template.Spec.Containers[0].WorkingDir = service.WorkingDir
template.Spec.Containers[0].VolumeMounts = volumesMount
template.Spec.Volumes = volumes
// Configure the container privileged mode
if service.Privileged == true {
template.Spec.Containers[0].SecurityContext = &api.SecurityContext{
Privileged: &service.Privileged,
}
}
template.Spec.Containers[0].Ports = ports
template.ObjectMeta.Labels = labels
// Configure the container restart policy.
switch service.Restart {
case "", "always":
template.Spec.RestartPolicy = api.RestartPolicyAlways
case "no":
template.Spec.RestartPolicy = api.RestartPolicyNever
case "on-failure":
template.Spec.RestartPolicy = api.RestartPolicyOnFailure
default:
logrus.Fatalf("Unknown restart policy %s for service %s", service.Restart, name)
}
}

// fillObjectMeta fills the metadata with the value calculated from config
fillObjectMeta := func(meta *api.ObjectMeta) {
meta.Labels = labels
meta.Annotations = annotations
}

// update supported controller
for _, obj := range objects {
kubernetes.UpdateController(obj, fillTemplate, fillObjectMeta)
}

// If ports not provided in configuration we will not make service
if kubernetes.PortsExist(name, service) {
svc := kubernetes.CreateService(name, service, objects)
objects = append(objects, svc)
}
allobjects = append(allobjects, objects...)
Expand Down