/
mqttconnector_app.go
114 lines (89 loc) · 3.5 KB
/
mqttconnector_app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) arkade author(s) 2022. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package apps
import (
"fmt"
"github.com/alexellis/arkade/pkg/config"
"strconv"
"github.com/alexellis/arkade/pkg/apps"
"github.com/alexellis/arkade/pkg/types"
"github.com/alexellis/arkade/pkg"
"github.com/spf13/cobra"
)
func MakeInstallMQTTConnector() *cobra.Command {
var command = &cobra.Command{
Use: "mqtt-connector",
Short: "Install mqtt-connector for OpenFaaS",
Long: `Install mqtt-connector for OpenFaaS so that you can invoke functions when
messages are received on a given topic on an MQTT broker.`,
Example: ` arkade install mqtt-connector`,
SilenceUsage: true,
}
command.Flags().StringP("namespace", "n", "openfaas", "The namespace used for installation")
command.Flags().Bool("update-repo", true, "Update the helm repo")
command.Flags().StringP("topics", "t", "", "The topics for the connector to bind to, currently supports one topic")
command.Flags().String("broker-host", "tcp://test.mosquitto.org:1883", "The host for the MQTT broker")
command.Flags().String("client-id", "mqtt-connector-1", "The client ID for the MQTT broker")
command.Flags().Bool("async", false, "Invoke functions asynchronously from events")
command.Flags().StringArray("set", []string{},
"Use custom flags or override existing flags \n(example --set key=value)")
command.RunE = func(command *cobra.Command, args []string) error {
kubeConfigPath, _ := command.Flags().GetString("kubeconfig")
updateRepo, _ := command.Flags().GetBool("update-repo")
async, err := command.Flags().GetBool("async")
if err != nil {
return err
}
namespace, _ := command.Flags().GetString("namespace")
clientID, _ := command.Flags().GetString("client-id")
topicsVal, err := command.Flags().GetString("topics")
if err != nil {
return err
}
brokerHostVal, err := command.Flags().GetString("broker-host")
if err != nil {
return err
}
overrides := map[string]string{
"topic": topicsVal,
"broker": brokerHostVal,
"clientID": clientID,
"asyncInvoke": strconv.FormatBool(async),
}
customFlags, err := command.Flags().GetStringArray("set")
if err != nil {
return fmt.Errorf("error with --set usage: %s", err)
}
if err := config.MergeFlags(overrides, customFlags); err != nil {
return err
}
if len(topicsVal) == 0 {
return fmt.Errorf("--topics is required")
}
if len(brokerHostVal) == 0 {
return fmt.Errorf("--broker-host is required")
}
mqttConnectorAppOptions := types.DefaultInstallOptions().
WithNamespace(namespace).
WithHelmRepo("openfaas/mqtt-connector").
WithHelmURL("https://openfaas.github.io/faas-netes/").
WithOverrides(overrides).
WithHelmUpdateRepo(updateRepo).
WithKubeconfigPath(kubeConfigPath)
_, err = apps.MakeInstallChart(mqttConnectorAppOptions)
if err != nil {
return err
}
fmt.Println(mqttConnectorInstallMsg)
return nil
}
return command
}
const MQTTConnectorInfoMsg = `# View the connector's logs:
kubectl logs deploy/mqtt-connector -n openfaas -f
# Find out more on the project homepage:
# https://github.com/openfaas/mqtt-connector/`
const mqttConnectorInstallMsg = `=======================================================================
= mqtt-connector has been installed. =
=======================================================================` +
"\n\n" + MQTTConnectorInfoMsg + "\n\n" + pkg.SupportMessageShort