@@ -48,6 +48,8 @@ func (r *PostgreSQLReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error)
48
48
49
49
// common controller functions
50
50
cw := NewControllerWrapper (* r , & ctx )
51
+ // garbage collector
52
+ gc := NewPostgreSQLGarbageCollector (r , cw , & log )
51
53
52
54
// get postgresql resource by namespaced name
53
55
var postgresql infrav1beta1.PostgreSQL
@@ -67,6 +69,11 @@ func (r *PostgreSQLReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error)
67
69
return r .updateAndReturn (& ctx , & postgresql , & log )
68
70
}
69
71
72
+ // Garbage Collection. If errors occur, log and proceed with reconciliation.
73
+ if err := gc .Clean (& postgresql ); err != nil {
74
+ log .Info ("Error while cleaning garbage" , "error" , err )
75
+ }
76
+
70
77
// get root database password from k8s secret
71
78
rootPassword , err := cw .GetRootPassword (postgresql .Spec .RootSecretLookup .Name , postgresql .Spec .RootSecretLookup .Namespace , postgresql .Spec .RootSecretLookup .Field )
72
79
if err != nil {
@@ -75,63 +82,41 @@ func (r *PostgreSQLReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error)
75
82
return r .updateAndReturn (& ctx , & postgresql , & log )
76
83
}
77
84
78
- // - garbage collection
79
- // if host is changed, drop credentials for old host. Keep database & data for now, until we figure out what we'll do with it
80
- // TODO refactor to have a general garbage collector mechanism, instead of code throughout controller
81
- if postgresql .Spec .HostName != postgresql .Status .DatabaseStatus .Host {
82
- // we might not be able to login with these old credentials anymore
83
- postgreSQLServerOld , err := r .ServerCache .Get (postgresql .Status .DatabaseStatus .Host , postgresql .Spec .RootUsername , rootPassword , postgresql .Spec .RootAuthenticationDatabase )
84
- if err != nil {
85
- // ignore the error for now
86
- } else {
87
- postgresql .Status .CredentialsStatus .ForEach (func (status * infrav1beta1.CredentialStatus ) {
88
- _ = postgreSQLServerOld .DropUser (status .Username , postgresql .Status .DatabaseStatus .Name )
89
- })
90
- }
91
- }
92
-
93
85
// postgresql connection to spec host, cached
94
86
postgreSQLServer , err := r .ServerCache .Get (postgresql .Spec .HostName , postgresql .Spec .RootUsername , rootPassword , postgresql .Spec .RootAuthenticationDatabase )
95
87
if err != nil {
96
- postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Unavailable , err .Error (), "" , postgresql .Spec .HostName )
88
+ postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Unavailable , err .Error (), "" , postgresql .Spec .HostName ).
89
+ WithUsername (postgresql .Spec .RootUsername ).
90
+ WithAuthDatabase (postgresql .Spec .RootAuthenticationDatabase ).
91
+ WithRootSecretLookup (postgresql .Spec .RootSecretLookup .Name , postgresql .Spec .RootSecretLookup .Namespace , postgresql .Spec .RootSecretLookup .Field )
97
92
postgresql .Status .CredentialsStatus = make (infrav1beta1.CredentialsStatus , 0 )
98
93
return r .updateAndReturn (& ctx , & postgresql , & log )
99
94
}
100
95
// vault connection, cached
101
96
vault , err := r .VaultCache .Get (postgresql .Spec .RootSecretLookup .Name )
102
97
if err != nil {
103
- postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Unavailable , err .Error (), "" , postgresql .Spec .HostName )
98
+ postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Unavailable , err .Error (), "" , postgresql .Spec .HostName ).
99
+ WithUsername (postgresql .Spec .RootUsername ).
100
+ WithAuthDatabase (postgresql .Spec .RootAuthenticationDatabase ).
101
+ WithRootSecretLookup (postgresql .Spec .RootSecretLookup .Name , postgresql .Spec .RootSecretLookup .Namespace , postgresql .Spec .RootSecretLookup .Field )
104
102
postgresql .Status .CredentialsStatus = make (infrav1beta1.CredentialsStatus , 0 )
105
103
return r .updateAndReturn (& ctx , & postgresql , & log )
106
104
}
107
105
108
- // - garbage collection
109
- // TODO for now, disallow database renaming
110
- if postgresql .Spec .DatabaseName != postgresql .Status .DatabaseStatus .Name && postgresql .Status .DatabaseStatus .Name != "" {
111
- postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Unavailable , "Cannot change the name of the database." , "" , postgresql .Spec .HostName )
112
- postgresql .Status .CredentialsStatus = make (infrav1beta1.CredentialsStatus , 0 )
113
- r .ServerCache .Remove (postgresql .Spec .HostName )
114
- return r .updateAndReturn (& ctx , & postgresql , & log )
115
- }
116
-
117
106
// Setup database
118
107
if err := postgreSQLServer .CreateDatabaseIfNotExists (postgresql .Spec .DatabaseName ); err != nil {
119
- postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Unavailable , err .Error (), "" , postgresql .Spec .HostName )
108
+ postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Unavailable , err .Error (), "" , postgresql .Spec .HostName ).
109
+ WithUsername (postgresql .Spec .RootUsername ).
110
+ WithAuthDatabase (postgresql .Spec .RootAuthenticationDatabase ).
111
+ WithRootSecretLookup (postgresql .Spec .RootSecretLookup .Name , postgresql .Spec .RootSecretLookup .Namespace , postgresql .Spec .RootSecretLookup .Field )
120
112
postgresql .Status .CredentialsStatus = make (infrav1beta1.CredentialsStatus , 0 )
121
113
r .ServerCache .Remove (postgresql .Spec .HostName )
122
114
return r .updateAndReturn (& ctx , & postgresql , & log )
123
115
}
124
- postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Available , "Database up." , postgresql .Spec .DatabaseName , postgresql .Spec .HostName )
125
-
126
- // - garbage collection
127
- // Delete all credentials if they exist, and are no longer required by spec
128
- if postgresql .Spec .Credentials == nil {
129
- postgresql .Status .CredentialsStatus .ForEach (func (status * infrav1beta1.CredentialStatus ) {
130
- _ = postgreSQLServer .DropUser (status .Username , postgresql .Status .DatabaseStatus .Name )
131
- })
132
- postgresql .Status .CredentialsStatus = make ([]* infrav1beta1.CredentialStatus , 0 )
133
- return r .updateAndReturn (& ctx , & postgresql , & log )
134
- }
116
+ postgresql .Status .DatabaseStatus .SetDatabaseStatus (infrav1beta1 .Available , "Database up." , postgresql .Spec .DatabaseName , postgresql .Spec .HostName ).
117
+ WithUsername (postgresql .Spec .RootUsername ).
118
+ WithAuthDatabase (postgresql .Spec .RootAuthenticationDatabase ).
119
+ WithRootSecretLookup (postgresql .Spec .RootSecretLookup .Name , postgresql .Spec .RootSecretLookup .Namespace , postgresql .Spec .RootSecretLookup .Field )
135
120
136
121
// setup credentials as per spec
137
122
for _ , credential := range postgresql .Spec .Credentials {
@@ -155,12 +140,6 @@ func (r *PostgreSQLReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error)
155
140
}
156
141
}
157
142
158
- // - garbage collection
159
- // remove all statuses for credentials that are no longer required by spec, and delete users in database
160
- postgresql .RemoveUnneededCredentialsStatus ().ForEach (func (status * infrav1beta1.CredentialStatus ) {
161
- _ = postgreSQLServer .DropUser (status .Username , postgresql .Status .DatabaseStatus .Name )
162
- })
163
-
164
143
return r .updateAndReturn (& ctx , & postgresql , & log )
165
144
}
166
145
0 commit comments