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

Commit

Permalink
Update godocs
Browse files Browse the repository at this point in the history
  • Loading branch information
criscola committed Jan 21, 2021
1 parent 396e6e5 commit 7d7d94c
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 102 deletions.
3 changes: 0 additions & 3 deletions api/v1/kubernetesdbaas_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// KubernetesDbaasSpec defines the desired state of KubernetesDbaas.
//
// Important: Run "make" to regenerate code after modifying this file. Json tags are required.
Expand Down
10 changes: 5 additions & 5 deletions controllers/kubernetesdbaas_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package controllers contain the Kubernetes controllers responsible for their Custom Resource.
package controllers

import (
Expand Down Expand Up @@ -51,6 +52,7 @@ const (
DateTimeLayout = time.UnixDate
)

// Reconcile tries to reconcile the state of the cluster with the state of KubernetesDbaas resources.
// +kubebuilder:rbac:groups=dbaas.bedag.ch,resources=kubernetesdbaas,verbs=list;watch;update
// +kubebuilder:rbac:groups=dbaas.bedag.ch,resources=kubernetesdbaas/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=list;watch;create;update;delete
Expand Down Expand Up @@ -236,11 +238,6 @@ func (r *KubernetesDbaasReconciler) addFinalizer(dbaasResource *KubernetesDbaas)
return nil
}

type TemplateInput struct {
Metadata map[string]interface{} // the whole "metadata" field of the kubernetesdbaas resource gets copied here
Parameters map[string]string // the spec.parameters field of the kubernetesdbaas resource gets copied here
}

// createDb creates a new database instance on the external provisioner based on the dbaasResource data.
func (r *KubernetesDbaasReconciler) createDb(dbaasResource *KubernetesDbaas) error {
r.Log.Info(fmt.Sprintf("Creating database instance for: %s", dbaasResource.UID))
Expand Down Expand Up @@ -359,6 +356,7 @@ func (r *KubernetesDbaasReconciler) createSecret(owner *KubernetesDbaas, output
return r.Client.Update(context.Background(), newSecret)
}

// newOpValuesFromResource constructs a database.OpValues struct starting from a KubernetesDbaas resource.
func newOpValuesFromResource(resource *KubernetesDbaas) (database.OpValues, error) {
metaIn := resource.ObjectMeta
var metadata map[string]interface{}
Expand Down Expand Up @@ -390,6 +388,7 @@ func newOpValuesFromResource(resource *KubernetesDbaas) (database.OpValues, erro
}, nil
}

// contains is a very small utility function which returns true if s has been found in list.
func contains(list []string, s string) bool {
for _, v := range list {
if v == s {
Expand All @@ -399,6 +398,7 @@ func contains(list []string, s string) bool {
return false
}

// randSeq generates a random alphanumeric string of length n
func randSeq(n int) string {
rand.Seed(time.Now().UnixNano())

Expand Down
79 changes: 0 additions & 79 deletions controllers/suite_test.go

This file was deleted.

1 change: 0 additions & 1 deletion docs/resources/system_arch_diag.puml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ namespace database {
class Dsn << (S,Aquamarine) >> {
+ GetDriver() string
+ String() string
+ WithTable(table string) Dsn

}
class Endpoint << (S,Aquamarine) >> {
Expand Down
Binary file added docs/resources/system_diag_ctx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package config contains the initialization of the operator configuration.
package config

import (
Expand All @@ -15,6 +16,7 @@ const (
DbmsMapKey = "dbms"
)

// c holds the operator configuration
var c OperatorConfig

// ReadOperatorConfig unmarshalls the operator configuration from a viper.Viper struct into a private struct.
Expand Down
10 changes: 10 additions & 0 deletions pkg/database/dbms.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package database contains all the code related to the interaction with databases. It doesn't contain any application
// state. Actual connections are retained in the pool package.
package database

import (
Expand Down Expand Up @@ -32,6 +34,7 @@ type Driver interface {
Ping() error
}

// OpValuesd represent the input values of an operation.
type OpValues struct {
Metadata map[string]interface{}
Parameters map[string]string
Expand Down Expand Up @@ -60,11 +63,14 @@ type Dbms struct {
Endpoints []Endpoint
}

// Endpoint represent the configuration of a DBMS endpoint identified by a name.
type Endpoint struct {
Name string
Dsn Dsn
}

// Operation represents an operation performed on a DBMS identified by name and containing a map of inputs and a map
// of outputs.
type Operation struct {
Name string
Inputs map[string]string
Expand Down Expand Up @@ -102,6 +108,7 @@ func New(dsn Dsn, ops map[string]Operation) (*DbmsConn, error) {
return dbmsConn, nil
}

// GetByDriverAndEndpoint gets a Dbms identified by driver and endpoint from a DbmsConfig type.
func (c DbmsConfig) GetByDriverAndEndpoint(driver, endpoint string) (Dbms, error) {
for _, dbms := range c {
if dbms.Driver == driver && contains(dbms.Endpoints, endpoint) {
Expand Down Expand Up @@ -151,14 +158,17 @@ func (d Dbms) RenderOperation(opKey string, values OpValues) (Operation, error)
return renderedOp, nil
}

// IsNamePresent return true if an endpoint name is not empty, else it returns false.
func (e Endpoint) IsNamePresent() bool {
return e.Name != ""
}

// IsDsnPresent return true if an endpoint dsn is not empty, else it returns false.
func (e Endpoint) IsDsnPresent() bool {
return e.Dsn != ""
}

// contains is a very small utility function which returns true if s has been found in list.
func contains(list []Endpoint, s string) bool {
for _, v := range list {
if v.Name == s {
Expand Down
7 changes: 2 additions & 5 deletions pkg/database/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ func (s Dsn) GetDriver() string {
return strings.Split(string(s), ":")[0]
}

// NewDsn initialized a new Dsn string.
func NewDsn(driver, username, password, host, port, dbName string) Dsn {
return Dsn(driver+"://"+username+":"+password+"@"+host+":"+port+"/"+dbName)
}

// String returns a string from a Dsn.
func (s Dsn) String() string {
return string(s)
}

// WithTable set the table associated with the connection.
func (s Dsn) WithTable(table string) Dsn {
// TODO: More checks
return Dsn(s.String() + "/" + table)
}
7 changes: 7 additions & 0 deletions pkg/database/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
_ "github.com/denisenkom/go-mssqldb"
)

// MssqlConn represents a connection to a SQL Server DBMS.
type MssqlConn struct {
c *sql.DB
}

// NewMssqlConn constructs a new SQL Server connection from a given dsn.
func NewMssqlConn(dsn Dsn) (*MssqlConn, error) {
dbConn, err := sql.Open("sqlserver", dsn.String())
if err != nil {
Expand All @@ -19,6 +21,8 @@ func NewMssqlConn(dsn Dsn) (*MssqlConn, error) {
return &conn, nil
}

// CreateDb attempts to create a new database as specified in the operation parameter. It returns an OpOutput with the
// result of the call.
func (c *MssqlConn) CreateDb(operation Operation) OpOutput {
var username string
var password string
Expand All @@ -45,6 +49,8 @@ func (c *MssqlConn) CreateDb(operation Operation) OpOutput {
return OpOutput{[]string{username, password, dbName, fqdn, port}, nil}
}

// DeleteDb attemps to delete a database instance as specified in the operation parameter. It returns an OpOutput with the
// result of the call.
func (c *MssqlConn) DeleteDb(operation Operation) OpOutput {
var inputParams []interface{}
for k, v := range operation.Inputs {
Expand All @@ -59,6 +65,7 @@ func (c *MssqlConn) DeleteDb(operation Operation) OpOutput {
return OpOutput{nil, nil}
}

// Ping returns an error if a connection cannot be established with the DBMS, else it returns nil.
func (c *MssqlConn) Ping() error {
return c.c.Ping()
}
3 changes: 3 additions & 0 deletions pkg/database/mssql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

var sqlserverDsn Dsn

// TestMain is executed before each test case.
func TestMain(m *testing.M) {
var resource *dockertest.Resource
var err error
Expand All @@ -37,6 +38,7 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

// TODO: Update test to reflect new templating feature
func TestMssqlConn_CreateDb(t *testing.T) {
/*
ops := GetMockOps()
Expand Down Expand Up @@ -68,6 +70,7 @@ func TestMssqlConn_CreateDb(t *testing.T) {
}*/
}

// TODO: Update test to reflect new templating feature
func TestMssqlConn_DeleteDb(t *testing.T) {
/*
t.Run("create db before deletion", TestMssqlConn_CreateDb)
Expand Down
12 changes: 3 additions & 9 deletions pkg/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Register(dbms database.Dbms) error {
return nil
}

// GetConnByDriverAndEndpointName tries to retrieve a database.DbmsConn from the pool of connections.
func GetConnByDriverAndEndpointName(driver, endpointName string) (*database.DbmsConn, error) {
for _, v := range pool[driver] {
if v.dbmsConfig.Name == endpointName {
Expand All @@ -48,19 +49,12 @@ func GetConnByDriverAndEndpointName(driver, endpointName string) (*database.Dbms
return nil, fmt.Errorf("entry '%s' with driver '%s' not found in dbms pool", endpointName, driver)
}

func GetDsnByDriverAndEndpointName(driver, endpointName string) (database.Dsn, error) {
for _, v := range pool[driver] {
if v.dbmsConfig.Name == endpointName {
return v.dbmsConfig.Dsn, nil
}
}
return "", fmt.Errorf("entry '%s' with driver '%s' not found in dbms pool", endpointName, driver)
}

// SizeOf returns the number of connections in the current pool
func SizeOf(driver string) int {
return len(pool[driver])
}

// String returns the pool formatted as a string.
func String() string {
return fmt.Sprint(pool)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var dsn1 database.Dsn
var dsn2 database.Dsn
var dsnSlice []database.Dsn

// TestMain is executed before each test case.
func TestMain(m *testing.M) {
var resource1 *dockertest.Resource
var resource2 *dockertest.Resource
Expand Down Expand Up @@ -51,6 +52,7 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

// TestRegister tests whether the pool is able to register a new instance.
func TestRegister(t *testing.T) {
endpoints := GetMockEndpoints(dsnSlice)
dbms := database.Dbms{
Expand All @@ -70,6 +72,7 @@ func TestRegister(t *testing.T) {
}
}

// TestGetByDriverAndEndpointName tests whether an entry by driver and endpoint name can be retrieved from the pool.
func TestGetByDriverAndEndpointName(t *testing.T) {
endpoints := GetMockEndpoints(dsnSlice)
for _, v := range endpoints {
Expand Down
1 change: 1 addition & 0 deletions pkg/test/docker.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package test provides the facilities to perform integration testing using the package dockertest.
package test

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/test/dpool/dpool.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package dpool contains a dockertest.Pool attribute which holds the pool of connections to docker containers. Checkout
// the package ory/dockertest to learn more.
package dpool

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/test/mockup.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
END`
)

// GetMockOps returns a mock of map[string]database.Operation
func GetMockOps() map[string]database.Operation {
// Mock map[string]Operation
ops := map[string]database.Operation{}
Expand All @@ -73,6 +74,7 @@ func GetMockOps() map[string]database.Operation {
return ops
}

// GetMockEndpoints returns a mock of []database.Endpoint given a slice of database.Dsn
func GetMockEndpoints(dsnSlice []database.Dsn) []database.Endpoint {
var endpts []database.Endpoint

Expand Down
3 changes: 3 additions & 0 deletions pkg/test/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func GetRootProjectPath() (string, error) {
return currentPath, nil
}

// GetTestdataFolderPath is a utility function which tries to get the "testdata" folder which should contain data files
// used for testing.
func GetTestdataFolderPath() (string, error) {
rootPath, err := GetRootProjectPath()
if err != nil {
Expand All @@ -44,6 +46,7 @@ func GetTestdataFolderPath() (string, error) {
return path.Join(rootPath, "testdata"), nil
}

// GetUnexportedField is a "hacky" function which enables to read private fields from a struct.
func GetUnexportedField(field reflect.Value) interface{} {
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface()
}

0 comments on commit 7d7d94c

Please sign in to comment.