Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Create agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Marrero committed Sep 16, 2019
0 parents commit 257291e
Show file tree
Hide file tree
Showing 84 changed files with 16,878 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# OSX leaves these everywhere on SMB shares
._*

# OSX trash
.DS_Store

# Eclipse files
.classpath
.project
.settings/**

# Emacs save files
*~
\#*\#
.\#*

# Vim-related files
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist

# NGINX Plus license files
*.crt
*.key

# Visual Studio Code settings
.vscode

# Default certificate and key
default.pem

# Dockerfiles for building
/Dockerfile

# Binary
nginx-ns1-gslb

# Development configuration file
dev_config.yaml
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: go
sudo: required
services:
- docker
go:
- "1.11"
script:
- echo "nginx-ns1-gslb - commit:${TRAVIS_COMMIT}"
- make BUILD_IN_CONTAINER=0 all
before_install:
- echo "PR Slug:${TRAVIS_PULL_REQUEST_SLUG}"
- if [[ "${TRAVIS_PULL_REQUEST_SLUG}" == "nginxinc/nginx-ns1-gslb" || "${TRAVIS_PULL_REQUEST}" == "false" ]]; then
wget https://${fossalocation}/${fossafile} && tar xzf ${fossafile}
&& ./fossa init
&& FOSSA_API_KEY=${fossapush} ./fossa analyze -t nginx-ns1-gslb -b ${TRAVIS_BRANCH} ; fi
44 changes: 44 additions & 0 deletions Gopkg.lock

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

42 changes: 42 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/nginxinc/nginx-plus-go-sdk"
version = "0.4.0"

[[constraint]]
branch = "v2"
name = "gopkg.in/ns1/ns1-go.v2"

[[constraint]]
name = "gopkg.in/yaml.v2"
version = "2.2.2"
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2017 Nginx, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
all: container

VERSION = 0.1-1
TAG = $(VERSION)
PREFIX = nginx/nginx-ns1-gslb

DOCKER_RUN = docker run --rm -v $(shell pwd):/go/src/github.com/nginxinc/nginx-ns1-gslb
DOCKER_BUILD_RUN = docker run --rm -v $(shell pwd):/go/src/github.com/nginxinc/nginx-ns1-gslb -w /go/src/github.com/nginxinc/nginx-ns1-gslb/cmd/agent/
BUILD_IN_CONTAINER = 1
DOCKERFILEPATH = build
GOLANG_CONTAINER = golang:1.11
CONFIG_FILE=configs/example_global.yaml

nginx-ns1-gslb:
ifeq ($(BUILD_IN_CONTAINER),1)
$(DOCKER_BUILD_RUN) -e CGO_ENABLED=0 $(GOLANG_CONTAINER) go build -installsuffix cgo -ldflags "-w" -o /go/src/github.com/nginxinc/nginx-ns1-gslb/nginx-ns1-gslb
else
CGO_ENABLED=0 GOOS=linux go build -installsuffix cgo -ldflags "-w" -o nginx-ns1-gslb github.com/nginxinc/nginx-ns1-gslb/cmd/agent
endif

test:
ifeq ($(BUILD_IN_CONTAINER),1)
$(DOCKER_RUN) $(GOLANG_CONTAINER) go test ./...
else
go test ./...
endif

lint:
golangci-lint run

container: test nginx-ns1-gslb
cp $(DOCKERFILEPATH)/Dockerfile ./Dockerfile
docker build --build-arg CONFIG_FILE=$(CONFIG_FILE) -f Dockerfile -t $(PREFIX):$(TAG) .

clean:
rm -f nginx-ns1-gslb
rm -f Dockerfile
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# NGINX Plus - NS1 Global Server Load Balancing

**nginx-ns1-gslb** allows [NGINX Plus](https://www.nginx.com/products/nginx) to connect with NS1 API to use [NS1 managed DNS](https://ns1.com/products/managed-dns) to create a Global Server Load Balancing solution that load balances connections or requests across two or more distinct data centers or points of presence (PoPs).

## Prerequisites
* At least 1 working instance of NGINX Plus reachable from the host where you are running this agent
* NS1 API credentials

## Configuration
The configuration of the agent is managed by a YAML file. Check the [configuration](configs/README.md) readme for more information.

The agent will try to open the file and configure itself, there might be 2 problems when doing this:

* The file can't be opened (due to a bad path or file permissions)
* The file is opened but the data can't be used (errors or missing required parameters)

Both cases will make the agent fail to start with an error to describe the problem.

## Running the agent

### Locally (using Go >= 1.11)

`go run cmd/agent/main.go --config-file <path/to/your_file.yaml>`

This will run an Agent that fetches stats from one or more NGINX Plus instances, and updates the remote NS1 feeds.

### Docker

1. Build the image specifying the config file:
`make CONFIG_FILE=<path/to/your_file.yaml> container`

1. Run the container:
`docker run nginx/nginx-ns1-gslb:<version>`

**Note:** consider the following while running the agent:

* All NGINX Plus instances must be reachable and running when the agent is run for the first time.
* While running, if at least 1 Instance of NGINX Plus is working, the agent will use the data from that one.
* While running, if all NGINX Plus instances are off, the agent will send `{up: false}` to NS1 API for all the configured services.

## Tests
Run `make test` to run the tests. Set `BUILD_IN_CONTAINER` to 0 to run the tests locally instead of inside a container.
9 changes: 9 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM alpine:3.9
ARG CONFIG_FILE

RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*

COPY nginx-ns1-gslb /
COPY ${CONFIG_FILE} /etc/nginx-ns1-gslb/config.yaml

ENTRYPOINT ["/nginx-ns1-gslb", "-config-file", "/etc/nginx-ns1-gslb/config.yaml"]
46 changes: 46 additions & 0 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"flag"
"log"
"os"
"os/signal"
"syscall"

"github.com/nginxinc/nginx-ns1-gslb/internal/agent"
)

var (
configFile = flag.String("config-file", "", "Path to the agent configuration file")
)

func main() {
flag.Parse()

if *configFile == "" {
log.Fatalf("config-file must be specified")
}

globalConfig, err := agent.ParseConfig(configFile)
if err != nil {
log.Fatalf("Error generating the config: %v", err)
}

a, err := agent.New(globalConfig)

if err != nil {
log.Fatalf("Error creating the agent: %v", err)
}

go handleTermination()
a.Run()
}

// handleTermination makes the agent exit on SIGTERM or SIGINT
func handleTermination() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
s := <-sigChan
log.Printf("%v signal received, shutting down the agent", s)
os.Exit(0)
}
Loading

0 comments on commit 257291e

Please sign in to comment.