Add code for slack bot and deployment
This commit is contained in:
parent
868fa5f5cd
commit
12bcc9ac57
8
.dockerignore
Normal file
8
.dockerignore
Normal file
@ -0,0 +1,8 @@
|
||||
bin/
|
||||
.vscode/
|
||||
.git/
|
||||
*.log
|
||||
config.hcl
|
||||
Dockerfile
|
||||
.gitignore
|
||||
manifests/
|
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
.idea/
|
||||
|
||||
.vscode/
|
||||
!.vscode/launch.json
|
||||
|
||||
bin/
|
||||
!bin/.gitkeep
|
||||
|
||||
*.log
|
||||
|
||||
config.hcl
|
53
Dockerfile
Normal file
53
Dockerfile
Normal file
@ -0,0 +1,53 @@
|
||||
FROM --platform=$BUILDPLATFORM golang:1.16.0-alpine3.13 AS builder
|
||||
# ex. amd64
|
||||
ARG TARGETARCH
|
||||
# ex. linux/amd64
|
||||
ARG BUILDPLATFORM
|
||||
|
||||
RUN apk update && \
|
||||
apk add --no-cache git ca-certificates tzdata gcc libc-dev && \
|
||||
update-ca-certificates
|
||||
|
||||
ENV USER=appuser
|
||||
ENV UID=10001
|
||||
ENV GID=10001
|
||||
WORKDIR /data
|
||||
|
||||
RUN addgroup \
|
||||
--gid "${GID}" \
|
||||
"${USER}" && \
|
||||
adduser \
|
||||
--disabled-password \
|
||||
--gecos "" \
|
||||
--home "/nonexistent" \
|
||||
--shell "/sbin/nologin" \
|
||||
--no-create-home \
|
||||
--uid "${UID}" \
|
||||
-G "${USER}" \
|
||||
"${USER}"
|
||||
|
||||
COPY go.mod .
|
||||
COPY go.sum .
|
||||
RUN go mod download && \
|
||||
go mod verify
|
||||
|
||||
COPY . .
|
||||
RUN export GOARCH=$TARGETARCH; \
|
||||
export GOOS=$(echo $BUILDPLATFORM | cut -d'/' -f1); \
|
||||
export GOBUILDFLAGS="-a -tags netgo -installsuffix netgo"; \
|
||||
export LDFLAGS="-w -s -d"; \
|
||||
export CGO_ENABLED=0; \
|
||||
go build -o bin/bot-$GOOS-$GOARCH cmd/main.go
|
||||
|
||||
FROM scratch
|
||||
|
||||
USER appuser:appuser
|
||||
|
||||
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
|
||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||
COPY --from=builder /etc/passwd /etc/passwd
|
||||
COPY --from=builder /etc/group /etc/group
|
||||
COPY --from=builder --chown=appuser:appuser /data/bin/* /go/bin/bot
|
||||
|
||||
WORKDIR /go
|
||||
ENTRYPOINT ["/go/bin/bot"]
|
19
Makefile
Normal file
19
Makefile
Normal file
@ -0,0 +1,19 @@
|
||||
.PHONY: docker-build
|
||||
|
||||
BOT_VERSION=latest
|
||||
GOARCH=amd64
|
||||
# GOARCH=arm
|
||||
GOOS=darwin
|
||||
# GOOS=linux
|
||||
|
||||
build: dependencies
|
||||
@echo "build for os $(GOOS) and arch $(GOARCH)"
|
||||
go build -o bin/slackbot-$(GOOS)-$(GOARCH) cmd/main.go
|
||||
|
||||
dependencies:
|
||||
go mod download
|
||||
go mod verify
|
||||
|
||||
docker-build:
|
||||
DOCKER_BUILDKIT=1 docker build --force-rm -t docker.registry:5000/slackbot:$(BOT_VERSION) .
|
||||
docker push docker.registry:5000/slackbot:$(BOT_VERSION)
|
74
cmd/main.go
Normal file
74
cmd/main.go
Normal file
@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/slack-bot/pkg/check"
|
||||
"go/slack-bot/pkg/notify"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
"github.com/hashicorp/hcl/v2/gohcl"
|
||||
"github.com/hashicorp/hcl/v2/hclsyntax"
|
||||
)
|
||||
|
||||
type SlackConf struct {
|
||||
BotID string `hcl:"bot_id"`
|
||||
DestChannelID string `hcl:"dest_channel_id"`
|
||||
WebhookID string `hcl:"webhook_id"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
var configFile string
|
||||
flag.StringVar(&configFile, "config", "config.hcl", "hcl application configuration file")
|
||||
flag.Parse()
|
||||
|
||||
conf := ParseConfiguration(configFile)
|
||||
slackNotifier := notify.NewSlackNotifier(conf.BotID, conf.DestChannelID, conf.WebhookID)
|
||||
|
||||
msg := check.NewCliChecker().
|
||||
Check()
|
||||
|
||||
slackNotifier.Notify(msg)
|
||||
}
|
||||
|
||||
//ParseConfiguration take filename path and parse it to Config.SlackConf
|
||||
func ParseConfiguration(filename string) *SlackConf {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatalf("Fail to determine current directory to load default ./config.hcl file, %+v\n", err)
|
||||
}
|
||||
log.Printf("current dir is %s", dir)
|
||||
|
||||
if filename == "config.hcl" {
|
||||
filename = fmt.Sprintf("%s%c%s", dir, os.PathSeparator, filename)
|
||||
}
|
||||
relativeFilename, err := filepath.Rel(dir, filename)
|
||||
if err != nil {
|
||||
log.Fatalf("Fail to determine configuration relative directory for file %s, %+v", filename, err)
|
||||
}
|
||||
|
||||
src, err := ioutil.ReadFile(relativeFilename)
|
||||
if err != nil {
|
||||
log.Fatalf("Missing required parameter filename got : %s, %+v", relativeFilename, err)
|
||||
}
|
||||
file, diags := hclsyntax.ParseConfig(src, relativeFilename, hcl.Pos{Line: 1, Column: 1})
|
||||
if diags.HasErrors() {
|
||||
log.Fatalf("parse config error, %s", diags.Error())
|
||||
}
|
||||
|
||||
config := &SlackConf{}
|
||||
|
||||
diags = gohcl.DecodeBody(file.Body, nil, config)
|
||||
if diags.HasErrors() {
|
||||
log.Fatalf("error config decode, %s", diags.Error())
|
||||
}
|
||||
|
||||
if config.BotID == "" || config.DestChannelID == "" || config.WebhookID == "" {
|
||||
log.Fatal("Missing required parameter : bot_id, dest_channel_id or webhook_id")
|
||||
}
|
||||
return config
|
||||
}
|
3
config.sample.hcl
Normal file
3
config.sample.hcl
Normal file
@ -0,0 +1,3 @@
|
||||
bot_id = ""
|
||||
dest_channel_id = ""
|
||||
webhook_id = ""
|
9
go.mod
Normal file
9
go.mod
Normal file
@ -0,0 +1,9 @@
|
||||
module go/slack-bot
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/hashicorp/hcl/v2 v2.9.1
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/slack-go/slack v0.8.2 // indirect
|
||||
)
|
82
go.sum
Normal file
82
go.sum
Normal file
@ -0,0 +1,82 @@
|
||||
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
|
||||
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
|
||||
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3 h1:ZSTrOEhiM5J5RFxEaFvMZVEAM1KvT1YzbEOwB2EAGjA=
|
||||
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
|
||||
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
|
||||
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
|
||||
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
|
||||
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
|
||||
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.4 h1:87PNWwrRvUSnqS4dlcBU/ftvOIBep4sYuBLlh6rX2wk=
|
||||
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hashicorp/hcl/v2 v2.9.1 h1:eOy4gREY0/ZQHNItlfuEZqtcQbXIxzojlP301hDpnac=
|
||||
github.com/hashicorp/hcl/v2 v2.9.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4=
|
||||
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/slack-go/slack v0.8.2 h1:D7jNu0AInBfdQ4QyKPtVSp+ZxQes3EzWW17RZ/va4JE=
|
||||
github.com/slack-go/slack v0.8.2/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM=
|
||||
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
|
||||
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvcciqcxEHac4CYj89xI=
|
||||
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
|
||||
github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvCazn8G65U=
|
||||
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
|
||||
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
|
||||
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
|
||||
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
|
||||
github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA=
|
||||
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
|
||||
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY34Wul7O/MSKey3txpPYyCqVO5ZyceuQJEI=
|
||||
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo=
|
||||
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82 h1:vsphBvatvfbhlb4PO1BYSr9dzugGxJ/SQHoNufZJq1w=
|
||||
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
|
||||
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
34
manifests/.gitignore
vendored
Normal file
34
manifests/.gitignore
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
# Local .terraform directories
|
||||
**/.terraform/*
|
||||
|
||||
# .tfstate files
|
||||
*.tfstate
|
||||
*.tfstate.*
|
||||
|
||||
# Crash log files
|
||||
crash.log
|
||||
|
||||
# Exclude all .tfvars files, which are likely to contain sentitive data, such as
|
||||
# password, private keys, and other secrets. These should not be part of version
|
||||
# control as they are data points which are potentially sensitive and subject
|
||||
# to change depending on the environment.
|
||||
#
|
||||
*.tfvars
|
||||
|
||||
# Ignore override files as they are usually used to override resources locally and so
|
||||
# are not checked in
|
||||
override.tf
|
||||
override.tf.json
|
||||
*_override.tf
|
||||
*_override.tf.json
|
||||
|
||||
# Include override files you do wish to add to version control using negated pattern
|
||||
#
|
||||
# !example_override.tf
|
||||
|
||||
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
|
||||
# example: *tfplan*
|
||||
|
||||
# Ignore CLI configuration files
|
||||
.terraformrc
|
||||
terraform.rc
|
21
manifests/.terraform.lock.hcl
generated
Normal file
21
manifests/.terraform.lock.hcl
generated
Normal file
@ -0,0 +1,21 @@
|
||||
# This file is maintained automatically by "terraform init".
|
||||
# Manual edits may be lost in future updates.
|
||||
|
||||
provider "registry.terraform.io/hashicorp/kubernetes" {
|
||||
version = "2.0.3"
|
||||
constraints = ">= 2.0.0"
|
||||
hashes = [
|
||||
"h1:u3QLP3ca1ZYtdx4t9LrNLzAA3HY/GAT06hznb2pqNC4=",
|
||||
"zh:3847733636ed2aca8694227ee6936fecc6cec9573818ecf64acc2a01a4fb3ae4",
|
||||
"zh:44d3e88227174d2e51e0273e732e54bb5f5d8150bbf1adecd2be198c857a0264",
|
||||
"zh:7c147baeac90ddc71b8c106409dc2e06e6fe04448a0ae36c7ef919b8b27c8d5f",
|
||||
"zh:832ebd1eec844fdc00ecbc5e694ecf532d0c7a94f2a1697ea0c7bc159696d529",
|
||||
"zh:920c8f64925d346a0621aadede6a4ac1bbe650f16c2074a0a6b5eeb7a5c35cf7",
|
||||
"zh:a26906cd3aeb28f402972154dbb3ae1ac6d739ce3e4b00906c9d4b1e263d5a56",
|
||||
"zh:a7b3cbd06684f843f700ee9281c583cc593e00e3acc2c0a34f6dad4e35a1ec60",
|
||||
"zh:b43c05ade832995a09c40a1c2f080586f38de1edc8c46be2e6f90b96c58a2482",
|
||||
"zh:cba2a979889e79ffe48bb5ea7e63f88c344ae2cd7a341afb77b3c8d417658d6c",
|
||||
"zh:e00b2ec7357a58a435a69146da3c256178caab83a6574969d4660b69996c7955",
|
||||
"zh:f95f179f68aed39b8d73d7914176baf74509d74f90a1e3a600a8eb4461f8f0a3",
|
||||
]
|
||||
}
|
72
manifests/common.tf
Normal file
72
manifests/common.tf
Normal file
@ -0,0 +1,72 @@
|
||||
resource "kubernetes_namespace" "monitoring_namespace" {
|
||||
metadata {
|
||||
name = "monitoring"
|
||||
}
|
||||
}
|
||||
|
||||
resource "kubernetes_config_map" "slack_bot_config" {
|
||||
metadata {
|
||||
name = "slack-bot-hcl"
|
||||
namespace = kubernetes_namespace.monitoring_namespace.id
|
||||
}
|
||||
data = {
|
||||
"config.hcl" = <<EOF
|
||||
bot_id = "${var.bot_id}"
|
||||
dest_channel_id = "${var.dest_channel_id}"
|
||||
webhook_id = "${var.webhook_id}"
|
||||
EOF
|
||||
}
|
||||
}
|
||||
|
||||
locals {
|
||||
config_volume_name = "system-bot-volume"
|
||||
}
|
||||
|
||||
resource "kubernetes_cron_job" "slack_bot_checker" {
|
||||
metadata {
|
||||
name = "system-cron-bot-job"
|
||||
namespace = kubernetes_namespace.monitoring_namespace.id
|
||||
}
|
||||
spec {
|
||||
concurrency_policy = "Replace"
|
||||
failed_jobs_history_limit = 5
|
||||
# each day at 00:01
|
||||
schedule = "1 0 * * *"
|
||||
starting_deadline_seconds = 10
|
||||
successful_jobs_history_limit = 10
|
||||
job_template {
|
||||
metadata {
|
||||
name = "system-bot-job"
|
||||
}
|
||||
spec {
|
||||
backoff_limit = 2
|
||||
ttl_seconds_after_finished = 10
|
||||
template {
|
||||
metadata {
|
||||
labels = {
|
||||
app = "system-bot"
|
||||
env = var.environment
|
||||
}
|
||||
}
|
||||
spec {
|
||||
volume {
|
||||
name = local.config_volume_name
|
||||
config_map {
|
||||
name = kubernetes_config_map.slack_bot_config.metadata.0.name
|
||||
}
|
||||
}
|
||||
container {
|
||||
name = "slackbot"
|
||||
image = format("docker.registry/slackbot:%s", var.bot_version)
|
||||
args = ["-config", "/conf/config.hcl"]
|
||||
volume_mount {
|
||||
mount_path = "/conf"
|
||||
name = local.config_volume_name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
28
manifests/configure.tf
Normal file
28
manifests/configure.tf
Normal file
@ -0,0 +1,28 @@
|
||||
terraform {
|
||||
required_version = ">= 0.12"
|
||||
|
||||
required_providers {
|
||||
kubernetes = {
|
||||
source = "hashicorp/kubernetes"
|
||||
version = ">= 2.0"
|
||||
}
|
||||
}
|
||||
|
||||
backend "etcdv3" {
|
||||
endpoints = ["https://100.64.0.19:2379"]
|
||||
lock = true
|
||||
prefix = "/terraform-state/slackbot/"
|
||||
|
||||
cacert_path = "/Users/antoine/virtualization/kubernetes-the-hard-way/certs/ca.pem"
|
||||
cert_path = "/Users/antoine/virtualization/kubernetes-the-hard-way/certs/kubernetes.pem"
|
||||
key_path = "/Users/antoine/virtualization/kubernetes-the-hard-way/certs/kubernetes-key.pem"
|
||||
}
|
||||
}
|
||||
|
||||
provider "kubernetes" {
|
||||
config_path = "~/.kube/config.kubeconfig"
|
||||
|
||||
config_context = "my-context"
|
||||
config_context_cluster = "cluster-1"
|
||||
config_context_auth_info = "admin"
|
||||
}
|
23
manifests/variable.tf
Normal file
23
manifests/variable.tf
Normal file
@ -0,0 +1,23 @@
|
||||
variable "environment" {
|
||||
default = "prod"
|
||||
}
|
||||
|
||||
variable "bot_id" {
|
||||
description = "slack bot id"
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "dest_channel_id" {
|
||||
description = "slack alerting target channel id"
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "webhook_id" {
|
||||
description = "slack webhook secret id"
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "bot_version" {
|
||||
default = "latest"
|
||||
description = "slack bot container version"
|
||||
}
|
24
pkg/check/command.go
Normal file
24
pkg/check/command.go
Normal file
@ -0,0 +1,24 @@
|
||||
package check
|
||||
|
||||
import "go/slack-bot/pkg/notify"
|
||||
|
||||
type Checker interface {
|
||||
Check() notify.Message
|
||||
}
|
||||
|
||||
//CliChecker implement checker with cli command
|
||||
type CliChecker struct {
|
||||
}
|
||||
|
||||
//NewCliChecker buld new cli checker
|
||||
func NewCliChecker() *CliChecker {
|
||||
return &CliChecker{}
|
||||
}
|
||||
|
||||
//Check function use to run cli call to check some parameter
|
||||
func (cc *CliChecker) Check() notify.Message {
|
||||
return notify.Message{
|
||||
Content: "full disk",
|
||||
ShouldNotify: true,
|
||||
}
|
||||
}
|
51
pkg/notify/slack.go
Normal file
51
pkg/notify/slack.go
Normal file
@ -0,0 +1,51 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
const slackHookBaseURL = "hooks.slack.com/services"
|
||||
|
||||
//Notifier interface represent mean to send notification
|
||||
type Notifier interface {
|
||||
Notify(message string)
|
||||
}
|
||||
|
||||
//Message store and represent alerting message
|
||||
type Message struct {
|
||||
ShouldNotify bool
|
||||
Content string
|
||||
}
|
||||
|
||||
type SlackNotifier struct {
|
||||
BotID string
|
||||
DestChannelID string
|
||||
WebhookID string
|
||||
}
|
||||
|
||||
//NewSlackNotifier create slack notifier
|
||||
func NewSlackNotifier(BotID, DestChannelID, WebhookID string) *SlackNotifier {
|
||||
return &SlackNotifier{
|
||||
BotID: BotID,
|
||||
DestChannelID: DestChannelID,
|
||||
WebhookID: WebhookID,
|
||||
}
|
||||
}
|
||||
|
||||
//Notify send notification to channel
|
||||
func (sn *SlackNotifier) Notify(message Message) {
|
||||
if message.ShouldNotify {
|
||||
msg := &slack.WebhookMessage{
|
||||
Text: message.Content,
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://%s/%s/%s/%s", slackHookBaseURL, sn.BotID, sn.DestChannelID, sn.WebhookID)
|
||||
err := slack.PostWebhook(url, msg)
|
||||
if err != nil {
|
||||
log.Printf("Send message error %+v", err)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user