forked from LaconicNetwork/kompose
move k8s base functions into k8s package
This commit is contained in:
+13
-267
@@ -6,7 +6,6 @@ import (
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
@@ -15,16 +14,13 @@ import (
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/util/intstr"
|
||||
"github.com/skippbox/kompose/pkg/transformer/kubernetes"
|
||||
)
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
|
||||
// RandStringBytes generates randomly n-character string
|
||||
func randStringBytes(n int) string {
|
||||
func RandStringBytes(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
@@ -33,7 +29,7 @@ func randStringBytes(n int) string {
|
||||
}
|
||||
|
||||
// Create the file to write to if --out is specified
|
||||
func createOutFile(out string) *os.File {
|
||||
func CreateOutFile(out string) *os.File {
|
||||
var f *os.File
|
||||
var err error
|
||||
if len(out) != 0 {
|
||||
@@ -45,133 +41,6 @@ func createOutFile(out string) *os.File {
|
||||
return f
|
||||
}
|
||||
|
||||
// Init RC object
|
||||
func InitRC(name string, service kobject.ServiceConfig, replicas int) *api.ReplicationController {
|
||||
rc := &api.ReplicationController{
|
||||
TypeMeta: unversioned.TypeMeta{
|
||||
Kind: "ReplicationController",
|
||||
APIVersion: "v1",
|
||||
},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: name,
|
||||
//Labels: map[string]string{"service": name},
|
||||
},
|
||||
Spec: api.ReplicationControllerSpec{
|
||||
Selector: map[string]string{"service": name},
|
||||
Replicas: int32(replicas),
|
||||
Template: &api.PodTemplateSpec{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
//Labels: map[string]string{"service": name},
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: name,
|
||||
Image: service.Image,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return rc
|
||||
}
|
||||
|
||||
// Init SC object
|
||||
func InitSC(name string, service kobject.ServiceConfig) *api.Service {
|
||||
sc := &api.Service{
|
||||
TypeMeta: unversioned.TypeMeta{
|
||||
Kind: "Service",
|
||||
APIVersion: "v1",
|
||||
},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: name,
|
||||
//Labels: map[string]string{"service": name},
|
||||
},
|
||||
Spec: api.ServiceSpec{
|
||||
Selector: map[string]string{"service": name},
|
||||
},
|
||||
}
|
||||
return sc
|
||||
}
|
||||
|
||||
// Init DC object
|
||||
func InitDC(name string, service kobject.ServiceConfig, replicas int) *extensions.Deployment {
|
||||
dc := &extensions.Deployment{
|
||||
TypeMeta: unversioned.TypeMeta{
|
||||
Kind: "Deployment",
|
||||
APIVersion: "extensions/v1beta1",
|
||||
},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: name,
|
||||
Labels: map[string]string{"service": name},
|
||||
},
|
||||
Spec: extensions.DeploymentSpec{
|
||||
Replicas: int32(replicas),
|
||||
Selector: &unversioned.LabelSelector{
|
||||
MatchLabels: map[string]string{"service": name},
|
||||
},
|
||||
//UniqueLabelKey: p.Name,
|
||||
Template: api.PodTemplateSpec{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Labels: map[string]string{"service": name},
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: name,
|
||||
Image: service.Image,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return dc
|
||||
}
|
||||
|
||||
// Init DS object
|
||||
func InitDS(name string, service kobject.ServiceConfig) *extensions.DaemonSet {
|
||||
ds := &extensions.DaemonSet{
|
||||
TypeMeta: unversioned.TypeMeta{
|
||||
Kind: "DaemonSet",
|
||||
APIVersion: "extensions/v1beta1",
|
||||
},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: extensions.DaemonSetSpec{
|
||||
Template: api.PodTemplateSpec{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: name,
|
||||
Image: service.Image,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return ds
|
||||
}
|
||||
|
||||
// Configure the environment variables.
|
||||
func ConfigEnvs(name string, service kobject.ServiceConfig) []api.EnvVar {
|
||||
envs := []api.EnvVar{}
|
||||
for _, v := range service.Environment {
|
||||
envs = append(envs, api.EnvVar{
|
||||
Name: v.Name,
|
||||
Value: v.Value,
|
||||
})
|
||||
}
|
||||
|
||||
return envs
|
||||
}
|
||||
|
||||
// Configure the container commands
|
||||
func ConfigCommands(service kobject.ServiceConfig) []string {
|
||||
var cmds []string
|
||||
@@ -182,40 +51,8 @@ func ConfigCommands(service kobject.ServiceConfig) []string {
|
||||
return cmds
|
||||
}
|
||||
|
||||
// Configure the container volumes.
|
||||
func ConfigVolumes(service kobject.ServiceConfig) ([]api.VolumeMount, []api.Volume) {
|
||||
volumesMount := []api.VolumeMount{}
|
||||
volumes := []api.Volume{}
|
||||
volumeSource := api.VolumeSource{}
|
||||
for _, volume := range service.Volumes {
|
||||
name, host, container, mode, err := parseVolume(volume)
|
||||
if err != nil {
|
||||
logrus.Warningf("Failed to configure container volume: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// if volume name isn't specified, set it to a random string of 20 chars
|
||||
if len(name) == 0 {
|
||||
name = randStringBytes(20)
|
||||
}
|
||||
// check if ro/rw mode is defined, default rw
|
||||
readonly := len(mode) > 0 && mode == "ro"
|
||||
|
||||
volumesMount = append(volumesMount, api.VolumeMount{Name: name, ReadOnly: readonly, MountPath: container})
|
||||
|
||||
if len(host) > 0 {
|
||||
volumeSource = api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: host}}
|
||||
} else {
|
||||
volumeSource = api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}
|
||||
}
|
||||
|
||||
volumes = append(volumes, api.Volume{Name: name, VolumeSource: volumeSource})
|
||||
}
|
||||
return volumesMount, volumes
|
||||
}
|
||||
|
||||
// parseVolume parse a given volume, which might be [name:][host:]container[:access_mode]
|
||||
func parseVolume(volume string) (name, host, container, mode string, err error) {
|
||||
func ParseVolume(volume string) (name, host, container, mode string, err error) {
|
||||
separator := ":"
|
||||
volumeStrings := strings.Split(volume, separator)
|
||||
if len(volumeStrings) == 0 {
|
||||
@@ -250,57 +87,6 @@ func isPath(substring string) bool {
|
||||
return strings.Contains(substring, "/")
|
||||
}
|
||||
|
||||
// Configure the container ports.
|
||||
func ConfigPorts(name string, service kobject.ServiceConfig) []api.ContainerPort {
|
||||
ports := []api.ContainerPort{}
|
||||
for _, port := range service.Port {
|
||||
var p api.Protocol
|
||||
switch port.Protocol {
|
||||
default:
|
||||
p = api.ProtocolTCP
|
||||
case kobject.ProtocolTCP:
|
||||
p = api.ProtocolTCP
|
||||
case kobject.ProtocolUDP:
|
||||
p = api.ProtocolUDP
|
||||
}
|
||||
ports = append(ports, api.ContainerPort{
|
||||
ContainerPort: port.ContainerPort,
|
||||
Protocol: p,
|
||||
})
|
||||
}
|
||||
|
||||
return ports
|
||||
}
|
||||
|
||||
// Configure the container service ports.
|
||||
func ConfigServicePorts(name string, service kobject.ServiceConfig) []api.ServicePort {
|
||||
servicePorts := []api.ServicePort{}
|
||||
for _, port := range service.Port {
|
||||
if port.HostPort == 0 {
|
||||
port.HostPort = port.ContainerPort
|
||||
}
|
||||
var p api.Protocol
|
||||
switch port.Protocol {
|
||||
default:
|
||||
p = api.ProtocolTCP
|
||||
case kobject.ProtocolTCP:
|
||||
p = api.ProtocolTCP
|
||||
case kobject.ProtocolUDP:
|
||||
p = api.ProtocolUDP
|
||||
}
|
||||
var targetPort intstr.IntOrString
|
||||
targetPort.IntVal = port.ContainerPort
|
||||
targetPort.StrVal = strconv.Itoa(int(port.ContainerPort))
|
||||
servicePorts = append(servicePorts, api.ServicePort{
|
||||
Name: strconv.Itoa(int(port.HostPort)),
|
||||
Protocol: p,
|
||||
Port: port.HostPort,
|
||||
TargetPort: targetPort,
|
||||
})
|
||||
}
|
||||
return servicePorts
|
||||
}
|
||||
|
||||
// Configure label
|
||||
func ConfigLabels(name string) map[string]string {
|
||||
return map[string]string{"service": name}
|
||||
@@ -338,21 +124,21 @@ func TransformData(obj runtime.Object, GenerateYaml bool) ([]byte, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Either print to stdout or to file/s
|
||||
func Print(name, trailing string, data []byte, toStdout, generateYaml bool, f *os.File) string {
|
||||
|
||||
func print(name, trailing string, data []byte, toStdout, generateYaml bool, f *os.File) {
|
||||
file := fmt.Sprintf("%s-%s.json", name, trailing)
|
||||
file := ""
|
||||
if generateYaml {
|
||||
file = fmt.Sprintf("%s-%s.yaml", name, trailing)
|
||||
}
|
||||
separator := ""
|
||||
if generateYaml {
|
||||
separator = "---"
|
||||
} else {
|
||||
file = fmt.Sprintf("%s-%s.json", name, trailing)
|
||||
}
|
||||
if toStdout {
|
||||
fmt.Fprintf(os.Stdout, "%s%s\n", string(data), separator)
|
||||
fmt.Fprintf(os.Stdout, "%s\n", string(data))
|
||||
return ""
|
||||
} else if f != nil {
|
||||
// Write all content to a single file f
|
||||
if _, err := f.WriteString(fmt.Sprintf("%s%s\n", string(data), separator)); err != nil {
|
||||
if _, err := f.WriteString(fmt.Sprintf("%s\n", string(data))); err != nil {
|
||||
logrus.Fatalf("Failed to write %s to file: %v", trailing, err)
|
||||
}
|
||||
f.Sync()
|
||||
@@ -361,47 +147,7 @@ func print(name, trailing string, data []byte, toStdout, generateYaml bool, f *o
|
||||
if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil {
|
||||
logrus.Fatalf("Failed to write %s: %v", trailing, err)
|
||||
}
|
||||
fmt.Fprintf(os.Stdout, "file %q created\n", file)
|
||||
}
|
||||
}
|
||||
|
||||
func PrintControllers(mServices, mDeployments, mDaemonSets, mReplicationControllers, mDeploymentConfigs map[string][]byte, svcnames []string, opt kobject.ConvertOptions) {
|
||||
f := createOutFile(opt.OutFile)
|
||||
defer f.Close()
|
||||
|
||||
for k, v := range mServices {
|
||||
if v != nil {
|
||||
print(k, "svc", v, opt.ToStdout, opt.GenerateYaml, f)
|
||||
}
|
||||
}
|
||||
|
||||
// If --out or --stdout is set, the validation should already prevent multiple controllers being generated
|
||||
if opt.CreateD {
|
||||
for k, v := range mDeployments {
|
||||
print(k, "deployment", v, opt.ToStdout, opt.GenerateYaml, f)
|
||||
}
|
||||
}
|
||||
|
||||
if opt.CreateDS {
|
||||
for k, v := range mDaemonSets {
|
||||
print(k, "daemonset", v, opt.ToStdout, opt.GenerateYaml, f)
|
||||
}
|
||||
}
|
||||
|
||||
if opt.CreateRC {
|
||||
for k, v := range mReplicationControllers {
|
||||
print(k, "rc", v, opt.ToStdout, opt.GenerateYaml, f)
|
||||
}
|
||||
}
|
||||
|
||||
if f != nil {
|
||||
fmt.Fprintf(os.Stdout, "file %q created\n", opt.OutFile)
|
||||
}
|
||||
|
||||
if opt.CreateChart {
|
||||
err := kubernetes.GenerateHelm(opt.InputFile, svcnames, opt.GenerateYaml, opt.CreateD, opt.CreateDS, opt.CreateRC, opt.OutFile)
|
||||
if err != nil {
|
||||
logrus.Fatalf("Failed to create Chart data: %s\n", err)
|
||||
}
|
||||
logrus.Printf("file %q created", file)
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user