forked from LaconicNetwork/kompose
return errors instead of logrus.Fatal calls
This commit refactors the code to remove more or less
all occurences of logrus.Fatalf() from the code under
pkg/ except for app.go where all the errors are being
handled currently.
This is being done since random logrus.Fatalf() calls
all around the code was making handling the errors,
unit testing and troubleshooting a bit more painful.
logrus.Fatalf() calls are either replaced by
return errors.New("new error")
or
return errors.Wrap(err, "annonate error")
calls, and the function signatures are accordingly
changed to accomodate the new return values.
The unit tests which previously used to check
if logrus.Fatalf() calls worked fine have also
been fixed to only check for errors now.
Fixes #416
This commit is contained in:
+19
-18
@@ -29,6 +29,7 @@ import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/fatih/structs"
|
||||
"github.com/kubernetes-incubator/kompose/pkg/kobject"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Bundle is docker bundle file loader, implements Loader interface
|
||||
@@ -106,16 +107,16 @@ func checkUnsupportedKey(bundleStruct *Bundlefile) []string {
|
||||
}
|
||||
|
||||
// load image from dab file
|
||||
func loadImage(service Service) (string, string) {
|
||||
func loadImage(service Service) (string, error) {
|
||||
character := "@"
|
||||
if strings.Contains(service.Image, character) {
|
||||
return service.Image[0:strings.Index(service.Image, character)], ""
|
||||
return service.Image[0:strings.Index(service.Image, character)], nil
|
||||
}
|
||||
return "", "Invalid image format"
|
||||
return "", errors.New("Invalid image format")
|
||||
}
|
||||
|
||||
// load environment variables from dab file
|
||||
func loadEnvVars(service Service) ([]kobject.EnvVar, string) {
|
||||
func loadEnvVars(service Service) ([]kobject.EnvVar, error) {
|
||||
envs := []kobject.EnvVar{}
|
||||
for _, env := range service.Env {
|
||||
character := "="
|
||||
@@ -144,15 +145,15 @@ func loadEnvVars(service Service) ([]kobject.EnvVar, string) {
|
||||
Value: value,
|
||||
})
|
||||
} else {
|
||||
return envs, "Invalid container env " + env
|
||||
return envs, errors.New("Invalid container env")
|
||||
}
|
||||
}
|
||||
}
|
||||
return envs, ""
|
||||
return envs, nil
|
||||
}
|
||||
|
||||
// load ports from dab file
|
||||
func loadPorts(service Service) ([]kobject.Ports, string) {
|
||||
func loadPorts(service Service) ([]kobject.Ports, error) {
|
||||
ports := []kobject.Ports{}
|
||||
for _, port := range service.Ports {
|
||||
var p api.Protocol
|
||||
@@ -170,11 +171,11 @@ func loadPorts(service Service) ([]kobject.Ports, string) {
|
||||
Protocol: p,
|
||||
})
|
||||
}
|
||||
return ports, ""
|
||||
return ports, nil
|
||||
}
|
||||
|
||||
// LoadFile loads dab file into KomposeObject
|
||||
func (b *Bundle) LoadFile(files []string) kobject.KomposeObject {
|
||||
func (b *Bundle) LoadFile(files []string) (kobject.KomposeObject, error) {
|
||||
komposeObject := kobject.KomposeObject{
|
||||
ServiceConfigs: make(map[string]kobject.ServiceConfig),
|
||||
LoadedFrom: "bundle",
|
||||
@@ -182,12 +183,12 @@ func (b *Bundle) LoadFile(files []string) kobject.KomposeObject {
|
||||
file := files[0]
|
||||
buf, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read bundles file: %s ", err)
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "ioutil.ReadFile failed, Failed to read bundles file")
|
||||
}
|
||||
reader := strings.NewReader(string(buf))
|
||||
bundle, err := loadFile(reader)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse bundles file: %s", err)
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "loadFile failed, Failed to parse bundles file")
|
||||
}
|
||||
|
||||
noSupKeys := checkUnsupportedKey(bundle)
|
||||
@@ -204,20 +205,20 @@ func (b *Bundle) LoadFile(files []string) kobject.KomposeObject {
|
||||
serviceConfig.Annotations = service.Labels
|
||||
|
||||
image, err := loadImage(service)
|
||||
if err != "" {
|
||||
log.Fatalf("Failed to load image from bundles file: " + err)
|
||||
if err != nil {
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "loadImage failed, Failed to load image from bundles file")
|
||||
}
|
||||
serviceConfig.Image = image
|
||||
|
||||
envs, err := loadEnvVars(service)
|
||||
if err != "" {
|
||||
log.Fatalf("Failed to load envvar from bundles file: " + err)
|
||||
if err != nil {
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "loadEnvVars failed, Failed to load envvar from bundles file")
|
||||
}
|
||||
serviceConfig.Environment = envs
|
||||
|
||||
ports, err := loadPorts(service)
|
||||
if err != "" {
|
||||
log.Fatalf("Failed to load ports from bundles file: " + err)
|
||||
if err != nil {
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "loadPorts failed, Failed to load ports from bundles file")
|
||||
}
|
||||
serviceConfig.Port = ports
|
||||
|
||||
@@ -228,7 +229,7 @@ func (b *Bundle) LoadFile(files []string) kobject.KomposeObject {
|
||||
komposeObject.ServiceConfigs[name] = serviceConfig
|
||||
}
|
||||
|
||||
return komposeObject
|
||||
return komposeObject, nil
|
||||
}
|
||||
|
||||
// LoadFile loads a bundlefile from a path to the file
|
||||
|
||||
@@ -33,6 +33,7 @@ import (
|
||||
"github.com/docker/libcompose/project"
|
||||
"github.com/fatih/structs"
|
||||
"github.com/kubernetes-incubator/kompose/pkg/kobject"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Compose is docker compose file loader, implements Loader interface
|
||||
@@ -275,7 +276,7 @@ func loadPorts(composePorts []string) ([]kobject.Ports, error) {
|
||||
}
|
||||
|
||||
// LoadFile loads compose file into KomposeObject
|
||||
func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
|
||||
func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
|
||||
komposeObject := kobject.KomposeObject{
|
||||
ServiceConfigs: make(map[string]kobject.ServiceConfig),
|
||||
LoadedFrom: "compose",
|
||||
@@ -290,7 +291,7 @@ func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
|
||||
if context.EnvironmentLookup == nil {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return kobject.KomposeObject{}
|
||||
return kobject.KomposeObject{}, nil
|
||||
}
|
||||
context.EnvironmentLookup = &lookup.ComposableEnvLookup{
|
||||
Lookups: []config.EnvironmentLookup{
|
||||
@@ -306,7 +307,7 @@ func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
|
||||
composeObject := project.NewProject(context, nil, nil)
|
||||
err := composeObject.Parse()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load compose file: %v", err)
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "composeObject.Parse() failed, Failed to load compose file")
|
||||
}
|
||||
|
||||
noSupKeys := checkUnsupportedKey(composeObject)
|
||||
@@ -329,7 +330,7 @@ func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
|
||||
// load ports
|
||||
ports, err := loadPorts(composeServiceConfig.Ports)
|
||||
if err != nil {
|
||||
log.Fatalf("%q failed to load ports from compose file: %v", name, err)
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "loadPorts failed. "+name+" failed to load ports from compose file")
|
||||
}
|
||||
serviceConfig.Port = ports
|
||||
|
||||
@@ -347,7 +348,12 @@ func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
|
||||
for key, value := range composeServiceConfig.Labels {
|
||||
switch key {
|
||||
case "kompose.service.type":
|
||||
serviceConfig.ServiceType = handleServiceType(value)
|
||||
serviceType, err := handleServiceType(value)
|
||||
if err != nil {
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "handleServiceType failed")
|
||||
}
|
||||
|
||||
serviceConfig.ServiceType = serviceType
|
||||
case "kompose.service.expose":
|
||||
serviceConfig.ExposeService = strings.ToLower(value)
|
||||
}
|
||||
@@ -373,20 +379,19 @@ func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
|
||||
komposeObject.ServiceConfigs[name] = serviceConfig
|
||||
}
|
||||
|
||||
return komposeObject
|
||||
return komposeObject, nil
|
||||
}
|
||||
|
||||
func handleServiceType(ServiceType string) string {
|
||||
func handleServiceType(ServiceType string) (string, error) {
|
||||
switch strings.ToLower(ServiceType) {
|
||||
case "", "clusterip":
|
||||
return string(api.ServiceTypeClusterIP)
|
||||
return string(api.ServiceTypeClusterIP), nil
|
||||
case "nodeport":
|
||||
return string(api.ServiceTypeNodePort)
|
||||
return string(api.ServiceTypeNodePort), nil
|
||||
case "loadbalancer":
|
||||
return string(api.ServiceTypeLoadBalancer)
|
||||
return string(api.ServiceTypeLoadBalancer), nil
|
||||
default:
|
||||
log.Fatalf("Unknown value '%s', supported values are 'NodePort, ClusterIP or LoadBalancer'", ServiceType)
|
||||
return ""
|
||||
return "", errors.New("Unknown value " + ServiceType + " , supported values are 'NodePort, ClusterIP or LoadBalancer'")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/docker/libcompose/config"
|
||||
"github.com/docker/libcompose/project"
|
||||
"github.com/docker/libcompose/yaml"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Test if service types are parsed properly on user input
|
||||
@@ -47,7 +48,10 @@ func TestHandleServiceType(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
result := handleServiceType(tt.labelValue)
|
||||
result, err := handleServiceType(tt.labelValue)
|
||||
if err != nil {
|
||||
t.Error(errors.Wrap(err, "handleServiceType failed"))
|
||||
}
|
||||
if result != tt.serviceType {
|
||||
t.Errorf("Expected %q, got %q", tt.serviceType, result)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
|
||||
// Loader interface defines loader that loads files and converts it to kobject representation
|
||||
type Loader interface {
|
||||
LoadFile(files []string) kobject.KomposeObject
|
||||
LoadFile(files []string) (kobject.KomposeObject, error)
|
||||
///Name() string
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user