mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-08-22 05:57:02 +00:00
* fix(controller): decode old object for delete requests Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * chore: modernize golang Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * chore: modernize golang Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * chore: modernize golang Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> * fix: tls controller Signed-off-by: Oliver Baehler <oliver@sudo-i.net> * feat: add tenantowner tenant status reference Signed-off-by: Oliver Baehler <oliver@sudo-i.net> * fix: tlsreconciler only patches cabundles Signed-off-by: Oliver Baehler <oliver@sudo-i.net> * chore: refactor logger usage Signed-off-by: Oliver Baehler <oliver@sudo-i.net> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * fix: tlsreconciler only patches cabundles Signed-off-by: Oliver Baehler <oliver@sudo-i.net> * fix: tlsreconciler only patches cabundles Signed-off-by: Oliver Baehler <oliver@sudo-i.net> --------- Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com> Signed-off-by: Oliver Baehler <oliver@sudo-i.net> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
131 lines
3.2 KiB
Go
131 lines
3.2 KiB
Go
// Copyright 2020-2026 Project Capsule Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package tenant
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
corev1 "k8s.io/api/core/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"k8s.io/apimachinery/pkg/selection"
|
|
"k8s.io/client-go/util/retry"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
|
|
"github.com/projectcapsule/capsule/pkg/api/meta"
|
|
"github.com/projectcapsule/capsule/pkg/utils"
|
|
)
|
|
|
|
func readyTenantNamespaces(tnt *capsulev1beta2.Tenant) []string {
|
|
namespaces := make([]string, 0, len(tnt.Status.Spaces))
|
|
|
|
for _, ns := range tnt.Status.Spaces {
|
|
ready := ns.Conditions.GetConditionByType(meta.ReadyCondition)
|
|
if ready != nil && ready.Status != metav1.ConditionTrue {
|
|
continue
|
|
}
|
|
|
|
terminating := ns.Conditions.GetConditionByType(meta.TerminatingCondition)
|
|
if terminating != nil && terminating.Status == metav1.ConditionTrue {
|
|
continue
|
|
}
|
|
|
|
namespaces = append(namespaces, ns.Name)
|
|
}
|
|
|
|
return namespaces
|
|
}
|
|
|
|
func runForTenantNamespaces(
|
|
ctx context.Context,
|
|
tnt *capsulev1beta2.Tenant,
|
|
fn func(context.Context, string) error,
|
|
) error {
|
|
errs := make(chan error, len(tnt.Status.Spaces))
|
|
group := new(errgroup.Group)
|
|
|
|
for _, namespace := range readyTenantNamespaces(tnt) {
|
|
group.Go(func() error {
|
|
if err := fn(ctx, namespace); err != nil {
|
|
errs <- fmt.Errorf("namespace %q: %w", namespace, err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
_ = group.Wait()
|
|
|
|
close(errs)
|
|
|
|
var joined []error
|
|
for err := range errs {
|
|
joined = append(joined, err)
|
|
}
|
|
|
|
return errors.Join(joined...)
|
|
}
|
|
|
|
// pruningResources is taking care of removing the no more requested sub-resources as LimitRange, ResourceQuota or
|
|
// NetworkPolicy using the "exists" and "notin" LabelSelector to perform an outer-join removal.
|
|
func (r *Manager) pruningResources(ctx context.Context, ns string, keys []string, obj client.Object) (err error) {
|
|
var capsuleLabel string
|
|
|
|
if capsuleLabel, err = utils.GetTypeLabel(obj); err != nil {
|
|
return err
|
|
}
|
|
|
|
selector := labels.NewSelector()
|
|
|
|
var exists *labels.Requirement
|
|
|
|
if exists, err = labels.NewRequirement(capsuleLabel, selection.Exists, []string{}); err != nil {
|
|
return err
|
|
}
|
|
|
|
selector = selector.Add(*exists)
|
|
|
|
if len(keys) > 0 {
|
|
var notIn *labels.Requirement
|
|
|
|
if notIn, err = labels.NewRequirement(capsuleLabel, selection.NotIn, keys); err != nil {
|
|
return err
|
|
}
|
|
|
|
selector = selector.Add(*notIn)
|
|
}
|
|
|
|
r.Log.V(4).Info("pruning objects with label selector " + selector.String())
|
|
|
|
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
|
|
err := r.DeleteAllOf(ctx, obj, &client.DeleteAllOfOptions{
|
|
ListOptions: client.ListOptions{
|
|
LabelSelector: selector,
|
|
Namespace: ns,
|
|
},
|
|
DeleteOptions: client.DeleteOptions{},
|
|
})
|
|
if err != nil {
|
|
if apierrors.IsNotFound(err) || apierrors.HasStatusCause(err, corev1.NamespaceTerminatingCause) {
|
|
r.Log.V(4).Info(
|
|
"skipping pruning because target namespace or object is gone/terminating",
|
|
"namespace", ns,
|
|
"labelSelector", selector.String(),
|
|
)
|
|
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|