Skip to content

Commit

Permalink
Rework oprhaned Templates removal: addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eromanova committed Nov 6, 2024
1 parent 3d17d81 commit d617632
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 103 deletions.
16 changes: 16 additions & 0 deletions api/v1alpha1/clustertemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package v1alpha1

import (
"context"
"fmt"

"github.com/Masterminds/semver/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -108,6 +110,20 @@ func (t *ClusterTemplate) GetCommonStatus() *TemplateStatusCommon {
return &t.Status.TemplateStatusCommon
}

// IsOrphaned checks whether the Template is orphaned or not.
func (t *ClusterTemplate) IsOrphaned(ctx context.Context, cl client.Client) (bool, error) {
list := new(ClusterTemplateChainList)
if err := cl.List(ctx, list, client.InNamespace(t.Namespace), client.MatchingFields{SupportedTemplateKey: t.Name}); err != nil {
return false, fmt.Errorf("failed to list %s: %w", list.GroupVersionKind(), err)
}
for _, chain := range list.Items {
if chain.DeletionTimestamp == nil {
return false, nil
}
}
return true, nil
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=clustertmpl
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/providertemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package v1alpha1

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ProviderTemplateKind denotes the providertemplate resource Kind.
Expand Down Expand Up @@ -68,6 +70,11 @@ func (t *ProviderTemplate) GetCommonStatus() *TemplateStatusCommon {
return &t.Status.TemplateStatusCommon
}

// IsOrphaned checks whether the Template is orphaned or not.
func (*ProviderTemplate) IsOrphaned(_ context.Context, _ client.Client) (bool, error) {
return false, nil
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=providertmpl,scope=Cluster
Expand Down
16 changes: 16 additions & 0 deletions api/v1alpha1/servicetemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package v1alpha1

import (
"context"
"fmt"

"github.com/Masterminds/semver/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -85,6 +87,20 @@ func (t *ServiceTemplate) GetCommonStatus() *TemplateStatusCommon {
return &t.Status.TemplateStatusCommon
}

// IsOrphaned checks whether the Template is orphaned or not.
func (t *ServiceTemplate) IsOrphaned(ctx context.Context, cl client.Client) (bool, error) {
list := new(ServiceTemplateChainList)
if err := cl.List(ctx, list, client.InNamespace(t.Namespace), client.MatchingFields{SupportedTemplateKey: t.Name}); err != nil {
return false, fmt.Errorf("failed to list %s: %w", list.GroupVersionKind(), err)
}
for _, chain := range list.Items {
if chain.DeletionTimestamp == nil {
return false, nil
}
}
return true, nil
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=svctmpl
Expand Down
1 change: 1 addition & 0 deletions internal/controller/template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ type templateCommon interface {
GetHelmSpec() *hmc.HelmSpec
GetCommonStatus() *hmc.TemplateStatusCommon
FillStatusWithProviders(map[string]string) error
IsOrphaned(context.Context, client.Client) (bool, error)
}

func (r *TemplateReconciler) ReconcileTemplate(ctx context.Context, template templateCommon) (ctrl.Result, error) {
Expand Down
Loading

0 comments on commit d617632

Please sign in to comment.