checkUnsupportedKeys for transformers

This commit is contained in:
Tomas Kral
2016-12-20 12:21:52 +05:30
committed by Ratnadeep Debnath
parent e4f9b59b4d
commit f20e6f3fa9
3 changed files with 92 additions and 23 deletions
+54
View File
@@ -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
+13 -1
View File
@@ -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