Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Container Hosts/Projects register in Consul + refactoring
Browse files Browse the repository at this point in the history
Reorganized a lot of code to do with external connections and moved them out of the connections package
Saves project metadata in Consul KV under its windlass_worker@hostname for below reasons
Worker will check Consul KV for any projects associated with it on startup and spin up goroutine for health check
Same topic, implemented Ping for LXD container host for said health check
On creation of a project, it is also registered in Consul, KV and service
LXD Host repo keeps in-memory the client TLS certs/keys while also persisting them in Vault for on next startup
EstablishConnections checks connections on startup instead. Do we wanna change this to be in degraded state rather than outright not running?
  • Loading branch information
Strum355 committed Oct 30, 2019
1 parent ed64cc4 commit 3d9ef06
Show file tree
Hide file tree
Showing 18 changed files with 564 additions and 338 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
6 changes: 1 addition & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
FROM golang:1.12 AS dev
FROM golang:1.13 AS dev

WORKDIR /windlass-worker

RUN go get github.com/go-task/task/cmd/task \
github.com/derekparker/delve/cmd/dlv \
github.com/nomad-software/vend

ENV GO111MODULES=on

COPY go.mod .
COPY go.sum .

Expand All @@ -19,8 +17,6 @@ RUN go install github.com/UCCNetworkingSociety/Windlass-worker/cmd/windlass-work

RUN go mod vendor && vend

ENV GO111MODULES=off

CMD [ "go", "run", "cmd/windlass-worker/main.go" ]

FROM alpine
Expand Down
2 changes: 1 addition & 1 deletion app/api/v1/projectsEndpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewProjectEndpoints(r chi.Router) {
}

r.Route("/projects", func(r chi.Router) {
r.Post("/", middleware.WithContext(projectEndpoint.createProject, time.Second*20))
r.Post("/", middleware.WithContext(projectEndpoint.createProject, time.Second*40))
})
}

Expand Down
97 changes: 15 additions & 82 deletions app/connections/connections.go
Original file line number Diff line number Diff line change
@@ -1,106 +1,39 @@
package connections

import (
consul "github.com/hashicorp/consul/api"
vault "github.com/hashicorp/vault/api"

lxd "github.com/lxc/lxd/client"

"github.com/Strum355/log"
"github.com/spf13/viper"
)

type Connections struct {
lxd lxd.ContainerServer
consul *ConsulProvider
vault *VaultProvider
}

var group Connections
"github.com/UCCNetworkingSociety/Windlass-worker/app/repositories/providers"
)

func EstablishConnections() error {
func TestConnections() error {
var err error

if _, err = GetConsul(); err != nil {
return err
}

if _, err = GetVault(); err != nil {
return err
}

if err := group.consul.GetAndSetSharedSecret(); err != nil {
if err = testConsul(); err != nil {
return err
}

if _, err = GetLXD(); err != nil {
if err = testVault(); err != nil {
return err
}

log.Debug("connections established")
log.Debug("connections tested successfully")
return nil
}

func Close() {

func testVault() error {
_, err := providers.NewVaultProvider()
return err
}

func GetVault() (*VaultProvider, error) {
if group.vault != nil {
return group.vault, nil
}

config := vault.Config{
Address: viper.GetString("vault.url"),
}

provider, err := newVaultProvider(&config)
func testConsul() error {
p, err := providers.NewConsulProvider()
if err != nil {
return nil, err
}

provider.client.SetToken(viper.GetString("vault.token"))

return provider, nil
}

func GetConsul() (*ConsulProvider, error) {
if group.consul != nil {
return group.consul, nil
}

config := consul.Config{
Address: viper.GetString("consul.url"),
Token: viper.GetString("consul.token"),
}

provider, err := newConsulProvider(&config)
if err != nil {
return nil, NewConnectionError(err, "Consul")
}

if err := provider.Register(); err != nil {
return nil, NewConnectionError(err, "Consul")
}

group.consul = provider

return group.consul, nil
}

func GetLXD() (lxd.ContainerServer, error) {
if group.lxd != nil {
return group.lxd, nil
return err
}

lxdConn, err := lxd.ConnectLXDUnix(viper.GetString("lxd.socket"), &lxd.ConnectionArgs{
UserAgent: "Windlass",
})
if err != nil {
return nil, NewConnectionError(err, "LXD")
if err := p.Register(); err != nil {
return err
}

group.lxd = lxdConn

return lxdConn, nil
return p.GetAndSetSharedSecret()
}
153 changes: 0 additions & 153 deletions app/connections/consulProvider.go

This file was deleted.

34 changes: 0 additions & 34 deletions app/connections/vaultProvider.go

This file was deleted.

19 changes: 14 additions & 5 deletions app/repositories/containerHost/containerHostRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package host

import (
"context"
)
"fmt"

type Pinger interface {
Ping(ctx context.Context) error
}
"github.com/spf13/viper"
)

type ContainerHostRepository interface {
Pinger
Ping(ctx context.Context) error
UseCerts(clientKeyPEM, clientCertPEM, caPEM []byte)
GetContainerHostIP(ctx context.Context, name string) (string, error)
CreateContainerHost(ctx context.Context, opts ContainerHostCreateOptions) error
DeleteContainerHost(ctx context.Context, opts ContainerHostDeleteOptions) error
Expand All @@ -19,6 +19,15 @@ type ContainerHostRepository interface {
RestartNGINX(ctx context.Context, name string) error
}

func NewContainerHostRepository() ContainerHostRepository {
hostProvider := viper.GetString("containerHost.type")

if hostProvider == "lxd" {
return NewLXDRepository()
}
panic(fmt.Sprintf("invalid container host %s", hostProvider))
}

type ContainerName struct {
Name string
}
Expand Down
Loading

0 comments on commit 3d9ef06

Please sign in to comment.