Skip to content

Commit

Permalink
Before deleting, check if there are other configurations using the sa…
Browse files Browse the repository at this point in the history
…me backend. If there are, do not proceed with the deletion (#374)

Signed-off-by: 吴就业 <wujiuye@lizhi.fm>
  • Loading branch information
吴就业 committed Aug 18, 2023
1 parent 1dfb4a3 commit 8d2072d
Show file tree
Hide file tree
Showing 3 changed files with 409 additions and 2 deletions.
70 changes: 70 additions & 0 deletions controllers/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package configuration
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"

Expand Down Expand Up @@ -159,3 +160,72 @@ func GetProviderNamespacedName(configuration v1beta2.Configuration) *crossplane.
Namespace: provider.DefaultNamespace,
}
}

// GetConfigurationsWithSameBackendReference will get configurations referencing the same backend
func GetConfigurationsWithSameBackendReference(ctx context.Context, k8sClient client.Client, configuration *v1beta2.Configuration) ([]*crossplane.Reference, error) {
var (
configurationRefs = make([]*crossplane.Reference, 0)
selector func(referenceBackend, backend *v1beta2.Backend) bool
)

backend := configuration.Spec.Backend
if backend == nil {
return configurationRefs, nil
}

switch {
case backend.Inline != "":
selector = func(referenceBackend, backend *v1beta2.Backend) bool {
if backend == nil {
return false
}
return referenceBackend.Inline == backend.Inline
}
case backend.BackendType == "s3" && backend.S3 != nil:
selector = func(referenceBackend, backend *v1beta2.Backend) bool {
if backend == nil {
return false
}
return referenceBackend.BackendType == backend.BackendType && reflect.DeepEqual(referenceBackend.S3, backend.S3)
}
case backend.BackendType == "kubernetes" && backend.Kubernetes != nil:
selector = func(referenceBackend, backend *v1beta2.Backend) bool {
if backend == nil {
return false
}
return referenceBackend.BackendType == backend.BackendType && reflect.DeepEqual(referenceBackend.Kubernetes, backend.Kubernetes)
}
case backend.SecretSuffix != "":
selector = func(referenceBackend, backend *v1beta2.Backend) bool {
if backend == nil {
return false
}
return referenceBackend.SecretSuffix == backend.SecretSuffix
}
}

if selector == nil {
return configurationRefs, nil
}

configurations := &v1beta2.ConfigurationList{}
if err := k8sClient.List(ctx, configurations); err != nil {
return configurationRefs, client.IgnoreNotFound(err)
}

for _, item := range configurations.Items {
if item.Name == configuration.Name && item.Namespace == configuration.Namespace {
continue
}
// reflect.DeepEqual(backend, item.Spec.Backend) This approach may not yield accurate results.
if !selector(backend, item.Spec.Backend) {
continue
}
configurationRefs = append(configurationRefs, &crossplane.Reference{
Name: item.Name,
Namespace: item.Namespace,
})
}

return configurationRefs, nil
}
Loading

0 comments on commit 8d2072d

Please sign in to comment.