Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
Pull example code into eventing-contrib (#801) (#803)
Browse files Browse the repository at this point in the history
(cherry picked from commit de462bc)

add appender to release, alphatebize files (#802)

(cherry picked from commit ffe7343)
  • Loading branch information
vaikas authored and knative-prow-robot committed Dec 17, 2019
1 parent c409ee0 commit f93b3df
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
98 changes: 98 additions & 0 deletions cmd/appender/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"log"
"os"

cloudevents "github.com/cloudevents/sdk-go"
"github.com/kelseyhightower/envconfig"
)

/*
Small function that demonstrates two concepts:
1. Deserializing / Serializing data
2. Using direct reply to modify incoming events (decorating in this example)
Incoming data is assumbed to be CloudEventBaseData with two fields
Sequence int
Message string
This function appends env[MESSAGE] to the Message portion of the data.
It is used by the samples in the Knative sequence section:
https://knative.dev/docs/eventing/sequence/
*/

type envConfig struct {
Msg string `envconfig:"MESSAGE" default:"boring default msg, change me with env[MESSAGE]"`
}

var (
env envConfig
)

// Define a small struct representing the data for our expected data.
type cloudEventBaseData struct {
Sequence int `json:"id"`
Message string `json:"message"`
}

func gotEvent(event cloudevents.Event, resp *cloudevents.EventResponse) error {
ctx := event.Context.AsV1()

data := &cloudEventBaseData{}
if err := event.DataAs(data); err != nil {
log.Printf("Got Data Error: %s\n", err.Error())
return err
}

log.Println("Received a new event: ")
log.Printf("[%s] %s %s: %+v", ctx.Time.String(), ctx.GetSource(), ctx.GetType(), data)

// append eventMsgAppender to message of the data
data.Message = data.Message + env.Msg
r := cloudevents.Event{
Context: ctx,
Data: data,
}

r.SetDataContentType(cloudevents.ApplicationJSON)

log.Println("Transform the event to: ")
log.Printf("[%s] %s %s: %+v", ctx.Time.String(), ctx.GetSource(), ctx.GetType(), data)

resp.RespondWith(200, &r)
return nil
}

func main() {
if err := envconfig.Process("", &env); err != nil {
log.Printf("[ERROR] Failed to process env var: %s", err)
os.Exit(1)
}

c, err := cloudevents.NewDefaultClient()
if err != nil {
log.Fatalf("failed to create client, %v", err)
}

log.Printf("listening on 8080, appending %q to events", env.Msg)
log.Fatalf("failed to start receiver: %s", c.StartReceiver(context.Background(), gotEvent))
}
30 changes: 30 additions & 0 deletions config/tools/appender/appender.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is a very simple Knative Service that writes the incoming CloudEvent to its log.
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: first
namespace: seqtest4
labels:
contrib.eventing.knative.dev/release: devel
spec:
template:
spec:
containers:
- env:
- name: MESSAGE
value: "...your message goes here..."
image: knative.dev/eventing-contrib/cmd/appender
11 changes: 6 additions & 5 deletions hack/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ source $(dirname $0)/../vendor/knative.dev/test-infra/scripts/release.sh
# Yaml files to generate, and the source config dir for them.
declare -A COMPONENTS
COMPONENTS=(
["github.yaml"]="github/config"
["event-display.yaml"]="config/tools/event-display"
["appender.yaml"]="config/tools/appender"
["awssqs.yaml"]="awssqs/config"
["camel.yaml"]="camel/source/config"
["couchdb.yaml"]="couchdb/source/config"
["event-display.yaml"]="config/tools/event-display"
["github.yaml"]="github/config"
["kafka-source.yaml"]="kafka/source/config"
["kafka-channel.yaml"]="kafka/channel/config"
["natss-channel.yaml"]="natss/config"
["awssqs.yaml"]="awssqs/config"
["couchdb.yaml"]="couchdb/source/config"
["prometheus-source.yaml"]="prometheus/config"
)
readonly COMPONENTS
Expand All @@ -47,7 +48,7 @@ function build_release() {
local all_yamls=()
for yaml in "${!COMPONENTS[@]}"; do
local config="${COMPONENTS[${yaml}]}"
echo "Building Knative Eventing Sources - ${config}"
echo "Building Knative Eventing Contrib - ${config}"
ko resolve ${KO_FLAGS} -f ${config}/ | "${LABEL_YAML_CMD[@]}" > ${yaml}
all_yamls+=(${yaml})
done
Expand Down

0 comments on commit f93b3df

Please sign in to comment.