Skip to content

Commit

Permalink
move back vap binding methods
Browse files Browse the repository at this point in the history
Signed-off-by: Avinash Patnala <[email protected]>
  • Loading branch information
Avinash Patnala committed Oct 29, 2024
1 parent 78f720f commit 02c4374
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
15 changes: 13 additions & 2 deletions pkg/controller/constraint/constraint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (r *ReconcileConstraint) Reconcile(ctx context.Context, request reconcile.R
r.log.Info("constraint controller", "generateVAPB", generateVAPB)
// generate vapbinding resources
if generateVAPB && groupVersion != nil {
currentVapBinding, err := transform.VapBindingForVersion(*groupVersion)
currentVapBinding, err := vapBindingForVersion(*groupVersion)
if err != nil {
return reconcile.Result{}, r.reportErrorOnConstraintStatus(ctx, status, err, "could not get ValidatingAdmissionPolicyBinding API version")
}
Expand Down Expand Up @@ -388,7 +388,7 @@ func (r *ReconcileConstraint) Reconcile(ctx context.Context, request reconcile.R
// do not generate vapbinding resources
// remove if exists
if !generateVAPB && groupVersion != nil {
currentVapBinding, err := transform.VapBindingForVersion(*groupVersion)
currentVapBinding, err := vapBindingForVersion(*groupVersion)
if err != nil {
return reconcile.Result{}, r.reportErrorOnConstraintStatus(ctx, status, err, "could not get ValidatingAdmissionPolicyBinding API version")
}
Expand Down Expand Up @@ -619,6 +619,17 @@ func (c *ConstraintsCache) reportTotalConstraints(ctx context.Context, reporter
}
}

func vapBindingForVersion(gvk schema.GroupVersion) (client.Object, error) {
switch gvk.Version {
case "v1":
return &admissionregistrationv1.ValidatingAdmissionPolicyBinding{}, nil
case "v1beta1":
return &admissionregistrationv1beta1.ValidatingAdmissionPolicyBinding{}, nil
default:
return nil, errors.New("unrecognized version")
}
}

func getRunTimeVAPBinding(gvk *schema.GroupVersion, transformedVapBinding *admissionregistrationv1beta1.ValidatingAdmissionPolicyBinding, currentVapBinding client.Object) (client.Object, error) {
if currentVapBinding == nil {
if gvk.Version == "v1" {
Expand Down
15 changes: 13 additions & 2 deletions pkg/controller/constrainttemplate/constrainttemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (r *ReconcileConstraintTemplate) handleUpdate(
}
// generating vap resources
if generateVap && isVapAPIEnabled && groupVersion != nil {
currentVap, err := transform.VapForVersion(groupVersion)
currentVap, err := vapForVersion(groupVersion)
if err != nil {
logger.Error(err, "error getting vap object with respective groupVersion")
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Could not get VAP with runtime group version", status, err)
Expand Down Expand Up @@ -545,7 +545,7 @@ func (r *ReconcileConstraintTemplate) handleUpdate(
// do not generate vap resources
// remove if exists
if !generateVap && isVapAPIEnabled && groupVersion != nil {
currentVap, err := transform.VapForVersion(groupVersion)
currentVap, err := vapForVersion(groupVersion)
if err != nil {
logger.Error(err, "error getting vap object with respective groupVersion")
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Could not get VAP with correct group version", status, err)
Expand Down Expand Up @@ -747,6 +747,17 @@ func makeGvk(kind string) schema.GroupVersionKind {
}
}

func vapForVersion(gvk *schema.GroupVersion) (client.Object, error) {
switch gvk.Version {
case "v1":
return &admissionregistrationv1.ValidatingAdmissionPolicy{}, nil
case "v1beta1":
return &admissionregistrationv1beta1.ValidatingAdmissionPolicy{}, nil
default:
return nil, errors.New("unrecognized version")
}
}

func getRunTimeVAP(gvk *schema.GroupVersion, transformedVap *admissionregistrationv1beta1.ValidatingAdmissionPolicy, currentVap client.Object) (client.Object, error) {
if currentVap == nil {
if gvk.Version == "v1" {
Expand Down
24 changes: 0 additions & 24 deletions pkg/drivers/k8scel/transform/vap_util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package transform

import (
"errors"
"sync"

"github.com/go-logr/logr"
Expand All @@ -10,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
rest "k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var vapMux sync.RWMutex
Expand Down Expand Up @@ -79,25 +77,3 @@ func IsVapAPIEnabled(log *logr.Logger) (bool, *schema.GroupVersion) {
*VapAPIEnabled = false
return false, nil
}

func VapForVersion(gvk *schema.GroupVersion) (client.Object, error) {
switch gvk.Version {
case "v1":
return &admissionregistrationv1.ValidatingAdmissionPolicy{}, nil
case "v1beta1":
return &admissionregistrationv1beta1.ValidatingAdmissionPolicy{}, nil
default:
return nil, errors.New("unrecognized version")
}
}

func VapBindingForVersion(gvk schema.GroupVersion) (client.Object, error) {
switch gvk.Version {
case "v1":
return &admissionregistrationv1.ValidatingAdmissionPolicyBinding{}, nil
case "v1beta1":
return &admissionregistrationv1beta1.ValidatingAdmissionPolicyBinding{}, nil
default:
return nil, errors.New("unrecognized version")
}
}

0 comments on commit 02c4374

Please sign in to comment.