Skip to content

Commit

Permalink
Update files
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherime committed Apr 10, 2023
1 parent c6a0456 commit 6ab4636
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 57 deletions.
20 changes: 7 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# Binaries for programs and plugins
.env
*.dll
*.exe
*.exe~
*.dll
*.so
*.dylib

.env

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
*.so
*.test
build/
output/
binary/
18 changes: 8 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19-alpine AS builder
FROM golang:1.20-alpine AS builder

ARG ARCH=amd64

Expand All @@ -11,17 +11,15 @@ ENV CGO_ENABLED=0

# Build dependencies
WORKDIR /go/src/
COPY ./src/* .
COPY ./go.mod .
COPY ./go.sum .
COPY ./templates ./templates
RUN apk update && apk add make git
RUN mkdir /go/src/build
RUN go build -o build/amnf
COPY . .
RUN apk update && apk add git
RUN go get ./...
RUN mkdir /go/src/build
RUN go build -a -gcflags=all="-l -B" -ldflags="-w -s" -o build/amnf ./...

# Second stage
FROM alpine:latest
FROM alpine:3.17

COPY --from=builder /go/src/build/amnf /usr/local/bin/amnf
RUN mkdir /etc/amnf
CMD ["/usr/local/bin/amnf"]
EXPOSE 9740
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
IMAGE_NAME = $(shell basename $(shell pwd))
TAG_LATEST = $(IMAGE_NAME):latest
TAG_DATE = $(IMAGE_NAME):$(shell date +"%Y-%m-%dT%H_%M")

.PHONY: all image binary clean

all: image build

image:
docker build . \
-t $(TAG_LATEST) \
-t $(TAG_DATE)

build:
mkdir -p binary
go build -a \
-gcflags=all="-l -B" \
-ldflags="-w -s" \
-o binary/$(IMAGE_NAME) \
./...

clean:
rm -rf binary
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ services:
container_name: redis
networks:
- default
ports:
- 6379:6379
expose:
- 6379
2 changes: 1 addition & 1 deletion src/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ smtpConfig:
password: "example"
from: "me@example.com"
targetEmail:
- "you@yourself.com"
- "you@yourself.com"
53 changes: 31 additions & 22 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strconv"

"github.com/go-redis/redis/v8"
Expand All @@ -18,26 +19,6 @@ import (
"gopkg.in/yaml.v3"
)

// ConfigObject is the object that contains the config information
type ConfigObject struct {
IgnoreConfigFlag bool `yaml:"configFlag"` // If the configFlag is true, config.yaml | config.yml is to be ignored
ConfigFlag bool `yaml:"configFlag"` // If the configFlag is true, config.yaml | config.yml exists
SMTPConfig SMTPConfigObject `yaml:"smtpConfig"` // SMTP config information
}

type SMTPConfigObject struct {
SMTPServer []SMTPServerObject `yaml:"smtpServer"` // SMTP server information (host, port, username, password, fromEmail)
TargetEmail []string `yaml:"targetEmail"` // Target email address
}

type SMTPServerObject struct {
Host string `yaml:"host"` // SMTP server host
Port int `yaml:"port"` // SMTP server port
Username string `yaml:"username"` // SMTP server username
Password string `yaml:"password"` // SMTP server password
FromEmail string `yaml:"fromEmail"` // SMTP server from email address
}

// ConfigLoader loads the config.yaml | config.yml file
func getConfig(c *ConfigObject) *ConfigObject {
// Check if there is a config.yaml | config.yml file in the current directory
Expand Down Expand Up @@ -120,19 +101,47 @@ func getConfig(c *ConfigObject) *ConfigObject {
return c
}

// func shouldSendEmail(alert AlertObject) bool {
// // Add your custom label matching conditions here
// if alert.Labels.Env == "production" && alert.Labels.Severity == "critical" {
// return true
// }
// return false
// }

func templater(a AlertObject) (string, error) {

// Check if the banner image exists
assetPath := "assets/banner.png"
var banner string
if _, err := os.Stat(assetPath); !os.IsNotExist(err) {
banner = filepath.Base(assetPath)
}

// Create the assets object
assets := Assets{
Banner: banner,
}

// Create a new buffer to store the template result
buff := new(bytes.Buffer)

// Parse the template file
t, err := template.ParseFiles("template.html")
t, err := template.ParseFiles("templates/template.html")
if err != nil {
return "", err
}

// Generate the value from the template
err = t.ExecuteTemplate(buff, "template.html", a)
err = t.ExecuteTemplate(buff, "templates/template.html", struct {
Assets interface{}
Labels interface{}
CommonAnnotations interface{}
}{
Assets: assets,
Labels: a.Labels,
CommonAnnotations: a.Annotations,
})
if err != nil {
return "", err
}
Expand Down
25 changes: 25 additions & 0 deletions src/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@ package main

import "time"

type Assets struct {
Banner string
}

// ConfigObject is the object that contains the config information
type ConfigObject struct {
IgnoreConfigFlag bool `yaml:"configFlag"` // If the configFlag is true, config.yaml | config.yml is to be ignored
ConfigFlag bool `yaml:"configFlag"` // If the configFlag is true, config.yaml | config.yml exists
SMTPConfig SMTPConfigObject `yaml:"smtpConfig"` // SMTP config information
}

type SMTPConfigObject struct {
SMTPServer []SMTPServerObject `yaml:"smtpServer"` // SMTP server information (host, port, username, password, fromEmail)
TargetEmail []string `yaml:"targetEmail"` // Target email address
}

type SMTPServerObject struct {
Host string `yaml:"host"` // SMTP server host
Port int `yaml:"port"` // SMTP server port
Username string `yaml:"username"` // SMTP server username
Password string `yaml:"password"` // SMTP server password
FromEmail string `yaml:"fromEmail"` // SMTP server from email address
}

// AlertManagerPayloadObject is the object that contains the payload information
type AlertManagerPayloadObject struct {
Receiver string `json:"receiver"`
Status string `json:"status"`
Expand Down
56 changes: 47 additions & 9 deletions templates/template.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,54 @@
<!--
Customize this template
-->
<!DOCTYPE html>
<html>

<body>
<head>
<link rel="stylesheet" href="styles/dracula-ui.css" />
<style>
/* Add your custom styles here */
.content {
max-width: 800px;
margin: 0 auto;
padding: 2rem;
}

.summary-table {
width: 100%;
border-collapse: collapse;
margin-top: 1rem;
}

.summary-table th,
.summary-table td {
border: 1px solid;
padding: 0.5rem;
text-align: left;
}

.summary-table th {
font-weight: bold;
}
</style>
<div>
<h1>
</head>
<body class="drac-bg-black">
<div class="content drac-text-white">
{{ if .Assets.Banner }}
<img src="{{ .Assets.Banner }}" alt="Banner" style="max-width: 100%; height: auto;"/>
{{ end }}
<h1 class="drac-text-red">
Alert {{ .Labels.Severity }}
</h1>
<div>
<h2>Summary:</h2>
<table class="summary-table drac-text">
<tbody>
{{ range $key, $value := .CommonAnnotations }}
<tr>
<td>{{ $key }}:</td>
<td>{{ $value }}</td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
</body>

</html>
</html>

0 comments on commit 6ab4636

Please sign in to comment.