diff --git a/pkg/controller/constraint/constraint_controller.go b/pkg/controller/constraint/constraint_controller.go index c4f3551fd1c..21feeca5c51 100644 --- a/pkg/controller/constraint/constraint_controller.go +++ b/pkg/controller/constraint/constraint_controller.go @@ -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") } @@ -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") } @@ -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" { diff --git a/pkg/controller/constrainttemplate/constrainttemplate_controller.go b/pkg/controller/constrainttemplate/constrainttemplate_controller.go index 380e4a63c84..e331d03f752 100644 --- a/pkg/controller/constrainttemplate/constrainttemplate_controller.go +++ b/pkg/controller/constrainttemplate/constrainttemplate_controller.go @@ -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) @@ -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) @@ -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" { diff --git a/pkg/drivers/k8scel/transform/vap_util.go b/pkg/drivers/k8scel/transform/vap_util.go index 7775a2af840..0e88b4dafc9 100644 --- a/pkg/drivers/k8scel/transform/vap_util.go +++ b/pkg/drivers/k8scel/transform/vap_util.go @@ -1,7 +1,6 @@ package transform import ( - "errors" "sync" "github.com/go-logr/logr" @@ -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 @@ -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") - } -}