This is an automated email from the ASF dual-hosted git repository.

astefanutti 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 3356260  feat(native): Add support for Knative service gradual roll-out
3356260 is described below

commit 335626003fe6016427c1b66c0d2af94ecc5e31df
Author: Antonin Stefanutti <[email protected]>
AuthorDate: Fri Oct 1 11:58:26 2021 +0200

    feat(native): Add support for Knative service gradual roll-out
---
 docs/modules/traits/pages/knative-service.adoc | 12 +++--
 pkg/resources/resources.go                     |  4 +-
 pkg/trait/knative_service.go                   | 46 ++++++++++------
 pkg/trait/knative_service_test.go              | 74 ++++++++++++++++++++++++++
 resources/traits.yaml                          | 13 +++--
 5 files changed, 125 insertions(+), 24 deletions(-)

diff --git a/docs/modules/traits/pages/knative-service.adoc 
b/docs/modules/traits/pages/knative-service.adoc
index 27d91b5..aed8751 100755
--- a/docs/modules/traits/pages/knative-service.adoc
+++ b/docs/modules/traits/pages/knative-service.adoc
@@ -1,11 +1,11 @@
 = Knative Service Trait
 
 // Start of autogenerated code - DO NOT EDIT! (description)
-The Knative Service trait allows to configure options when running the 
integration as Knative service instead of
+The Knative Service trait allows configuring options when running the 
Integration as a Knative service, instead of
 a standard Kubernetes Deployment.
 
-Running integrations as Knative Services adds auto-scaling (and 
scaling-to-zero) features, but those features
-are only meaningful when the routes use a HTTP endpoint consumer.
+Running an Integration as a Knative Service enables auto-scaling (and 
scaling-to-zero), but those features
+are only relevant when the Camel route(s) use(s) an HTTP endpoint consumer.
 
 
 This trait is available in the following profiles: **Knative**.
@@ -61,6 +61,12 @@ Knative has its own cap value that depends on the 
installation.
 
 Refer to the Knative documentation for more information.
 
+| knative-service.rollout-duration
+| string
+| 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.
+
 | knative-service.auto
 | bool
 | Automatically deploy the integration as Knative service when all conditions 
hold:
diff --git a/pkg/resources/resources.go b/pkg/resources/resources.go
index 619ae95..0564232 100644
--- a/pkg/resources/resources.go
+++ b/pkg/resources/resources.go
@@ -539,9 +539,9 @@ var assets = func() http.FileSystem {
                "/traits.yaml": &vfsgen۰CompressedFileInfo{
                        name:             "traits.yaml",
                        modTime:          time.Time{},
-                       uncompressedSize: 42535,
+                       uncompressedSize: 43087,
 
-                       compressedContent: 
[]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\x6b\x73\x5b\xb9\xb1\xe0\x77\xff\x0a\x94\xee\x56\x59\x52\x91\x94\x27\xb9\x49\x66\xb5\x3b\x9b\xd2\x78\x3c\x89\x66\xfc\xd0\xda\xce\x64\x6f\x79\xa7\x42\xf0\x9c\x26\x09\x0b\x04\x4e\x00\x1c\xc9\xcc\xde\xfd\xef\x5b\xe8\x6e\x3c\x0e\x49\x49\x94\xc7\x9a\x8d\x6e\xdd\xe4\xc3\x58\xd2\x01\xd0\x68\x34\xfa\xdd\x8d\xe0\xa4\x0a\xfe\xf4\xc9\x58\x18\xb9\x82\x53\x21\xe7\x73\x65\x54\x58\x3f\x11\xa2\xd3\x32\xcc\xad\x5b\x9d\x8a\xb9\x
 [...]
+                       compressedContent: 
[]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\xbd\x7d\x73\x1b\x37\xd2\x20\xfe\xbf\x3f\x05\x8a\xcf\xaf\xca\x92\x8a\xa4\x9c\xdd\x67\x77\xf3\xd3\x9d\x6f\x4b\xb1\x9d\x44\x89\x5f\x74\xb6\x93\xbd\x2d\x5f\x6a\x09\xce\x34\x49\x58\x33\xc0\x2c\x80\x91\xcc\xbd\xe7\xbe\xfb\x15\xba\x1b\x2f\x43\x52\x12\xe5\x58\xb9\xe8\xea\xd9\xfd\x23\x96\x34\x00\x1a\x8d\x46\xbf\x77\xc3\x5b\xa9\xbc\x3b\x79\x34\x11\x5a\xb6\x70\x22\xe4\x62\xa1\xb4\xf2\xeb\x47\x42\x74\x8d\xf4\x0b\x63\xdb\x13\x
 [...]
                },
        }
        fs["/"].(*vfsgen۰DirInfo).entries = []os.FileInfo{
diff --git a/pkg/trait/knative_service.go b/pkg/trait/knative_service.go
index 34d5063..7859e08 100644
--- a/pkg/trait/knative_service.go
+++ b/pkg/trait/knative_service.go
@@ -33,18 +33,21 @@ import (
 )
 
 const (
+       // Auto-scaling annotations
        knativeServingClassAnnotation    = "autoscaling.knative.dev/class"
        knativeServingMetricAnnotation   = "autoscaling.knative.dev/metric"
        knativeServingTargetAnnotation   = "autoscaling.knative.dev/target"
        knativeServingMinScaleAnnotation = "autoscaling.knative.dev/minScale"
        knativeServingMaxScaleAnnotation = "autoscaling.knative.dev/maxScale"
+       // Rollout annotation
+       knativeServingRolloutDurationAnnotation = 
"serving.knative.dev/rolloutDuration"
 )
 
-// The Knative Service trait allows to configure options when running the 
integration as Knative service instead of
+// The Knative Service trait allows configuring options when running the 
Integration as a Knative service, instead of
 // a standard Kubernetes Deployment.
 //
-// Running integrations as Knative Services adds auto-scaling (and 
scaling-to-zero) features, but those features
-// are only meaningful when the routes use a HTTP endpoint consumer.
+// Running an Integration as a Knative Service enables auto-scaling (and 
scaling-to-zero), but those features
+// are only relevant when the Camel route(s) use(s) an HTTP endpoint consumer.
 //
 // +camel-k:trait=knative-service
 type knativeServiceTrait struct {
@@ -71,6 +74,10 @@ type knativeServiceTrait struct {
        //
        // Refer to the Knative documentation for more information.
        MaxScale *int `property:"max-scale" json:"maxScale,omitempty"`
+       // 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.
+       RolloutDuration string `property:"rollout-duration" 
json:"rolloutDuration,omitempty"`
        // Automatically deploy the integration as Knative service when all 
conditions hold:
        //
        // * Integration is using the Knative profile
@@ -214,29 +221,38 @@ func (t *knativeServiceTrait) 
ControllerStrategySelectorOrder() int {
 }
 
 func (t *knativeServiceTrait) getServiceFor(e *Environment) (*serving.Service, 
error) {
-       // Copy annotations from the integration resource
-       annotations := make(map[string]string)
+       serviceAnnotations := make(map[string]string)
        if e.Integration.Annotations != nil {
-               for k, v := range 
filterTransferableAnnotations(e.Integration.Annotations) {
-                       annotations[k] = v
+               for k, v := range e.Integration.Annotations {
+                       serviceAnnotations[k] = v
                }
        }
+       // Set Knative rollout
+       if t.RolloutDuration != "" {
+               serviceAnnotations[knativeServingRolloutDurationAnnotation] = 
t.RolloutDuration
+       }
 
-       // Set Knative auto-scaling behavior
+       revisionAnnotations := make(map[string]string)
+       if e.Integration.Annotations != nil {
+               for k, v := range 
filterTransferableAnnotations(e.Integration.Annotations) {
+                       revisionAnnotations[k] = v
+               }
+       }
+       // Set Knative auto-scaling
        if t.Class != "" {
-               annotations[knativeServingClassAnnotation] = t.Class
+               revisionAnnotations[knativeServingClassAnnotation] = t.Class
        }
        if t.Metric != "" {
-               annotations[knativeServingMetricAnnotation] = t.Metric
+               revisionAnnotations[knativeServingMetricAnnotation] = t.Metric
        }
        if t.Target != nil {
-               annotations[knativeServingTargetAnnotation] = 
strconv.Itoa(*t.Target)
+               revisionAnnotations[knativeServingTargetAnnotation] = 
strconv.Itoa(*t.Target)
        }
        if t.MinScale != nil && *t.MinScale > 0 {
-               annotations[knativeServingMinScaleAnnotation] = 
strconv.Itoa(*t.MinScale)
+               revisionAnnotations[knativeServingMinScaleAnnotation] = 
strconv.Itoa(*t.MinScale)
        }
        if t.MaxScale != nil && *t.MaxScale > 0 {
-               annotations[knativeServingMaxScaleAnnotation] = 
strconv.Itoa(*t.MaxScale)
+               revisionAnnotations[knativeServingMaxScaleAnnotation] = 
strconv.Itoa(*t.MaxScale)
        }
 
        svc := serving.Service{
@@ -250,14 +266,14 @@ func (t *knativeServiceTrait) getServiceFor(e 
*Environment) (*serving.Service, e
                        Labels: map[string]string{
                                v1.IntegrationLabel: e.Integration.Name,
                        },
-                       Annotations: e.Integration.Annotations,
+                       Annotations: serviceAnnotations,
                },
                Spec: serving.ServiceSpec{
                        ConfigurationSpec: serving.ConfigurationSpec{
                                Template: serving.RevisionTemplateSpec{
                                        ObjectMeta: metav1.ObjectMeta{
                                                Labels:      
label.AddLabels(e.Integration.Name),
-                                               Annotations: annotations,
+                                               Annotations: 
revisionAnnotations,
                                        },
                                        Spec: serving.RevisionSpec{
                                                PodSpec: corev1.PodSpec{
diff --git a/pkg/trait/knative_service_test.go 
b/pkg/trait/knative_service_test.go
index 333449b..faa5ed5 100644
--- a/pkg/trait/knative_service_test.go
+++ b/pkg/trait/knative_service_test.go
@@ -21,8 +21,10 @@ import (
        "testing"
 
        "github.com/stretchr/testify/assert"
+
        corev1 "k8s.io/api/core/v1"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
        serving "knative.dev/serving/pkg/apis/serving/v1"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
@@ -321,3 +323,75 @@ func TestKnativeServiceWithResr(t *testing.T) {
                return service.Name == KnativeServiceTestName
        }))
 }
+
+func TestKnativeServiceWithRollout(t *testing.T) {
+       catalog, err := camel.DefaultCatalog()
+       assert.Nil(t, err)
+
+       traitCatalog := NewCatalog(nil)
+
+       environment := Environment{
+               CamelCatalog: catalog,
+               Catalog:      traitCatalog,
+               Integration: &v1.Integration{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      KnativeServiceTestName,
+                               Namespace: KnativeServiceTestNamespace,
+                       },
+                       Status: v1.IntegrationStatus{
+                               Phase: v1.IntegrationPhaseDeploying,
+                       },
+                       Spec: v1.IntegrationSpec{
+                               Profile: v1.TraitProfileKnative,
+                               Sources: []v1.SourceSpec{
+                                       {
+                                               DataSpec: v1.DataSpec{
+                                                       Name:    "routes.js",
+                                                       Content: 
`from("direct:test").log("hello")`,
+                                               },
+                                               Language: v1.LanguageJavaScript,
+                                       },
+                                       {
+                                               DataSpec: v1.DataSpec{
+                                                       Name:    "rests.xml",
+                                                       Content: `<rest 
path="/test"></rest>`,
+                                               },
+                                               Language: v1.LanguageXML,
+                                       },
+                               },
+                               Traits: map[string]v1.TraitSpec{
+                                       "knative-service": 
test.TraitSpecFromMap(t, map[string]interface{}{
+                                               "rolloutDuration": "60s",
+                                       }),
+                               },
+                       },
+               },
+               IntegrationKit: &v1.IntegrationKit{
+                       Status: v1.IntegrationKitStatus{
+                               Phase: v1.IntegrationKitPhaseReady,
+                       },
+               },
+               Platform: &v1.IntegrationPlatform{
+                       Spec: v1.IntegrationPlatformSpec{
+                               Cluster: 
v1.IntegrationPlatformClusterKubernetes,
+                       },
+               },
+               EnvVars:        make([]corev1.EnvVar, 0),
+               ExecutedTraits: make([]Trait, 0),
+               Resources:      kubernetes.NewCollection(),
+       }
+       environment.Platform.ResyncStatusFullConfig()
+
+       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")
+}
diff --git a/resources/traits.yaml b/resources/traits.yaml
index 36b9031..4d0357b 100755
--- a/resources/traits.yaml
+++ b/resources/traits.yaml
@@ -487,10 +487,10 @@ traits:
   platform: false
   profiles:
   - Knative
-  description: The Knative Service trait allows to configure options when 
running
-    the integration as Knative service instead of a standard Kubernetes 
Deployment.
-    Running integrations as Knative Services adds auto-scaling (and 
scaling-to-zero)
-    features, but those features are only meaningful when the routes use a 
HTTP endpoint
+  description: The Knative Service trait allows configuring options when 
running the
+    Integration as a Knative service, instead of a standard Kubernetes 
Deployment.
+    Running an Integration as a Knative Service enables auto-scaling (and 
scaling-to-zero),
+    but those features are only relevant when the Camel route(s) use(s) an 
HTTP endpoint
     consumer.
   properties:
   - name: enabled
@@ -523,6 +523,11 @@ traits:
     description: An upper bound for the number of Pods that can be running in 
parallel
       for the integration.Knative has its own cap value that depends on the 
installation.Refer
       to the Knative documentation for more information.
+  - name: rollout-duration
+    type: string
+    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: auto
     type: bool
     description: Automatically deploy the integration as Knative service when 
all

Reply via email to