Skip to content

Commit

Permalink
Merge pull request #1 from benjamin-wright/refactor
Browse files Browse the repository at this point in the history
- Swap out cockroach for standard postgres
- Version updates
- Simplify generic state management to avoid needing pointer references
- Use migrations image, to promote bundling migrations into images instead of loading etcd with them in CRDs
- Simplify kube clients and split out resource types into their own packages
  • Loading branch information
benjamin-wright authored May 27, 2024
2 parents 723cb9a + 75a64d9 commit 9e412a8
Show file tree
Hide file tree
Showing 102 changed files with 4,472 additions and 4,070 deletions.
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

export KUBECONFIG=$(pwd)/.scratch/kubeconfig
31 changes: 30 additions & 1 deletion .github/workflows/tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.19'
go-version: '1.22'
- name: Install dependencies
run: go mod download
- name: Build
Expand All @@ -34,6 +34,35 @@ jobs:
file: ./deploy/Dockerfile
push: true
tags: benwright/db-operator:${{ github.ref_name }}
migrations:
runs-on: ubuntu-latest
environment: production
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USER_NAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'
- name: Install dependencies
run: go mod download
- name: Build
run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -v -o ./dist/mig ./cmd/migrations/main.go
- name: Image
uses: docker/build-push-action@v4
with:
context: ./dist
file: ./deploy/Dockerfile
push: true
tags: benwright/db-pg-migrations:${{ github.ref_name }}
chart:
runs-on: ubuntu-latest
environment: production
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.scratch
dist
*.tgz
*.tgz
.DS_Store
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
An operator for creating and managing development databases and other stateful infrastructure

Currently supports:
- CockroachDB
- Postgres
- Redis
- NATs
- NATs

## NOTES

- use the database client to fetch the owner when setting and unsetting permissions
48 changes: 48 additions & 0 deletions cmd/migrations/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"os"

"github.com/benjamin-wright/db-operator/pkg/postgres/config"
"github.com/benjamin-wright/db-operator/pkg/postgres/migrations"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
path, ok := os.LookupEnv("POSTGRES_MIGRATIONS_PATH")
if !ok {
path = "/migrations"
}

log.Info().Msgf("loading migrations from %s", path)
m, err := migrations.LoadMigrations(path)
if err != nil {
log.Fatal().Err(err).Msg("failed to load migrations")
}

cfg, err := config.FromEnv()
if err != nil {
log.Fatal().Err(err).Msg("failed to load config from env")
}

client, err := migrations.New(cfg)
if err != nil {
log.Fatal().Err(err).Msg("failed to create client")
}

log.Info().Msg("initializing client")
err = client.Init()
if err != nil {
log.Fatal().Err(err).Msg("failed to init client")
}

log.Info().Msg("running migrations")
err = client.Run(m)
if err != nil {
log.Fatal().Err(err).Msg("failed to run migrations")
}

log.Info().Msg("migrations complete")
}
1 change: 1 addition & 0 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
}

zerolog.SetGlobalLevel(level)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

log.Info().Msg("Starting operator...")

Expand Down
4 changes: 3 additions & 1 deletion deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM scratch

COPY app /app
ARG BINARY_NAME

COPY ${BINARY_NAME} /app

ENTRYPOINT [ "/app" ]
2 changes: 1 addition & 1 deletion deploy/Test.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ FROM scratch
COPY tests /tests

ENTRYPOINT [ "/tests" ]
CMD [ "-test.run", "Integration", "-test.v" ]
CMD [ "-test.run", "Integration" ]
52 changes: 0 additions & 52 deletions deploy/chart/crds/cockroach-migration.crd.yaml

This file was deleted.

8 changes: 4 additions & 4 deletions deploy/chart/crds/nats-client.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spec:
spec:
type: object
properties:
dbRef:
cluster:
type: object
properties:
name:
Expand All @@ -25,7 +25,7 @@ spec:
required: [ name, namespace ]
secret:
type: string
required: [ dbRef, secret ]
required: [ cluster, secret ]
status:
type: object
properties:
Expand All @@ -44,5 +44,5 @@ spec:
singular: natsclient
kind: NatsClient
shortNames:
- nc
- ncs
- ncl
- ncls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: natsdbs.ponglehub.co.uk
name: natsclusters.ponglehub.co.uk
spec:
group: ponglehub.co.uk
versions:
Expand All @@ -26,9 +26,9 @@ spec:
jsonPath: .status.ready
scope: Namespaced
names:
plural: natsdbs
singular: natsdb
kind: NatsDB
plural: natsclusters
singular: natscluster
kind: NatsCluster
shortNames:
- ndb
- ndbs
- nc
- ncs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: cockroachclients.ponglehub.co.uk
name: postgresclients.ponglehub.co.uk
spec:
group: ponglehub.co.uk
versions:
Expand All @@ -15,7 +15,7 @@ spec:
spec:
type: object
properties:
dbRef:
cluster:
type: object
properties:
name:
Expand All @@ -25,11 +25,13 @@ spec:
required: [ name, namespace ]
database:
type: string
owner:
type: boolean
username:
type: string
secret:
type: string
required: [ dbRef, database, username, secret ]
required: [ cluster, database, username, secret ]
status:
type: object
properties:
Expand All @@ -44,9 +46,9 @@ spec:
jsonPath: .status.ready
scope: Namespaced
names:
plural: cockroachclients
singular: cockroachclient
kind: CockroachClient
plural: postgresclients
singular: postgresclient
kind: PostgresClient
shortNames:
- cc
- ccs
- pgcl
- pgcls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: cockroachdbs.ponglehub.co.uk
name: postgresclusters.ponglehub.co.uk
spec:
group: ponglehub.co.uk
versions:
Expand All @@ -27,13 +27,13 @@ spec:
additionalPrinterColumns:
- name: Ready
type: boolean
description: Defines whether the database is running
description: Defines whether the database cluster is running
jsonPath: .status.ready
scope: Namespaced
names:
plural: cockroachdbs
singular: cockroachdb
kind: CockroachDB
plural: postgresclusters
singular: postgresclusters
kind: PostgresCluster
shortNames:
- cdb
- cdbs
- pgc
- pgcs
8 changes: 4 additions & 4 deletions deploy/chart/crds/redis-client.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spec:
spec:
type: object
properties:
dbRef:
cluster:
type: object
properties:
name:
Expand All @@ -27,7 +27,7 @@ spec:
type: integer
secret:
type: string
required: [ dbRef, unit, secret ]
required: [ cluster, unit, secret ]
status:
type: object
properties:
Expand All @@ -46,5 +46,5 @@ spec:
singular: redisclient
kind: RedisClient
shortNames:
- rc
- rcs
- rcl
- rcls
12 changes: 6 additions & 6 deletions deploy/chart/crds/redis-db.crd.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: redisdbs.ponglehub.co.uk
name: redisclusters.ponglehub.co.uk
spec:
group: ponglehub.co.uk
versions:
Expand Down Expand Up @@ -31,9 +31,9 @@ spec:
jsonPath: .status.ready
scope: Namespaced
names:
plural: redisdbs
singular: redisdb
kind: RedisDB
plural: redisclusters
singular: rediscluster
kind: RedisCluster
shortNames:
- rdb
- rdbs
- rc
- rcs
2 changes: 1 addition & 1 deletion deploy/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ rbacs:
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "delete"]
- apiGroups: ["ponglehub.co.uk"]
resources: ["cockroachdbs", "cockroachmigrations", "cockroachclients", "redisdbs", "redisclients", "natsdbs", "natsclients"]
resources: ["postgresclusters", "postgresmigrations", "postgresclients", "redisclusters", "redisclients", "natsclusters", "natsclients"]
verbs: ["get", "list", "watch", "update"]
Loading

0 comments on commit 9e412a8

Please sign in to comment.