Skip to content

Commit

Permalink
chore(knative): configure via properties
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Feb 27, 2024
1 parent 87345e8 commit 9c058da
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 199 deletions.
1 change: 0 additions & 1 deletion docs/modules/ROOT/partials/apis/camel-k-crds.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7183,7 +7183,6 @@ Automatically deploy the integration as Knative service when all conditions hold
The Knative trait automatically discovers addresses of Knative resources and inject them into the
running integration.
The full Knative configuration is injected in the CAMEL_KNATIVE_CONFIGURATION in JSON format.
The Camel Knative component will then use the full configuration to configure the routes.
The trait is enabled by default when the Knative profile is active.
Expand Down
1 change: 0 additions & 1 deletion docs/modules/traits/pages/knative.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
The Knative trait automatically discovers addresses of Knative resources and inject them into the
running integration.

The full Knative configuration is injected in the CAMEL_KNATIVE_CONFIGURATION in JSON format.
The Camel Knative component will then use the full configuration to configure the routes.

The trait is enabled by default when the Knative profile is active.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/camel/v1/knative/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type CamelServiceDefinition struct {
URL string `json:"url,omitempty"`
Path string `json:"path,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
SinkBinding bool `json:"sinkBinding,omitempty"`
}

// CamelEndpointKind -- .
Expand Down
41 changes: 41 additions & 0 deletions pkg/apis/camel/v1/knative/types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package knative

import (
"encoding/json"
"fmt"
"net/url"
)

Expand All @@ -41,6 +42,46 @@ func BuildCamelServiceDefinition(name string, endpointKind CamelEndpointKind, se
return definition, nil
}

// SetSinkBinding marks one of the service as SinkBinding.
func (env *CamelEnvironment) SetSinkBinding(name string, endpointKind CamelEndpointKind, serviceType CamelServiceType, apiVersion, kind string) {
for i, svc := range env.Services {
svc := svc
if svc.Name == name &&
svc.Metadata[CamelMetaEndpointKind] == string(endpointKind) &&
svc.ServiceType == serviceType &&
(apiVersion == "" || svc.Metadata[CamelMetaKnativeAPIVersion] == apiVersion) &&
(kind == "" || svc.Metadata[CamelMetaKnativeKind] == kind) {
svc.SinkBinding = true
env.Services[i] = svc
}
}
}

// ToCamelProperties returns the application properties representation of the services.
func (env *CamelEnvironment) ToCamelProperties() map[string]string {
mappedServices := make(map[string]string)
for i, service := range env.Services {
resource := fmt.Sprintf("camel.component.knative.environment.resources[%d]", i)
mappedServices[fmt.Sprintf("%s.name", resource)] = service.Name
mappedServices[fmt.Sprintf("%s.type", resource)] = string(service.ServiceType)
mappedServices[fmt.Sprintf("%s.objectKind", resource)] = service.Metadata[CamelMetaKnativeKind]
mappedServices[fmt.Sprintf("%s.objectApiVersion", resource)] = service.Metadata[CamelMetaKnativeAPIVersion]
mappedServices[fmt.Sprintf("%s.endpointKind", resource)] = service.Metadata[CamelMetaEndpointKind]
mappedServices[fmt.Sprintf("%s.reply", resource)] = service.Metadata[CamelMetaKnativeReply]
if service.ServiceType == CamelServiceTypeEvent {
mappedServices[fmt.Sprintf("%s.objectName", resource)] = service.Metadata[CamelMetaKnativeName]
}
if service.SinkBinding {
mappedServices[fmt.Sprintf("%s.url", resource)] = "${K_SINK}"
mappedServices[fmt.Sprintf("%s.ceOverrides", resource)] = "${K_CE_OVERRIDES}"
} else {
mappedServices[fmt.Sprintf("%s.url", resource)] = service.URL
mappedServices[fmt.Sprintf("%s.path", resource)] = service.Path
}
}
return mappedServices
}

// Serialize serializes a CamelEnvironment.
func (env *CamelEnvironment) Serialize() (string, error) {
res, err := json.Marshal(env)
Expand Down
61 changes: 61 additions & 0 deletions pkg/apis/camel/v1/knative/types_support_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package knative

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSetSinkBinding(t *testing.T) {
camelEnv := NewCamelEnvironment()
svc1, err := BuildCamelServiceDefinition(
"test",
CamelEndpointKindSink,
CamelServiceTypeChannel,
url.URL{},
"apiVersion",
"InMemoryChannel",
)
assert.Nil(t, err)
camelEnv.Services = append(camelEnv.Services, svc1)
svc := camelEnv.FindService("test",
CamelEndpointKindSink,
CamelServiceTypeChannel,
"apiVersion",
"InMemoryChannel",
)
assert.NotNil(t, svc)
assert.False(t, svc.SinkBinding)
camelEnv.SetSinkBinding("test",
CamelEndpointKindSink,
CamelServiceTypeChannel,
"apiVersion",
"InMemoryChannel",
)
svc = camelEnv.FindService("test",
CamelEndpointKindSink,
CamelServiceTypeChannel,
"apiVersion",
"InMemoryChannel",
)
assert.NotNil(t, svc)
assert.True(t, svc.SinkBinding)
}
1 change: 0 additions & 1 deletion pkg/apis/camel/v1/trait/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package trait
// The Knative trait automatically discovers addresses of Knative resources and inject them into the
// running integration.
//
// The full Knative configuration is injected in the CAMEL_KNATIVE_CONFIGURATION in JSON format.
// The Camel Knative component will then use the full configuration to configure the routes.
//
// The trait is enabled by default when the Knative profile is active.
Expand Down
Loading

0 comments on commit 9c058da

Please sign in to comment.