Separate unsupported key check for every loader

This commit is contained in:
Tomas Kral
2016-12-20 12:21:03 +05:30
committed by Ratnadeep Debnath
parent c485a870d8
commit e4f9b59b4d
4 changed files with 159 additions and 112 deletions
+55 -1
View File
@@ -21,11 +21,13 @@ import (
"fmt"
"io"
"io/ioutil"
"reflect"
"strings"
"k8s.io/kubernetes/pkg/api"
"github.com/Sirupsen/logrus"
"github.com/fatih/structs"
"github.com/kubernetes-incubator/kompose/pkg/kobject"
)
@@ -57,6 +59,53 @@ type Port struct {
Port uint32
}
// checkUnsupportedKey checks if dab contains
// keys that are not supported by this loader.
// list of all unsupported keys are stored in unsupportedKey variable
// returns list of unsupported JSON/YAML keys
func checkUnsupportedKey(bundleStruct *Bundlefile) []string {
// list of all unsupported keys for this loader
// this is map to make searching for keys easier
// also counts how many times was given key found in service
// to make sure that we show warning only once for every key
var unsupportedKey = map[string]int{
"Networks": 0,
}
// collect all keys found in project
var keysFound []string
for _, service := range bundleStruct.Services {
// this reflection is used in check for empty arrays
val := reflect.ValueOf(service)
s := structs.New(service)
for _, f := range s.Fields() {
if f.IsExported() && !f.IsZero() {
jsonTagName := strings.Split(f.Tag("json"), ",")[0]
if jsonTagName == "" {
jsonTagName = f.Name()
}
// 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 {
keysFound = append(keysFound, jsonTagName)
}
unsupportedKey[f.Name()]++
}
}
}
}
return keysFound
}
// load image from dab file
func loadImage(service Service) (string, string) {
character := "@"
@@ -129,6 +178,7 @@ func loadPorts(service Service) ([]kobject.Ports, string) {
func (b *Bundle) LoadFile(file string) kobject.KomposeObject {
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
LoadedFrom: "bundle",
}
buf, err := ioutil.ReadFile(file)
@@ -141,8 +191,12 @@ func (b *Bundle) LoadFile(file string) kobject.KomposeObject {
logrus.Fatalf("Failed to parse bundles file: %s", err)
}
noSupKeys := checkUnsupportedKey(bundle)
for _, keyName := range noSupKeys {
logrus.Warningf("Unsupported %s key - ignoring", keyName)
}
for name, service := range bundle.Services {
kobject.CheckUnsupportedKey(service)
serviceConfig := kobject.ServiceConfig{}
serviceConfig.Command = service.Command