forked from LaconicNetwork/kompose
add support for golangci-lint (#1373)
This commit is contained in:
parent
d1e538e2d4
commit
395d99fb7d
15
.github/workflows/golangci-lint.yml
vendored
Normal file
15
.github/workflows/golangci-lint.yml
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
name: golangci-lint
|
||||
on:
|
||||
pull_request:
|
||||
jobs:
|
||||
golangci:
|
||||
name: lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
version: v1.32.2
|
||||
args: --timeout 5m
|
||||
29
.golangci.yml
Normal file
29
.golangci.yml
Normal file
@ -0,0 +1,29 @@
|
||||
# Golang CI pipeline configuration
|
||||
linters:
|
||||
disable-all: true
|
||||
|
||||
# Run golangci-lint.yml linters to see the list of all linters
|
||||
# Please keep them sorted alphabetically
|
||||
enable:
|
||||
- bodyclose
|
||||
- deadcode
|
||||
- depguard
|
||||
# - dogsled
|
||||
# - errcheck
|
||||
# - goconst
|
||||
- goimports
|
||||
# - golint
|
||||
- goprintffuncname
|
||||
# - gosimple
|
||||
- govet
|
||||
# - ineffassign
|
||||
- misspell
|
||||
# - nakedret
|
||||
- nolintlint
|
||||
- rowserrcheck
|
||||
- structcheck
|
||||
- stylecheck
|
||||
- typecheck
|
||||
- unconvert
|
||||
- varcheck
|
||||
- whitespace
|
||||
@ -36,13 +36,12 @@ Will load the shell completion code.
|
||||
|
||||
// Generate the appropriate autocompletion file
|
||||
func Generate(cmd *cobra.Command, args []string) error {
|
||||
|
||||
// Check the passed in arguments
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("Shell not specified. ex. kompose completion [bash|zsh]")
|
||||
return fmt.Errorf("shell not specified. ex. kompose completion [bash|zsh]")
|
||||
}
|
||||
if len(args) > 1 {
|
||||
return fmt.Errorf("Too many arguments. Expected only the shell type. ex. kompose completion [bash|zsh]")
|
||||
return fmt.Errorf("too many arguments. Expected only the shell type. ex. kompose completion [bash|zsh]")
|
||||
}
|
||||
shell := args[0]
|
||||
|
||||
@ -70,7 +69,6 @@ func init() {
|
||||
in order to generate ZSH completion support.
|
||||
*/
|
||||
func runCompletionZsh(out io.Writer, kompose *cobra.Command) error {
|
||||
|
||||
zshInitialization := `
|
||||
__kompose_bash_source() {
|
||||
alias shopt=':'
|
||||
|
||||
@ -108,7 +108,6 @@ var convertCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
// Automatically grab environment variables
|
||||
viper.AutomaticEnv()
|
||||
|
||||
|
||||
@ -54,7 +54,6 @@ var inputFormat = "compose"
|
||||
|
||||
// ValidateFlags validates all command line flags
|
||||
func ValidateFlags(bundle string, args []string, cmd *cobra.Command, opt *kobject.ConvertOptions) {
|
||||
|
||||
// Check to see if the "file" has changed from the default flag value
|
||||
isFileSet := cmd.Flags().Lookup("file").Changed
|
||||
|
||||
@ -169,7 +168,6 @@ func ValidateComposeFile(opt *kobject.ConvertOptions) {
|
||||
}
|
||||
|
||||
func validateControllers(opt *kobject.ConvertOptions) {
|
||||
|
||||
singleOutput := len(opt.OutFile) != 0 || opt.OutFile == "-" || opt.ToStdout
|
||||
if opt.Provider == ProviderKubernetes {
|
||||
// create deployment by default if no controller has been set
|
||||
@ -191,7 +189,6 @@ func validateControllers(opt *kobject.ConvertOptions) {
|
||||
log.Fatalf("Error: only one kind of Kubernetes resource can be generated when --out or --stdout is specified")
|
||||
}
|
||||
}
|
||||
|
||||
} else if opt.Provider == ProviderOpenshift {
|
||||
// create deploymentconfig by default if no controller has been set
|
||||
if !opt.CreateDeploymentConfig {
|
||||
@ -214,7 +211,6 @@ func validateControllers(opt *kobject.ConvertOptions) {
|
||||
|
||||
// Convert transforms docker compose or dab file to k8s objects
|
||||
func Convert(opt kobject.ConvertOptions) {
|
||||
|
||||
validateControllers(&opt)
|
||||
|
||||
// loader parses input from file into komposeObject.
|
||||
|
||||
@ -211,7 +211,6 @@ func (s *ServiceConfig) GetConfigMapKeyFromMeta(name string) (string, error) {
|
||||
}
|
||||
|
||||
return filepath.Base(config.File), nil
|
||||
|
||||
}
|
||||
|
||||
// GetKubernetesUpdateStrategy from compose update_config
|
||||
@ -225,7 +224,6 @@ func (s *ServiceConfig) GetKubernetesUpdateStrategy() *v1.RollingUpdateDeploymen
|
||||
if config.Parallelism != nil {
|
||||
v := intstr.FromInt(cast.ToInt(*config.Parallelism))
|
||||
r.MaxUnavailable = &v
|
||||
|
||||
}
|
||||
|
||||
v := intstr.FromInt(0)
|
||||
@ -243,7 +241,6 @@ func (s *ServiceConfig) GetKubernetesUpdateStrategy() *v1.RollingUpdateDeploymen
|
||||
return &r
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// GetOSUpdateStrategy ...
|
||||
|
||||
@ -197,7 +197,6 @@ func (b *Bundle) LoadFile(files []string) (kobject.KomposeObject, error) {
|
||||
}
|
||||
|
||||
for name, service := range bundle.Services {
|
||||
|
||||
serviceConfig := kobject.ServiceConfig{}
|
||||
serviceConfig.Command = service.Command
|
||||
serviceConfig.Args = service.Args
|
||||
|
||||
@ -83,5 +83,4 @@ func TestUnsupportedKeys(t *testing.T) {
|
||||
t.Errorf("ERROR: Expecting unsupported keys: ['%s']. Got: ['%s']", strings.Join(test.expectedUnsupportedKeys, "', '"), strings.Join(keys, "', '"))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -42,7 +42,6 @@ type Compose struct {
|
||||
// list of all unsupported keys are stored in unsupportedKey variable
|
||||
// returns list of unsupported YAML keys from docker-compose
|
||||
func checkUnsupportedKey(composeProject *project.Project) []string {
|
||||
|
||||
// list of all unsupported keys for this loader
|
||||
// this is map to make searching for keys easier
|
||||
// to make sure that unsupported key is not going to be reported twice
|
||||
@ -132,7 +131,6 @@ func checkUnsupportedKey(composeProject *project.Project) []string {
|
||||
if !findUnsupportedLinksFlag {
|
||||
continue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
keysFound = append(keysFound, yamlTagName)
|
||||
@ -146,7 +144,6 @@ func checkUnsupportedKey(composeProject *project.Project) []string {
|
||||
|
||||
// LoadFile loads a compose file into KomposeObject
|
||||
func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
|
||||
|
||||
// Load the json / yaml file in order to get the version value
|
||||
var version string
|
||||
|
||||
@ -183,9 +180,8 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
|
||||
}
|
||||
return komposeObject, nil
|
||||
default:
|
||||
return kobject.KomposeObject{}, fmt.Errorf("Version %s of Docker Compose is not supported. Please use version 1, 2 or 3", version)
|
||||
return kobject.KomposeObject{}, fmt.Errorf("version %s of Docker Compose is not supported. Please use version 1, 2 or 3", version)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func getVersionFromFile(file string) (string, error) {
|
||||
|
||||
@ -34,10 +34,6 @@ import (
|
||||
api "k8s.io/api/core/v1"
|
||||
)
|
||||
|
||||
func durationPtr(value time.Duration) *time.Duration {
|
||||
return &value
|
||||
}
|
||||
|
||||
func durationTypesPtr(value time.Duration) *types.Duration {
|
||||
target := types.Duration(value)
|
||||
return &target
|
||||
@ -85,7 +81,6 @@ func TestLoadV3Volumes(t *testing.T) {
|
||||
if output[0] != expected {
|
||||
t.Errorf("Expected %s, got %s", expected, output[0])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestLoadV3Ports(t *testing.T) {
|
||||
@ -400,7 +395,6 @@ func TestUnsupportedKeys(t *testing.T) {
|
||||
t.Errorf("ERROR: Expecting unsupported keys: ['%s']. Got: ['%s']", strings.Join(test.expectedUnsupportedKeys, "', '"), strings.Join(keys, "', '"))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNormalizeServiceNames(t *testing.T) {
|
||||
@ -463,14 +457,11 @@ func TestCheckLabelsPorts(t *testing.T) {
|
||||
err = checkLabelsPorts(testcase.noOfPort, testcase.labels, testcase.svcName)
|
||||
if testcase.expectError && err == nil {
|
||||
t.Log("Expected error, got ", err)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPlacementCustomLabels(t *testing.T) {
|
||||
|
||||
placement := types.Placement{
|
||||
Constraints: []string{
|
||||
"node.labels.something == anything",
|
||||
@ -484,5 +475,4 @@ func TestCheckPlacementCustomLabels(t *testing.T) {
|
||||
if output["something"] != expected["something"] {
|
||||
t.Errorf("Expected %s, got %s", expected, output)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -168,5 +168,4 @@ func ReadFile(fileName string) ([]byte, error) {
|
||||
return StdinData, nil
|
||||
}
|
||||
return ioutil.ReadFile(fileName)
|
||||
|
||||
}
|
||||
|
||||
@ -41,7 +41,6 @@ import (
|
||||
// Parse Docker Compose with libcompose (only supports v1 and v2). Eventually we will
|
||||
// switch to using only libcompose once v3 is supported.
|
||||
func parseV1V2(files []string) (kobject.KomposeObject, error) {
|
||||
|
||||
// Gather the appropriate context for parsing
|
||||
context := &project.Context{}
|
||||
context.ComposeFiles = files
|
||||
@ -169,7 +168,6 @@ func loadPorts(composePorts []string, expose []string) ([]kobject.Ports, error)
|
||||
|
||||
// Uses libcompose's APIProject type and converts it to a Kompose object for us to understand
|
||||
func libComposeToKomposeMapping(composeObject *project.Project) (kobject.KomposeObject, error) {
|
||||
|
||||
// Initialize what's going to be returned
|
||||
komposeObject := kobject.KomposeObject{
|
||||
ServiceConfigs: make(map[string]kobject.ServiceConfig),
|
||||
@ -336,7 +334,6 @@ func retrieveVolume(svcName string, komposeObject kobject.KomposeObject) (volume
|
||||
cv.PVCName = dv.PVCName
|
||||
}
|
||||
volume = append(volume, cv)
|
||||
|
||||
}
|
||||
// iterating over dependent volumes
|
||||
for _, dv := range dVols {
|
||||
@ -366,7 +363,6 @@ func checkVolDependent(dv kobject.Volumes, volume []kobject.Volumes) bool {
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// ParseVols parse volumes
|
||||
@ -409,7 +405,6 @@ func getGroupAdd(group []string) ([]int64, error) {
|
||||
return nil, errors.Wrap(err, "unable to get group_add key")
|
||||
}
|
||||
groupAdd = append(groupAdd, int64(j))
|
||||
|
||||
}
|
||||
return groupAdd, nil
|
||||
}
|
||||
|
||||
16
pkg/loader/compose/v3.go
Executable file → Normal file
16
pkg/loader/compose/v3.go
Executable file → Normal file
@ -60,7 +60,6 @@ func buildEnvironment() (map[string]string, error) {
|
||||
// v3 of Docker Compose into a suitable format. In this case, whatever is returned
|
||||
// by docker/cli's ServiceConfig
|
||||
func parseV3(files []string) (kobject.KomposeObject, error) {
|
||||
|
||||
// In order to get V3 parsing to work, we have to go through some preliminary steps
|
||||
// for us to hack up github.com/docker/cli in order to correctly convert to a kobject.KomposeObject
|
||||
|
||||
@ -163,7 +162,6 @@ func loadV3Placement(constraints []string) map[string]string {
|
||||
// TODO: Refactor it similar to loadV3Ports
|
||||
// See: https://docs.docker.com/compose/compose-file/#long-syntax-3
|
||||
func loadV3Volumes(volumes []types.ServiceVolumeConfig) []string {
|
||||
|
||||
var volArray []string
|
||||
for _, vol := range volumes {
|
||||
// There will *always* be Source when parsing
|
||||
@ -197,11 +195,10 @@ func loadV3Ports(ports []types.ServicePortConfig, expose []string) []kobject.Por
|
||||
HostPort: int32(port.Published),
|
||||
ContainerPort: int32(port.Target),
|
||||
HostIP: "",
|
||||
Protocol: api.Protocol(strings.ToUpper(string(port.Protocol))),
|
||||
Protocol: api.Protocol(strings.ToUpper(port.Protocol)),
|
||||
})
|
||||
|
||||
exist[cast.ToString(port.Target)+strings.ToUpper(string(port.Protocol))] = true
|
||||
|
||||
exist[cast.ToString(port.Target)+strings.ToUpper(port.Protocol)] = true
|
||||
}
|
||||
|
||||
if expose != nil {
|
||||
@ -233,7 +230,6 @@ func loadV3Ports(ports []types.ServicePortConfig, expose []string) []kobject.Por
|
||||
a Kubernetes-compatible format.
|
||||
*/
|
||||
func parseHealthCheckReadiness(labels types.Labels) (kobject.HealthCheck, error) {
|
||||
|
||||
// initialize with CMD as default to not break at return (will be ignored if no test is informed)
|
||||
test := []string{"CMD"}
|
||||
var timeout, interval, retries, startPeriod int32
|
||||
@ -293,7 +289,6 @@ func parseHealthCheckReadiness(labels types.Labels) (kobject.HealthCheck, error)
|
||||
a Kubernetes-compatible format.
|
||||
*/
|
||||
func parseHealthCheck(composeHealthCheck types.HealthCheckConfig) (kobject.HealthCheck, error) {
|
||||
|
||||
var timeout, interval, retries, startPeriod int32
|
||||
|
||||
// Here we convert the timeout from 1h30s (example) to 36030 seconds.
|
||||
@ -336,7 +331,6 @@ func parseHealthCheck(composeHealthCheck types.HealthCheckConfig) (kobject.Healt
|
||||
}
|
||||
|
||||
func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.KomposeObject, error) {
|
||||
|
||||
// Step 1. Initialize what's going to be returned
|
||||
komposeObject := kobject.KomposeObject{
|
||||
ServiceConfigs: make(map[string]kobject.ServiceConfig),
|
||||
@ -348,7 +342,6 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
||||
// Here we "clean up" the service configuration so we return something that includes
|
||||
// all relevant information as well as avoid the unsupported keys as well.
|
||||
for _, composeServiceConfig := range composeObject.Services {
|
||||
|
||||
// Standard import
|
||||
// No need to modify before importation
|
||||
name := composeServiceConfig.Name
|
||||
@ -500,7 +493,6 @@ func parseV3Network(composeServiceConfig *types.ServiceConfig, serviceConfig *ko
|
||||
|
||||
func parseV3Resources(composeServiceConfig *types.ServiceConfig, serviceConfig *kobject.ServiceConfig) error {
|
||||
if (composeServiceConfig.Deploy.Resources != types.Resources{}) {
|
||||
|
||||
// memory:
|
||||
// TODO: Refactor yaml.MemStringorInt in kobject.go to int64
|
||||
// cpu:
|
||||
@ -533,7 +525,6 @@ func parseV3Resources(composeServiceConfig *types.ServiceConfig, serviceConfig *
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func parseV3Environment(composeServiceConfig *types.ServiceConfig, serviceConfig *kobject.ServiceConfig) {
|
||||
@ -648,7 +639,7 @@ func getV3VolumeLabels(name string, volumes *map[string]types.VolumeConfig) (str
|
||||
|
||||
func mergeComposeObject(oldCompose *types.Config, newCompose *types.Config) (*types.Config, error) {
|
||||
if oldCompose == nil || newCompose == nil {
|
||||
return nil, fmt.Errorf("Merge multiple compose error, compose config is nil")
|
||||
return nil, fmt.Errorf("merge multiple compose error, compose config is nil")
|
||||
}
|
||||
oldComposeServiceNameMap := make(map[string]int, len(oldCompose.Services))
|
||||
for index, service := range oldCompose.Services {
|
||||
@ -895,7 +886,6 @@ func checkUnsupportedKeyForV3(composeObject *types.Config) []string {
|
||||
|
||||
if service.CredentialSpec.Registry != "" || service.CredentialSpec.File != "" {
|
||||
keysFound = append(keysFound, "credential_spec")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -40,9 +40,8 @@ func GetLoader(format string) (Loader, error) {
|
||||
case "compose":
|
||||
l = new(compose.Compose)
|
||||
default:
|
||||
return nil, fmt.Errorf("Input file format %s is not supported", format)
|
||||
return nil, fmt.Errorf("input file format %s is not supported", format)
|
||||
}
|
||||
|
||||
return l, nil
|
||||
|
||||
}
|
||||
|
||||
@ -111,7 +111,6 @@ home:
|
||||
|
||||
// Check if given path is a directory
|
||||
func isDir(name string) (bool, error) {
|
||||
|
||||
// Open file to get stat later
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
@ -156,7 +155,6 @@ func objectToRaw(object runtime.Object) runtime.RawExtension {
|
||||
r.Raw = bytes
|
||||
|
||||
return r
|
||||
|
||||
}
|
||||
|
||||
// PrintList will take the data converted and decide on the commandline attributes given
|
||||
@ -203,7 +201,6 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
|
||||
}
|
||||
|
||||
list.Items = append(list.Items, objectToRaw(versionedObject))
|
||||
|
||||
}
|
||||
// version list itself
|
||||
listVersion := metav1.GroupVersion{Group: "", Version: "v1"}
|
||||
@ -264,7 +261,6 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
|
||||
// Use reflect to access ObjectMeta struct inside runtime.Object.
|
||||
// cast it to correct type - api.ObjectMeta
|
||||
objectMeta = val.FieldByName("ObjectMeta").Interface().(metav1.ObjectMeta)
|
||||
|
||||
}
|
||||
|
||||
file, err = transformer.Print(objectMeta.Name, finalDirName, strings.ToLower(typeMeta.Kind), data, opt.ToStdout, opt.GenerateJSON, f, opt.Provider)
|
||||
@ -341,7 +337,6 @@ func marshalWithIndent(o interface{}, indent int) ([]byte, error) {
|
||||
// Convert object to versioned object
|
||||
// if groupVersion is empty (metav1.GroupVersion{}), use version from original object (obj)
|
||||
func convertToVersion(obj runtime.Object, groupVersion metav1.GroupVersion) (runtime.Object, error) {
|
||||
|
||||
// ignore unstruct object
|
||||
if _, ok := obj.(*unstructured.Unstructured); ok {
|
||||
return obj, nil
|
||||
@ -445,7 +440,6 @@ func (k *Kubernetes) CreateHeadlessService(name string, service kobject.ServiceC
|
||||
|
||||
// UpdateKubernetesObjects loads configurations to k8s objects
|
||||
func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.ServiceConfig, opt kobject.ConvertOptions, objects *[]runtime.Object) error {
|
||||
|
||||
// Configure the environment variables.
|
||||
envs, err := k.ConfigEnvs(name, service, opt)
|
||||
if err != nil {
|
||||
@ -464,7 +458,6 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
|
||||
volumes = append(volumes, TmpVolumes...)
|
||||
|
||||
volumesMount = append(volumesMount, TmpVolumesMount...)
|
||||
|
||||
}
|
||||
|
||||
if pvc != nil {
|
||||
@ -593,7 +586,6 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
|
||||
} else {
|
||||
securityContext.RunAsUser = &uid
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//set capabilities if it is not empty
|
||||
@ -692,7 +684,6 @@ func TranslatePodResource(service *kobject.ServiceConfig, template *api.PodTempl
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
// GetImagePullPolicy get image pull settings
|
||||
@ -709,7 +700,6 @@ func GetImagePullPolicy(name, policy string) (api.PullPolicy, error) {
|
||||
return "", errors.New("Unknown image-pull-policy " + policy + " for service " + name)
|
||||
}
|
||||
return "", nil
|
||||
|
||||
}
|
||||
|
||||
// GetRestartPolicy ...
|
||||
@ -764,7 +754,6 @@ func (k *Kubernetes) RemoveDupObjects(objs *[]runtime.Object) {
|
||||
} else {
|
||||
result = append(result, obj)
|
||||
}
|
||||
|
||||
}
|
||||
*objs = result
|
||||
}
|
||||
@ -842,5 +831,4 @@ func FormatFileName(name string) string {
|
||||
func FormatContainerName(name string) string {
|
||||
name = strings.Replace(name, "_", "-", -1)
|
||||
return name
|
||||
|
||||
}
|
||||
|
||||
@ -34,7 +34,6 @@ import (
|
||||
Test the creation of a service
|
||||
*/
|
||||
func TestCreateService(t *testing.T) {
|
||||
|
||||
// An example service
|
||||
service := kobject.ServiceConfig{
|
||||
ContainerName: "name",
|
||||
@ -78,7 +77,6 @@ func TestCreateService(t *testing.T) {
|
||||
Test the creation of a service with a memory limit and reservation
|
||||
*/
|
||||
func TestCreateServiceWithMemLimit(t *testing.T) {
|
||||
|
||||
// An example service
|
||||
service := kobject.ServiceConfig{
|
||||
ContainerName: "name",
|
||||
@ -131,7 +129,6 @@ func TestCreateServiceWithMemLimit(t *testing.T) {
|
||||
Test the creation of a service with a cpu limit and reservation
|
||||
*/
|
||||
func TestCreateServiceWithCPULimit(t *testing.T) {
|
||||
|
||||
// An example service
|
||||
service := kobject.ServiceConfig{
|
||||
ContainerName: "name",
|
||||
@ -185,7 +182,6 @@ func TestCreateServiceWithCPULimit(t *testing.T) {
|
||||
The expected result is that Kompose will set user in PodSpec
|
||||
*/
|
||||
func TestCreateServiceWithServiceUser(t *testing.T) {
|
||||
|
||||
// An example service
|
||||
service := kobject.ServiceConfig{
|
||||
ContainerName: "name",
|
||||
@ -226,7 +222,6 @@ func TestCreateServiceWithServiceUser(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTransformWithPid(t *testing.T) {
|
||||
@ -392,7 +387,6 @@ func TestServiceWithHealthCheck(t *testing.T) {
|
||||
if err := testutils.CheckForHealthCheckLivenessAndReadiness(objects); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TestServiceWithoutPort this tests if Headless Service is created for services without Port.
|
||||
@ -415,7 +409,6 @@ func TestServiceWithoutPort(t *testing.T) {
|
||||
if err := testutils.CheckForHeadless(objects); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Tests if deployment strategy is being set to Recreate when volumes are
|
||||
|
||||
30
pkg/transformer/kubernetes/kubernetes.go
Executable file → Normal file
30
pkg/transformer/kubernetes/kubernetes.go
Executable file → Normal file
@ -85,7 +85,6 @@ func (k *Kubernetes) CheckUnsupportedKey(komposeObject *kobject.KomposeObject, u
|
||||
for _, f := range s.Fields() {
|
||||
// Check if given key is among unsupported keys, and skip it if we already saw this key
|
||||
if alreadySaw, ok := unsupportedKey[f.Name()]; ok && !alreadySaw {
|
||||
|
||||
if f.IsExported() && !f.IsZero() {
|
||||
// IsZero returns false for empty array/slice ([])
|
||||
// this check if field is Slice, and then it checks its size
|
||||
@ -108,7 +107,6 @@ func (k *Kubernetes) CheckUnsupportedKey(komposeObject *kobject.KomposeObject, u
|
||||
|
||||
// InitPodSpec creates the pod specification
|
||||
func (k *Kubernetes) InitPodSpec(name string, image string, pullSecret string) api.PodSpec {
|
||||
|
||||
if image == "" {
|
||||
image = name
|
||||
}
|
||||
@ -175,7 +173,6 @@ func (k *Kubernetes) InitPodSpecWithConfigMap(name string, image string, service
|
||||
SubPath: subPath,
|
||||
})
|
||||
volumes = append(volumes, cmVol)
|
||||
|
||||
}
|
||||
|
||||
pod := api.PodSpec{
|
||||
@ -211,7 +208,6 @@ func (k *Kubernetes) InitSvc(name string, service kobject.ServiceConfig) *api.Se
|
||||
|
||||
// InitConfigMapForEnv initializes a ConfigMap object
|
||||
func (k *Kubernetes) InitConfigMapForEnv(name string, service kobject.ServiceConfig, opt kobject.ConvertOptions, envFile string) *api.ConfigMap {
|
||||
|
||||
envs, err := GetEnvsFromFile(envFile, opt)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to retrieve env file: %s", err)
|
||||
@ -333,7 +329,6 @@ func (k *Kubernetes) InitConfigMapFromFile(name string, service kobject.ServiceC
|
||||
|
||||
// InitD initializes Kubernetes Deployment object
|
||||
func (k *Kubernetes) InitD(name string, service kobject.ServiceConfig, replicas int) *appsv1.Deployment {
|
||||
|
||||
var podSpec api.PodSpec
|
||||
if len(service.Configs) > 0 {
|
||||
podSpec = k.InitPodSpecWithConfigMap(name, service.Image, service)
|
||||
@ -401,7 +396,6 @@ func (k *Kubernetes) InitDS(name string, service kobject.ServiceConfig) *appsv1.
|
||||
}
|
||||
|
||||
func (k *Kubernetes) initIngress(name string, service kobject.ServiceConfig, port int32) *networkingv1beta1.Ingress {
|
||||
|
||||
hosts := regexp.MustCompile("[ ,]*,[ ,]*").Split(service.ExposeService, -1)
|
||||
|
||||
ingress := &networkingv1beta1.Ingress{
|
||||
@ -492,7 +486,6 @@ func (k *Kubernetes) CreateSecrets(komposeObject kobject.KomposeObject) ([]*api.
|
||||
}
|
||||
}
|
||||
return objects, nil
|
||||
|
||||
}
|
||||
|
||||
// CreatePVC initializes PersistentVolumeClaim
|
||||
@ -557,7 +550,6 @@ func (k *Kubernetes) ConfigPorts(name string, service kobject.ServiceConfig) []a
|
||||
})
|
||||
}
|
||||
exist[string(port.ContainerPort)+string(port.Protocol)] = true
|
||||
|
||||
}
|
||||
|
||||
return ports
|
||||
@ -592,7 +584,6 @@ func (k *Kubernetes) ConfigLBServicePorts(name string, service kobject.ServiceCo
|
||||
}
|
||||
}
|
||||
return tcpPorts, udpPorts
|
||||
|
||||
}
|
||||
|
||||
// ConfigServicePorts configure the container service ports.
|
||||
@ -793,7 +784,6 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
|
||||
var count int
|
||||
//iterating over array of `Vols` struct as it contains all necessary information about volumes
|
||||
for _, volume := range service.Volumes {
|
||||
|
||||
// check if ro/rw mode is defined, default rw
|
||||
readonly := len(volume.Mode) > 0 && volume.Mode == "ro"
|
||||
|
||||
@ -842,7 +832,6 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
|
||||
volMount.SubPath = volsource.ConfigMap.Items[0].Path
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
volsource = k.ConfigPVCVolumeSource(volumeName, readonly)
|
||||
if volume.VFrom == "" {
|
||||
@ -866,7 +855,6 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
|
||||
|
||||
PVCs = append(PVCs, createdPVC)
|
||||
}
|
||||
|
||||
}
|
||||
volumeMounts = append(volumeMounts, volMount)
|
||||
|
||||
@ -880,7 +868,6 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
|
||||
if len(volume.Host) > 0 && (!useHostPath && !useConfigMap) {
|
||||
log.Warningf("Volume mount on the host %q isn't supported - ignoring path on the host", volume.Host)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return volumeMounts, volumes, PVCs, cms, nil
|
||||
@ -894,14 +881,12 @@ func (k *Kubernetes) ConfigEmptyVolumeSource(key string) *api.VolumeSource {
|
||||
return &api.VolumeSource{
|
||||
EmptyDir: &api.EmptyDirVolumeSource{Medium: api.StorageMediumMemory},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//if key is volume
|
||||
return &api.VolumeSource{
|
||||
EmptyDir: &api.EmptyDirVolumeSource{},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ConfigConfigMapVolumeSource config a configmap to use as volume source
|
||||
@ -925,7 +910,6 @@ func (k *Kubernetes) ConfigConfigMapVolumeSource(cmName string, targetPath strin
|
||||
return &api.VolumeSource{
|
||||
ConfigMap: &s,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ConfigHostPathVolumeSource is a helper function to create a HostPath api.VolumeSource
|
||||
@ -956,7 +940,6 @@ func (k *Kubernetes) ConfigPVCVolumeSource(name string, readonly bool) *api.Volu
|
||||
|
||||
// ConfigEnvs configures the environment variables.
|
||||
func (k *Kubernetes) ConfigEnvs(name string, service kobject.ServiceConfig, opt kobject.ConvertOptions) ([]api.EnvVar, error) {
|
||||
|
||||
envs := transformer.EnvSort{}
|
||||
|
||||
keysFromEnvFile := make(map[string]bool)
|
||||
@ -965,11 +948,9 @@ func (k *Kubernetes) ConfigEnvs(name string, service kobject.ServiceConfig, opt
|
||||
// already specified
|
||||
|
||||
if len(service.EnvFile) > 0 {
|
||||
|
||||
// Load each env_file
|
||||
|
||||
for _, file := range service.EnvFile {
|
||||
|
||||
envName := FormatEnvName(file)
|
||||
|
||||
// Load environment variables from file
|
||||
@ -1003,7 +984,6 @@ func (k *Kubernetes) ConfigEnvs(name string, service kobject.ServiceConfig, opt
|
||||
Value: v.Value,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Stable sorts data while keeping the original order of equal elements
|
||||
@ -1033,7 +1013,6 @@ func (k *Kubernetes) CreateKubernetesObjects(name string, service kobject.Servic
|
||||
} else if opt.Controller != "daemonset" {
|
||||
log.Warnf("Global deploy mode service is best converted to daemonset, now it convert to %s", opt.Controller)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Resolve labels first
|
||||
@ -1102,7 +1081,6 @@ func (k *Kubernetes) InitPod(name string, service kobject.ServiceConfig) *api.Po
|
||||
|
||||
// CreateNetworkPolicy initializes Network policy
|
||||
func (k *Kubernetes) CreateNetworkPolicy(name string, networkName string) (*networkingv1.NetworkPolicy, error) {
|
||||
|
||||
str := "true"
|
||||
np := &networkingv1.NetworkPolicy{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
@ -1133,7 +1111,6 @@ func (k *Kubernetes) CreateNetworkPolicy(name string, networkName string) (*netw
|
||||
// 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, error) {
|
||||
|
||||
// this will hold all the converted data
|
||||
var allobjects []runtime.Object
|
||||
|
||||
@ -1160,7 +1137,6 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
|
||||
// Check that there's actually a Build key
|
||||
// Lastly, we must have an Image name to continue
|
||||
if opt.Build == "local" && opt.InputFiles != nil && service.Build != "" {
|
||||
|
||||
// If there's no "image" key, use the name of the container that's built
|
||||
if service.Image == "" {
|
||||
service.Image = name
|
||||
@ -1213,7 +1189,6 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
|
||||
objects = append(objects, k.initIngress(name, service, svc.Spec.Ports[0].Port))
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if service.ServiceType == "Headless" {
|
||||
svc := k.CreateHeadlessService(name, service, objects)
|
||||
@ -1229,9 +1204,7 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
|
||||
}
|
||||
|
||||
if len(service.Network) > 0 {
|
||||
|
||||
for _, net := range service.Network {
|
||||
|
||||
log.Infof("Network %s is detected at Source, shall be converted to equivalent NetworkPolicy at Destination", net)
|
||||
np, err := k.CreateNetworkPolicy(name, net)
|
||||
|
||||
@ -1239,13 +1212,10 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
|
||||
return nil, errors.Wrapf(err, "Unable to create Network Policy for network %v for service %v", net, name)
|
||||
}
|
||||
objects = append(objects, np)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
allobjects = append(allobjects, objects...)
|
||||
|
||||
}
|
||||
|
||||
// sort all object so Services are first
|
||||
|
||||
@ -216,7 +216,6 @@ func checkMeta(config kobject.ServiceConfig, meta metav1.ObjectMeta, expectedNam
|
||||
}
|
||||
|
||||
func TestKomposeConvertIngress(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
komposeObject kobject.KomposeObject
|
||||
opt kobject.ConvertOptions
|
||||
@ -227,7 +226,6 @@ func TestKomposeConvertIngress(t *testing.T) {
|
||||
}
|
||||
|
||||
for name, test := range testCases {
|
||||
|
||||
var expectedHost string
|
||||
|
||||
t.Log("Test case:", name)
|
||||
@ -267,7 +265,6 @@ func TestKomposeConvertIngress(t *testing.T) {
|
||||
}
|
||||
if ing.Spec.Rules[0].Host != expectedHost {
|
||||
t.Errorf("Expected Rules[0].Host to be %s, got %s instead", expectedHost, ing.Spec.Rules[0].Host)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -332,10 +329,8 @@ func TestKomposeConvert(t *testing.T) {
|
||||
t.Errorf("Expected %d replicas, got %d", replicas, d.Spec.Replicas)
|
||||
}
|
||||
} else {
|
||||
|
||||
if (int)(*d.Spec.Replicas) != newServiceConfig().Replicas {
|
||||
t.Errorf("Expected %d replicas, got %d", newServiceConfig().Replicas, d.Spec.Replicas)
|
||||
|
||||
}
|
||||
}
|
||||
foundD = true
|
||||
@ -365,18 +360,14 @@ func TestKomposeConvert(t *testing.T) {
|
||||
t.Errorf("Expected %d replicas, got %d", replicas, d.Spec.Replicas)
|
||||
}
|
||||
} else {
|
||||
|
||||
if (int)(*d.Spec.Replicas) != newServiceConfig().Replicas {
|
||||
t.Errorf("Expected %d replicas, got %d", newServiceConfig().Replicas, d.Spec.Replicas)
|
||||
|
||||
}
|
||||
}
|
||||
foundD = true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if test.opt.CreateDS {
|
||||
if ds, ok := obj.(*appsv1.DaemonSet); ok {
|
||||
@ -413,11 +404,8 @@ func TestKomposeConvert(t *testing.T) {
|
||||
}
|
||||
foundDS = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TODO: k8s & openshift transformer is now separated; either separate the test or combine the transformer
|
||||
@ -481,7 +469,6 @@ func TestConvertRestartOptions(t *testing.T) {
|
||||
|
||||
for _, obj := range objs {
|
||||
if pod, ok := obj.(*api.Pod); ok {
|
||||
|
||||
if pod.Spec.RestartPolicy != test.restartPolicy {
|
||||
t.Errorf("Expected restartPolicy as %s, got %#v", test.restartPolicy, pod.Spec.RestartPolicy)
|
||||
}
|
||||
@ -493,7 +480,6 @@ func TestConvertRestartOptions(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRestartOnFailure(t *testing.T) {
|
||||
|
||||
kobjectWithRestartOnFailure := newKomposeObject()
|
||||
serviceConfig := kobjectWithRestartOnFailure.ServiceConfigs["app"]
|
||||
serviceConfig.Restart = "on-failure"
|
||||
@ -538,7 +524,6 @@ func TestConfigTmpfs(t *testing.T) {
|
||||
if resultVolumeMount[0].Name != "foo-tmpfs0" || resultVolume[0].EmptyDir.Medium != "Memory" {
|
||||
t.Fatalf("Tmpfs not found")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestConfigCapabilities(t *testing.T) {
|
||||
|
||||
@ -79,7 +79,6 @@ func (o *OpenShift) initImageStream(name string, service kobject.ServiceConfig,
|
||||
},
|
||||
ImportPolicy: importPolicy,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
is := &imageapi.ImageStream{
|
||||
@ -303,7 +302,6 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
// Check that there's actually a Build key
|
||||
// Lastly, we must have an Image name to continue
|
||||
if opt.Build == "local" && opt.InputFiles != nil && service.Build != "" {
|
||||
|
||||
// If there's no "image" key, use the name of the container that's built
|
||||
if service.Image == "" {
|
||||
service.Image = name
|
||||
@ -326,7 +324,6 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
log.Fatalf("Unable to push Docker image for service %v: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Generate pod only and nothing more
|
||||
@ -349,7 +346,6 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
// buildconfig needs to be added to objects after imagestream because of this Openshift bug: https://github.com/openshift/origin/issues/4518
|
||||
// Generate BuildConfig if the parameter has been passed
|
||||
if service.Build != "" && opt.Build == "build-config" {
|
||||
|
||||
// Get the compose file directory
|
||||
composeFileDir, err = transformer.GetComposeFileDir(opt.InputFiles)
|
||||
if err != nil {
|
||||
@ -391,7 +387,6 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
// Log what we're doing
|
||||
log.Infof("Buildconfig using %s::%s as source.", buildRepo, buildBranch)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if o.PortsExist(service) {
|
||||
@ -411,7 +406,6 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
objects = append(objects, o.initRoute(name, service, svc.Spec.Ports[0].Port))
|
||||
}
|
||||
}
|
||||
|
||||
} else if service.ServiceType == "Headless" {
|
||||
svc := o.CreateHeadlessService(name, service, objects)
|
||||
objects = append(objects, svc)
|
||||
|
||||
@ -97,7 +97,6 @@ func TestInitDeploymentConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKomposeConvertRoute(t *testing.T) {
|
||||
|
||||
o := OpenShift{}
|
||||
name := "app"
|
||||
sc := newServiceConfig()
|
||||
@ -317,7 +316,6 @@ func TestInitBuildConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
||||
bc, err := initBuildConfig(serviceName, test.ServiceConfig, repo, branch)
|
||||
if err != nil {
|
||||
t.Error(errors.Wrap(err, "initBuildConfig failed"))
|
||||
@ -366,11 +364,9 @@ func TestServiceWithoutPort(t *testing.T) {
|
||||
if err := testutils.CheckForHeadless(objects); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRestartOnFailure(t *testing.T) {
|
||||
|
||||
service := kobject.ServiceConfig{
|
||||
Restart: "on-failure",
|
||||
}
|
||||
@ -398,7 +394,6 @@ func TestRestartOnFailure(t *testing.T) {
|
||||
t.Errorf("Expected an error, got %v instead", err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Tests if deployment strategy is being set to Recreate when volumes are
|
||||
|
||||
@ -28,7 +28,6 @@ func GetImageTag(image string) string {
|
||||
return p[1]
|
||||
}
|
||||
return "latest"
|
||||
|
||||
}
|
||||
|
||||
// GetAbsBuildContext returns build context relative to project root dir
|
||||
|
||||
@ -133,7 +133,6 @@ func ConfigLabels(name string) map[string]string {
|
||||
|
||||
// ConfigLabelsWithNetwork configures label and add Network Information in labels
|
||||
func ConfigLabelsWithNetwork(name string, net []string) map[string]string {
|
||||
|
||||
labels := map[string]string{}
|
||||
labels[Selector] = name
|
||||
|
||||
@ -153,12 +152,10 @@ func ConfigAllLabels(name string, service *kobject.ServiceConfig) map[string]str
|
||||
}
|
||||
}
|
||||
return base
|
||||
|
||||
}
|
||||
|
||||
// ConfigAnnotations configures annotations
|
||||
func ConfigAnnotations(service kobject.ServiceConfig) map[string]string {
|
||||
|
||||
annotations := map[string]string{}
|
||||
for key, value := range service.Annotations {
|
||||
annotations[key] = value
|
||||
@ -173,7 +170,6 @@ func ConfigAnnotations(service kobject.ServiceConfig) map[string]string {
|
||||
out, err := versionCmd.Output()
|
||||
if err != nil {
|
||||
errors.Wrap(err, "Failed to get kompose version")
|
||||
|
||||
}
|
||||
annotations["kompose.version"] = strings.Trim(string(out), " \n")
|
||||
|
||||
@ -205,7 +201,7 @@ func Print(name, path string, trailing string, data []byte, toStdout, generateJS
|
||||
} else {
|
||||
// Write content separately to each file
|
||||
file = filepath.Join(path, file)
|
||||
if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil {
|
||||
if err := ioutil.WriteFile(file, data, 0644); err != nil {
|
||||
return "", errors.Wrap(err, "Failed to write %s: "+trailing)
|
||||
}
|
||||
log.Printf("%s file %q created", formatProviderName(provider), file)
|
||||
@ -313,7 +309,6 @@ func BuildDockerImage(service kobject.ServiceConfig, name string) error {
|
||||
|
||||
// PushDockerImage pushes docker image
|
||||
func PushDockerImage(service kobject.ServiceConfig, serviceName string) error {
|
||||
|
||||
log.Debugf("Pushing Docker image '%s'", service.Image)
|
||||
|
||||
// Don't do anything if service.Image is blank, but at least WARN about it
|
||||
|
||||
@ -40,7 +40,6 @@ and image name and then builds the appropriate image. Tarball is utilized
|
||||
in order to make building easier.
|
||||
*/
|
||||
func (c *Build) BuildImage(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg) error {
|
||||
|
||||
log.Infof("Building image '%s' from directory '%s'", image, path.Base(source))
|
||||
|
||||
// Create a temporary file for tarball image packaging
|
||||
|
||||
@ -24,7 +24,6 @@ import (
|
||||
|
||||
// Client connects to Docker client on host
|
||||
func Client() (*docker.Client, error) {
|
||||
|
||||
var (
|
||||
err error
|
||||
client *docker.Client
|
||||
|
||||
Loading…
Reference in New Issue
Block a user