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

allow to configure default configurations on platform #622

Merged
merged 3 commits into from
Apr 18, 2019
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
5 changes: 5 additions & 0 deletions pkg/apis/camel/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ type FailureRecovery struct {
type TraitSpec struct {
Configuration map[string]string `json:"configuration,omitempty"`
}

// Configurable --
type Configurable interface {
Configurations() []ConfigurationSpec
}
26 changes: 22 additions & 4 deletions pkg/apis/camel/v1alpha1/integration_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func NewIntegrationList() IntegrationList {
}

// Sources return a new slice containing all the sources associated to the integration
func (i *Integration) Sources() []SourceSpec {
allSources := make([]SourceSpec, 0, len(i.Spec.Sources)+len(i.Status.GeneratedSources))
allSources = append(allSources, i.Spec.Sources...)
allSources = append(allSources, i.Status.GeneratedSources...)
func (in *Integration) Sources() []SourceSpec {
allSources := make([]SourceSpec, 0, len(in.Spec.Sources)+len(in.Status.GeneratedSources))
allSources = append(allSources, in.Spec.Sources...)
allSources = append(allSources, in.Status.GeneratedSources...)

return allSources
}
Expand Down Expand Up @@ -90,6 +90,24 @@ func (is *IntegrationSpec) AddDependency(dependency string) {
}
}

// Configurations --
func (is *IntegrationSpec) Configurations() []ConfigurationSpec {
if is == nil {
return []ConfigurationSpec{}
}

return is.Configuration
}

// Configurations --
func (in *Integration) Configurations() []ConfigurationSpec {
if in == nil {
return []ConfigurationSpec{}
}

return in.Spec.Configurations()
}

// NewSourceSpec --
func NewSourceSpec(name string, content string, language Language) SourceSpec {
return SourceSpec{
Expand Down
26 changes: 22 additions & 4 deletions pkg/apis/camel/v1alpha1/integrationcontext_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,27 @@ func NewIntegrationContextList() IntegrationContextList {
}

// ImageForIntegration returns the image to use when using it for running an integration
func (c IntegrationContext) ImageForIntegration() string {
if c.Status.PublicImage != "" {
return c.Status.PublicImage
func (in *IntegrationContext) ImageForIntegration() string {
if in.Status.PublicImage != "" {
return in.Status.PublicImage
}
return c.Status.Image
return in.Status.Image
}

// Configurations --
func (in *IntegrationContextSpec) Configurations() []ConfigurationSpec {
if in == nil {
return []ConfigurationSpec{}
}

return in.Configuration
}

// Configurations --
func (in *IntegrationContext) Configurations() []ConfigurationSpec {
if in == nil {
return []ConfigurationSpec{}
}

return in.Spec.Configuration
}
13 changes: 7 additions & 6 deletions pkg/apis/camel/v1alpha1/integrationplatform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

// IntegrationPlatformSpec defines the desired state of IntegrationPlatform
type IntegrationPlatformSpec struct {
Cluster IntegrationPlatformCluster `json:"cluster,omitempty"`
Profile TraitProfile `json:"profile,omitempty"`
Build IntegrationPlatformBuildSpec `json:"build,omitempty"`
Resources IntegrationPlatformResourcesSpec `json:"resources,omitempty"`
Traits map[string]TraitSpec `json:"traits,omitempty"`
Cluster IntegrationPlatformCluster `json:"cluster,omitempty"`
Profile TraitProfile `json:"profile,omitempty"`
Build IntegrationPlatformBuildSpec `json:"build,omitempty"`
Resources IntegrationPlatformResourcesSpec `json:"resources,omitempty"`
Traits map[string]TraitSpec `json:"traits,omitempty"`
Configuration []ConfigurationSpec `json:"configuration,omitempty"`
}

// IntegrationPlatformResourcesSpec contains platform related resources
Expand Down Expand Up @@ -97,7 +98,7 @@ type IntegrationPlatformRegistrySpec struct {
type IntegrationPlatformBuildStrategy string

const (
// IntegrationPlatformBuildPublishStrategyRoutine performs the build in a routine
// IntegrationPlatformBuildStrategyRoutine performs the build in a routine
IntegrationPlatformBuildStrategyRoutine = "routine"

// IntegrationPlatformBuildStrategyPod performs the build in a pod
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/camel/v1alpha1/integrationplatform_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,21 @@ func TraitProfileByName(name string) TraitProfile {
}
return ""
}

// Configurations --
func (in *IntegrationPlatformSpec) Configurations() []ConfigurationSpec {
if in == nil {
return []ConfigurationSpec{}
}

return in.Configuration
}

// Configurations --
func (in *IntegrationPlatform) Configurations() []ConfigurationSpec {
if in == nil {
return []ConfigurationSpec{}
}

return in.Spec.Configuration
}
4 changes: 2 additions & 2 deletions pkg/metadata/metadata_dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ package metadata
import (
"testing"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/test"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -188,4 +188,4 @@ func TestXMLHystrixDependency(t *testing.T) {

// assert all dependencies are found and sorted (removing duplicates)
assert.Equal(t, []string{"camel:core", "camel:hystrix", "camel:kafka"}, meta.Dependencies)
}
}
7 changes: 3 additions & 4 deletions pkg/trait/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ func (t *deploymentTrait) getDeploymentFor(e *Environment) *appsv1.Deployment {
paths := e.ComputeSourcesURI(t.deployer.ContainerImage)
environment := make([]corev1.EnvVar, 0)

// combine Environment of integration with context, integration
// Environment has the priority
VisitKeyValConfigurations("env", e.IntegrationContext, e.Integration, func(key string, value string) {
// combine Environment of integration with platform, context, integration
for key, value := range e.CollectConfigurationPairs("env") {
envvar.SetVal(&environment, key, value)
})
}

// set env vars needed by the runtime
envvar.SetVal(&environment, "JAVA_MAIN_CLASS", "org.apache.camel.k.jvm.Application")
Expand Down
5 changes: 2 additions & 3 deletions pkg/trait/knative_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,9 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) (*serving.Service, e
environment := &svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.Env

// combine Environment of integration with context, integration
// Environment has the priority
VisitKeyValConfigurations("env", e.IntegrationContext, e.Integration, func(key string, value string) {
for key, value := range e.CollectConfigurationPairs("env") {
envvar.SetVal(environment, key, value)
})
}

// set env vars needed by the runtime
envvar.SetVal(environment, "JAVA_MAIN_CLASS", "org.apache.camel.k.jvm.Application")
Expand Down
17 changes: 8 additions & 9 deletions pkg/trait/knative_service_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ func (t *knativeServiceTrait) bindToEnvVar(e *Environment, service *serving.Serv
//
// Properties
//

properties := make(map[string]string)

VisitKeyValConfigurations("property", e.IntegrationContext, e.Integration, func(key string, val string) {
properties[key] = val
})

VisitConfigurations("configmap", e.IntegrationContext, e.Integration, func(cmName string) {
for _, cmName := range e.CollectConfigurationValues("configmap") {
cm, err := kubernetes.GetConfigMap(e.C, e.Client, cmName, e.Integration.Namespace)
if err != nil {
t.L.Errorf(err, "failed to lookup ConfigMap %s", cmName)
Expand All @@ -55,9 +50,9 @@ func (t *knativeServiceTrait) bindToEnvVar(e *Environment, service *serving.Serv
t.L.Errorf(err, "failed to extract properties from ConfigMap %s", cmName)
}
}
})
}

VisitConfigurations("secret", e.IntegrationContext, e.Integration, func(secretName string) {
for _, secretName := range e.CollectConfigurationValues("secret") {
cm, err := kubernetes.GetSecret(e.C, e.Client, secretName, e.Integration.Namespace)
if err != nil {
t.L.Errorf(err, "failed to lookup Secret %s", secretName)
Expand All @@ -70,7 +65,11 @@ func (t *knativeServiceTrait) bindToEnvVar(e *Environment, service *serving.Serv
t.L.Errorf(err, "failed to extract properties from Secret %s", secretName)
}
}
})
}

for key, value := range e.CollectConfigurationPairs("property") {
properties[key] = value
}

p := ""

Expand Down
22 changes: 16 additions & 6 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ func (e *Environment) ComputeConfigMaps(container bool) []runtime.Object {
// properties have the priority
properties := ""

VisitKeyValConfigurations("property", e.IntegrationContext, e.Integration, func(key string, val string) {
for key, val := range e.CollectConfigurationPairs("property") {
properties += fmt.Sprintf("%s=%s\n", key, val)
})
}

maps = append(
maps,
Expand Down Expand Up @@ -514,7 +514,7 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V
// Volumes :: Additional ConfigMaps
//

VisitConfigurations("configmap", e.IntegrationContext, e.Integration, func(cmName string) {
for _, cmName := range e.CollectConfigurationValues("configmap") {
refName := kubernetes.SanitizeLabel(cmName)
fileName := "integration-cm-" + strings.ToLower(cmName)

Expand All @@ -533,13 +533,13 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V
Name: refName,
MountPath: path.Join("/etc/camel/conf.d", fileName),
})
})
}

//
// Volumes :: Additional Secrets
//

VisitConfigurations("secret", e.IntegrationContext, e.Integration, func(secretName string) {
for _, secretName := range e.CollectConfigurationValues("secret") {
refName := kubernetes.SanitizeLabel(secretName)
fileName := "integration-secret-" + strings.ToLower(secretName)

Expand All @@ -556,5 +556,15 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V
Name: refName,
MountPath: path.Join("/etc/camel/conf.d", fileName),
})
})
}
}

// CollectConfigurationValues --
func (e *Environment) CollectConfigurationValues(configurationType string) []string {
return CollectConfigurationValues(configurationType, e.Platform, e.IntegrationContext, e.Integration)
}

// CollectConfigurationPairs --
func (e *Environment) CollectConfigurationPairs(configurationType string) map[string]string {
return CollectConfigurationPairs(configurationType, e.Platform, e.IntegrationContext, e.Integration)
}
89 changes: 47 additions & 42 deletions pkg/trait/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ package trait
import (
"context"
"fmt"
"reflect"
"regexp"
"strings"

"github.com/scylladb/go-set/strset"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/client"

Expand All @@ -45,62 +48,64 @@ func GetIntegrationContext(ctx context.Context, c client.Client, integration *v1
return &ictx, err
}

// VisitConfigurations --
func VisitConfigurations(
configurationType string,
context *v1alpha1.IntegrationContext,
integration *v1alpha1.Integration,
consumer func(string)) {

if context != nil {
// Add context properties first so integrations can
// override it
for _, c := range context.Spec.Configuration {
if c.Type == configurationType {
consumer(c.Value)
}
// CollectConfigurationValues --
func CollectConfigurationValues(configurationType string, configurable ...v1alpha1.Configurable) []string {
result := strset.New()

for _, c := range configurable {
c := c

if c == nil || reflect.ValueOf(c).IsNil() {
continue
}
}

if integration != nil {
for _, c := range integration.Spec.Configuration {
if c.Type == configurationType {
consumer(c.Value)
entries := c.Configurations()
if entries == nil {
continue
}

for _, entry := range entries {
if entry.Type == configurationType {
result.Add(entry.Value)
}
}
}

return result.List()
}

// VisitKeyValConfigurations --
func VisitKeyValConfigurations(
configurationType string,
context *v1alpha1.IntegrationContext,
integration *v1alpha1.Integration,
consumer func(string, string)) {

if context != nil {
// Add context properties first so integrations can
// override it
for _, c := range context.Spec.Configuration {
if c.Type == configurationType {
pair := strings.Split(c.Value, "=")
if len(pair) == 2 {
consumer(pair[0], pair[1])
}
}
// CollectConfigurationPairs --
func CollectConfigurationPairs(configurationType string, configurable ...v1alpha1.Configurable) map[string]string {
result := make(map[string]string)

for _, c := range configurable {
c := c

if c == nil || reflect.ValueOf(c).IsNil() {
continue
}
}

if integration != nil {
for _, c := range integration.Spec.Configuration {
if c.Type == configurationType {
pair := strings.Split(c.Value, "=")
entries := c.Configurations()
if entries == nil {
continue
}

for _, entry := range entries {
if entry.Type == configurationType {
pair := strings.SplitN(entry.Value, "=", 2)
if len(pair) == 2 {
consumer(pair[0], pair[1])
k := strings.TrimSpace(pair[0])
v := strings.TrimSpace(pair[1])

if len(k) > 0 && len(v) > 0 {
result[k] = v
}
}
}
}
}

return result
}

var (
Expand Down
Loading