-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
584 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
test/crd/constraintpodstatus-customresourcedefinition.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.