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: 吴就业 <[email protected]>
  • 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
}

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

0 comments on commit 8d2072d

Please sign in to comment.