You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2020/01/15 07:41:40 UTC

[camel-k] 02/02: chore(jolokia): Factorize map keys sorting util method

This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 9b54ffb363553168b4eabcdcebe0efa7474dbefa
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Tue Jan 14 18:12:34 2020 +0100

    chore(jolokia): Factorize map keys sorting util method
---
 pkg/trait/jolokia.go      | 10 +---------
 pkg/util/digest/digest.go | 25 ++++++-------------------
 pkg/util/util.go          | 12 ++++++++++++
 3 files changed, 19 insertions(+), 28 deletions(-)

diff --git a/pkg/trait/jolokia.go b/pkg/trait/jolokia.go
index e7dfa1f..5022d90 100644
--- a/pkg/trait/jolokia.go
+++ b/pkg/trait/jolokia.go
@@ -19,7 +19,6 @@ package trait
 
 import (
 	"fmt"
-	"sort"
 	"strconv"
 	"strings"
 
@@ -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, ","))
diff --git a/pkg/util/digest/digest.go b/pkg/util/digest/digest.go
index ea5afa7..62ec3fa 100644
--- a/pkg/util/digest/digest.go
+++ b/pkg/util/digest/digest.go
@@ -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"
 )
 
@@ -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
@@ -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
diff --git a/pkg/util/util.go b/pkg/util/util.go
index 542e7cf..c6004fd 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -24,6 +24,7 @@ import (
 	"os/signal"
 	"path"
 	"regexp"
+	"sort"
 	"syscall"
 
 	"github.com/scylladb/go-set/strset"
@@ -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
+}