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

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 1a844c75ac0aeff48bc6f2ea72c0bc09a21e3578
Author: nferraro <ni.ferr...@gmail.com>
AuthorDate: Thu Dec 6 01:10:53 2018 +0100

    Fix knative service generation and documentation
---
 examples/knative/README.adoc | 26 +++++++++++++++++
 pkg/trait/knative.go         | 67 ++++++++++++++++++--------------------------
 2 files changed, 54 insertions(+), 39 deletions(-)

diff --git a/examples/knative/README.adoc b/examples/knative/README.adoc
index d7f8e9b..e392d6b 100644
--- a/examples/knative/README.adoc
+++ b/examples/knative/README.adoc
@@ -3,6 +3,8 @@ Knative Example (Apache Camel K)
 
 This example shows how Camel K can be used to connect Knative building blocks 
to create awesome applications.
 
+A video version of this https://youtu.be/btf_e2GniXM[demo is available on 
YouTube].
+
 It's assumed that both Camel K and Knative are properly installed (including 
Knative Build, Serving and Eventing) into the cluster.
 Refer to the specific documentation to install and configure all components.
 
@@ -98,6 +100,8 @@ If you now stop the feed integration (`kamel delete feed`) 
you will notice that
 
 And if you reinstall the feed again (`kamel run feed.groovy`), the other 
integration will scale up again as soon as they receive messages (splitter 
first, then printer).
 
+== Playing harder
+
 You can also play with different kind of feeds. E.g. the following simple feed 
can be used to bind messages from Telegram to the system:
 
 ```
@@ -108,3 +112,25 @@ 
from('telegram:bots/<put-here-your-botfather-authorization>')
 ```
 
 Now just send messages to your bot with the Telegram client app to see all 
single words appearing in the printer service.
+
+You can also replace the printer with a Slack-based printer like:
+
+```
+from('knative:channel/words')
+  .log('Received: ${body}')
+  .to('slack:#camel-k-tests')
+
+
+context {
+  components {
+    slack {
+      webhookUrl '<put-here-your-slack-incoming-webhook-url>'
+    }
+  }
+}
+```
+
+Now the single words will be printed in the log but also forwarded to the
+slack channel named `#camel-k-tests`.
+
+You have infinite possibilities with Camel!
\ No newline at end of file
diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index 786d475..1687581 100644
--- a/pkg/trait/knative.go
+++ b/pkg/trait/knative.go
@@ -24,7 +24,7 @@ import (
 
        "github.com/operator-framework/operator-sdk/pkg/sdk"
        "github.com/pkg/errors"
-       "k8s.io/api/apps/v1"
+       "strconv"
        "strings"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
@@ -38,22 +38,21 @@ import (
 )
 
 const (
-       knativeKindDeployment = "deployment"
-       knativeKindService    = "service"
+       knativeMinScaleAnnotation = "autoscaling.knative.dev/minScale"
+       knativeMaxScaleAnnotation = "autoscaling.knative.dev/maxScale"
 )
 
 type knativeTrait struct {
-       BaseTrait          `property:",squash"`
-       Kind               string `property:"kind"`
-       Sources            string `property:"sources"`
-       Sinks              string `property:"sinks"`
-       deploymentDelegate *deploymentTrait
+       BaseTrait `property:",squash"`
+       Sources   string `property:"sources"`
+       Sinks     string `property:"sinks"`
+       MinScale  *int   `property:"minScale"`
+       MaxScale  *int   `property:"maxScale"`
 }
 
 func newKnativeTrait() *knativeTrait {
        return &knativeTrait{
-               BaseTrait:          newBaseTrait("knative"),
-               deploymentDelegate: newDeploymentTrait(),
+               BaseTrait: newBaseTrait("knative"),
        }
 }
 
@@ -70,12 +69,12 @@ func (t *knativeTrait) autoconfigure(e *Environment) error {
                channels := t.getSinkChannels(e)
                t.Sinks = strings.Join(channels, ",")
        }
-       if t.Kind == "" {
+       // Check the right value for minScale, as not all services are allowed 
to scale down to 0
+       if t.MinScale == nil {
                meta := metadata.ExtractAll(e.Integration.Spec.Sources)
-               if meta.RequiresHTTPService && meta.PassiveEndpoints {
-                       t.Kind = knativeKindService
-               } else {
-                       t.Kind = knativeKindDeployment
+               if !meta.RequiresHTTPService || !meta.PassiveEndpoints {
+                       single := 1
+                       t.MinScale = &single
                }
        }
        return nil
@@ -88,17 +87,11 @@ func (t *knativeTrait) apply(e *Environment) error {
        for _, sub := range t.getSubscriptionsFor(e) {
                e.Resources.Add(sub)
        }
-       switch t.Kind {
-       case knativeKindService:
-               svc, err := t.getServiceFor(e)
-               if err != nil {
-                       return err
-               }
-               e.Resources.Add(svc)
-               return nil
-       case knativeKindDeployment:
-               return t.addDeployment(e)
+       svc, err := t.getServiceFor(e)
+       if err != nil {
+               return err
        }
+       e.Resources.Add(svc)
        return nil
 }
 
@@ -112,19 +105,6 @@ func (t *knativeTrait) prepareEnvVars(e *Environment) 
error {
        return nil
 }
 
-func (t *knativeTrait) addDeployment(e *Environment) error {
-       if err := t.deploymentDelegate.apply(e); err != nil {
-               return err
-       }
-       e.Resources.VisitDeployment(func(d *v1.Deployment) {
-               if d.Spec.Template.Annotations == nil {
-                       d.Spec.Template.Annotations = make(map[string]string)
-               }
-               d.Spec.Template.Annotations["sidecar.istio.io/inject"] = "true"
-       })
-       return nil
-}
-
 func (t *knativeTrait) getServiceFor(e *Environment) (*serving.Service, error) 
{
        // combine properties of integration with context, integration
        // properties have the priority
@@ -171,6 +151,14 @@ func (t *knativeTrait) getServiceFor(e *Environment) 
(*serving.Service, error) {
                "camel.apache.org/integration": e.Integration.Name,
        }
 
+       annotations := make(map[string]string)
+       if t.MinScale != nil {
+               annotations[knativeMinScaleAnnotation] = 
strconv.Itoa(*t.MinScale)
+       }
+       if t.MaxScale != nil {
+               annotations[knativeMaxScaleAnnotation] = 
strconv.Itoa(*t.MaxScale)
+       }
+
        svc := serving.Service{
                TypeMeta: metav1.TypeMeta{
                        Kind:       "Service",
@@ -187,7 +175,8 @@ func (t *knativeTrait) getServiceFor(e *Environment) 
(*serving.Service, error) {
                                Configuration: serving.ConfigurationSpec{
                                        RevisionTemplate: 
serving.RevisionTemplateSpec{
                                                ObjectMeta: metav1.ObjectMeta{
-                                                       Labels: labels,
+                                                       Labels:      labels,
+                                                       Annotations: 
annotations,
                                                },
                                                Spec: serving.RevisionSpec{
                                                        Container: 
corev1.Container{

Reply via email to