Skip to content

Commit a522323

Browse files
Reconcile to NooBaa for secret creation
Signed-off-by: Anisur Rahman <[email protected]>
1 parent b9cb9f0 commit a522323

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ build-releases/
1616
noobaa.cfg.yaml
1717

1818
*.IGNORE
19+
.idea/

pkg/controller/noobaa/noobaa_controller.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ func Add(mgr manager.Manager) error {
135135
return err
136136
}
137137

138+
secretsHandler := handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
139+
return system.MapSecretToNooBaa(types.NamespacedName{
140+
Name: obj.GetName(),
141+
Namespace: obj.GetNamespace(),
142+
})
143+
})
144+
err = c.Watch(source.Kind[client.Object](mgr.GetCache(), &corev1.Secret{}, secretsHandler, logEventsPredicate))
145+
if err != nil {
146+
return err
147+
}
148+
138149
// handler for global RPC message and ,simply trigger a reconcile on every message
139150
nb.GlobalRPC.Handler = func(req *nb.RPCMessage) (interface{}, error) {
140151
logrus.Infof("RPC Handle: {Op: %s, API: %s, Method: %s, Error: %s, Params: %+v}", req.Op, req.API, req.Method, req.Error, req.Params)

pkg/system/system.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,36 @@ func LoadConfigMapFromFlags() {
12941294
}
12951295
}
12961296

1297+
// MapSecretToBackingStores returns a list of backingstores that uses the secret in their secretReference
1298+
// used by backingstore_controller to watch secrets changes
1299+
func MapSecretToNooBaa(secret types.NamespacedName) []reconcile.Request {
1300+
log := util.Logger()
1301+
log.Infof("checking which nooBaas to reconcile. mapping secret %v to nooBaas external postgres secret", secret)
1302+
nbList := &nbv1.NooBaaList{
1303+
TypeMeta: metav1.TypeMeta{Kind: "NooBaaList"},
1304+
}
1305+
if !util.KubeList(nbList, &client.ListOptions{Namespace: secret.Namespace}) {
1306+
log.Infof("Could not found NooBaa in namespace %q, while trying to find NooBaa that uses %s secret", secret.Namespace, secret.Name)
1307+
return nil
1308+
}
1309+
1310+
reqs := []reconcile.Request{}
1311+
1312+
for _, nb := range nbList.Items {
1313+
nbSecret := util.GetNooBaaExternalPgSecret(&nb)
1314+
if nbSecret != nil && nbSecret.Name == secret.Name {
1315+
reqs = append(reqs, reconcile.Request{
1316+
NamespacedName: types.NamespacedName{
1317+
Name: nb.Name,
1318+
Namespace: nb.Namespace,
1319+
},
1320+
})
1321+
}
1322+
}
1323+
1324+
return reqs
1325+
}
1326+
12971327
// SetAllowNoobaaDeletion sets AllowNoobaaDeletion Noobaa CR field to true so the webhook won't block the deletion
12981328
func SetAllowNoobaaDeletion(noobaa *nbv1.NooBaa) error {
12991329
// Explicitly allow deletion of NooBaa CR

pkg/util/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,21 @@ func NooBaaCondition(noobaa *nbv1.NooBaa, t conditionsv1.ConditionType, s corev1
18621862
return found
18631863
}
18641864

1865+
// GetNooBaaExternalPgSecret returns the secret and adding the namespace if it is missing
1866+
func GetNooBaaExternalPgSecret(nb *nbv1.NooBaa) *corev1.SecretReference {
1867+
var secretRef *corev1.SecretReference
1868+
if nb.Spec.ExternalPgSecret != nil {
1869+
secretRef = &corev1.SecretReference{
1870+
Name: nb.Spec.ExternalPgSecret.Name,
1871+
Namespace: nb.Spec.ExternalPgSecret.Namespace,
1872+
}
1873+
if secretRef.Namespace == "" {
1874+
secretRef.Namespace = nb.Namespace
1875+
}
1876+
}
1877+
return secretRef
1878+
}
1879+
18651880
// GetAvailabeKubeCli will check which k8s cli command is availabe in the system: oc or kubectl
18661881
// returns one of: "oc" or "kubectl"
18671882
func GetAvailabeKubeCli() string {

0 commit comments

Comments
 (0)