forked from LaconicNetwork/kompose
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
181 lines
5.1 KiB
Go
181 lines
5.1 KiB
Go
/*
|
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package transformer
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"math/rand"
|
|
"os"
|
|
"strings"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
"github.com/ghodss/yaml"
|
|
"github.com/kubernetes-incubator/kompose/pkg/kobject"
|
|
|
|
"path/filepath"
|
|
|
|
"github.com/pkg/errors"
|
|
"k8s.io/kubernetes/pkg/api"
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
)
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
// RandStringBytes generates randomly n-character string
|
|
func RandStringBytes(n int) string {
|
|
b := make([]byte, n)
|
|
for i := range b {
|
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
// CreateOutFile creates the file to write to if --out is specified
|
|
func CreateOutFile(out string) (*os.File, error) {
|
|
var f *os.File
|
|
var err error
|
|
if len(out) != 0 {
|
|
f, err = os.Create(out)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error creating file, os.Create failed")
|
|
}
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
// ParseVolume parses a given volume, which might be [name:][host:]container[:access_mode]
|
|
func ParseVolume(volume string) (name, host, container, mode string, err error) {
|
|
separator := ":"
|
|
|
|
// Parse based on ":"
|
|
volumeStrings := strings.Split(volume, separator)
|
|
if len(volumeStrings) == 0 {
|
|
return
|
|
}
|
|
|
|
// Set name if existed
|
|
if !isPath(volumeStrings[0]) {
|
|
name = volumeStrings[0]
|
|
volumeStrings = volumeStrings[1:]
|
|
}
|
|
|
|
// Check if *anything* has been passed
|
|
if len(volumeStrings) == 0 {
|
|
err = fmt.Errorf("invalid volume format: %s", volume)
|
|
return
|
|
}
|
|
|
|
// Get the last ":" passed which is presumingly the "access mode"
|
|
possibleAccessMode := volumeStrings[len(volumeStrings)-1]
|
|
|
|
// Check to see if :Z or :z exists. We do not support SELinux relabeling at the moment.
|
|
// See https://github.com/kubernetes-incubator/kompose/issues/176
|
|
// Otherwise, check to see if "rw" or "ro" has been passed
|
|
if possibleAccessMode == "z" || possibleAccessMode == "Z" {
|
|
log.Warnf("Volume mount \"%s\" will be mounted without labeling support. :z or :Z not supported", volume)
|
|
mode = ""
|
|
volumeStrings = volumeStrings[:len(volumeStrings)-1]
|
|
} else if possibleAccessMode == "rw" || possibleAccessMode == "ro" {
|
|
mode = possibleAccessMode
|
|
volumeStrings = volumeStrings[:len(volumeStrings)-1]
|
|
}
|
|
|
|
// Check the volume format as well as host
|
|
container = volumeStrings[len(volumeStrings)-1]
|
|
volumeStrings = volumeStrings[:len(volumeStrings)-1]
|
|
if len(volumeStrings) == 1 {
|
|
host = volumeStrings[0]
|
|
}
|
|
if !isPath(container) || (len(host) > 0 && !isPath(host)) || len(volumeStrings) > 1 {
|
|
err = fmt.Errorf("invalid volume format: %s", volume)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func isPath(substring string) bool {
|
|
return strings.Contains(substring, "/")
|
|
}
|
|
|
|
// ConfigLabels configures label
|
|
func ConfigLabels(name string) map[string]string {
|
|
return map[string]string{"service": name}
|
|
}
|
|
|
|
// ConfigAnnotations configures annotations
|
|
func ConfigAnnotations(service kobject.ServiceConfig) map[string]string {
|
|
annotations := map[string]string{}
|
|
for key, value := range service.Annotations {
|
|
annotations[key] = value
|
|
}
|
|
|
|
return annotations
|
|
}
|
|
|
|
// TransformData transforms data to json/yaml
|
|
func TransformData(obj runtime.Object, GenerateJSON bool) ([]byte, error) {
|
|
// Convert to versioned object
|
|
objectVersion := obj.GetObjectKind().GroupVersionKind()
|
|
version := unversioned.GroupVersion{Group: objectVersion.Group, Version: objectVersion.Version}
|
|
versionedObj, err := api.Scheme.ConvertToVersion(obj, version)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// convert data to json / yaml
|
|
data, err := yaml.Marshal(versionedObj)
|
|
if GenerateJSON == true {
|
|
data, err = json.MarshalIndent(versionedObj, "", " ")
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
log.Debugf("%s\n", data)
|
|
return data, nil
|
|
}
|
|
|
|
// Print either prints to stdout or to file/s
|
|
func Print(name, path string, trailing string, data []byte, toStdout, generateJSON bool, f *os.File) (string, error) {
|
|
file := ""
|
|
if generateJSON {
|
|
file = fmt.Sprintf("%s-%s.json", name, trailing)
|
|
} else {
|
|
file = fmt.Sprintf("%s-%s.yaml", name, trailing)
|
|
}
|
|
if toStdout {
|
|
fmt.Fprintf(os.Stdout, "%s\n", string(data))
|
|
return "", nil
|
|
} else if f != nil {
|
|
// Write all content to a single file f
|
|
if _, err := f.WriteString(fmt.Sprintf("%s\n", string(data))); err != nil {
|
|
return "", errors.Wrap(err, "f.WriteString failed, Failed to write %s to file: "+trailing)
|
|
}
|
|
f.Sync()
|
|
} else {
|
|
// Write content separately to each file
|
|
file = filepath.Join(path, file)
|
|
if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil {
|
|
return "", errors.Wrap(err, "Failed to write %s: "+trailing)
|
|
}
|
|
log.Printf("file %q created", file)
|
|
}
|
|
return file, nil
|
|
}
|