Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent Integration environment variables ordering randomization #2196

Merged
merged 2 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ func (t *containerTrait) configureContainer(e *Environment) error {
}

// combine Environment of integration with platform, kit, integration
for key, value := range e.collectConfigurationPairs("env") {
envvar.SetVal(&container.Env, key, value)
for _, env := range e.collectConfigurationPairs("env") {
envvar.SetVal(&container.Env, env.Name, env.Value)
}

envvar.SetVal(&container.Env, "CAMEL_K_DIGEST", e.Integration.Status.Digest)
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (t *cronTrait) getCronJobFor(e *Environment) *v1beta1.CronJob {

// Copy annotations from the integration resource
if e.Integration.Annotations != nil {
for k, v := range FilterTransferableAnnotations(e.Integration.Annotations) {
for k, v := range filterTransferableAnnotations(e.Integration.Annotations) {
annotations[k] = v
}
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/trait/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ func (t *deploymentTrait) Configure(e *Environment) (bool, error) {
return condition != nil && condition.Status == corev1.ConditionTrue, nil
}

//
// Don't deploy when a different strategy is needed (e.g. Knative, Cron)
//
strategy, err := e.DetermineControllerStrategy()
if err != nil {
e.Integration.Status.SetErrorCondition(
Expand Down Expand Up @@ -141,7 +139,7 @@ func (t *deploymentTrait) getDeploymentFor(e *Environment) *appsv1.Deployment {
// create a copy to avoid sharing the underlying annotation map
annotations := make(map[string]string)
if e.Integration.Annotations != nil {
for k, v := range FilterTransferableAnnotations(e.Integration.Annotations) {
for k, v := range filterTransferableAnnotations(e.Integration.Annotations) {
annotations[k] = v
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/knative_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) *serving.Service {

// Copy annotations from the integration resource
if e.Integration.Annotations != nil {
for k, v := range FilterTransferableAnnotations(e.Integration.Annotations) {
for k, v := range filterTransferableAnnotations(e.Integration.Annotations) {
annotations[k] = v
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/trait/trait.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ package trait
import (
"context"

"github.com/pkg/errors"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/pkg/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
)

// Apply --
func Apply(ctx context.Context, c client.Client, integration *v1.Integration, kit *v1.IntegrationKit) (*Environment, error) {
environment, err := newEnvironment(ctx, c, integration, kit)
if err != nil {
Expand Down Expand Up @@ -77,7 +77,7 @@ func newEnvironment(ctx context.Context, c client.Client, integration *v1.Integr
}

if kit == nil {
kit, err = GetIntegrationKit(ctx, c, integration)
kit, err = getIntegrationKit(ctx, c, integration)
if err != nil {
return nil, err
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ func (e *Environment) computeConfigMaps() []ctrl.Object {
// properties have the priority
userProperties := ""

for key, val := range e.collectConfigurationPairs("property") {
userProperties += fmt.Sprintf("%s=%s\n", key, val)
for _, prop := range e.collectConfigurationPairs("property") {
userProperties += fmt.Sprintf("%s=%s\n", prop.Name, prop.Value)
}

if userProperties != "" {
Expand Down Expand Up @@ -783,8 +783,12 @@ func (e *Environment) collectConfigurationValues(configurationType string) []str
return collectConfigurationValues(configurationType, e.Platform, e.IntegrationKit, e.Integration)
}

func (e *Environment) collectConfigurationPairs(configurationType string) map[string]string {
return CollectConfigurationPairs(configurationType, e.Platform, e.IntegrationKit, e.Integration)
type variable struct {
Name, Value string
}

func (e *Environment) collectConfigurationPairs(configurationType string) []variable {
return collectConfigurationPairs(configurationType, e.Platform, e.IntegrationKit, e.Integration)
}

func (e *Environment) getIntegrationContainer() *corev1.Container {
Expand Down
42 changes: 26 additions & 16 deletions pkg/trait/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
user "github.com/mitchellh/go-homedir"
"github.com/scylladb/go-set/strset"

k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/client"
Expand All @@ -39,17 +39,13 @@ import (

var exactVersionRegexp = regexp.MustCompile(`^(\d+)\.(\d+)\.([\w-.]+)$`)

// GetIntegrationKit retrieves the kit set on the integration
func GetIntegrationKit(ctx context.Context, c client.Client, integration *v1.Integration) (*v1.IntegrationKit, error) {
// getIntegrationKit retrieves the kit set on the integration
func getIntegrationKit(ctx context.Context, c client.Client, integration *v1.Integration) (*v1.IntegrationKit, error) {
if integration.Status.IntegrationKit == nil {
return nil, nil
}
kit := v1.NewIntegrationKit(integration.Status.IntegrationKit.Namespace, integration.Status.IntegrationKit.Name)
key := k8sclient.ObjectKey{
Namespace: integration.Status.IntegrationKit.Namespace,
Name: integration.Status.IntegrationKit.Name,
}
err := c.Get(ctx, key, &kit)
err := c.Get(ctx, ctrl.ObjectKeyFromObject(&kit), &kit)
return &kit, err
}

Expand Down Expand Up @@ -80,8 +76,8 @@ func collectConfigurationValues(configurationType string, configurable ...v1.Con
return s
}

func CollectConfigurationPairs(configurationType string, configurable ...v1.Configurable) map[string]string {
result := make(map[string]string)
func collectConfigurationPairs(configurationType string, configurable ...v1.Configurable) []variable {
result := make([]variable, 0)

for _, c := range configurable {
c := c
Expand All @@ -98,14 +94,28 @@ func CollectConfigurationPairs(configurationType string, configurable ...v1.Conf
for _, entry := range entries {
if entry.Type == configurationType {
pair := strings.SplitN(entry.Value, "=", 2)
var k, v string
if len(pair) >= 1 {
k = strings.TrimSpace(pair[0])
}
if len(pair) == 2 {
k := strings.TrimSpace(pair[0])
v := strings.TrimSpace(pair[1])
v = strings.TrimSpace(pair[1])
}
if k == "" {
continue
}

if len(k) > 0 && len(v) > 0 {
result[k] = v
ok := false
for i, variable := range result {
if variable.Name == k {
result[i].Value = v
ok = true
break
}
}
if !ok {
result = append(result, variable{Name: k, Value: v})
}
}
}
}
Expand All @@ -129,8 +139,8 @@ func keyValuePairArrayAsStringMap(pairs []string) (map[string]string, error) {
return m, nil
}

// FilterTransferableAnnotations returns a map containing annotations that are meaningful for being transferred to child resources.
func FilterTransferableAnnotations(annotations map[string]string) map[string]string {
// filterTransferableAnnotations returns a map containing annotations that are meaningful for being transferred to child resources.
func filterTransferableAnnotations(annotations map[string]string) map[string]string {
res := make(map[string]string)
for k, v := range annotations {
if strings.HasPrefix(k, "kubectl.kubernetes.io") {
Expand Down
10 changes: 6 additions & 4 deletions pkg/trait/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ func TestCollectConfigurationPairs(t *testing.T) {
e.Platform.ResyncStatusFullConfig()

pairs := e.collectConfigurationPairs("property")
assert.Equal(t, "integration", pairs["p1"])
assert.Equal(t, "kit", pairs["p2"])
assert.Equal(t, "platform", pairs["p3"])
assert.Equal(t, "integration", pairs["p4"])
assert.Equal(t, pairs, []variable{
{Name: "p1", Value: "integration"},
{Name: "p2", Value: "kit"},
{Name: "p3", Value: "platform"},
{Name: "p4", Value: "integration"},
})
}