diff --git a/api/v1alpha1/database_types.go b/api/v1alpha1/database_types.go index 876e04b3..14c4f152 100644 --- a/api/v1alpha1/database_types.go +++ b/api/v1alpha1/database_types.go @@ -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 diff --git a/api/v1alpha1/database_webhook.go b/api/v1alpha1/database_webhook.go index 8b0bd39f..85bf4d61 100644 --- a/api/v1alpha1/database_webhook.go +++ b/api/v1alpha1/database_webhook.go @@ -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 @@ -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 { @@ -151,7 +152,7 @@ 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")) } @@ -159,17 +160,17 @@ func instanceSpecValidatorForCreate(r *Database, allErrs field.ErrorList, instan 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")) } diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 0d692b41..cc4e1858 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -134,11 +134,31 @@ func (in *DatabaseStatus) DeepCopy() *DatabaseStatus { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Instance) DeepCopyInto(out *Instance) { *out = *in + if in.DatabaseInstanceName != nil { + in, out := &in.DatabaseInstanceName, &out.DatabaseInstanceName + *out = new(string) + **out = **in + } if in.DatabaseNames != nil { in, out := &in.DatabaseNames, &out.DatabaseNames *out = make([]string, len(*in)) copy(*out, *in) } + if in.CredentialSecret != nil { + in, out := &in.CredentialSecret, &out.CredentialSecret + *out = new(string) + **out = **in + } + if in.TimeZone != nil { + in, out := &in.TimeZone, &out.TimeZone + *out = new(string) + **out = **in + } + if in.Type != nil { + in, out := &in.Type, &out.Type + *out = new(string) + **out = **in + } if in.Profiles != nil { in, out := &in.Profiles, &out.Profiles *out = new(Profiles) diff --git a/common/constants.go b/common/constants.go index 2310564d..a3bbd002 100644 --- a/common/constants.go +++ b/common/constants.go @@ -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" diff --git a/controller_adapters/database.go b/controller_adapters/database.go index 4d0ed731..0156cc53 100644 --- a/controller_adapters/database.go +++ b/controller_adapters/database.go @@ -44,11 +44,11 @@ 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 { @@ -56,7 +56,7 @@ func (d *Database) GetDBInstanceDatabaseNames() string { } func (d *Database) GetDBInstanceTimeZone() string { - return d.Spec.Instance.TimeZone + return *d.Spec.Instance.TimeZone } func (d *Database) GetDBInstanceSize() int { diff --git a/controller_adapters/database_test.go b/controller_adapters/database_test.go index 89193ecd..90e3883f 100644 --- a/controller_adapters/database_test.go +++ b/controller_adapters/database_test.go @@ -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 @@ -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"}, }, }, @@ -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"}, }, }, @@ -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"}, }, }, @@ -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"}, }, }, diff --git a/controllers/database_controller_test.go b/controllers/database_controller_test.go index 9b85def8..b1d58b5c 100644 --- a/controllers/database_controller_test.go +++ b/controllers/database_controller_test.go @@ -41,7 +41,9 @@ 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" @@ -49,7 +51,7 @@ var _ = Describe("Database controller", func() { 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" @@ -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, diff --git a/controllers/database_reconciler_helpers.go b/controllers/database_reconciler_helpers.go index a09ff2f4..47863ddc 100644 --- a/controllers/database_reconciler_helpers.go +++ b/controllers/database_reconciler_helpers.go @@ -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 { @@ -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 { @@ -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 {