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 @@
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"

Expand Down Expand Up @@ -89,8 +90,8 @@
// - it's in force-delete state
func IsDeletable(ctx context.Context, k8sClient client.Client, configuration *v1beta2.Configuration) (bool, error) {
if feature.DefaultFeatureGate.Enabled(features.AllowDeleteProvisioningResource) {
return true, nil
}

Check warning on line 94 in controllers/configuration/configuration.go

View check run for this annotation

Codecov / codecov/patch

controllers/configuration/configuration.go#L93-L94

Added lines #L93 - L94 were not covered by tests
if configuration.Spec.ForceDelete != nil && *configuration.Spec.ForceDelete {
return true, nil
}
Expand Down Expand Up @@ -159,3 +160,72 @@
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
}

Check warning on line 209 in controllers/configuration/configuration.go

View check run for this annotation

Codecov / codecov/patch

controllers/configuration/configuration.go#L208-L209

Added lines #L208 - L209 were not covered by tests

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

Check warning on line 214 in controllers/configuration/configuration.go

View check run for this annotation

Codecov / codecov/patch

controllers/configuration/configuration.go#L213-L214

Added lines #L213 - L214 were not covered by tests

for _, item := range configurations.Items {
if item.Name == configuration.Name && item.Namespace == configuration.Namespace {
continue

Check warning on line 218 in controllers/configuration/configuration.go

View check run for this annotation

Codecov / codecov/patch

controllers/configuration/configuration.go#L218

Added line #L218 was not covered by tests
}
// 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
Loading