fix tests related to secrets (#1198)

1. fix the path used to read secret data
without this fix, the path to the compose file is added to the already
absolute path, hence readFile was unable to find the secrets file

2. fix the expected json format
mountPath includes the entire path including the name of the secret
This commit is contained in:
Chander G 2019-12-04 08:02:29 +05:30 committed by Hang Yan
parent 30736a3973
commit 356c64890c
6 changed files with 17 additions and 18 deletions

View File

@ -621,17 +621,6 @@ func GetEnvsFromFile(file string, opt kobject.ConvertOptions) (map[string]string
return envLoad, nil return envLoad, nil
} }
// GetSecretDataFromFile load secret content data
func GetSecretDataFromFile(file string, opt kobject.ConvertOptions) ([]byte, error) {
composeDir, err := transformer.GetComposeFileDir(opt.InputFiles)
if err != nil {
return nil, errors.Wrap(err, "Unable to load file context")
}
fileLocation := path.Join(composeDir, file)
return ioutil.ReadFile(fileLocation)
}
// TODO(hang): merge these two functions
// GetContentFromFile gets the content from the file.. // GetContentFromFile gets the content from the file..
func GetContentFromFile(file string, opt kobject.ConvertOptions) (string, error) { func GetContentFromFile(file string, opt kobject.ConvertOptions) (string, error) {
fileBytes, err := ioutil.ReadFile(file) fileBytes, err := ioutil.ReadFile(file)

View File

@ -45,6 +45,7 @@ import (
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/intstr" "k8s.io/kubernetes/pkg/util/intstr"
//"k8s.io/kubernetes/pkg/controller/daemon" //"k8s.io/kubernetes/pkg/controller/daemon"
"sort" "sort"
"strings" "strings"
@ -397,11 +398,12 @@ func (k *Kubernetes) CreateSecrets(komposeObject kobject.KomposeObject) ([]*api.
var objects []*api.Secret var objects []*api.Secret
for name, config := range komposeObject.Secrets { for name, config := range komposeObject.Secrets {
if config.File != "" { if config.File != "" {
data, err := GetSecretDataFromFile(config.File, k.Opt) dataString, err := GetContentFromFile(config.File, k.Opt)
if err != nil { if err != nil {
log.Fatal("unable to read secret from file: ", config.File) log.Fatal("unable to read secret from file: ", config.File)
return nil, err return nil, err
} }
data := []byte(dataString)
secret := &api.Secret{ secret := &api.Secret{
TypeMeta: unversioned.TypeMeta{ TypeMeta: unversioned.TypeMeta{
Kind: "Secret", Kind: "Secret",

View File

@ -39,6 +39,10 @@
"creationTimestamp": null, "creationTimestamp": null,
"labels": { "labels": {
"io.kompose.service": "redis" "io.kompose.service": "redis"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
} }
}, },
"spec": { "spec": {
@ -65,7 +69,7 @@
"volumeMounts": [ "volumeMounts": [
{ {
"name": "my_secret", "name": "my_secret",
"mountPath": "/run/secrets" "mountPath": "/run/secrets/my_secret"
} }
] ]
} }

View File

@ -90,7 +90,7 @@
"volumeMounts": [ "volumeMounts": [
{ {
"name": "my_secret", "name": "my_secret",
"mountPath": "/run/secrets" "mountPath": "/run/secrets/my_secret"
} }
] ]
} }

View File

@ -39,6 +39,10 @@
"creationTimestamp": null, "creationTimestamp": null,
"labels": { "labels": {
"io.kompose.service": "redis" "io.kompose.service": "redis"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
} }
}, },
"spec": { "spec": {
@ -76,11 +80,11 @@
"volumeMounts": [ "volumeMounts": [
{ {
"name": "my_secret", "name": "my_secret",
"mountPath": "/run/secrets" "mountPath": "/run/secrets/my_secret"
}, },
{ {
"name": "my_other_secret", "name": "my_other_secret",
"mountPath": "/run/secrets" "mountPath": "/run/secrets/my_other_secret"
} }
] ]
} }

View File

@ -101,11 +101,11 @@
"volumeMounts": [ "volumeMounts": [
{ {
"name": "my_secret", "name": "my_secret",
"mountPath": "/run/secrets" "mountPath": "/run/secrets/my_secret"
}, },
{ {
"name": "my_other_secret", "name": "my_other_secret",
"mountPath": "/run/secrets" "mountPath": "/run/secrets/my_other_secret"
} }
] ]
} }