Skip to content

Commit

Permalink
refactor to support multi-namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-wright committed Jun 17, 2023
1 parent 3ed870f commit 723cb9a
Show file tree
Hide file tree
Showing 58 changed files with 899 additions and 548 deletions.
7 changes: 1 addition & 6 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ func main() {

zerolog.SetGlobalLevel(level)

namespace := os.Getenv("NAMESPACE")
if namespace == "" {
log.Fatal().Msg("Must set NAMESPACE environment variable")
}

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

stopper, err := runtime.Run(namespace)
stopper, err := runtime.Run()
if err != nil {
log.Fatal().Err(err).Msg("Failed to start manager")
}
Expand Down
12 changes: 9 additions & 3 deletions deploy/chart/crds/cockroach-client.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ spec:
spec:
type: object
properties:
deployment:
type: string
dbRef:
type: object
properties:
name:
type: string
namespace:
type: string
required: [ name, namespace ]
database:
type: string
username:
type: string
secret:
type: string
required: [ deployment, database, username, secret ]
required: [ dbRef, database, username, secret ]
status:
type: object
properties:
Expand Down
12 changes: 9 additions & 3 deletions deploy/chart/crds/cockroach-migration.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ spec:
spec:
type: object
properties:
deployment:
type: string
dbRef:
type: object
properties:
name:
type: string
namespace:
type: string
required: [ name, namespace ]
database:
type: string
migration:
type: string
index:
type: integer
required: [ deployment, database, migration, index ]
required: [ dbRef, database, migration, index ]
status:
type: object
properties:
Expand Down
12 changes: 9 additions & 3 deletions deploy/chart/crds/nats-client.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ spec:
spec:
type: object
properties:
deployment:
type: string
dbRef:
type: object
properties:
name:
type: string
namespace:
type: string
required: [ name, namespace ]
secret:
type: string
required: [ deployment, secret ]
required: [ dbRef, secret ]
status:
type: object
properties:
Expand Down
12 changes: 9 additions & 3 deletions deploy/chart/crds/redis-client.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ spec:
spec:
type: object
properties:
deployment:
type: string
dbRef:
type: object
properties:
name:
type: string
namespace:
type: string
required: [ name, namespace ]
unit:
type: integer
secret:
type: string
required: [ deployment, unit, secret ]
required: [ dbRef, unit, secret ]
status:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
kind: ClusterRoleBinding
metadata:
name: {{ .Values.name }}
subjects:
- kind: ServiceAccount
name: {{ .Values.name }}
namespace: {{ .Release.Namespace }}
roleRef:
kind: Role
kind: ClusterRole
name: {{ .Values.name }}
apiGroup: rbac.authorization.k8s.io
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
kind: ClusterRole
metadata:
name: {{ .Values.name }}
rules:
Expand Down
2 changes: 1 addition & 1 deletion deploy/test-chart/templates/role.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
kind: ClusterRole
metadata:
name: {{ .Values.name }}
rules:
Expand Down
4 changes: 2 additions & 2 deletions deploy/test-chart/templates/rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
kind: ClusterRoleBinding
metadata:
name: {{ .Values.name }}
subjects:
- kind: ServiceAccount
name: {{ .Values.name }}
namespace: {{ .Release.Namespace }}
roleRef:
kind: Role
kind: ClusterRole
name: {{ .Values.name }}
apiGroup: rbac.authorization.k8s.io
25 changes: 18 additions & 7 deletions internal/dbs/cockroach/database/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

type Client struct {
conn *postgres.AdminConn
database string
conn *postgres.AdminConn
database string
namespace string
}

func New(database string, namespace string) (*Client, error) {
Expand All @@ -25,8 +26,9 @@ func New(database string, namespace string) (*Client, error) {
}

return &Client{
conn: conn,
database: database,
conn: conn,
database: database,
namespace: namespace,
}, nil
}

Expand All @@ -52,7 +54,10 @@ func (c *Client) ListDBs() ([]Database, error) {
}

databases = append(databases, Database{
DB: c.database,
DB: DBRef{
Name: c.database,
Namespace: c.namespace,
},
Name: name,
})
}
Expand Down Expand Up @@ -95,7 +100,10 @@ func (c *Client) ListUsers() ([]User, error) {
}

users = append(users, User{
DB: c.database,
DB: DBRef{
Name: c.database,
Namespace: c.namespace,
},
Name: name,
})
}
Expand Down Expand Up @@ -134,7 +142,10 @@ func (c *Client) ListPermitted(db Database) ([]Permission, error) {
}

permissions = append(permissions, Permission{
DB: c.database,
DB: DBRef{
Name: c.database,
Namespace: c.namespace,
},
Database: db.Name,
User: user,
})
Expand Down
7 changes: 6 additions & 1 deletion internal/dbs/cockroach/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type MigrationsClient struct {
conn *pgx.Conn
deployment string
namespace string
database string
}

Expand All @@ -31,6 +32,7 @@ func NewMigrations(deployment string, namespace string, database string) (*Migra
return &MigrationsClient{
conn: conn,
deployment: deployment,
namespace: namespace,
database: database,
}, nil
}
Expand Down Expand Up @@ -80,7 +82,10 @@ func (c *MigrationsClient) AppliedMigrations() ([]Migration, error) {
}

migrations = append(migrations, Migration{
DB: c.deployment,
DB: DBRef{
Name: c.deployment,
Namespace: c.namespace,
},
Database: c.database,
Index: id,
})
Expand Down
37 changes: 29 additions & 8 deletions internal/dbs/cockroach/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,61 @@ package database

import "strconv"

type DBRef struct {
Name string
Namespace string
}

type Database struct {
Name string
DB string
DB DBRef
}

func (d *Database) GetName() string {
return d.DB + d.Name
return d.Name
}

func (d *Database) GetNamespace() string {
return d.DB.Namespace + ":" + d.DB.Name
}

type User struct {
Name string
DB string
DB DBRef
}

func (u *User) GetName() string {
return u.DB + u.Name
return u.Name
}

func (u *User) GetNamespace() string {
return u.DB.Namespace + ":" + u.DB.Name
}

type Permission struct {
User string
Database string
DB string
DB DBRef
}

func (u *Permission) GetName() string {
return u.DB + u.Database + u.User
return u.Database + u.User
}

func (u *Permission) GetNamespace() string {
return u.DB.Namespace + ":" + u.DB.Name
}

type Migration struct {
DB string
DB DBRef
Database string
Index int64
}

func (m *Migration) GetName() string {
return m.DB + m.Database + strconv.FormatInt(m.Index, 10)
return m.Database + strconv.FormatInt(m.Index, 10)
}

func (m *Migration) GetNamespace() string {
return m.DB.Namespace + ":" + m.DB.Name
}
8 changes: 4 additions & 4 deletions internal/dbs/cockroach/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type K8sClient[T any] interface {
Watch(ctx context.Context, cancel context.CancelFunc) (<-chan k8s_generic.Update[T], error)
Create(ctx context.Context, resource T) error
Delete(ctx context.Context, name string) error
Delete(ctx context.Context, name string, namespace string) error
Update(ctx context.Context, resource T) error
Event(ctx context.Context, obj T, eventtype, reason, message string)
}
Expand All @@ -19,10 +19,10 @@ type Client struct {
builder *k8s_generic.Builder
}

func New(namespace string) (*Client, error) {
builder, err := k8s_generic.NewBuilder(namespace)
func New() (*Client, error) {
builder, err := k8s_generic.NewBuilder()
if err != nil {
return nil, fmt.Errorf("failed to create k8s builder: %w", err)
return nil, fmt.Errorf("failed to create k8s builder: %+v", err)
}

return &Client{
Expand Down
Loading

0 comments on commit 723cb9a

Please sign in to comment.