@@ -3,6 +3,7 @@ package configuration
33import (
44 "context"
55 "fmt"
6+ "reflect"
67 "strconv"
78 "strings"
89
@@ -159,3 +160,72 @@ func GetProviderNamespacedName(configuration v1beta2.Configuration) *crossplane.
159160 Namespace : provider .DefaultNamespace ,
160161 }
161162}
163+
164+ // GetConfigurationsWithSameBackendReference will get configurations referencing the same backend
165+ func GetConfigurationsWithSameBackendReference (ctx context.Context , k8sClient client.Client , configuration * v1beta2.Configuration ) ([]* crossplane.Reference , error ) {
166+ var (
167+ configurationRefs = make ([]* crossplane.Reference , 0 )
168+ selector func (referenceBackend , backend * v1beta2.Backend ) bool
169+ )
170+
171+ backend := configuration .Spec .Backend
172+ if backend == nil {
173+ return configurationRefs , nil
174+ }
175+
176+ switch {
177+ case backend .Inline != "" :
178+ selector = func (referenceBackend , backend * v1beta2.Backend ) bool {
179+ if backend == nil {
180+ return false
181+ }
182+ return referenceBackend .Inline == backend .Inline
183+ }
184+ case backend .BackendType == "s3" && backend .S3 != nil :
185+ selector = func (referenceBackend , backend * v1beta2.Backend ) bool {
186+ if backend == nil {
187+ return false
188+ }
189+ return referenceBackend .BackendType == backend .BackendType && reflect .DeepEqual (referenceBackend .S3 , backend .S3 )
190+ }
191+ case backend .BackendType == "kubernetes" && backend .Kubernetes != nil :
192+ selector = func (referenceBackend , backend * v1beta2.Backend ) bool {
193+ if backend == nil {
194+ return false
195+ }
196+ return referenceBackend .BackendType == backend .BackendType && reflect .DeepEqual (referenceBackend .Kubernetes , backend .Kubernetes )
197+ }
198+ case backend .SecretSuffix != "" :
199+ selector = func (referenceBackend , backend * v1beta2.Backend ) bool {
200+ if backend == nil {
201+ return false
202+ }
203+ return referenceBackend .SecretSuffix == backend .SecretSuffix
204+ }
205+ }
206+
207+ if selector == nil {
208+ return configurationRefs , nil
209+ }
210+
211+ configurations := & v1beta2.ConfigurationList {}
212+ if err := k8sClient .List (ctx , configurations ); err != nil {
213+ return configurationRefs , client .IgnoreNotFound (err )
214+ }
215+
216+ for _ , item := range configurations .Items {
217+ if item .Name == configuration .Name && item .Namespace == configuration .Namespace {
218+ continue
219+ }
220+ // reflect.DeepEqual(backend, item.Spec.Backend) This approach may not yield accurate results.
221+ if ! selector (backend , item .Spec .Backend ) {
222+ continue
223+ }
224+ configurationRefs = append (configurationRefs , & crossplane.Reference {
225+ Name : item .Name ,
226+ Namespace : item .Namespace ,
227+ })
228+ }
229+
230+ return configurationRefs , nil
231+ }
0 commit comments