This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/main by this push: new 1c8fb0148 Feat(trait): Knative service visibility support (#1712) 1c8fb0148 is described below commit 1c8fb01487999f9204946a0074e5bf28b7afc6c8 Author: halmakidon <1182835+halmaki...@users.noreply.github.com> AuthorDate: Wed Aug 10 19:51:08 2022 +0900 Feat(trait): Knative service visibility support (#1712) - Add visibility option to knative service trait. - Extract creating test environment method for Knative service trait --- .../camel.apache.org_integrationplatforms.yaml | 12 ++++++ .../crd/bases/camel.apache.org_integrations.yaml | 6 +++ .../bases/camel.apache.org_kameletbindings.yaml | 7 ++++ docs/modules/ROOT/partials/apis/camel-k-crds.adoc | 10 +++++ docs/modules/traits/pages/knative-service.adoc | 7 ++++ helm/camel-k/crds/crd-integration-platform.yaml | 12 ++++++ helm/camel-k/crds/crd-integration.yaml | 6 +++ helm/camel-k/crds/crd-kamelet-binding.yaml | 7 ++++ pkg/apis/camel/v1/trait/knative_service.go | 5 +++ pkg/trait/knative_service.go | 17 +++++--- pkg/trait/knative_service_test.go | 48 ++++++++++++++++------ resources/traits.yaml | 5 +++ 12 files changed, 124 insertions(+), 18 deletions(-) diff --git a/config/crd/bases/camel.apache.org_integrationplatforms.yaml b/config/crd/bases/camel.apache.org_integrationplatforms.yaml index cda07f954..f01c288f1 100644 --- a/config/crd/bases/camel.apache.org_integrationplatforms.yaml +++ b/config/crd/bases/camel.apache.org_integrationplatforms.yaml @@ -1204,6 +1204,12 @@ spec: default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. type: string + visibility: + description: "Setting `cluster-local`, Knative service becomes + a private service. Specifically, this option applies the + `networking.knative.dev/visibility` label to Knative service. + \n Refer to the Knative documentation for more information." + type: string type: object logging: description: The configuration of Logging trait @@ -2832,6 +2838,12 @@ spec: default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. type: string + visibility: + description: "Setting `cluster-local`, Knative service becomes + a private service. Specifically, this option applies the + `networking.knative.dev/visibility` label to Knative service. + \n Refer to the Knative documentation for more information." + type: string type: object logging: description: The configuration of Logging trait diff --git a/config/crd/bases/camel.apache.org_integrations.yaml b/config/crd/bases/camel.apache.org_integrations.yaml index 8108cf337..5c8bf1f2c 100644 --- a/config/crd/bases/camel.apache.org_integrations.yaml +++ b/config/crd/bases/camel.apache.org_integrations.yaml @@ -6681,6 +6681,12 @@ spec: default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. type: string + visibility: + description: "Setting `cluster-local`, Knative service becomes + a private service. Specifically, this option applies the + `networking.knative.dev/visibility` label to Knative service. + \n Refer to the Knative documentation for more information." + type: string type: object logging: description: The configuration of Logging trait diff --git a/config/crd/bases/camel.apache.org_kameletbindings.yaml b/config/crd/bases/camel.apache.org_kameletbindings.yaml index 5821108e2..de5240eb0 100644 --- a/config/crd/bases/camel.apache.org_kameletbindings.yaml +++ b/config/crd/bases/camel.apache.org_kameletbindings.yaml @@ -6975,6 +6975,13 @@ spec: `time.Duration` string representation, rounded to a second precision. type: string + visibility: + description: "Setting `cluster-local`, Knative service + becomes a private service. Specifically, this option + applies the `networking.knative.dev/visibility` label + to Knative service. \n Refer to the Knative documentation + for more information." + type: string type: object logging: description: The configuration of Logging trait diff --git a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc index 79006bfd8..96eb012ad 100644 --- a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc +++ b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc @@ -4880,6 +4880,16 @@ Enables to gradually shift traffic to the latest Revision and sets the rollout d It's disabled by default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. +|`visibility` + +string +| + + +Setting `cluster-local`, Knative service becomes a private service. +Specifically, this option applies the `networking.knative.dev/visibility` label to Knative service. + +Refer to the Knative documentation for more information. + |`auto` + bool | diff --git a/docs/modules/traits/pages/knative-service.adoc b/docs/modules/traits/pages/knative-service.adoc index aed875154..08c9a1e6a 100755 --- a/docs/modules/traits/pages/knative-service.adoc +++ b/docs/modules/traits/pages/knative-service.adoc @@ -67,6 +67,13 @@ Refer to the Knative documentation for more information. It's disabled by default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. +| knative-service.visibility +| string +| Setting `cluster-local`, Knative service becomes a private service. +Specifically, this option applies the `networking.knative.dev/visibility` label to Knative service. + +Refer to the Knative documentation for more information. + | knative-service.auto | bool | Automatically deploy the integration as Knative service when all conditions hold: diff --git a/helm/camel-k/crds/crd-integration-platform.yaml b/helm/camel-k/crds/crd-integration-platform.yaml index cda07f954..f01c288f1 100644 --- a/helm/camel-k/crds/crd-integration-platform.yaml +++ b/helm/camel-k/crds/crd-integration-platform.yaml @@ -1204,6 +1204,12 @@ spec: default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. type: string + visibility: + description: "Setting `cluster-local`, Knative service becomes + a private service. Specifically, this option applies the + `networking.knative.dev/visibility` label to Knative service. + \n Refer to the Knative documentation for more information." + type: string type: object logging: description: The configuration of Logging trait @@ -2832,6 +2838,12 @@ spec: default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. type: string + visibility: + description: "Setting `cluster-local`, Knative service becomes + a private service. Specifically, this option applies the + `networking.knative.dev/visibility` label to Knative service. + \n Refer to the Knative documentation for more information." + type: string type: object logging: description: The configuration of Logging trait diff --git a/helm/camel-k/crds/crd-integration.yaml b/helm/camel-k/crds/crd-integration.yaml index 8108cf337..5c8bf1f2c 100644 --- a/helm/camel-k/crds/crd-integration.yaml +++ b/helm/camel-k/crds/crd-integration.yaml @@ -6681,6 +6681,12 @@ spec: default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. type: string + visibility: + description: "Setting `cluster-local`, Knative service becomes + a private service. Specifically, this option applies the + `networking.knative.dev/visibility` label to Knative service. + \n Refer to the Knative documentation for more information." + type: string type: object logging: description: The configuration of Logging trait diff --git a/helm/camel-k/crds/crd-kamelet-binding.yaml b/helm/camel-k/crds/crd-kamelet-binding.yaml index 5821108e2..de5240eb0 100644 --- a/helm/camel-k/crds/crd-kamelet-binding.yaml +++ b/helm/camel-k/crds/crd-kamelet-binding.yaml @@ -6975,6 +6975,13 @@ spec: `time.Duration` string representation, rounded to a second precision. type: string + visibility: + description: "Setting `cluster-local`, Knative service + becomes a private service. Specifically, this option + applies the `networking.knative.dev/visibility` label + to Knative service. \n Refer to the Knative documentation + for more information." + type: string type: object logging: description: The configuration of Logging trait diff --git a/pkg/apis/camel/v1/trait/knative_service.go b/pkg/apis/camel/v1/trait/knative_service.go index 3c1e83d56..af8764e2b 100644 --- a/pkg/apis/camel/v1/trait/knative_service.go +++ b/pkg/apis/camel/v1/trait/knative_service.go @@ -52,6 +52,11 @@ type KnativeServiceTrait struct { // It's disabled by default and must be expressed as a Golang `time.Duration` string representation, // rounded to a second precision. RolloutDuration string `property:"rollout-duration" json:"rolloutDuration,omitempty"` + // Setting `cluster-local`, Knative service becomes a private service. + // Specifically, this option applies the `networking.knative.dev/visibility` label to Knative service. + // + // Refer to the Knative documentation for more information. + Visibility string `property:"visibility" json:"visibility,omitempty"` // Automatically deploy the integration as Knative service when all conditions hold: // // * Integration is using the Knative profile diff --git a/pkg/trait/knative_service.go b/pkg/trait/knative_service.go index 129781028..e0a8bf62a 100644 --- a/pkg/trait/knative_service.go +++ b/pkg/trait/knative_service.go @@ -45,6 +45,8 @@ const ( knativeServingMaxScaleAnnotation = "autoscaling.knative.dev/maxScale" // Rollout annotation. knativeServingRolloutDurationAnnotation = "serving.knative.dev/rolloutDuration" + // visibility label. + knativeServingVisibilityLabel = "networking.knative.dev/visibility" ) type knativeServiceTrait struct { @@ -224,17 +226,22 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) (*serving.Service, e revisionAnnotations[knativeServingMaxScaleAnnotation] = strconv.Itoa(*t.MaxScale) } + serviceLabels := map[string]string{ + v1.IntegrationLabel: e.Integration.Name, + } + if t.Visibility != "" { + serviceLabels[knativeServingVisibilityLabel] = t.Visibility + } + svc := serving.Service{ TypeMeta: metav1.TypeMeta{ Kind: "Service", APIVersion: serving.SchemeGroupVersion.String(), }, ObjectMeta: metav1.ObjectMeta{ - Name: e.Integration.Name, - Namespace: e.Integration.Namespace, - Labels: map[string]string{ - v1.IntegrationLabel: e.Integration.Name, - }, + Name: e.Integration.Name, + Namespace: e.Integration.Namespace, + Labels: serviceLabels, Annotations: serviceAnnotations, }, Spec: serving.ServiceSpec{ diff --git a/pkg/trait/knative_service_test.go b/pkg/trait/knative_service_test.go index 8e28ae373..1cab8fdff 100644 --- a/pkg/trait/knative_service_test.go +++ b/pkg/trait/knative_service_test.go @@ -331,12 +331,42 @@ func TestKnativeServiceWithResr(t *testing.T) { } func TestKnativeServiceWithRollout(t *testing.T) { + environment := createKnativeServiceTestEnvironment(t, &traitv1.KnativeServiceTrait{RolloutDuration: "60s"}) + assert.NotEmpty(t, environment.ExecutedTraits) + assert.NotNil(t, environment.GetTrait("knative-service")) + + ksvc := environment.Resources.GetKnativeService(func(service *serving.Service) bool { + return service.Name == KnativeServiceTestName + }) + assert.NotNil(t, ksvc) + + assert.Equal(t, ksvc.Annotations[knativeServingRolloutDurationAnnotation], "60s") +} + +func TestKnativeServiceWithVisibility(t *testing.T) { + environment := createKnativeServiceTestEnvironment(t, &traitv1.KnativeServiceTrait{ + Visibility: "cluster-local", + }) + assert.NotEmpty(t, environment.ExecutedTraits) + assert.NotNil(t, environment.GetTrait("knative-service")) + + ksvc := environment.Resources.GetKnativeService(func(service *serving.Service) bool { + return service.Name == KnativeServiceTestName + }) + assert.NotNil(t, ksvc) + + assert.Equal(t, ksvc.Labels[knativeServingVisibilityLabel], "cluster-local") +} + +func createKnativeServiceTestEnvironment(t *testing.T, trait *traitv1.KnativeServiceTrait) *Environment { + t.Helper() + catalog, err := camel.DefaultCatalog() assert.Nil(t, err) traitCatalog := NewCatalog(nil) - environment := Environment{ + environment := &Environment{ CamelCatalog: catalog, Catalog: traitCatalog, Integration: &v1.Integration{ @@ -366,9 +396,7 @@ func TestKnativeServiceWithRollout(t *testing.T) { }, }, Traits: v1.Traits{ - KnativeService: &traitv1.KnativeServiceTrait{ - RolloutDuration: "60s", - }, + KnativeService: trait, }, }, }, @@ -386,18 +414,12 @@ func TestKnativeServiceWithRollout(t *testing.T) { ExecutedTraits: make([]Trait, 0), Resources: kubernetes.NewCollection(), } + environment.Platform.ResyncStatusFullConfig() - err = traitCatalog.apply(&environment) + err = traitCatalog.apply(environment) assert.Nil(t, err) - assert.NotEmpty(t, environment.ExecutedTraits) - assert.NotNil(t, environment.GetTrait("knative-service")) - ksvc := environment.Resources.GetKnativeService(func(service *serving.Service) bool { - return service.Name == KnativeServiceTestName - }) - assert.NotNil(t, ksvc) - - assert.Equal(t, ksvc.Annotations[knativeServingRolloutDurationAnnotation], "60s") + return environment } diff --git a/resources/traits.yaml b/resources/traits.yaml index 5d9aa7c9d..2f52eb1dc 100755 --- a/resources/traits.yaml +++ b/resources/traits.yaml @@ -777,6 +777,11 @@ traits: description: Enables to gradually shift traffic to the latest Revision and sets the rollout duration. It's disabled by default and must be expressed as a Golang `time.Duration` string representation, rounded to a second precision. + - name: visibility + type: string + description: Setting `cluster-local`, Knative service becomes a private service. + Specifically, this option applies the `networking.knative.dev/visibility` label + to Knative service. Refer to the Knative documentation for more information. - name: auto type: bool description: 'Automatically deploy the integration as Knative service when all