Skip to content

Commit

Permalink
Delete from graph only deleted objects
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Aug 29, 2024
1 parent 8fecc47 commit 646e927
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/feature/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"gomodules.xyz/pointer"
"gomodules.xyz/sets"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
cu "kmodules.xyz/client-go/client"
"kmodules.xyz/resource-metadata/apis/ui/v1alpha1"
Expand All @@ -34,7 +34,7 @@ func (r *frReconciler) updateAllFeatureSetDependencies(ctx context.Context) erro
for _, name := range r.feature.Spec.Requirements.Features {
f := &v1alpha1.Feature{}
if err := r.client.Get(ctx, types.NamespacedName{Name: name}, f); err != nil {
if errors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
// required feature isn't available yet
continue
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/controllers/feature/feature_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
fluxhelm "github.com/fluxcd/helm-controller/api/v2"
"github.com/go-logr/logr"
"gomodules.xyz/pointer"
kerr "k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -86,14 +86,14 @@ func (r *FeatureReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

if fr.feature.DeletionTimestamp != nil {
err = fr.updateFeatureSetAndRemoveFinalizer(ctx)
if err != nil && kerr.IsNotFound(err) {
if err != nil && apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}

err = fr.reconcile(ctx)
if err != nil && !kerr.IsNotFound(err) {
if err != nil && !apierrors.IsNotFound(err) {
logger.Error(err, "failed to reconcile")
return ctrl.Result{}, nil
}
Expand All @@ -103,7 +103,7 @@ func (r *FeatureReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

err = fr.updateFeatureSetEntry(ctx)
if err != nil && !kerr.IsNotFound(err) {
if err != nil && !apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -224,7 +224,7 @@ func (r *frReconciler) evaluateStatus(ctx context.Context) (featureStatus, error

release, err := r.getHelmRelease(ctx)
if err != nil {
if kerr.IsNotFound(err) {
if apierrors.IsNotFound(err) {
status.managed = false
return status, nil
}
Expand Down Expand Up @@ -260,7 +260,7 @@ func (r *frReconciler) getHelmRelease(ctx context.Context) (fluxhelm.HelmRelease
if len(releases.Items) > 0 {
return releases.Items[0], nil
}
return fluxhelm.HelmRelease{}, kerr.NewNotFound(schema.GroupResource{
return fluxhelm.HelmRelease{}, apierrors.NewNotFound(schema.GroupResource{
Group: fluxhelm.GroupVersion.Group,
Resource: "helmreleases",
}, r.feature.Name)
Expand All @@ -269,7 +269,7 @@ func (r *frReconciler) getHelmRelease(ctx context.Context) (fluxhelm.HelmRelease
func (r *frReconciler) isFeatureEnabled(ctx context.Context, status featureStatus) (bool, error) {
_, err := r.getHelmRelease(ctx)
if err != nil {
if kerr.IsNotFound(err) {
if apierrors.IsNotFound(err) {
status.managed = false
return isRequiredResourcesExist(status) && isWorkloadOrReleaseExist(status), nil
}
Expand Down Expand Up @@ -297,7 +297,7 @@ func (r *frReconciler) checkDependencyExistence(ctx context.Context) (*requireme
for _, d := range r.feature.Spec.Requirements.Features {
f := &uiapi.Feature{}
if err := r.client.Get(ctx, types.NamespacedName{Name: d}, f); err != nil {
if kerr.IsNotFound(err) {
if apierrors.IsNotFound(err) {
status.reason = fmt.Sprintf("Dependency not satisfied. Feature %q does not exist.", d)
return status, nil
}
Expand Down Expand Up @@ -413,7 +413,7 @@ func (r *frReconciler) checkWorkload(ctx context.Context, w uiapi.WorkloadInfo)

func (r *frReconciler) updateFeatureSetAndRemoveFinalizer(ctx context.Context) error {
if err := r.updateFeatureSetEntry(ctx); err != nil {
if !kerr.IsNotFound(err) {
if !apierrors.IsNotFound(err) {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/feature/feature_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"gomodules.xyz/pointer"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -397,7 +397,7 @@ func TestFeatureSetStatus(t *testing.T) {
expectedReadyStatus *bool
}{
"Should return not found error when FeatureSet does not exist": {
expectedErr: kerr.NewNotFound(
expectedErr: apierrors.NewNotFound(
schema.GroupResource{
Group: uiapi.SchemeGroupVersion.Group,
Resource: uiapi.ResourceFeatureSets,
Expand Down Expand Up @@ -478,7 +478,7 @@ func TestFeatureSetStatus(t *testing.T) {
err = r.updateFeatureSetEntry(context.Background())
if tt.expectedErr != nil {
if assert.NotNil(t, err) {
assert.True(t, kerr.IsNotFound(err))
assert.True(t, apierrors.IsNotFound(err))
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/scanner/workload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
apps "k8s.io/api/apps/v1"
batch "k8s.io/api/batch/v1"
core "k8s.io/api/core/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -113,7 +113,7 @@ func (r *WorkloadReconciler) shouldScan(ref string) (bool, error) {
err := r.Get(context.TODO(), types.NamespacedName{
Name: scannerapi.GetReportName(ref),
}, &rep)
if kerr.IsNotFound(err) {
if apierrors.IsNotFound(err) {
return true, nil
}
if err != nil {
Expand Down
15 changes: 9 additions & 6 deletions pkg/graph/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

core "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -49,13 +50,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
var obj unstructured.Unstructured
obj.SetGroupVersionKind(gvk)
if err := r.Get(context.TODO(), req.NamespacedName, &obj); err != nil {
oid := kmapi.ObjectID{
Group: gvk.Group,
Kind: gvk.Kind,
Namespace: req.Namespace,
Name: req.Name,
if apierrors.IsNotFound(err) {
oid := kmapi.ObjectID{
Group: gvk.Group,
Kind: gvk.Kind,
Namespace: req.Namespace,
Name: req.Name,
}
objGraph.Delete(oid.OID())
}
objGraph.Delete(oid.OID())

// we'll ignore not-found errors, since they can't be fixed by an immediate
// requeue (we'll need to wait for a new notification), and we can get them
Expand Down

0 comments on commit 646e927

Please sign in to comment.