-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configmap limit is 1 MB. We partition into more configmaps if the bundle exceed such limit.
- Loading branch information
Showing
6 changed files
with
235 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters