Files
fission-src/fission/mqtrigger.go
T
Peter HueneandSoam Vasani 4d0e6af5de Implement support for Azure storage message queue triggers (#371)
These commits implement support for consuming messages from an Azure storage queue to trigger Fission functions.

* Add stubbed Azure message queue implementation and modify Helm charts.

This commit stubs an implementation for an Azure storage message queue trigger
that will be completed by future commits.

It also modifies the Helm chart to add support for deploying Fission with an
mqtrigger configured for Azure storage queue triggers.

* Add Azure Go SDK to glide.

This commit adds the Azure Go SDK to glide for the upcoming work to support
Azure storage queue triggers.

* Implement Azure message queue trigger.

This commit implements a message queue trigger based on Azure storage queues.

Required message queue trigger manager environment variables:

* AZURE_STORAGE_ACCOUNT_NAME - the Azure storage account to use.
* AZURE_STORAGE_ACCOUNT_KEY - the Azure storage account key.

When creating a message queue trigger, the topic will be the Azure storage
queue to receive messages from.

* Add CA certificates to fission-bundle.

This commit adds the root CA certificates to the fission-bundle image. This
allows Fission to contact third-party APIs that use HTTPS with root CA
signed certificates.

* Add Makefile to build and test.

This commit adds a simple Makefile for building the client and bundle, running
tests, creating the Docker image, and pushing the Docker image.
2018-02-09 10:38:06 -08:00

196 lines
4.9 KiB
Go

/*
Copyrigtt 2017 The Fission 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
tttp://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 (
"fmt"
"os"
"text/tabwriter"
"github.com/satori/go.uuid"
"github.com/urfave/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/fission/fission"
"github.com/fission/fission/crd"
"github.com/fission/fission/mqtrigger/messageQueue"
)
func mqtCreate(c *cli.Context) error {
client := getClient(c.GlobalString("server"))
mqtName := c.String("name")
if len(mqtName) == 0 {
mqtName = uuid.NewV4().String()
}
fnName := c.String("function")
if len(fnName) == 0 {
fatal("Need a function name to create a trigger, use --function")
}
mqType := c.String("mqtype")
switch mqType {
case "":
mqType = messageQueue.NATS
case messageQueue.NATS:
mqType = messageQueue.NATS
case messageQueue.ASQ:
mqType = messageQueue.ASQ
default:
fatal("Unknown message queue type, currently only \"nats-streaming, azure-storage-queue \" is supported")
}
// TODO: check topic availability
topic := c.String("topic")
if len(topic) == 0 {
fatal("Listen topic cannot be empty")
}
respTopic := c.String("resptopic")
if topic == respTopic {
// TODO maybe this should just be a warning, perhaps
// allow it behind a --force flag
fatal("Listen topic should not equal to response topic")
}
contentType := c.String("contenttype")
if len(contentType) == 0 {
contentType = "application/json"
}
checkMQTopicAvailability(mqType, topic, respTopic)
mqt := crd.MessageQueueTrigger{
Metadata: metav1.ObjectMeta{
Name: mqtName,
Namespace: metav1.NamespaceDefault,
},
Spec: fission.MessageQueueTriggerSpec{
FunctionReference: fission.FunctionReference{
Type: fission.FunctionReferenceTypeFunctionName,
Name: fnName,
},
MessageQueueType: mqType,
Topic: topic,
ResponseTopic: respTopic,
ContentType: contentType,
},
}
_, err := client.MessageQueueTriggerCreate(&mqt)
checkErr(err, "create message queue trigger")
fmt.Printf("trigger '%s' created\n", mqtName)
return err
}
func mqtGet(c *cli.Context) error {
return nil
}
func mqtUpdate(c *cli.Context) error {
client := getClient(c.GlobalString("server"))
mqtName := c.String("name")
if len(mqtName) == 0 {
fatal("Need name of trigger, use --name")
}
topic := c.String("topic")
respTopic := c.String("resptopic")
fnName := c.String("function")
contentType := c.String("contenttype")
mqt, err := client.MessageQueueTriggerGet(&metav1.ObjectMeta{
Name: mqtName,
Namespace: metav1.NamespaceDefault,
})
checkErr(err, "get Time trigger")
checkMQTopicAvailability(mqt.Spec.MessageQueueType, topic, respTopic)
updated := false
if len(topic) > 0 {
mqt.Spec.Topic = topic
updated = true
}
if len(respTopic) > 0 {
mqt.Spec.ResponseTopic = respTopic
updated = true
}
if len(fnName) > 0 {
mqt.Spec.FunctionReference.Name = fnName
updated = true
}
if len(contentType) > 0 {
mqt.Spec.ContentType = contentType
updated = true
}
if !updated {
fatal("Nothing to update. Use --topic, --resptopic, or --function.")
}
_, err = client.MessageQueueTriggerUpdate(mqt)
checkErr(err, "update Time trigger")
fmt.Printf("trigger '%v' updated\n", mqtName)
return nil
}
func mqtDelete(c *cli.Context) error {
client := getClient(c.GlobalString("server"))
mqtName := c.String("name")
if len(mqtName) == 0 {
fatal("Need name of trigger to delete, use --name")
}
err := client.MessageQueueTriggerDelete(&metav1.ObjectMeta{
Name: mqtName,
Namespace: metav1.NamespaceDefault,
})
checkErr(err, "delete trigger")
fmt.Printf("trigger '%v' deleted\n", mqtName)
return nil
}
func mqtList(c *cli.Context) error {
client := getClient(c.GlobalString("server"))
mqts, err := client.MessageQueueTriggerList(c.String("mqtype"))
checkErr(err, "list message queue triggers")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\n",
"NAME", "FUNCTION_NAME", "MESSAGE_QUEUE_TYPE", "TOPIC", "RESPONSE_TOPIC", "PUB_MSG_CONTENT_TYPE")
for _, mqt := range mqts {
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\n",
mqt.Metadata.Name, mqt.Spec.FunctionReference.Name, mqt.Spec.MessageQueueType, mqt.Spec.Topic, mqt.Spec.ResponseTopic, mqt.Spec.ContentType)
}
w.Flush()
return nil
}
func checkMQTopicAvailability(mqType string, topics ...string) {
for _, t := range topics {
if len(t) > 0 && !messageQueue.IsTopicValid(mqType, t) {
fatal(fmt.Sprintf("Invalid topic for %s: %s", mqType, t))
}
}
}