Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Before deleting, check if there are other configurations using the same backend. If there are, do not proceed with the deletion #375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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