From f20e6f3fa997ca862ec244116aa9f07434f32eda Mon Sep 17 00:00:00 2001 From: Tomas Kral Date: Fri, 9 Dec 2016 14:20:41 +0100 Subject: [PATCH] checkUnsupportedKeys for transformers --- pkg/kobject/kobject.go | 47 +++++++++++---------- pkg/transformer/kubernetes/kubernetes.go | 54 ++++++++++++++++++++++++ pkg/transformer/openshift/openshift.go | 14 +++++- 3 files changed, 92 insertions(+), 23 deletions(-) diff --git a/pkg/kobject/kobject.go b/pkg/kobject/kobject.go index a52506b4..8bf78451 100644 --- a/pkg/kobject/kobject.go +++ b/pkg/kobject/kobject.go @@ -21,6 +21,8 @@ import "k8s.io/kubernetes/pkg/api" // KomposeObject holds the generic struct of Kompose transformation type KomposeObject struct { ServiceConfigs map[string]ServiceConfig + // name of the loader that was created KomposeObject + LoadedFrom string } type ConvertOptions struct { @@ -40,29 +42,30 @@ type ConvertOptions struct { // ServiceConfig holds the basic struct of a container type ServiceConfig struct { + // use tags to mark from what element this value comes ContainerName string - Image string - Environment []EnvVar - Port []Ports - Command []string - WorkingDir string - Args []string - Volumes []string - Network []string - Labels map[string]string - Annotations map[string]string - CPUSet string - CPUShares int64 - CPUQuota int64 - CapAdd []string - CapDrop []string - Expose []string - Privileged bool - Restart string - User string - VolumesFrom []string - ServiceType string - Build string + Image string `compose:"image",bundle:"Image"` + Environment []EnvVar `compose:"environment",bundle:"Env"` + Port []Ports `compose:"ports",bundle:"Ports"` + Command []string `compose:"command",bundle:"Command"` + WorkingDir string `compose:"",bundle:"WorkingDir"` + Args []string `compose:"args",bundle:"Args"` + Volumes []string `compose:"volumes",bundle:"Volumes"` + Network []string `compose:"network",bundle:"Networks"` + Labels map[string]string `compose:"labels",bundle:"Labels"` + Annotations map[string]string `compose:"",bundle:""` + CPUSet string `compose:"cpuset",bundle:""` + CPUShares int64 `compose:"cpu_shares",bundle:""` + CPUQuota int64 `compose:"cpu_quota",bundle:""` + CapAdd []string `compose:"cap_add",bundle:""` + CapDrop []string `compose:"cap_drop",bundle:""` + Expose []string `compose:"expose",bundle:""` + Privileged bool `compose:"privileged",bundle:""` + Restart string `compose:"restart",bundle:""` + User string `compose:"user",bundle:"User"` + VolumesFrom []string `compose:"volumes_from",bundle:""` + ServiceType string `compose:"kompose.service.type",bundle:""` + Build string `compose:"build",bundle:""` } // EnvVar holds the environment variable struct of a container diff --git a/pkg/transformer/kubernetes/kubernetes.go b/pkg/transformer/kubernetes/kubernetes.go index d2163182..1a59ac18 100644 --- a/pkg/transformer/kubernetes/kubernetes.go +++ b/pkg/transformer/kubernetes/kubernetes.go @@ -18,11 +18,13 @@ package kubernetes import ( "fmt" + "reflect" "sort" "strconv" "time" "github.com/Sirupsen/logrus" + "github.com/fatih/structs" "github.com/kubernetes-incubator/kompose/pkg/kobject" "github.com/kubernetes-incubator/kompose/pkg/transformer" deployapi "github.com/openshift/origin/pkg/deploy/api" @@ -52,6 +54,52 @@ type Kubernetes struct { // used when undeploying resources from kubernetes const TIMEOUT = 300 +// list of all unsupported keys for this transformer +// Keys are names of variables in kobject struct. +// this is map to make searching for keys easier +// also counts how many times was given key found in kobject +// to make sure that we show warning only once for every key +var unsupportedKey = map[string]int{ + "Build": 0, +} + +// checkUnsupportedKey checks if given komposeObject contains +// keys that are not supported by this tranfomer. +// list of all unsupported keys are stored in unsupportedKey variable +// returns list of TODO: .... +func (k *Kubernetes) CheckUnsupportedKey(komposeObject *kobject.KomposeObject, unsupportedKey map[string]int) []string { + // collect all keys found in project + var keysFound []string + + for _, serviceConfig := range komposeObject.ServiceConfigs { + // this reflection is used in check for empty arrays + val := reflect.ValueOf(serviceConfig) + s := structs.New(serviceConfig) + + for _, f := range s.Fields() { + if f.IsExported() && !f.IsZero() { + // IsZero returns false for empty array/slice ([]) + // this check if field is Slice, and then it checks its size + if field := val.FieldByName(f.Name()); field.Kind() == reflect.Slice { + if field.Len() == 0 { + // array is empty it doesn't metter if it is in unsupportedKey or not + continue + } + } + if counter, ok := unsupportedKey[f.Name()]; ok { + if counter == 0 { + //get tag from kobject service configure + tag := f.Tag(komposeObject.LoadedFrom) + keysFound = append(keysFound, tag) + } + unsupportedKey[f.Name()]++ + } + } + } + } + return keysFound +} + // Init RC object func (k *Kubernetes) InitRC(name string, service kobject.ServiceConfig, replicas int) *api.ReplicationController { rc := &api.ReplicationController{ @@ -351,6 +399,12 @@ func (k *Kubernetes) InitPod(name string, service kobject.ServiceConfig) *api.Po // Transform maps komposeObject to k8s objects // returns object that are already sorted in the way that Services are first func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.ConvertOptions) []runtime.Object { + + noSupKeys := k.CheckUnsupportedKey(&komposeObject, unsupportedKey) + for _, keyName := range noSupKeys { + logrus.Warningf("Kubernetes provider doesn't support %s key - ignoring", keyName) + } + // this will hold all the converted data var allobjects []runtime.Object diff --git a/pkg/transformer/openshift/openshift.go b/pkg/transformer/openshift/openshift.go index b71358b7..a1986000 100644 --- a/pkg/transformer/openshift/openshift.go +++ b/pkg/transformer/openshift/openshift.go @@ -33,11 +33,12 @@ import ( oclient "github.com/openshift/origin/pkg/client" ocliconfig "github.com/openshift/origin/pkg/cmd/cli/config" + "time" + deployapi "github.com/openshift/origin/pkg/deploy/api" deploymentconfigreaper "github.com/openshift/origin/pkg/deploy/cmd" imageapi "github.com/openshift/origin/pkg/image/api" "k8s.io/kubernetes/pkg/kubectl" - "time" ) type OpenShift struct { @@ -51,6 +52,13 @@ type OpenShift struct { // used when undeploying resources from OpenShift const TIMEOUT = 300 +// list of all unsupported keys for this transformer +// Keys are names of variables in kobject struct. +// this is map to make searching for keys easier +// also counts how many times was given key found in kobject +// to make sure that we show warning only once for every key +var unsupportedKey = map[string]int{} + // getImageTag get tag name from image name // if no tag is specified return 'latest' func getImageTag(image string) string { @@ -151,6 +159,10 @@ func (o *OpenShift) initDeploymentConfig(name string, service kobject.ServiceCon // Transform maps komposeObject to openshift objects // returns objects that are already sorted in the way that Services are first func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.ConvertOptions) []runtime.Object { + noSupKeys := o.Kubernetes.CheckUnsupportedKey(&komposeObject, unsupportedKey) + for _, keyName := range noSupKeys { + logrus.Warningf("OpenShift provider doesn't support %s key - ignoring", keyName) + } // this will hold all the converted data var allobjects []runtime.Object