Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
feat: add mqtt util
Browse files Browse the repository at this point in the history
add util functions for publishing message to MQTT topics.

address #51
  • Loading branch information
Frank Mai authored and guangbochen committed Jun 15, 2020
1 parent 4467133 commit e2d7c48
Show file tree
Hide file tree
Showing 120 changed files with 15,197 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .golangci.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"api\\.pb\\.go"
],
"deadline": "5m",
"tests": true
"tests": true,
"build-tags": [
"test"
]
}
}
13 changes: 13 additions & 0 deletions api/v1alpha1/devicelink_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

// DeviceLinkReferenceRelationship defines the relationship to refer the reference item of DeviceLink.
// +kubebuilder:object:generate=true
// +kubebuilder:object:root=false
type DeviceLinkReferenceRelationship struct {
// Specifies the name of reference.
// +kubebuilder:validation:Required
Name string `json:"name"`

// Specifies the item name of the referred reference.
// +kubebuilder:validation:Required
Item string `json:"item"`
}

// DeviceLinkReferenceSecretSource defines the source of a same name Secret instance.
type DeviceLinkReferenceSecretSource struct {
// Specifies the name of the Secret in the same Namespace to use.
Expand Down
15 changes: 15 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/brain/brain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package brain

import (
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"

ctrl "sigs.k8s.io/controller-runtime"

Expand Down
1 change: 1 addition & 0 deletions cmd/limb/limb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package limb

import (
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"

ctrl "sigs.k8s.io/controller-runtime"

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/rancher/octopus
go 1.13

require (
github.com/256dpi/gomqtt v0.14.2
github.com/bettercap/gatt v0.0.0-20191018133023-569d3d9372bb
github.com/davecgh/go-spew v1.1.1
github.com/eclipse/paho.mqtt.golang v1.2.0
Expand Down
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
github.com/256dpi/gomqtt v0.14.2 h1:TBUKjqBJtU8tfZwiRbpZevzvMWUlzZlbhuzL0hIghok=
github.com/256dpi/gomqtt v0.14.2/go.mod h1:2y4qpvxskFCnQpLIdEY5HG55zqXgAjB88xqrBbZsQRg=
github.com/256dpi/mercury v0.2.0 h1:ImB0JYuZ28kwp2MpqnMdQFSD3z9mgaNYHrSjYuyP0LI=
github.com/256dpi/mercury v0.2.0/go.mod h1:xxgxZSQO7VUwxGLpk8yRVe/WF0MKH7nCIwSh4kUVMy4=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/Azure/go-autorest/autorest v0.9.0 h1:MRvx8gncNaXJqOoLmhNjUAKh33JJF8LyxPhomEtOsjs=
Expand All @@ -20,6 +24,8 @@ github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/abiosoft/ishell v2.0.0+incompatible/go.mod h1:HQR9AqF2R3P4XXpMpI0NAzgHf/aS6+zVXRj14cVk9qg=
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand All @@ -35,6 +41,8 @@ github.com/bettercap/gatt v0.0.0-20191018133023-569d3d9372bb h1:GOaknHS3DCZcBbQR
github.com/bettercap/gatt v0.0.0-20191018133023-569d3d9372bb/go.mod h1:xbRFD+l8RcbQ3DscCiYX0dgEnXbwozZgm6oP2GXic+0=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down Expand Up @@ -70,6 +78,7 @@ github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi
github.com/evanphx/json-patch v4.5.0+incompatible h1:ouOWdg56aJriqS0huScTkVXPC5IcNrDCXZ6OoTAWu7M=
github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:rZfgFAXFS/z/lEd6LJmf9HVZ1LkgYiHx5pHhV5DR16M=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -171,6 +180,8 @@ github.com/gopcua/opcua v0.1.10/go.mod h1:O2l+/u0jM6f3WKRbz5L5ep7yNpQX2l5DOagXqX
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand All @@ -187,6 +198,8 @@ github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpillora/backoff v0.0.0-20170918002102-8eab2debe79d h1:ix3WmphUvN0GDd0DO9MH0v6/5xTv+Xm1bPN+1UJn58k=
github.com/jpillora/backoff v0.0.0-20170918002102-8eab2debe79d/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0=
github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
Expand Down Expand Up @@ -296,6 +309,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down Expand Up @@ -456,6 +470,8 @@ gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/square/go-jose.v2 v2.2.2/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 h1:yiW+nvdHb9LVqSHQBXfZCieqV4fzYhNBql77zY0ykqs=
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637/go.mod h1:BHsqpu/nsuzkT5BpiH1EMZPLyqSMM8JbIavyFACoFNk=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
55 changes: 55 additions & 0 deletions hack/lib/cfssl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"intermediate": {
"usages": [
"signing",
"digital signature",
"key encipherment",
"cert sign",
"crl sign",
"server auth",
"client auth"
],
"expiry": "87600h",
"is_ca": true,
"ca_constraint": {
"is_ca": true,
"max_path_len": 0,
"max_path_len_zero": true
}
},
"peer": {
"usages": [
"signing",
"digital signature",
"key encipherment",
"server auth",
"client auth"
],
"expiry": "87600h"
},
"server": {
"usages": [
"signing",
"digital signing",
"key encipherment",
"server auth"
],
"expiry": "87600h"
},
"client": {
"usages": [
"signing",
"digital signature",
"key encipherment",
"client auth"
],
"expiry": "87600h"
}
}
}
}
24 changes: 24 additions & 0 deletions hack/lib/cfssl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

function octopus::cfssl::install() {
tmp_dir=$(mktemp -d)
pushd "${tmp_dir}" >/dev/null || exit 1
go mod init tmp
go get github.com/cloudflare/cfssl/cmd/...
rm -rf "${tmp_dir}"
popd >/dev/null || return
}

function octopus::cfssl::validate() {
if [[ -n "$(command -v cfssl)" ]] || [[ -n "$(command -v cfssljson)" ]]; then
return 0
fi

octopus::log::info "installing cfssl"
if octopus::cfssl::install; then
octopus::log::info "$(cfssl version 2>&1)"
return 0
fi
octopus::log::error "no cfssl available"
return 1
}
4 changes: 2 additions & 2 deletions hack/lib/cluster-kind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function octopus::cluster_kind::validate() {
return 1
}

function octopus::cluster_kind:setup_configuration() {
function octopus::cluster_kind::setup_configuration() {
local config="/tmp/kind-${CLUSTER_NAME}-cluster-config.yaml"
cat >"${config}" <<EOF
kind: Cluster
Expand Down Expand Up @@ -107,7 +107,7 @@ function octopus::cluster_kind::startup() {

octopus::log::info "creating ${CLUSTER_NAME} cluster with ${K8S_VERSION}"
# setup cluster
octopus::cluster_kind:setup_configuration
octopus::cluster_kind::setup_configuration
local kind_image="kindest/node:${K8S_VERSION}"
kind create cluster --name "${CLUSTER_NAME}" --config "/tmp/kind-${CLUSTER_NAME}-cluster-config.yaml" --image "${kind_image}" --wait 5m

Expand Down
5 changes: 5 additions & 0 deletions hack/make-rules/octopus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function generate() {
octopus::controller_gen::generate \
object:headerFile="${ROOT_DIR}/hack/boilerplate.go.txt" \
paths="${CURR_DIR}/api/..."
octopus::controller_gen::generate \
object:headerFile="${ROOT_DIR}/hack/boilerplate.go.txt" \
paths="${CURR_DIR}/pkg/mqtt/api/..."

octopus::log::info "generating protos"
rm -f "${CURR_DIR}/pkg/adaptor/api/*/*.pb.go"
Expand Down Expand Up @@ -281,10 +284,12 @@ function test() {
# NB(thxCode): race detector doesn't support `arm` arch, ref to:
# - https://golang.org/doc/articles/race_detector.html#Supported_Systems
GOOS=${os} GOARCH=${arch} CGO_ENABLED=1 go test \
-tags=test \
-cover -coverprofile "${CURR_DIR}/dist/coverage_${os}_${arch}.out" \
"${unit_test_targets[@]}"
else
GOOS=${os} GOARCH=${arch} CGO_ENABLED=1 go test \
-tags=test \
-race \
-cover -coverprofile "${CURR_DIR}/dist/coverage_${os}_${arch}.out" \
"${unit_test_targets[@]}"
Expand Down
56 changes: 56 additions & 0 deletions pkg/adaptor/api/v1alpha1/api.pb_extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package v1alpha1

// ReferencesHandler is a convenient `map[string]*ConnectRequestReferenceEntry` handler for obtaining data while avoiding the nil pointer error.
type ReferencesHandler map[string]*ConnectRequestReferenceEntry

// GetData returns the data of specified name and itemName,
// it's always return nil if the data bytes is not existed or empty.
func (h ReferencesHandler) GetData(name, itemName string) []byte {
if len(h) == 0 {
return nil
}

var refItems, refExist = h[name]
if !refExist || len(refItems.Items) == 0 {
return nil
}

var refItem, refItemExist = refItems.Items[itemName]
if !refItemExist {
return nil
}

if len(refItem) == 0 {
return nil
}
return refItem
}

// ToDataMap returns a `map[string]map[string][]byte` that constructed the references data,
// it's always return nil if the references is nil or empty.
func (h ReferencesHandler) ToDataMap() map[string]map[string][]byte {
if len(h) == 0 {
return nil
}

var refMap = make(map[string]map[string][]byte, len(h))
for refKey, refValue := range h {
if refValue == nil || len(refValue.Items) == 0 {
continue
}
var refItems = make(map[string][]byte, len(refValue.Items))
for refItemKey, refItemValue := range refValue.Items {
refItems[refItemKey] = refItemValue
}
refMap[refKey] = refItems
}
return refMap
}

// GetReferencesHandler returns a ReferencesHandler for obtaining data.
func (m *ConnectRequest) GetReferencesHandler() ReferencesHandler {
if m != nil {
return m.References
}
return nil
}
Loading

0 comments on commit e2d7c48

Please sign in to comment.