Skip to content

Commit

Permalink
Converting more primitive spec fields to pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
akshmish committed Jul 14, 2023
1 parent d0e8562 commit 9daf1ba
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 28 deletions.
8 changes: 4 additions & 4 deletions api/v1alpha1/database_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ type NDB struct {
// Database instance specific details
type Instance struct {
// Name of the database instance
DatabaseInstanceName string `json:"databaseInstanceName"`
DatabaseInstanceName *string `json:"databaseInstanceName"`
// Name(s) of the database(s) to be provisiond inside the database instance
// default [ "database_one", "database_two", "database_three" ]
// +optional
DatabaseNames []string `json:"databaseNames"`
// Name of the secret holding the credentials for the database instance (password and ssh key)
CredentialSecret string `json:"credentialSecret"`
CredentialSecret *string `json:"credentialSecret"`
// Size of the database instance, minimum 10 (GBs)
Size int `json:"size"`
// default UTC
// +optional
TimeZone string `json:"timezone"`
Type string `json:"type"`
TimeZone *string `json:"timezone"`
Type *string `json:"type"`
// +optional
Profiles *Profiles `json:"profiles"`
// +optional
Expand Down
17 changes: 9 additions & 8 deletions api/v1alpha1/database_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func instanceSpecDefaulterForCreate(r *Database) {
r.Spec.Instance.DatabaseNames = api.DefaultDatabaseNames
}

if r.Spec.Instance.TimeZone == "" {
r.Spec.Instance.TimeZone = "UTC"
if r.Spec.Instance.TimeZone == nil {
utc := common.TIMEZONE_UTC
r.Spec.Instance.TimeZone = &utc
}

// initialize Profiles block if that has not been added by the user
Expand All @@ -76,11 +77,11 @@ func instanceSpecDefaulterForCreate(r *Database) {
}

if r.Spec.Instance.TMInfo.Name == "" {
r.Spec.Instance.TMInfo.Name = r.Spec.Instance.DatabaseInstanceName + "_TM"
r.Spec.Instance.TMInfo.Name = *(r.Spec.Instance.DatabaseInstanceName) + "_TM"
}

if r.Spec.Instance.TMInfo.Description == "" {
r.Spec.Instance.TMInfo.Description = "Time Machine for " + r.Spec.Instance.DatabaseInstanceName
r.Spec.Instance.TMInfo.Description = "Time Machine for " + *(r.Spec.Instance.DatabaseInstanceName)
}

if r.Spec.Instance.TMInfo.SnapshotsPerDay == 0 {
Expand Down Expand Up @@ -151,25 +152,25 @@ func ndbServerSpecValidatorForCreate(r *Database, allErrs field.ErrorList, ndbPa
func instanceSpecValidatorForCreate(r *Database, allErrs field.ErrorList, instancePath *field.Path) field.ErrorList {
databaselog.Info("Entering instanceSpecValidatorForCreate...")

if r.Spec.Instance.DatabaseInstanceName == "" {
if r.Spec.Instance.DatabaseInstanceName == nil {
allErrs = append(allErrs, field.Invalid(instancePath.Child("databaseInstanceName"), r.Spec.Instance.DatabaseInstanceName, "A unique Database Instance Name must be specified"))
}

if r.Spec.Instance.Size < 10 {
allErrs = append(allErrs, field.Invalid(instancePath.Child("size"), r.Spec.Instance.Size, "Initial Database size must be 10 GBs or more"))
}

if r.Spec.Instance.CredentialSecret == "" {
if r.Spec.Instance.CredentialSecret == nil {
allErrs = append(allErrs, field.Invalid(instancePath.Child("credentialSecret"), r.Spec.Instance.CredentialSecret, "CredentialSecret must be provided"))
}

if _, isPresent := api.AllowedDatabaseTypes[r.Spec.Instance.Type]; !isPresent {
if _, isPresent := api.AllowedDatabaseTypes[*r.Spec.Instance.Type]; !isPresent {
allErrs = append(allErrs, field.Invalid(instancePath.Child("type"), r.Spec.Instance.Type,
fmt.Sprintf("A valid database type must be specified. Valid values are: %s", reflect.ValueOf(api.AllowedDatabaseTypes).MapKeys()),
))
}

if _, isPresent := api.ClosedSourceDatabaseTypes[r.Spec.Instance.Type]; isPresent {
if _, isPresent := api.ClosedSourceDatabaseTypes[*r.Spec.Instance.Type]; isPresent {
if r.Spec.Instance.Profiles == &(Profiles{}) || r.Spec.Instance.Profiles.Software == (Profile{}) {
allErrs = append(allErrs, field.Invalid(instancePath.Child("profiles").Child("software"), r.Spec.Instance.Profiles.Software, "Software Profile must be provided for the closed-source database engines"))
}
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 2 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const (

PROFILE_STATUS_READY = "READY"

TIMEZONE_UTC = "UTC"

PROFILE_TYPE_COMPUTE = "Compute"
PROFILE_TYPE_DATABASE_PARAMETER = "Database_Parameter"
PROFILE_TYPE_DATABASE_PARAMETER_INSTANCE = "Database_Parameter_Instance"
Expand Down
6 changes: 3 additions & 3 deletions controller_adapters/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ type Database struct {
}

func (d *Database) GetDBInstanceName() string {
return d.Spec.Instance.DatabaseInstanceName
return *d.Spec.Instance.DatabaseInstanceName
}

func (d *Database) GetDBInstanceType() string {
return d.Spec.Instance.Type
return *d.Spec.Instance.Type
}

func (d *Database) GetDBInstanceDatabaseNames() string {
return strings.Join(d.Spec.Instance.DatabaseNames, ",")
}

func (d *Database) GetDBInstanceTimeZone() string {
return d.Spec.Instance.TimeZone
return *d.Spec.Instance.TimeZone
}

func (d *Database) GetDBInstanceSize() int {
Expand Down
10 changes: 6 additions & 4 deletions controller_adapters/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ func TestDatabase_GetTMSchedule(t *testing.T) {
// 4. SLA name is non empty, returns default values for other empty fields
func TestDatabase_GetTMDetails(t *testing.T) {

testDatabaseName1 := "test-database"

tests := []struct {
name string
database Database
Expand All @@ -167,7 +169,7 @@ func TestDatabase_GetTMDetails(t *testing.T) {
Database: v1alpha1.Database{
Spec: v1alpha1.DatabaseSpec{
Instance: v1alpha1.Instance{
DatabaseInstanceName: "test-database",
DatabaseInstanceName: &testDatabaseName1,
TMInfo: &v1alpha1.DBTimeMachineInfo{Name: "", Description: "", SLAName: "", DailySnapshotTime: "12:34:56", SnapshotsPerDay: 1, LogCatchUpFrequency: 30, WeeklySnapshotDay: "FRIDAY", MonthlySnapshotDay: 15, QuarterlySnapshotMonth: "Jan"},
},
},
Expand All @@ -183,7 +185,7 @@ func TestDatabase_GetTMDetails(t *testing.T) {
Database: v1alpha1.Database{
Spec: v1alpha1.DatabaseSpec{
Instance: v1alpha1.Instance{
DatabaseInstanceName: "test-database",
DatabaseInstanceName: &testDatabaseName1,
TMInfo: &v1alpha1.DBTimeMachineInfo{Name: "test-name", Description: "", SLAName: "", DailySnapshotTime: "12:34:56", SnapshotsPerDay: 1, LogCatchUpFrequency: 30, WeeklySnapshotDay: "FRIDAY", MonthlySnapshotDay: 15, QuarterlySnapshotMonth: "Jan"},
},
},
Expand All @@ -199,7 +201,7 @@ func TestDatabase_GetTMDetails(t *testing.T) {
Database: v1alpha1.Database{
Spec: v1alpha1.DatabaseSpec{
Instance: v1alpha1.Instance{
DatabaseInstanceName: "test-database",
DatabaseInstanceName: &testDatabaseName1,
TMInfo: &v1alpha1.DBTimeMachineInfo{Name: "", Description: "test-description", SLAName: "", DailySnapshotTime: "12:34:56", SnapshotsPerDay: 1, LogCatchUpFrequency: 30, WeeklySnapshotDay: "FRIDAY", MonthlySnapshotDay: 15, QuarterlySnapshotMonth: "Jan"},
},
},
Expand All @@ -215,7 +217,7 @@ func TestDatabase_GetTMDetails(t *testing.T) {
Database: v1alpha1.Database{
Spec: v1alpha1.DatabaseSpec{
Instance: v1alpha1.Instance{
DatabaseInstanceName: "test-database",
DatabaseInstanceName: &testDatabaseName1,
TMInfo: &v1alpha1.DBTimeMachineInfo{Name: "", Description: "", SLAName: "test-sla", DailySnapshotTime: "12:34:56", SnapshotsPerDay: 1, LogCatchUpFrequency: 30, WeeklySnapshotDay: "FRIDAY", MonthlySnapshotDay: 15, QuarterlySnapshotMonth: "Jan"},
},
},
Expand Down
14 changes: 8 additions & 6 deletions controllers/database_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ import (
var _ = Describe("Database controller", func() {
Context("Database controller test", func() {

const DatabaseName = "test-database-name"
DatabaseName := "test-database-name"
timezoneUTC := "UTC"
typePostgres := "postgres"
const namespaceName = "test-namespace"
const testNDBServer = "http://123.123.123.123:123"
const testDatabaseIP = "111.222.123.234"

ctx := context.Background()

const ndbSecretName = "test-ndb-secret-name"
const instanceSecretName = "test-instance-secret-name"
instanceSecretName := "test-instance-secret-name"
const username = "test-username"
const password = "test-password"
const sshPublicKey = "test-ssh-key"
Expand Down Expand Up @@ -87,12 +89,12 @@ var _ = Describe("Database controller", func() {
Server: testNDBServer,
},
Instance: ndbv1alpha1.Instance{
DatabaseInstanceName: DatabaseName,
DatabaseInstanceName: &DatabaseName,
DatabaseNames: []string{"database_1"},
CredentialSecret: instanceSecretName,
CredentialSecret: &instanceSecretName,
Size: 10,
TimeZone: "UTC",
Type: "postgres",
TimeZone: &timezoneUTC,
Type: &typePostgres,
TMInfo: &ndbv1alpha1.DBTimeMachineInfo{
QuarterlySnapshotMonth: "Jan",
SnapshotsPerDay: 4,
Expand Down
6 changes: 3 additions & 3 deletions controllers/database_reconciler_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (r *DatabaseReconciler) handleSync(ctx context.Context, database *ndbv1alph
// DB Status.Status is empty => Provision a DB
log.Info("Provisioning a database instance with NDB.")

dbPassword, sshPublicKey, err := r.getDatabaseInstanceCredentials(ctx, database.Spec.Instance.CredentialSecret, req.Namespace)
dbPassword, sshPublicKey, err := r.getDatabaseInstanceCredentials(ctx, *database.Spec.Instance.CredentialSecret, req.Namespace)
if err != nil || dbPassword == "" || sshPublicKey == "" {
var errStatement string
if err == nil {
Expand Down Expand Up @@ -240,7 +240,7 @@ func (r *DatabaseReconciler) handleSync(ctx context.Context, database *ndbv1alph
database.Status.Id = taskResponse.EntityId

// Updating the type in the Database Status based on the input
database.Status.Type = database.Spec.Instance.Type
database.Status.Type = *database.Spec.Instance.Type

err = r.Status().Update(ctx, database)
if err != nil {
Expand Down Expand Up @@ -301,7 +301,7 @@ func (r *DatabaseReconciler) setupConnectivity(ctx context.Context, database *nd
Name: database.Name + "-svc",
Namespace: req.Namespace,
}
targetPort := ndb_api.GetDatabasePortByType(database.Spec.Instance.Type)
targetPort := ndb_api.GetDatabasePortByType(*database.Spec.Instance.Type)

err = r.setupService(ctx, database, commonNamespacedName, commonMetadata, targetPort)
if err != nil {
Expand Down

0 comments on commit 9daf1ba

Please sign in to comment.