Skip to content

Commit

Permalink
Make Pasword optional in database CRD (#284)
Browse files Browse the repository at this point in the history
* Make Pasword optional in database CRD

* Test reconciliation without password
  • Loading branch information
tmablunar authored Mar 14, 2024
1 parent aa60fd1 commit d237f3a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/postgresqldatabase_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type PostgreSQLDatabaseSpec struct {
User ResourceVar `json:"user"`

// Password used with the User name to connect to the database
// +optional
Password ResourceVar `json:"password"`

// IsShared indicates whether the database is shared between multiple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.0
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: postgresqldatabases.postgresql.lunar.tech
spec:
Expand Down Expand Up @@ -199,7 +199,6 @@ spec:
type: object
required:
- name
- password
type: object
status:
description: PostgreSQLDatabaseStatus defines the observed state of PostgreSQLDatabase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.0
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: postgresqlhostcredentials.postgresql.lunar.tech
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.0
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: postgresqlusers.postgresql.lunar.tech
spec:
Expand Down
89 changes: 89 additions & 0 deletions controllers/postgresqldatabase_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,95 @@ func TestPostgreSQLDatabase_Reconcile_hostCredentialsResourceReference(t *testin
}, res, "result not as expected")
}

// TestPostgreSQLDatabase_Reconcile_noPassword tests that
// a PostgreSQLDatabase resource can reference a PostgreSQLHostCredentials
// resource.
func TestPostgreSQLDatabase_Reconcile_noPassword(t *testing.T) {
logf.SetLogger(zap.New(zap.UseDevMode(true)))

host := test.Integration(t)
var (
epoch = time.Now().UnixNano()
namespace = "default"
databaseName = fmt.Sprintf("database_%d", epoch)
hostCredentialsName = fmt.Sprintf("hostcredentials_%d", epoch)

credentialsResource = &lunarwayv1alpha1.PostgreSQLHostCredentials{
ObjectMeta: metav1.ObjectMeta{
Name: hostCredentialsName,
Namespace: namespace,
},
Spec: lunarwayv1alpha1.PostgreSQLHostCredentialsSpec{
Host: lunarwayv1alpha1.ResourceVar{
Value: "localhost",
},
User: lunarwayv1alpha1.ResourceVar{
Value: "admin",
},
Password: lunarwayv1alpha1.ResourceVar{
Value: "admin",
},
},
}

databaseResource = &lunarwayv1alpha1.PostgreSQLDatabase{
ObjectMeta: metav1.ObjectMeta{
Name: databaseName,
Namespace: namespace,
},
Spec: lunarwayv1alpha1.PostgreSQLDatabaseSpec{
Name: databaseName,
HostCredentials: hostCredentialsName,
User: lunarwayv1alpha1.ResourceVar{
Value: databaseName,
},
},
Status: lunarwayv1alpha1.PostgreSQLDatabaseStatus{
Phase: lunarwayv1alpha1.PostgreSQLDatabasePhaseRunning,
},
}
)

// Register operator types with the runtime scheme.
s := scheme.Scheme
s.AddKnownTypes(lunarwayv1alpha1.GroupVersion, databaseResource, credentialsResource, &lunarwayv1alpha1.PostgreSQLDatabaseList{})

// Add tracked objects to the fake client simulating their existence in a k8s
// cluster
objs := []runtime.Object{
databaseResource,
credentialsResource,
}
cl := fake.NewClientBuilder().
WithRuntimeObjects(objs...).
Build()

// Create a controller object with the fake client but otherwise "live" setup
// with database interaction
r := &PostgreSQLDatabaseReconciler{
Client: cl,
Log: ctrl.Log.WithName(t.Name()),
ManagerRoleName: managerRole,
HostCredentials: nil,
}

// seed database into the postgres host
seededDatabase(t, host, databaseName, databaseName, managerRole)

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: databaseName,
Namespace: namespace,
},
}
res, err := r.Reconcile(context.Background(), req)
assert.NoError(t, err, "reconciliation failed")
assert.Equal(t, reconcile.Result{
Requeue: false,
RequeueAfter: 0,
}, res, "result not as expected")
}

// TestPostgreSQLDatabase_Reconcile_unknownHostCredentialsResourceReference
// tests that references to an unknown host credentials resource will results in
// a requeued reconciliation.
Expand Down

0 comments on commit d237f3a

Please sign in to comment.