Merge pull request #631 from procrypt/sort_EnvVar_struct

Fix EnvSort struct
This commit is contained in:
Tomas Kral 2017-06-14 09:53:23 +02:00 committed by GitHub
commit bf20029009
3 changed files with 32 additions and 3 deletions

View File

@ -48,6 +48,7 @@ import (
"github.com/pkg/errors"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/labels"
"sort"
)
// Kubernetes implements Transformer interface and represents Kubernetes transformer
@ -480,14 +481,17 @@ func (k *Kubernetes) ConfigPVCVolumeSource(name string, readonly bool) *api.Volu
// ConfigEnvs configures the environment variables.
func (k *Kubernetes) ConfigEnvs(name string, service kobject.ServiceConfig) []api.EnvVar {
envs := []api.EnvVar{}
envs := transformer.EnvSort{}
for _, v := range service.Environment {
envs = append(envs, api.EnvVar{
Name: v.Name,
Value: v.Value,
})
}
// Stable sorts data while keeping the original order of equal elements
// we need this because envs are not populated in any random order
// this sorting ensures they are populated in a particular order
sort.Stable(envs)
return envs
}

View File

@ -52,6 +52,7 @@ import (
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/util/intstr"
"sort"
)
// OpenShift implements Transformer interface and represents OpenShift transformer
@ -201,13 +202,17 @@ func (o *OpenShift) initImageStream(name string, service kobject.ServiceConfig,
// initBuildConfig initialize Openshifts BuildConfig Object
func initBuildConfig(name string, service kobject.ServiceConfig, repo string, branch string) (*buildapi.BuildConfig, error) {
contextDir, err := getAbsBuildContext(service.Build)
envList := []kapi.EnvVar{}
envList := transformer.EnvSort{}
for envName, envValue := range service.BuildArgs {
if *envValue == "\x00" {
*envValue = os.Getenv(envName)
}
envList = append(envList, kapi.EnvVar{Name: envName, Value: *envValue})
}
// Stable sorts data while keeping the original order of equal elements
// we need this because envs are not populated in any random order
// this sorting ensures they are populated in a particular order
sort.Stable(envList)
if err != nil {
return nil, errors.Wrap(err, name+"buildconfig cannot be created due to error in creating build context, getAbsBuildContext failed")
}

View File

@ -28,6 +28,7 @@ import (
"path/filepath"
"github.com/pkg/errors"
"k8s.io/kubernetes/pkg/api"
)
const Selector = "io.kompose.service"
@ -151,3 +152,22 @@ func formatProviderName(provider string) string {
}
return provider
}
// Sort struct
type EnvSort []api.EnvVar
// returns the number of elements in the collection.
func (env EnvSort) Len() int {
return len(env)
}
// returns whether the element with index i should sort before
// the element with index j.
func (env EnvSort) Less(i, j int) bool {
return env[i].Name < env[j].Name
}
// swaps the elements with indexes i and j.
func (env EnvSort) Swap(i, j int) {
env[i], env[j] = env[j], env[i]
}