Skip to content

Commit

Permalink
fix: set owner references on validation result to ensure cleanup (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-ibra authored Nov 16, 2023
1 parent f346f63 commit 9c7c28d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/go-logr/logr v1.3.0
github.com/onsi/ginkgo/v2 v2.13.1
github.com/onsi/gomega v1.30.0
github.com/spectrocloud-labs/validator v0.0.18
github.com/spectrocloud-labs/validator v0.0.20
github.com/stretchr/testify v1.8.4
k8s.io/apimachinery v0.28.4
k8s.io/client-go v0.28.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO
github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg=
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/spectrocloud-labs/validator v0.0.18 h1:cqPWuKMbEP4x7hT1OEftphO58qkbYRl4iX8ja/ewg6E=
github.com/spectrocloud-labs/validator v0.0.18/go.mod h1:dQ3aX98ZpRLzHyDIJX3zyAm6Nty2HBq6TMAZDPPA+UA=
github.com/spectrocloud-labs/validator v0.0.20 h1:/H2Nqf+mCm7Oi7lW2E/am3rJJUr1w2ETy3GSlwM44ZQ=
github.com/spectrocloud-labs/validator v0.0.20/go.mod h1:AuZGLKAATKBLFXgwPSKv6z9jsq2v4FCgga6rDUATIu4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
41 changes: 32 additions & 9 deletions internal/controller/ocivalidator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/go-logr/logr"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ktypes "k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/spectrocloud-labs/validator-plugin-oci/internal/validators/ecr"
"github.com/spectrocloud-labs/validator-plugin-oci/internal/validators/oci"
vapi "github.com/spectrocloud-labs/validator/api/v1alpha1"
"github.com/spectrocloud-labs/validator/pkg/util/ptr"
vres "github.com/spectrocloud-labs/validator/pkg/validationresult"
)

Expand All @@ -53,18 +55,16 @@ func (r *OciValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Request

validator := &v1alpha1.OciValidator{}
if err := r.Get(ctx, req.NamespacedName, validator); err != nil {
// Ignore not-found errors, since they can't be fixed by an immediate requeue
if apierrs.IsNotFound(err) {
return ctrl.Result{}, nil
if !apierrs.IsNotFound(err) {
r.Log.Error(err, "failed to fetch OciValidator", "key", req)
}
r.Log.Error(err, "failed to fetch OciValidator")
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// Get the active validator's validation result
vr := &vapi.ValidationResult{}
nn := ktypes.NamespacedName{
Name: fmt.Sprintf("validator-plugin-oci-%s", validator.Name),
Name: validationResultName(validator),
Namespace: req.Namespace,
}
if err := r.Get(ctx, nn, vr); err == nil {
Expand All @@ -73,10 +73,7 @@ func (r *OciValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Request
if !apierrs.IsNotFound(err) {
r.Log.V(0).Error(err, "unexpected error getting ValidationResult", "name", nn.Name, "namespace", nn.Namespace)
}

if err := vres.HandleNewValidationResult(
r.Client, constants.PluginCode, validator.Spec.ResultCount(), nn, r.Log,
); err != nil {
if err := vres.HandleNewValidationResult(r.Client, buildValidationResult(validator), r.Log); err != nil {
return ctrl.Result{}, err
}
}
Expand Down Expand Up @@ -111,3 +108,29 @@ func (r *OciValidatorReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&v1alpha1.OciValidator{}).
Complete(r)
}

func buildValidationResult(validator *v1alpha1.OciValidator) *vapi.ValidationResult {
return &vapi.ValidationResult{
ObjectMeta: metav1.ObjectMeta{
Name: validationResultName(validator),
Namespace: validator.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: validator.APIVersion,
Kind: validator.Kind,
Name: validator.Name,
UID: validator.UID,
Controller: ptr.Ptr(true),
},
},
},
Spec: vapi.ValidationResultSpec{
Plugin: constants.PluginCode,
ExpectedResults: validator.Spec.ResultCount(),
},
}
}

func validationResultName(validator *v1alpha1.OciValidator) string {
return fmt.Sprintf("validator-plugin-oci-%s", validator.Name)
}

0 comments on commit 9c7c28d

Please sign in to comment.