Skip to content

Commit

Permalink
Merge pull request #21 from schemahero/mysql
Browse files Browse the repository at this point in the history
Support for mysql tables
  • Loading branch information
marccampbell committed May 27, 2019
2 parents 8129856 + 2f72e21 commit ab16617
Show file tree
Hide file tree
Showing 104 changed files with 9,602 additions and 807 deletions.
File renamed without changes.
17 changes: 17 additions & 0 deletions Dockerfile.schemahero
@@ -0,0 +1,17 @@
# Build the manager binary
FROM golang:1.10.3 as builder

# Copy in the go src
WORKDIR /go/src/github.com/schemahero/schemahero
COPY pkg/ pkg/
COPY cmd/ cmd/
COPY vendor/ vendor/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o schemahero github.com/schemahero/schemahero/cmd/schemahero

# Copy schemahero into a thin image
FROM ubuntu:latest
WORKDIR /
COPY --from=builder /go/src/github.com/schemahero/schemahero/schemahero .
ENTRYPOINT ["/schemahero"]
10 changes: 10 additions & 0 deletions Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Expand Up @@ -47,3 +47,7 @@ version="v1.4.7"
[[constraint]]
branch = "master"
name = "github.com/xo/dburl"

[[constraint]]
name = "github.com/go-sql-driver/mysql"
version = "1.4.1"
7 changes: 4 additions & 3 deletions Makefile
Expand Up @@ -17,7 +17,7 @@ run: generate fmt vet bin/schemahero
go run ./cmd/manager/main.go

# Install CRDs into a cluster
install: manifests
install: manifests microk8s
kubectl apply -f config/crds

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
Expand Down Expand Up @@ -89,7 +89,8 @@ snapshot-release: build-snapshot-release installable-manifests
build-snapshot-release:
curl -sL https://git.io/goreleaser | bash -s -- --rm-dist --snapshot --config deploy/.goreleaser.snapshot.yml

.PHONY: micok8s
microk8s: build-release
.PHONY: microk8s
microk8s:
docker build -t schemahero/schemahero -f ./Dockerfile.schemahero .
docker tag schemahero/schemahero localhost:32000/schemahero/schemahero:latest
docker push localhost:32000/schemahero/schemahero:latest
25 changes: 2 additions & 23 deletions README.md
Expand Up @@ -10,7 +10,7 @@

## What is SchemaHero?

SchemaHero is a Kubernetes Operator for Declarative Schema Management for various databases
SchemaHero is a Kubernetes Operator for Declarative Schema Management for various databases. SchemaHero has the following goals:

1. Database tables can be expressed as [Kubernetes resources](https://github.com/schemahero/schemahero/blob/master/config/samples/schemas_v1alpha1_table.yaml) that can be updated and deployed to the cluster.
2. Database migrations can be written as SQL statements, expressed as [Kubernetes resources](https://github.com/schemahero/schemahero/blob/master/config/samples/schemas_v1alpha1_migration.yaml) that can be deployed to the cluster.
Expand All @@ -25,26 +25,5 @@ The recommended way to deploy SchemaHero is:
kubectl apply -f https://raw.githubusercontent.com/schemahero/schemahero/master/install/schemahero/schemahero-operator.yaml
```

If you need any customizations, use [Ship](https://github.com/replicatedhq/ship):

```
brew install ship
ship init github.com/schemahero/schemahero/tree/masterinstall/k8s
```

### Questions

*What about rollbacks?*
You can include the "delete" script. And that will be executed on `kubectl delete`.

*How can I control the order?*
The order that migrations run in is important. Each migration supports a "requires" field. This is the name (or names) of other migrations that must be applied before this one.

This will allow your developers to submit migrations in any order, but each developer can choose their "base".

SchemaHero will calculate a Directed Acylic Graph (DAG) to determine the optimal order to apply migrations, and will always honor the `requires` field. But it's possible to run multiple migrations simultaneously, which improves performance when bootstrapping new databases.

*How about non-sql migrations? I need to write some go code?*
Currently, you can write these types of schema as a container, and include that in the schema.

To get started, read our [tutorial](https://github.com/schemahero/schemahero/blob/master/docs/tutorial) and the [how to use guide](https://github.com/schemahero/schemahero/blob/master/docs/how-to-use)

23 changes: 19 additions & 4 deletions config/crds/databases_v1alpha1_database.yaml
Expand Up @@ -23,12 +23,27 @@ spec:
type: string
connection:
properties:
mysql:
properties:
uri:
properties:
valueFrom:
properties:
secretKeyRef:
properties:
key:
type: string
name:
type: string
required:
- name
- key
type: object
type: object
type: object
type: object
postgres:
properties:
allowOrphanedTablesOnDelete:
type: boolean
disableDriftDetection:
type: boolean
uri:
properties:
valueFrom:
Expand Down
35 changes: 32 additions & 3 deletions config/crds/schemas_v1alpha1_table.yaml
Expand Up @@ -38,6 +38,36 @@ spec:
type: array
schema:
properties:
mysql:
properties:
columns:
items:
properties:
constraints:
properties:
notNull:
type: boolean
type: object
default:
type: string
name:
type: string
type:
type: string
required:
- name
- type
type: object
type: array
isDeleted:
type: boolean
primaryKey:
items:
type: string
type: array
required:
- primaryKey
type: object
postgres:
properties:
columns:
Expand All @@ -59,16 +89,15 @@ spec:
- type
type: object
type: array
isDeleted:
type: boolean
primaryKey:
items:
type: string
type: array
required:
- primaryKey
- columns
type: object
required:
- postgres
type: object
required:
- database
Expand Down
90 changes: 90 additions & 0 deletions config/dev/database/mysql-5.6.yaml
@@ -0,0 +1,90 @@

apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
type: ClusterIP
ports:
- name: mysql
port: 3306
targetPort: mysql
selector:
app: mysql
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
template:
metadata:
labels:
app: mysql
spec:
initContainers:
- name: "remove-lost-found"
image: "busybox:1.25.0"
imagePullPolicy: IfNotPresent
command: ["rm", "-fr", "/var/lib/mysql/lost+found"]
volumeMounts:
- name: schemahero-testdb-mysql
mountPath: /var/lib/mysql
containers:
- name: mysql
image: mysql:5.6
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: password
- name: MYSQL_ROOT_HOST
value: '%'
- name: MYSQL_DATABASE
value: testdb
- name: MYSQL_USER
value: testuser
- name: MYSQL_PASSWORD
value: password

ports:
- name: mysql
containerPort: 3306
livenessProbe:
exec:
command:
- sh
- -c
- mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}
initialDelaySeconds: 30
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
exec:
command:
- sh
- -c
- mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}
initialDelaySeconds: 5
timeoutSeconds: 1
periodSeconds: 10
volumeMounts:
- name: schemahero-testdb-mysql
mountPath: /var/lib/mysql

volumes:
- name: schemahero-testdb-mysql
persistentVolumeClaim:
claimName: schemahero-testdb-mysql

volumeClaimTemplates:
- metadata:
name: schemahero-testdb-mysql
spec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
Expand Up @@ -36,29 +36,26 @@ spec:
serviceName: postgresql
template:
metadata:
labels:
app: postgresql
chart: postgresql-3.18.1
name: postgresql
spec:
containers:
- env:
- name: PGDATA
value: /bitnami/postgresql
- name: POSTGRES_USER
value: schemahero
value: testuser
- name: POSTGRES_PASSWORD
value: password
- name: POSTGRES_DB
value: github
value: testdb
image: docker.io/bitnami/postgresql:10.7.0
imagePullPolicy: Always
livenessProbe:
exec:
command:
- sh
- -c
- exec pg_isready -U "schemahero" -d "github" -h 127.0.0.1
- exec pg_isready -U "testuser" -d "testdb" -h 127.0.0.1
failureThreshold: 6
initialDelaySeconds: 30
periodSeconds: 10
Expand All @@ -73,7 +70,7 @@ spec:
command:
- sh
- -c
- exec pg_isready -U "schemahero" -d "github" -h 127.0.0.1
- exec pg_isready -U "testuser" -d "testdb" -h 127.0.0.1
failureThreshold: 6
initialDelaySeconds: 5
periodSeconds: 10
Expand All @@ -87,7 +84,7 @@ spec:
runAsUser: 1001
volumeMounts:
- mountPath: /bitnami/postgresql
name: schemahero-github
name: schemahero-testdb-pg
subPath: null
initContainers:
- command:
Expand All @@ -109,7 +106,7 @@ spec:
runAsUser: 0
volumeMounts:
- mountPath: /bitnami/postgresql
name: schemahero-github
name: schemahero-testdb-pg
subPath: null
securityContext:
fsGroup: 1001
Expand All @@ -118,7 +115,7 @@ spec:
type: RollingUpdate
volumeClaimTemplates:
- metadata:
name: schemahero-github
name: schemahero-testdb-pg
spec:
accessModes:
- ReadWriteOnce
Expand Down
12 changes: 12 additions & 0 deletions config/dev/github/mysql.yaml
@@ -0,0 +1,12 @@
apiVersion: databases.schemahero.io/v1alpha1
kind: Database
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: testdb
schemahero:
image: "localhost:32000/schemahero/schemahero"
connection:
mysql:
uri:
value: testuser:password@tcp(mysql:3306)/testdb?tls=false
4 changes: 2 additions & 2 deletions config/dev/github/pg.yaml
Expand Up @@ -3,10 +3,10 @@ kind: Database
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: github-pg
name: testdb
schemahero:
image: "localhost:32000/schemahero/schemahero"
connection:
postgres:
uri:
value: postgres://schemahero:password@postgresql:5432/github?sslmode=disable
value: postgres://testuser:password@postgresql:5432/testdb?sslmode=disable

0 comments on commit ab16617

Please sign in to comment.