Skip to content

Commit

Permalink
feat(trait): partition kamelets
Browse files Browse the repository at this point in the history
Configmap limit is 1 MB. We partition into more configmaps if the bundle exceed such limit.
  • Loading branch information
squakez committed Oct 13, 2023
1 parent f1882dd commit a6449e6
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 17 deletions.
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/architecture/kamelets.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ image::architecture/kamelets_deployment.png[Kamelets deployment model]

The operator creates a ConfigMap in order to bundle all the Kamelets which are eventually required by the application runtime. The Kamelets spec has to be available and in ready phase status. Once the application is created and ready to start, the operator mounts such a ConfigMap in a known location (default `/etc/camel/kamelets`) so that the Camel application will be able to read the definition from such location and run them according the logic expected in the same Camel framework.

NOTE: as the Configmap resource is limited to 1 MiB, the operator may split into more than a single Configmap bundle.

[[kamelet-parsing]]
=== Parsing capabilities defined in a Kamelet

Expand Down
34 changes: 22 additions & 12 deletions pkg/trait/kamelets.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ func newConfigurationKey(kamelet, configurationID string) configurationKey {
}

const (
contentKey = "content"
KameletLocationProperty = "camel.component.kamelet.location"
kameletLabel = "camel.apache.org/kamelet"
kameletConfigurationLabel = "camel.apache.org/kamelet.configuration"
contentKey = "content"
KameletLocationProperty = "camel.component.kamelet.location"
kameletLabel = "camel.apache.org/kamelet"
kameletConfigurationLabel = "camel.apache.org/kamelet.configuration"
kameletMountPointAnnotation = "camel.apache.org/kamelet.mount-point"
)

func newKameletsTrait() Trait {
Expand Down Expand Up @@ -182,7 +183,7 @@ func (t *kameletsTrait) addKamelets(e *Environment) error {
if err != nil {
return err
}
kameletsBundleConfigmap := initializeConfigmapBundle(e.Integration.Name, e.Integration.Namespace)
kb := newKameletBundle()
for _, key := range t.getKameletKeys() {
kamelet := kamelets[key]
if kamelet.Status.Phase != v1.KameletPhaseReady {
Expand All @@ -195,18 +196,27 @@ func (t *kameletsTrait) addKamelets(e *Environment) error {
// Adding explicit dependencies from Kamelets
util.StringSliceUniqueConcat(&e.Integration.Status.Dependencies, kamelet.Spec.Dependencies)
// Add to Kamelet bundle configmap
serialized, err := kubernetes.ToYAMLNoManagedFields(kamelet)
if err != nil {
return err
}
kameletsBundleConfigmap.Data[fmt.Sprintf("%s.kamelet.yaml", kamelet.Name)] = string(serialized)
kb.add(kamelet)
}
bundleConfigmaps, err := kb.toConfigmaps(e.Integration.Name, e.Integration.Namespace)
if err != nil {
return err
}
// set kamelets runtime location
if e.ApplicationProperties == nil {
e.ApplicationProperties = map[string]string{}
}
e.ApplicationProperties[KameletLocationProperty] = fmt.Sprintf("file:%s,classpath:/kamelets", t.MountPoint)
e.Resources.Add(&kameletsBundleConfigmap)
for _, cm := range bundleConfigmaps {
kameletMountPoint := fmt.Sprintf("%s/%s", t.MountPoint, cm.Name)
cm.Annotations[kameletMountPointAnnotation] = kameletMountPoint
e.Resources.Add(&cm)
if e.ApplicationProperties[KameletLocationProperty] == "" {
e.ApplicationProperties[KameletLocationProperty] = fmt.Sprintf("file:%s", kameletMountPoint)
} else {
e.ApplicationProperties[KameletLocationProperty] = e.ApplicationProperties[KameletLocationProperty] + fmt.Sprintf(",file:%s", kameletMountPoint)
}
}
e.ApplicationProperties[KameletLocationProperty] = e.ApplicationProperties[KameletLocationProperty] + ",classpath:/kamelets"
// resort dependencies
sort.Strings(e.Integration.Status.Dependencies)
}
Expand Down
91 changes: 91 additions & 0 deletions pkg/trait/kamelets_support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
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 trait

import (
"fmt"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/cmd/source"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type kameletBundle struct {
kamelets []*v1.Kamelet
}

func newKameletBundle() *kameletBundle {
return &kameletBundle{
kamelets: make([]*v1.Kamelet, 0),
}
}

func (kb *kameletBundle) add(k *v1.Kamelet) {
kb.kamelets = append(kb.kamelets, k)
}

// Split the contents of the Kamelets into one ore more configmap, making sure not to overpass the 1 MB limit
func (kb *kameletBundle) toConfigmaps(itName, itNamespace string) ([]corev1.ConfigMap, error) {
configmaps := make([]corev1.ConfigMap, 0)
cmSize := 0
cmId := 1
cm := newBundleConfigmap(itName, itNamespace, cmId)
for _, k := range kb.kamelets {
serialized, err := kubernetes.ToYAMLNoManagedFields(k)
if err != nil {
return nil, err
}
// Add if it fits into a configmap, otherwise, create a new configmap
if cmSize+len(serialized) > source.Megabyte {
configmaps = append(configmaps, cm)
// create a new configmap
cmSize = 0
cmId++
cm = newBundleConfigmap(itName, itNamespace, cmId)
}
cm.Data[fmt.Sprintf("%s.kamelet.yaml", k.Name)] = string(serialized)
cmSize += len(serialized)
}
// Add the last configmap
configmaps = append(configmaps, cm)

return configmaps, nil
}

func newBundleConfigmap(name, namespace string, id int) corev1.ConfigMap {
return corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("kamelets-bundle-%s-%03d", name, id),
Namespace: namespace,
Labels: map[string]string{
v1.IntegrationLabel: name,
kubernetes.ConfigMapTypeLabel: "kamelets-bundle",
},
Annotations: map[string]string{
kubernetes.ConfigMapAutogenLabel: "true",
},
},
Data: map[string]string{},
}
}
117 changes: 117 additions & 0 deletions pkg/trait/kamelets_support_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
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 trait

import (
"fmt"
"testing"

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

func TestKameletBundleSingle(t *testing.T) {
kb := newKameletBundle()
kb.add(kamelet("default", "test"))
cmBundle, err := kb.toConfigmaps("my-it", "default")
assert.Nil(t, err)
assert.NotNil(t, cmBundle)
assert.Len(t, cmBundle, 1)
assert.Equal(t, "default", cmBundle[0].Namespace)
assert.Equal(t, "kamelets-bundle-my-it-001", cmBundle[0].Name)
assert.Equal(t, "my-it", cmBundle[0].Labels[v1.IntegrationLabel])
assert.NotNil(t, cmBundle[0].Data["test.kamelet.yaml"])
}

func TestKameletBundleMultiKameletsSingleConfigmap(t *testing.T) {
kb := newKameletBundle()
kb.add(kamelet("default", "test1"))
kb.add(kamelet("default", "test2"))
kb.add(kamelet("default", "test3"))
kb.add(kamelet("default", "test4"))
kb.add(kamelet("default", "test5"))
kb.add(kamelet("default", "test6"))
cmBundle, err := kb.toConfigmaps("my-it", "default")
assert.Nil(t, err)
assert.NotNil(t, cmBundle)
assert.Len(t, cmBundle, 1)
assert.Equal(t, "default", cmBundle[0].Namespace)
assert.Equal(t, "kamelets-bundle-my-it-001", cmBundle[0].Name)
assert.Equal(t, "my-it", cmBundle[0].Labels[v1.IntegrationLabel])
assert.Len(t, cmBundle[0].Data, 6)
assert.NotNil(t, cmBundle[0].Data["test1.kamelet.yaml"])
assert.NotNil(t, cmBundle[0].Data["test2.kamelet.yaml"])
assert.NotNil(t, cmBundle[0].Data["test3.kamelet.yaml"])
assert.NotNil(t, cmBundle[0].Data["test4.kamelet.yaml"])
assert.NotNil(t, cmBundle[0].Data["test5.kamelet.yaml"])
assert.NotNil(t, cmBundle[0].Data["test6.kamelet.yaml"])
}

func TestKameletBundleMultiKameletsMultiConfigmap(t *testing.T) {
kb := newKameletBundle()
for i := 0; i < 2000; i++ {
kb.add(kamelet("default", fmt.Sprintf("test%d", i)))
}
cmBundle, err := kb.toConfigmaps("my-it", "default")
assert.Nil(t, err)
assert.NotNil(t, cmBundle)
assert.Len(t, cmBundle, 2)
assert.Equal(t, "default", cmBundle[0].Namespace)
assert.Equal(t, "kamelets-bundle-my-it-001", cmBundle[0].Name)
assert.Equal(t, "my-it", cmBundle[0].Labels[v1.IntegrationLabel])
assert.Equal(t, "default", cmBundle[1].Namespace)
assert.Equal(t, "kamelets-bundle-my-it-002", cmBundle[1].Name)
assert.Equal(t, "my-it", cmBundle[1].Labels[v1.IntegrationLabel])
assert.Equal(t, 2000, len(cmBundle[0].Data)+len(cmBundle[1].Data))
assert.NotNil(t, cmBundle[0].Data["test1.kamelet.yaml"])
assert.NotNil(t, cmBundle[1].Data["test1999.kamelet.yaml"])
}

func kamelet(ns, name string) *v1.Kamelet {
kamelet := v1.NewKamelet(ns, name)
kamelet.Spec = v1.KameletSpec{
Sources: []v1.SourceSpec{
{
DataSpec: v1.DataSpec{
Name: "mykamelet.groovy",
Content: `from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")
from("timer").to("log:info")`,
},
Type: v1.SourceTypeTemplate,
},
},
}
kamelet.Status = v1.KameletStatus{Phase: v1.KameletPhaseReady}

return &kamelet
}
4 changes: 2 additions & 2 deletions pkg/trait/kamelets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestKameletLookup(t *testing.T) {
require.NoError(t, err)
cm := environment.Resources.GetConfigMap(func(_ *corev1.ConfigMap) bool { return true })
assert.NotNil(t, cm)
assert.Equal(t, "kamelets-bundle-it", cm.Name)
assert.Equal(t, "kamelets-bundle-it-001", cm.Name)
assert.Equal(t, "test", cm.Namespace)

assert.Len(t, environment.Integration.Status.GeneratedSources, 1)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestNonYAMLKameletLookup(t *testing.T) {
require.NoError(t, err)
cm := environment.Resources.GetConfigMap(func(_ *corev1.ConfigMap) bool { return true })
assert.NotNil(t, cm)
assert.Equal(t, "kamelets-bundle-it", cm.Name)
assert.Equal(t, "kamelets-bundle-it-001", cm.Name)
assert.Equal(t, "test", cm.Namespace)

assert.Len(t, environment.Integration.Status.GeneratedSources, 1)
Expand Down
4 changes: 1 addition & 3 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,7 @@ func (e *Environment) configureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c
}
} else if configMap.Labels[kubernetes.ConfigMapTypeLabel] == "kamelets-bundle" {
// Kamelets bundle configmap
kameletMountPoint := strings.ReplaceAll(e.ApplicationProperties[KameletLocationProperty], "file:", "")
// We need also to remove the default location provided
kameletMountPoint = strings.ReplaceAll(kameletMountPoint, ",classpath:/kamelets", "")
kameletMountPoint := configMap.Annotations[kameletMountPointAnnotation]
refName := "kamelets-bundle"
vol := getVolume(refName, "configmap", configMap.Name, "", "")
mnt := getMount(refName, kameletMountPoint, "", true)
Expand Down

0 comments on commit a6449e6

Please sign in to comment.