From 0fdfcaa8f6aa47e4a8fa8c6c8767aae721dccf21 Mon Sep 17 00:00:00 2001 From: Pasquale Congiusti Date: Wed, 23 Oct 2024 21:45:45 +0200 Subject: [PATCH] feat(trait): service annotations Closes #5901 --- .../ROOT/partials/apis/camel-k-crds.adoc | 7 ++ docs/modules/traits/pages/service.adoc | 4 + helm/camel-k/crds/camel-k-crds.yaml | 40 +++++++++ pkg/apis/camel/v1/trait/service.go | 2 + .../camel/v1/trait/zz_generated.deepcopy.go | 7 ++ ...camel.apache.org_integrationplatforms.yaml | 10 +++ .../camel.apache.org_integrationprofiles.yaml | 10 +++ .../bases/camel.apache.org_integrations.yaml | 10 +++ .../camel.apache.org_kameletbindings.yaml | 5 ++ .../crd/bases/camel.apache.org_pipes.yaml | 5 ++ pkg/trait/service.go | 13 +-- pkg/trait/service_test.go | 81 +++++++++++++++++++ 12 files changed, 188 insertions(+), 6 deletions(-) diff --git a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc index 4c25032774..6a58a7a565 100644 --- a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc +++ b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc @@ -8790,6 +8790,13 @@ Deprecated: Use service type instead. The type of service to be used, either 'ClusterIP', 'NodePort' or 'LoadBalancer'. +|`annotations` + +map[string]string +| + + +The annotations added to the Service object. + |=== diff --git a/docs/modules/traits/pages/service.adoc b/docs/modules/traits/pages/service.adoc index 6208a346ba..e66dbcca07 100755 --- a/docs/modules/traits/pages/service.adoc +++ b/docs/modules/traits/pages/service.adoc @@ -45,6 +45,10 @@ Deprecated: Use service type instead. | github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait.ServiceType | The type of service to be used, either 'ClusterIP', 'NodePort' or 'LoadBalancer'. +| service.annotations +| map[string]string +| The annotations added to the Service object. + |=== // End of autogenerated code - DO NOT EDIT! (configuration) diff --git a/helm/camel-k/crds/camel-k-crds.yaml b/helm/camel-k/crds/camel-k-crds.yaml index 12e238d8aa..3bb34c12b8 100644 --- a/helm/camel-k/crds/camel-k-crds.yaml +++ b/helm/camel-k/crds/camel-k-crds.yaml @@ -5212,6 +5212,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -7352,6 +7357,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -9395,6 +9405,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -11414,6 +11429,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -19474,6 +19494,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -21455,6 +21480,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -29591,6 +29621,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -40517,6 +40552,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. diff --git a/pkg/apis/camel/v1/trait/service.go b/pkg/apis/camel/v1/trait/service.go index 4452576d98..924fbcc14d 100644 --- a/pkg/apis/camel/v1/trait/service.go +++ b/pkg/apis/camel/v1/trait/service.go @@ -35,6 +35,8 @@ type ServiceTrait struct { // The type of service to be used, either 'ClusterIP', 'NodePort' or 'LoadBalancer'. // +kubebuilder:validation:Enum=ClusterIP;NodePort;LoadBalancer Type *ServiceType `property:"type" json:"type,omitempty"` + // The annotations added to the Service object. + Annotations map[string]string `property:"annotations" json:"annotations,omitempty"` } type ServiceType string diff --git a/pkg/apis/camel/v1/trait/zz_generated.deepcopy.go b/pkg/apis/camel/v1/trait/zz_generated.deepcopy.go index 42b190f709..36235e591c 100644 --- a/pkg/apis/camel/v1/trait/zz_generated.deepcopy.go +++ b/pkg/apis/camel/v1/trait/zz_generated.deepcopy.go @@ -1156,6 +1156,13 @@ func (in *ServiceTrait) DeepCopyInto(out *ServiceTrait) { *out = new(ServiceType) **out = **in } + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ServiceTrait. diff --git a/pkg/resources/config/crd/bases/camel.apache.org_integrationplatforms.yaml b/pkg/resources/config/crd/bases/camel.apache.org_integrationplatforms.yaml index 163232d424..dc2cb3a04a 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_integrationplatforms.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_integrationplatforms.yaml @@ -2054,6 +2054,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -4194,6 +4199,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. diff --git a/pkg/resources/config/crd/bases/camel.apache.org_integrationprofiles.yaml b/pkg/resources/config/crd/bases/camel.apache.org_integrationprofiles.yaml index fd964cb8ea..748fba573b 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_integrationprofiles.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_integrationprofiles.yaml @@ -1923,6 +1923,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -3942,6 +3947,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. diff --git a/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml b/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml index 520f496bcc..fdf0894246 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml @@ -7943,6 +7943,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. @@ -9924,6 +9929,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. diff --git a/pkg/resources/config/crd/bases/camel.apache.org_kameletbindings.yaml b/pkg/resources/config/crd/bases/camel.apache.org_kameletbindings.yaml index 93ead8554e..bafbf10808 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_kameletbindings.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_kameletbindings.yaml @@ -8012,6 +8012,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. diff --git a/pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml b/pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml index 91af8ae443..5c0bacc7b0 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml @@ -8010,6 +8010,11 @@ spec: service: description: The configuration of Service trait properties: + annotations: + additionalProperties: + type: string + description: The annotations added to the Service object. + type: object auto: description: To automatically detect from the code if a Service needs to be created. diff --git a/pkg/trait/service.go b/pkg/trait/service.go index 9cb3e85224..2c42825761 100644 --- a/pkg/trait/service.go +++ b/pkg/trait/service.go @@ -97,7 +97,7 @@ func (t *serviceTrait) Apply(e *Environment) error { svc := e.Resources.GetServiceForIntegration(e.Integration) // add a new service if not already created if svc == nil { - svc = getServiceFor(e) + svc = t.getServiceFor(e.Integration.Name, e.Integration.Namespace) var serviceType corev1.ServiceType if t.Type != nil { @@ -121,23 +121,24 @@ func (t *serviceTrait) Apply(e *Environment) error { return nil } -func getServiceFor(e *Environment) *corev1.Service { +func (t *serviceTrait) getServiceFor(itName, itNamespace string) *corev1.Service { return &corev1.Service{ TypeMeta: metav1.TypeMeta{ Kind: "Service", APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: e.Integration.Name, - Namespace: e.Integration.Namespace, + Name: itName, + Namespace: itNamespace, Labels: map[string]string{ - v1.IntegrationLabel: e.Integration.Name, + v1.IntegrationLabel: itName, }, + Annotations: t.Annotations, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{}, Selector: map[string]string{ - v1.IntegrationLabel: e.Integration.Name, + v1.IntegrationLabel: itName, }, }, } diff --git a/pkg/trait/service_test.go b/pkg/trait/service_test.go index 4d0f3f0859..2d9ab23539 100644 --- a/pkg/trait/service_test.go +++ b/pkg/trait/service_test.go @@ -740,3 +740,84 @@ func TestServiceAutoConfiguration(t *testing.T) { require.NoError(t, err) assert.Equal(t, ptr.To(true), traits.Service.Enabled) } + +func TestServiceAnnotations(t *testing.T) { + catalog, err := camel.DefaultCatalog() + require.NoError(t, err) + + client, _ := test.NewFakeClient() + traitCatalog := NewCatalog(nil) + + compressedRoute, err := gzip.CompressBase64([]byte(`from("netty-http:test").log("hello")`)) + require.NoError(t, err) + + environment := Environment{ + CamelCatalog: catalog, + Catalog: traitCatalog, + Client: client, + Integration: &v1.Integration{ + ObjectMeta: metav1.ObjectMeta{ + Name: ServiceTestName, + Namespace: "ns", + }, + Status: v1.IntegrationStatus{ + Phase: v1.IntegrationPhaseDeploying, + }, + Spec: v1.IntegrationSpec{ + Profile: v1.TraitProfileKubernetes, + Sources: []v1.SourceSpec{ + { + DataSpec: v1.DataSpec{ + Name: "routes.js", + Content: string(compressedRoute), + Compression: true, + }, + Language: v1.LanguageJavaScript, + }, + }, + Traits: v1.Traits{ + Service: &traitv1.ServiceTrait{ + Auto: ptr.To(true), + Annotations: map[string]string{ + "annotation-1": "value-1", + "annotation-2": "value-2", + }, + }, + }, + }, + }, + IntegrationKit: &v1.IntegrationKit{ + Status: v1.IntegrationKitStatus{ + Phase: v1.IntegrationKitPhaseReady, + }, + }, + Platform: &v1.IntegrationPlatform{ + Spec: v1.IntegrationPlatformSpec{ + Cluster: v1.IntegrationPlatformClusterOpenShift, + Build: v1.IntegrationPlatformBuildSpec{ + PublishStrategy: v1.IntegrationPlatformBuildPublishStrategyS2I, + Registry: v1.RegistrySpec{Address: "registry"}, + RuntimeVersion: catalog.Runtime.Version, + }, + }, + Status: v1.IntegrationPlatformStatus{ + Phase: v1.IntegrationPlatformPhaseReady, + }, + }, + EnvVars: make([]corev1.EnvVar, 0), + ExecutedTraits: make([]Trait, 0), + Resources: kubernetes.NewCollection(), + } + environment.Platform.ResyncStatusFullConfig() + + _, _, err = traitCatalog.apply(&environment) + + require.NoError(t, err) + s := environment.Resources.GetService(func(service *corev1.Service) bool { + return service.Name == ServiceTestName + }) + assert.NotNil(t, s) + assert.NotNil(t, s.Annotations) + assert.Equal(t, "value-1", s.Annotations["annotation-1"]) + assert.Equal(t, "value-2", s.Annotations["annotation-2"]) +}