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

feat(trait): service annotations #5902

Merged
merged 1 commit into from
Oct 24, 2024
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
7 changes: 7 additions & 0 deletions docs/modules/ROOT/partials/apis/camel-k-crds.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.


|===

Expand Down
4 changes: 4 additions & 0 deletions docs/modules/traits/pages/service.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
40 changes: 40 additions & 0 deletions helm/camel-k/crds/camel-k-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/camel/v1/trait/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/camel/v1/trait/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 7 additions & 6 deletions pkg/trait/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
},
},
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/trait/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}