make loader, transformer as interfaces

This commit is contained in:
Tuna
2016-08-11 23:33:45 +07:00
parent d532b29d95
commit f10d6afecf
10 changed files with 548 additions and 320 deletions
+110
View File
@@ -0,0 +1,110 @@
/*
Copyright 2016 Skippbox, Ltd All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubernetes
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/Sirupsen/logrus"
)
/**
* Generate Helm Chart configuration
*/
func GenerateHelm(filename string, svcnames []string, generateYaml, createD, createDS, createRC bool, outFile string) error {
type ChartDetails struct {
Name string
}
extension := filepath.Ext(filename)
dirName := filename[0 : len(filename)-len(extension)]
details := ChartDetails{dirName}
manifestDir := dirName + string(os.PathSeparator) + "templates"
dir, err := os.Open(dirName)
/* Setup the initial directories/files */
if err == nil {
_ = dir.Close()
}
if err != nil {
err = os.Mkdir(dirName, 0755)
if err != nil {
return err
}
err = os.Mkdir(manifestDir, 0755)
if err != nil {
return err
}
/* Create the readme file */
readme := "This chart was created by Kompose\n"
err = ioutil.WriteFile(dirName+string(os.PathSeparator)+"README.md", []byte(readme), 0644)
if err != nil {
return err
}
/* Create the Chart.yaml file */
chart := `name: {{.Name}}
description: A generated Helm Chart for {{.Name}} from Skippbox Kompose
version: 0.0.1
keywords:
- {{.Name}}
sources:
home:
`
t, err := template.New("ChartTmpl").Parse(chart)
if err != nil {
logrus.Fatalf("Failed to generate Chart.yaml template: %s\n", err)
}
var chartData bytes.Buffer
_ = t.Execute(&chartData, details)
err = ioutil.WriteFile(dirName+string(os.PathSeparator)+"Chart.yaml", chartData.Bytes(), 0644)
if err != nil {
return err
}
}
/* Copy all related json/yaml files into the newly created manifests directory */
for _, filename := range outFiles {
if err = cpFileToChart(manifestDir, filename); err != nil {
logrus.Warningln(err)
}
if err = os.Remove(filename); err != nil {
logrus.Warningln(err)
}
}
logrus.Infof("chart created in %q\n", "."+string(os.PathSeparator)+dirName+string(os.PathSeparator))
return nil
}
func cpFileToChart(manifestDir, filename string) error {
infile, err := ioutil.ReadFile(filename)
if err != nil {
logrus.Warningf("Error reading %s: %s\n", filename, err)
return err
}
return ioutil.WriteFile(manifestDir+string(os.PathSeparator)+filename, infile, 0644)
}
+176
View File
@@ -0,0 +1,176 @@
/*
Copyright 2016 Skippbox, Ltd All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubernetes
import (
"fmt"
"os"
"github.com/Sirupsen/logrus"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/skippbox/kompose/pkg/kobject"
"github.com/skippbox/kompose/pkg/transformer"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/runtime"
)
type Kubernetes struct {
}
func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.ConvertOptions) (map[string][]byte, map[string][]byte, map[string][]byte, map[string][]byte, map[string][]byte, []string){
mServices := make(map[string][]byte)
mReplicationControllers := make(map[string][]byte)
mDeployments := make(map[string][]byte)
mDaemonSets := make(map[string][]byte)
var svcnames []string
for name, service := range komposeObject.ServiceConfigs {
svcnames = append(svcnames, name)
rc := transformer.InitRC(name, service, opt.Replicas)
sc := transformer.InitSC(name, service)
dc := transformer.InitDC(name, service, opt.Replicas)
ds := transformer.InitDS(name, service)
// Configure the environment variables.
envs := transformer.ConfigEnvs(name, service)
// Configure the container command.
cmds := transformer.ConfigCommands(service)
// Configure the container volumes.
volumesMount, volumes := transformer.ConfigVolumes(service)
// Configure the container ports.
ports := transformer.ConfigPorts(name, service)
// Configure the service ports.
servicePorts := transformer.ConfigServicePorts(name, service)
sc.Spec.Ports = servicePorts
// Configure label
labels := transformer.ConfigLabels(name)
sc.ObjectMeta.Labels = labels
// Configure annotations
annotations := transformer.ConfigAnnotations(service)
sc.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 each supported controllers
UpdateController(rc, fillTemplate, fillObjectMeta)
UpdateController(dc, fillTemplate, fillObjectMeta)
UpdateController(ds, fillTemplate, fillObjectMeta)
// convert datarc to json / yaml
datarc, err := transformer.TransformData(rc, opt.GenerateYaml)
if err != nil {
logrus.Fatalf(err.Error())
}
// convert datadc to json / yaml
datadc, err := transformer.TransformData(dc, opt.GenerateYaml)
if err != nil {
logrus.Fatalf(err.Error())
}
// convert datads to json / yaml
datads, err := transformer.TransformData(ds, opt.GenerateYaml)
if err != nil {
logrus.Fatalf(err.Error())
}
var datasvc []byte
// If ports not provided in configuration we will not make service
if len(ports) == 0 {
logrus.Warningf("[%s] Service cannot be created because of missing port.", name)
} else if len(ports) != 0 {
// convert datasvc to json / yaml
datasvc, err = transformer.TransformData(sc, opt.GenerateYaml)
if err != nil {
logrus.Fatalf(err.Error())
}
}
mServices[name] = datasvc
mReplicationControllers[name] = datarc
mDeployments[name] = datadc
mDaemonSets[name] = datads
}
return mServices, mDeployments, mDaemonSets, mReplicationControllers, nil, svcnames
}
// updateController updates the given object with the given pod template update function and ObjectMeta update function
func UpdateController(obj runtime.Object, updateTemplate func(*api.PodTemplateSpec), updateMeta func(meta *api.ObjectMeta)) {
switch t := obj.(type) {
case *api.ReplicationController:
if t.Spec.Template == nil {
t.Spec.Template = &api.PodTemplateSpec{}
}
updateTemplate(t.Spec.Template)
updateMeta(&t.ObjectMeta)
case *extensions.Deployment:
updateTemplate(&t.Spec.Template)
updateMeta(&t.ObjectMeta)
case *extensions.ReplicaSet:
updateTemplate(&t.Spec.Template)
updateMeta(&t.ObjectMeta)
case *extensions.DaemonSet:
updateTemplate(&t.Spec.Template)
updateMeta(&t.ObjectMeta)
case *deployapi.DeploymentConfig:
updateTemplate(t.Spec.Template)
updateMeta(&t.ObjectMeta)
}
}