Skip to content

Commit

Permalink
use default scope
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Sep 27, 2024
1 parent 9d18df2 commit 849e291
Show file tree
Hide file tree
Showing 8 changed files with 584 additions and 92 deletions.
88 changes: 88 additions & 0 deletions internal/controller/constraint_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package controller

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
templatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1"
"github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1"
constraintstatusv1beta1 "github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1"
"github.com/pluralsh/deployment-operator/pkg/test/mocks"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("ConstraintPodStatus Controller", Ordered, func() {
Context("When reconciling a resource", func() {
const (
resourceName = "default"
namespace = "default"
kind = "Test"
templateName = "test-template"
)

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default",
}

cps := new(constraintstatusv1beta1.ConstraintPodStatus)

BeforeAll(func() {
By("creating the custom resource for the Kind ConstraintPodStatus")
err := kClient.Get(ctx, typeNamespacedName, cps)
if err != nil && errors.IsNotFound(err) {
resource := &constraintstatusv1beta1.ConstraintPodStatus{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
Labels: map[string]string{v1beta1.ConstraintNameLabel: templateName, v1beta1.ConstraintKindLabel: kind, v1beta1.ConstraintTemplateNameLabel: templateName},
},
Status: constraintstatusv1beta1.ConstraintPodStatusStatus{},
}
Expect(kClient.Create(ctx, resource)).To(Succeed())
}
template := &templatesv1.ConstraintTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: templateName,
Namespace: namespace,
},
Spec: templatesv1.ConstraintTemplateSpec{},
}
Expect(kClient.Create(ctx, template)).To(Succeed())
})

AfterAll(func() {
resource := &constraintstatusv1beta1.ConstraintPodStatus{}
err := kClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance ConstraintPodStatus")
Expect(kClient.Delete(ctx, resource)).To(Succeed())

template := new(templatesv1.ConstraintTemplate)
Expect(kClient.Get(ctx, types.NamespacedName{Name: templateName}, template)).To(Succeed())
Expect(kClient.Delete(ctx, template)).To(Succeed())
})

It("should fail reconcile resource", func() {
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
reconciler := &ConstraintReconciler{
Client: kClient,
Scheme: kClient.Scheme(),
ConsoleClient: fakeConsoleClient,
Reader: kClient,
}
_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).To(HaveOccurred())
})
})

})
7 changes: 0 additions & 7 deletions internal/controller/customhealth_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ var _ = Describe("Customhealt Controller", Ordered, func() {

It("should successfully reconcile resource", func() {
By("Reconciling the import resource")
_ = struct {
expectedStatus v1alpha1.CustomHealthStatus
}{
expectedStatus: v1alpha1.CustomHealthStatus{
Conditions: []metav1.Condition{},
},
}
reconciler := &CustomHealthReconciler{
Client: kClient,
Scheme: kClient.Scheme(),
Expand Down
41 changes: 0 additions & 41 deletions internal/controller/job_scope.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/controller/pipelinegate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (r *PipelineGateReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, nil
}

scope, err := NewPipelineGateScope(ctx, r.Client, crGate)
scope, err := NewDefaultScope(ctx, r.Client, crGate)
if err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func (r *PipelineGateReconciler) updateJob(ctx context.Context, reconciledJob *b
reconciledJob.Spec.Template.Annotations[k] = v
}

jobScope, err := NewJobScope(ctx, r.Client, reconciledJob)
jobScope, err := NewDefaultScope(ctx, r.Client, reconciledJob)
if err != nil {
return err
}
Expand Down
42 changes: 0 additions & 42 deletions internal/controller/pipelinegate_scope.go

This file was deleted.

8 changes: 8 additions & 0 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
templatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1"
constraintstatusv1beta1 "github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1"
deploymentsv1alpha1 "github.com/pluralsh/deployment-operator/api/v1alpha1"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -72,6 +74,12 @@ var _ = BeforeSuite(func() {
err = apiextensionsv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = constraintstatusv1beta1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = templatesv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

kClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(kClient).NotTo(BeNil())
Expand Down
80 changes: 80 additions & 0 deletions test/crd/constraintpodstatus-customresourcedefinition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
labels:
gatekeeper.sh/system: "yes"
name: constraintpodstatuses.status.gatekeeper.sh
spec:
group: status.gatekeeper.sh
names:
kind: ConstraintPodStatus
listKind: ConstraintPodStatusList
plural: constraintpodstatuses
singular: constraintpodstatus
preserveUnknownFields: false
scope: Namespaced
versions:
- name: v1beta1
schema:
openAPIV3Schema:
description: ConstraintPodStatus is the Schema for the constraintpodstatuses API.
properties:
apiVersion:
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
status:
description: ConstraintPodStatusStatus defines the observed state of ConstraintPodStatus.
properties:
constraintUID:
description: |-
Storing the constraint UID allows us to detect drift, such as
when a constraint has been recreated after its CRD was deleted
out from under it, interrupting the watch
type: string
enforced:
type: boolean
errors:
items:
description: Error represents a single error caught while adding a constraint to engine.
properties:
code:
type: string
location:
type: string
message:
type: string
required:
- code
- message
type: object
type: array
id:
type: string
observedGeneration:
format: int64
type: integer
operations:
items:
type: string
type: array
type: object
type: object
served: true
storage: true
Loading

0 comments on commit 849e291

Please sign in to comment.