Skip to content

Commit

Permalink
chore(jolokia): Factorize map keys sorting util method
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jan 15, 2020
1 parent ed143d1 commit 9b54ffb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
10 changes: 1 addition & 9 deletions pkg/trait/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package trait

import (
"fmt"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -178,15 +177,8 @@ func (t *jolokiaTrait) Apply(e *Environment) (err error) {
// Lastly set the AB_JOLOKIA_OPTS environment variable from the fabric8/s2i-java base image
// Options must be sorted so that the environment variable value is consistent over iterations,
// otherwise the value changes which results in triggering a new deployment.
optionKeys := make([]string, len(options))
i := 0
for k := range options {
optionKeys[i] = k
i++
}
sort.Strings(optionKeys)
optionValues := make([]string, len(options))
for i, k := range optionKeys {
for i, k := range util.SortedStringMapKeys(options) {
optionValues[i] = k + "=" + options[k]
}
envvar.SetVal(&container.Env, "AB_JOLOKIA_OPTS", strings.Join(optionValues, ","))
Expand Down
25 changes: 6 additions & 19 deletions pkg/util/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ package digest
import (
"crypto/sha256"
"encoding/base64"
"math/rand"
"sort"
"strconv"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/defaults"
)

Expand Down Expand Up @@ -81,7 +80,7 @@ func ComputeForIntegration(integration *v1.Integration) (string, error) {
return "", err
}
spec := integration.Spec.Traits[name]
for _, prop := range sortedStringMapKeys(spec.Configuration) {
for _, prop := range util.SortedStringMapKeys(spec.Configuration) {
val := spec.Configuration[prop]
if _, err := hash.Write([]byte(prop + "=" + val + ",")); err != nil {
return "", err
Expand Down Expand Up @@ -122,24 +121,12 @@ func ComputeForIntegrationKit(kit *v1.IntegrationKit) (string, error) {
return digest, nil
}

// Random --
func Random() string {
return "v" + strconv.FormatInt(rand.Int63(), 10)
}

func sortedStringMapKeys(m map[string]string) []string {
res := make([]string, 0, len(m))
for k := range m {
res = append(res, k)
}
sort.Strings(res)
return res
}

func sortedTraitSpecMapKeys(m map[string]v1.TraitSpec) []string {
res := make([]string, 0, len(m))
res := make([]string, len(m))
i := 0
for k := range m {
res = append(res, k)
res[i] = k
i++
}
sort.Strings(res)
return res
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os/signal"
"path"
"regexp"
"sort"
"syscall"

"github.com/scylladb/go-set/strset"
Expand Down Expand Up @@ -196,3 +197,14 @@ func FileExists(name string) (bool, error) {
type BytesMarshaller interface {
MarshalBytes() ([]byte, error)
}

func SortedStringMapKeys(m map[string]string) []string {
res := make([]string, len(m))
i := 0
for k := range m {
res[i] = k
i++
}
sort.Strings(res)
return res
}

0 comments on commit 9b54ffb

Please sign in to comment.