Skip to content

Commit 0b1d89d

Browse files
authored
Upgrade constrainttemplate_controller to apiextensionsv1 (#1320)
This PR changes constrainttemplate_controller to use apiextensionsv1 to create Constraint kind v1 CRDs. That change revealed some additional problems. In particular, that byPod status was being pruned by the API server. This required a fix in Constraint Framework. open-policy-agent/frameworks#120 adds a status field to the Constraint kind CRD. This allows Gatekeeper to add byPodStatus to the object. This PR imports this new code and updates some tests. Contributes to #550 Signed-off-by: juliankatz <[email protected]>
1 parent e6ea240 commit 0b1d89d

File tree

9 files changed

+85
-38
lines changed

9 files changed

+85
-38
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package apis
17+
18+
import (
19+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
20+
)
21+
22+
func init() {
23+
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
24+
AddToSchemes = append(AddToSchemes, apiextensionsv1.AddToScheme)
25+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/onsi/ginkgo v1.14.1
1616
github.com/onsi/gomega v1.10.2
1717
github.com/open-policy-agent/cert-controller v0.2.0
18-
github.com/open-policy-agent/frameworks/constraint v0.0.0-20210518223409-ecad1fe8ed8d
18+
github.com/open-policy-agent/frameworks/constraint v0.0.0-20210522003146-5c034948ac29
1919
github.com/open-policy-agent/opa v0.24.0
2020
github.com/pkg/errors v0.9.1
2121
github.com/prometheus/client_golang v1.7.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs=
330330
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
331331
github.com/open-policy-agent/cert-controller v0.2.0 h1:Z+IPOYDor28l6cjEo2WvTZY6Bv5oYR6wECEIP8pyG/M=
332332
github.com/open-policy-agent/cert-controller v0.2.0/go.mod h1:SWS7Ame8oKHF11cDsQCFlULrrOMV5Z59FIGEAF/M6YI=
333-
github.com/open-policy-agent/frameworks/constraint v0.0.0-20210518223409-ecad1fe8ed8d h1:PJdSXI31Ul1VEqET4+JQtsJHZurBbR7Gl5iFj5gXD8w=
334-
github.com/open-policy-agent/frameworks/constraint v0.0.0-20210518223409-ecad1fe8ed8d/go.mod h1:y8wOVfZ6+bEmbhBMnLnFlQrJB9eQpVk+dIDa7YrtocI=
333+
github.com/open-policy-agent/frameworks/constraint v0.0.0-20210522003146-5c034948ac29 h1:o2IzbSyOZRmLwmZX52KPbQ/O6vafNnpq2ubgEDNi1Vk=
334+
github.com/open-policy-agent/frameworks/constraint v0.0.0-20210522003146-5c034948ac29/go.mod h1:y8wOVfZ6+bEmbhBMnLnFlQrJB9eQpVk+dIDa7YrtocI=
335335
github.com/open-policy-agent/opa v0.24.0 h1:fnGOIux+TTGZsC0du1bRBtV8F+KPN55Hks12uE3Fq3E=
336336
github.com/open-policy-agent/opa v0.24.0/go.mod h1:qEyD/i8j+RQettHGp4f86yjrjvv+ZYia+JHCMv2G7wA=
337337
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=

pkg/controller/constrainttemplate/constrainttemplate_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"github.com/open-policy-agent/opa/ast"
3939
errorpkg "github.com/pkg/errors"
4040
corev1 "k8s.io/api/core/v1"
41-
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
41+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
4242
"k8s.io/apimachinery/pkg/api/errors"
4343
"k8s.io/apimachinery/pkg/runtime"
4444
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -210,7 +210,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
210210

211211
// Watch for changes to Constraint CRDs
212212
err = c.Watch(
213-
&source.Kind{Type: &apiextensionsv1beta1.CustomResourceDefinition{}},
213+
&source.Kind{Type: &apiextensionsv1.CustomResourceDefinition{}},
214214
&handler.EnqueueRequestForOwner{
215215
OwnerType: &v1beta1.ConstraintTemplate{},
216216
IsController: true,
@@ -349,9 +349,9 @@ func (r *ReconcileConstraintTemplate) Reconcile(ctx context.Context, request rec
349349
return reconcile.Result{}, nil
350350
}
351351

352-
proposedCRD := &apiextensionsv1beta1.CustomResourceDefinition{}
352+
proposedCRD := &apiextensionsv1.CustomResourceDefinition{}
353353
if err := r.scheme.Convert(unversionedProposedCRD, proposedCRD, nil); err != nil {
354-
log.Error(err, "conversion error")
354+
log.Error(err, "CRD conversion error")
355355
r.tracker.TryCancelTemplate(unversionedCT) // Don't track templates that failed compilation
356356
r.metrics.registry.add(request.NamespacedName, metrics.ErrorStatus)
357357
logError(request.NamespacedName.Name)
@@ -363,7 +363,7 @@ func (r *ReconcileConstraintTemplate) Reconcile(ctx context.Context, request rec
363363
namespace := unversionedProposedCRD.GetNamespace()
364364
// Check if the constraint CRD already exists
365365
action := updatedAction
366-
currentCRD := &apiextensionsv1beta1.CustomResourceDefinition{}
366+
currentCRD := &apiextensionsv1.CustomResourceDefinition{}
367367
err = r.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, currentCRD)
368368
switch {
369369
case err == nil:
@@ -408,7 +408,7 @@ func (r *ReconcileConstraintTemplate) reportErrorOnCTStatus(code, message string
408408
func (r *ReconcileConstraintTemplate) handleUpdate(
409409
ct *v1beta1.ConstraintTemplate,
410410
unversionedCT *templates.ConstraintTemplate,
411-
proposedCRD, currentCRD *apiextensionsv1beta1.CustomResourceDefinition,
411+
proposedCRD, currentCRD *apiextensionsv1.CustomResourceDefinition,
412412
status *statusv1beta1.ConstraintTemplatePodStatus,
413413
) (reconcile.Result, error) {
414414
name := proposedCRD.GetName()
@@ -438,7 +438,7 @@ func (r *ReconcileConstraintTemplate) handleUpdate(
438438
t.Observe(unversionedCT)
439439
log.Info("[readiness] observed ConstraintTemplate", "name", unversionedCT.GetName())
440440

441-
var newCRD *apiextensionsv1beta1.CustomResourceDefinition
441+
var newCRD *apiextensionsv1.CustomResourceDefinition
442442
if currentCRD == nil {
443443
newCRD = proposedCRD.DeepCopy()
444444
} else {

pkg/controller/constrainttemplate/constrainttemplate_controller_test.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141
"golang.org/x/net/context"
4242
admissionv1 "k8s.io/api/admission/v1"
4343
corev1 "k8s.io/api/core/v1"
44-
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
44+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
4545
errors2 "k8s.io/apimachinery/pkg/api/errors"
4646
"k8s.io/apimachinery/pkg/api/meta"
4747
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -122,10 +122,10 @@ violation[{"msg": "denied!"}] {
122122
}
123123

124124
// Uncommenting the below enables logging of K8s internals like watch.
125-
//fs := flag.NewFlagSet("", flag.PanicOnError)
126-
//klog.InitFlags(fs)
127-
//fs.Parse([]string{"--alsologtostderr", "-v=10"})
128-
//klog.SetOutput(os.Stderr)
125+
// fs := flag.NewFlagSet("", flag.PanicOnError)
126+
// klog.InitFlags(fs)
127+
// fs.Parse([]string{"--alsologtostderr", "-v=10"})
128+
// klog.SetOutput(os.Stderr)
129129

130130
// Setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a
131131
// channel when it is finished.
@@ -174,7 +174,7 @@ violation[{"msg": "denied!"}] {
174174
defer testMgrStopped()
175175
// Clean up to remove the crd, constraint and constraint template
176176
defer func() {
177-
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
177+
crd := &apiextensionsv1.CustomResourceDefinition{}
178178
g.Expect(c.Get(ctx, crdKey, crd)).NotTo(gomega.HaveOccurred())
179179

180180
g.Expect(deleteObject(ctx, c, cstr, timeout)).To(gomega.BeNil())
@@ -189,7 +189,7 @@ violation[{"msg": "denied!"}] {
189189

190190
clientset := kubernetes.NewForConfigOrDie(cfg)
191191
g.Eventually(func() error {
192-
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
192+
crd := &apiextensionsv1.CustomResourceDefinition{}
193193
if err := c.Get(ctx, crdKey, crd); err != nil {
194194
return err
195195
}
@@ -241,14 +241,14 @@ violation[{"msg": "denied!"}] {
241241

242242
log.Info("Running test: Deleted constraint CRDs are recreated")
243243
t.Run("Deleted constraint CRDs are recreated", func(t *testing.T) {
244-
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
244+
crd := &apiextensionsv1.CustomResourceDefinition{}
245245
g.Expect(c.Get(ctx, crdKey, crd)).NotTo(gomega.HaveOccurred())
246246
origUID := crd.GetUID()
247-
crd.Spec = apiextensionsv1beta1.CustomResourceDefinitionSpec{}
247+
crd.Spec = apiextensionsv1.CustomResourceDefinitionSpec{}
248248
g.Expect(c.Delete(ctx, crd)).NotTo(gomega.HaveOccurred())
249249

250250
g.Eventually(func() error {
251-
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
251+
crd := &apiextensionsv1.CustomResourceDefinition{}
252252
if err := c.Get(ctx, crdKey, crd); err != nil {
253253
return err
254254
}
@@ -259,7 +259,7 @@ violation[{"msg": "denied!"}] {
259259
return errors.New("Not yet deleted")
260260
}
261261
for _, cond := range crd.Status.Conditions {
262-
if cond.Type == apiextensionsv1beta1.Established && cond.Status == apiextensionsv1beta1.ConditionTrue {
262+
if cond.Type == apiextensionsv1.Established && cond.Status == apiextensionsv1.ConditionTrue {
263263
return nil
264264
}
265265
}
@@ -429,6 +429,7 @@ violation[{"msg": "denied!"}] {
429429
},
430430
},
431431
}
432+
432433
err := c.Create(ctx, instance)
433434
g.Expect(err).NotTo(gomega.HaveOccurred())
434435

@@ -577,26 +578,36 @@ func getCByPodStatus(obj *unstructured.Unstructured) (*statusv1beta1.ConstraintP
577578
}
578579

579580
// makeCRD generates a CRD specified by GVK and plural for testing.
580-
func makeCRD(gvk schema.GroupVersionKind, plural string) *apiextensionsv1beta1.CustomResourceDefinition {
581-
return &apiextensionsv1beta1.CustomResourceDefinition{
581+
func makeCRD(gvk schema.GroupVersionKind, plural string) *apiextensionsv1.CustomResourceDefinition {
582+
trueBool := true
583+
return &apiextensionsv1.CustomResourceDefinition{
582584
ObjectMeta: metav1.ObjectMeta{
583585
Name: fmt.Sprintf("%s.%s", plural, gvk.Group),
584586
},
585587
TypeMeta: metav1.TypeMeta{
586588
Kind: "CustomResourceDefinition",
587-
APIVersion: "apiextensions/v1beta1",
589+
APIVersion: "apiextensions/v1",
588590
},
589-
Spec: apiextensionsv1beta1.CustomResourceDefinitionSpec{
591+
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
590592
Group: gvk.Group,
591-
Names: apiextensionsv1beta1.CustomResourceDefinitionNames{
593+
Names: apiextensionsv1.CustomResourceDefinitionNames{
592594
Plural: plural,
593595
Singular: strings.ToLower(gvk.Kind),
594596
Kind: gvk.Kind,
595597
},
596-
Versions: []apiextensionsv1beta1.CustomResourceDefinitionVersion{
597-
{Name: gvk.Version, Served: true, Storage: true},
598+
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
599+
{
600+
Name: gvk.Version,
601+
Served: true,
602+
Storage: true,
603+
Schema: &apiextensionsv1.CustomResourceValidation{
604+
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
605+
XPreserveUnknownFields: &trueBool,
606+
},
607+
},
608+
},
598609
},
599-
Scope: apiextensionsv1beta1.ClusterScoped,
610+
Scope: apiextensionsv1.ClusterScoped,
600611
},
601612
}
602613
}

vendor/github.com/open-policy-agent/frameworks/constraint/pkg/client/crd_helpers.go

Lines changed: 17 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/open-policy-agent/frameworks/constraint/pkg/client/e2e_tests.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/open-policy-agent/frameworks/constraint/pkg/client/test_handler.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ github.com/onsi/gomega/types
149149
# github.com/open-policy-agent/cert-controller v0.2.0
150150
## explicit
151151
github.com/open-policy-agent/cert-controller/pkg/rotator
152-
# github.com/open-policy-agent/frameworks/constraint v0.0.0-20210518223409-ecad1fe8ed8d
152+
# github.com/open-policy-agent/frameworks/constraint v0.0.0-20210522003146-5c034948ac29
153153
## explicit
154154
github.com/open-policy-agent/frameworks/constraint/deploy
155155
github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates

0 commit comments

Comments
 (0)