clean code

This commit is contained in:
Tuna 2016-08-01 16:47:35 +07:00
parent e867d35e39
commit d532b29d95
6 changed files with 46 additions and 59 deletions

View File

@ -449,7 +449,7 @@ func Convert(c *cli.Context) {
file = dabFile file = dabFile
} }
//komposeObject.Loader(file, inputFormat) // loader parses input from file into komposeObject.
switch inputFormat { switch inputFormat {
case "bundle": case "bundle":
komposeObject = loader.LoadBundle(file) komposeObject = loader.LoadBundle(file)
@ -475,7 +475,7 @@ func Convert(c *cli.Context) {
validateFlags(opt, singleOutput, dabFile, inputFile) validateFlags(opt, singleOutput, dabFile, inputFile)
// Convert komposeObject to K8S controllers // transformer maps komposeObject to provider(K8S, OpenShift) primitives
mServices, mDeployments, mDaemonSets, mReplicationControllers, mDeploymentConfigs, svcnames := transformer.Transform(komposeObject, opt) mServices, mDeployments, mDaemonSets, mReplicationControllers, mDeploymentConfigs, svcnames := transformer.Transform(komposeObject, opt)
// Print output // Print output

View File

@ -19,7 +19,6 @@ package kobject
import ( import (
"fmt" "fmt"
"github.com/fatih/structs" "github.com/fatih/structs"
"math/rand"
) )
var unsupportedKey = map[string]int{ var unsupportedKey = map[string]int{
@ -176,32 +175,12 @@ const (
ProtocolUDP Protocol = "UDP" ProtocolUDP Protocol = "UDP"
) )
// loader takes input and converts to KomposeObject
//func (k *KomposeObject) Loader(file string, inp string) {
// switch inp {
// case "bundle":
// //k.loadBundleFile(file)
// loader.LoadBundle(k, file)
// case "compose":
// //k.loadComposeFile(file)
// loader.LoadCompose(k, file)
// default:
// logrus.Fatalf("Input file format is not supported")
//
// }
//}
// transformer takes KomposeObject and converts to K8S / OpenShift primitives
//func (k *KomposeObject) Transformer(opt ConvertOptions) {
// transformer.Transform(k, opt)
//}
func CheckUnsupportedKey(service interface{}) { func CheckUnsupportedKey(service interface{}) {
s := structs.New(service) s := structs.New(service)
for _, f := range s.Fields() { for _, f := range s.Fields() {
if f.IsExported() && !f.IsZero() { if f.IsExported() && !f.IsZero() {
if count, ok := unsupportedKey[f.Name()]; ok && count == 0 { if count, ok := unsupportedKey[f.Name()]; ok && count == 0 {
fmt.Println("WARNING: Unsupported key " + f.Name() + " - ignoring") fmt.Println("WARNING: Unsupported key " + composeOptions[f.Name()] + " - ignoring")
unsupportedKey[f.Name()]++ unsupportedKey[f.Name()]++
} }
} }

View File

@ -93,7 +93,7 @@ func loadPortsfromBundle(service bundlefile.Service) ([]kobject.Ports, string) {
} }
// load Bundlefile into KomposeObject // load Bundlefile into KomposeObject
func LoadBundle(file string) (kobject.KomposeObject) { func LoadBundle(file string) kobject.KomposeObject {
komposeObject := kobject.KomposeObject{ komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig), ServiceConfigs: make(map[string]kobject.ServiceConfig),
} }

View File

@ -81,7 +81,7 @@ func loadPortsFromCompose(composePorts []string) ([]kobject.Ports, string) {
} }
// load Docker Compose file into KomposeObject // load Docker Compose file into KomposeObject
func LoadCompose(file string) (kobject.KomposeObject) { func LoadCompose(file string) kobject.KomposeObject {
komposeObject := kobject.KomposeObject{ komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig), ServiceConfigs: make(map[string]kobject.ServiceConfig),
} }

View File

@ -174,39 +174,6 @@ func initDS(name string, service kobject.ServiceConfig) *extensions.DaemonSet {
return ds return ds
} }
// initDeploymentConfig initialize OpenShifts DeploymentConfig object
func initDeploymentConfig(name string, service kobject.ServiceConfig, replicas int) *deployapi.DeploymentConfig {
dc := &deployapi.DeploymentConfig{
TypeMeta: unversioned.TypeMeta{
Kind: "DeploymentConfig",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: map[string]string{"service": name},
},
Spec: deployapi.DeploymentConfigSpec{
Replicas: int32(replicas),
Selector: 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
}
// Configure the environment variables. // Configure the environment variables.
func configEnvs(name string, service kobject.ServiceConfig) []api.EnvVar { func configEnvs(name string, service kobject.ServiceConfig) []api.EnvVar {
envs := []api.EnvVar{} envs := []api.EnvVar{}

View File

@ -15,3 +15,44 @@ limitations under the License.
*/ */
package transformer package transformer
import (
"github.com/skippbox/kompose/pkg/kobject"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
deployapi "github.com/openshift/origin/pkg/deploy/api"
)
// initDeploymentConfig initialize OpenShifts DeploymentConfig object
func initDeploymentConfig(name string, service kobject.ServiceConfig, replicas int) *deployapi.DeploymentConfig {
dc := &deployapi.DeploymentConfig{
TypeMeta: unversioned.TypeMeta{
Kind: "DeploymentConfig",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: map[string]string{"service": name},
},
Spec: deployapi.DeploymentConfigSpec{
Replicas: int32(replicas),
Selector: 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
}