Skip to content

Commit d057d46

Browse files
authored
Merge pull request #1200 from vincepri/clientobjectwebhooks
🌱 Use client.Object interface in envtest
2 parents 8ce28b5 + ac6ca74 commit d057d46

File tree

6 files changed

+35
-35
lines changed

6 files changed

+35
-35
lines changed

pkg/envtest/crd.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
3030
apierrors "k8s.io/apimachinery/pkg/api/errors"
3131
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
32-
"k8s.io/apimachinery/pkg/runtime"
3332
"k8s.io/apimachinery/pkg/runtime/schema"
3433
"k8s.io/apimachinery/pkg/util/sets"
3534
"k8s.io/apimachinery/pkg/util/wait"
@@ -45,7 +44,7 @@ type CRDInstallOptions struct {
4544
Paths []string
4645

4746
// CRDs is a list of CRDs to install
48-
CRDs []runtime.Object
47+
CRDs []client.Object
4948

5049
// ErrorIfPathMissing will cause an error if a Path does not exist
5150
ErrorIfPathMissing bool
@@ -66,7 +65,7 @@ const defaultPollInterval = 100 * time.Millisecond
6665
const defaultMaxWait = 10 * time.Second
6766

6867
// InstallCRDs installs a collection of CRDs into a cluster by reading the crd yaml files from a directory
69-
func InstallCRDs(config *rest.Config, options CRDInstallOptions) ([]runtime.Object, error) {
68+
func InstallCRDs(config *rest.Config, options CRDInstallOptions) ([]client.Object, error) {
7069
defaultCRDOptions(&options)
7170

7271
// Read the CRD yamls into options.CRDs
@@ -111,7 +110,7 @@ func defaultCRDOptions(o *CRDInstallOptions) {
111110
}
112111

113112
// WaitForCRDs waits for the CRDs to appear in discovery
114-
func WaitForCRDs(config *rest.Config, crds []runtime.Object, options CRDInstallOptions) error {
113+
func WaitForCRDs(config *rest.Config, crds []client.Object, options CRDInstallOptions) error {
115114
// Add each CRD to a map of GroupVersion to Resource
116115
waitingFor := map[schema.GroupVersion]*sets.String{}
117116
for _, crd := range runtimeCRDListToUnstructured(crds) {
@@ -244,7 +243,7 @@ func UninstallCRDs(config *rest.Config, options CRDInstallOptions) error {
244243
}
245244

246245
// CreateCRDs creates the CRDs
247-
func CreateCRDs(config *rest.Config, crds []runtime.Object) error {
246+
func CreateCRDs(config *rest.Config, crds []client.Object) error {
248247
cs, err := client.New(config, client.Options{})
249248
if err != nil {
250249
return err
@@ -274,7 +273,7 @@ func CreateCRDs(config *rest.Config, crds []runtime.Object) error {
274273
}
275274

276275
// renderCRDs iterate through options.Paths and extract all CRD files.
277-
func renderCRDs(options *CRDInstallOptions) ([]runtime.Object, error) {
276+
func renderCRDs(options *CRDInstallOptions) ([]client.Object, error) {
278277
var (
279278
err error
280279
info os.FileInfo
@@ -325,7 +324,7 @@ func renderCRDs(options *CRDInstallOptions) ([]runtime.Object, error) {
325324
}
326325

327326
// Converting map to a list to return
328-
var res []runtime.Object
327+
var res []client.Object
329328
for _, obj := range crds {
330329
res = append(res, obj)
331330
}

pkg/envtest/envtest_suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
admissionv1 "k8s.io/api/admissionregistration/v1"
2525
admissionv1beta1 "k8s.io/api/admissionregistration/v1beta1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27-
"k8s.io/apimachinery/pkg/runtime"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
2828
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
2929
logf "sigs.k8s.io/controller-runtime/pkg/log"
3030
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -61,7 +61,7 @@ func initializeWebhookInEnvironment() {
6161
webhookPathV1 := "/failing"
6262

6363
env.WebhookInstallOptions = WebhookInstallOptions{
64-
ValidatingWebhooks: []runtime.Object{
64+
ValidatingWebhooks: []client.Object{
6565
&admissionv1beta1.ValidatingWebhookConfiguration{
6666
ObjectMeta: metav1.ObjectMeta{
6767
Name: "deployment-validation-webhook-config",

pkg/envtest/envtest_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
)
3333

3434
var _ = Describe("Test", func() {
35-
var crds []runtime.Object
35+
var crds []client.Object
3636
var err error
3737
var s *runtime.Scheme
3838
var c client.Client
@@ -44,7 +44,7 @@ var _ = Describe("Test", func() {
4444

4545
// Initialize the client
4646
BeforeEach(func(done Done) {
47-
crds = []runtime.Object{}
47+
crds = []client.Object{}
4848
s = runtime.NewScheme()
4949
err = v1beta1.AddToScheme(s)
5050
Expect(err).NotTo(HaveOccurred())
@@ -114,7 +114,7 @@ var _ = Describe("Test", func() {
114114
Expect(err).NotTo(HaveOccurred())
115115
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
116116

117-
err = WaitForCRDs(env.Config, []runtime.Object{
117+
err = WaitForCRDs(env.Config, []client.Object{
118118
&apiextensionsv1.CustomResourceDefinition{
119119
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
120120
Group: "bar.example.com",
@@ -196,7 +196,7 @@ var _ = Describe("Test", func() {
196196
Expect(err).NotTo(HaveOccurred())
197197
Expect(crd.Spec.Names.Kind).To(Equal("Config"))
198198

199-
err = WaitForCRDs(env.Config, []runtime.Object{
199+
err = WaitForCRDs(env.Config, []client.Object{
200200
&v1beta1.CustomResourceDefinition{
201201
Spec: v1beta1.CustomResourceDefinitionSpec{
202202
Group: "foo.example.com",
@@ -240,7 +240,7 @@ var _ = Describe("Test", func() {
240240
Expect(err).NotTo(HaveOccurred())
241241
Expect(crd.Spec.Names.Kind).To(Equal("Foo"))
242242

243-
err = WaitForCRDs(env.Config, []runtime.Object{
243+
err = WaitForCRDs(env.Config, []client.Object{
244244
&apiextensionsv1.CustomResourceDefinition{
245245
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
246246
Group: "bar.example.com",
@@ -296,7 +296,7 @@ var _ = Describe("Test", func() {
296296
It("should return an error if the resource group version isn't found", func(done Done) {
297297
// Wait for a CRD where the Group and Version don't exist
298298
err := WaitForCRDs(env.Config,
299-
[]runtime.Object{
299+
[]client.Object{
300300
&v1beta1.CustomResourceDefinition{
301301
Spec: v1beta1.CustomResourceDefinitionSpec{
302302
Version: "v1",
@@ -319,7 +319,7 @@ var _ = Describe("Test", func() {
319319
Expect(err).NotTo(HaveOccurred())
320320

321321
// Wait for a CRD that doesn't exist, but the Group and Version do
322-
err = WaitForCRDs(env.Config, []runtime.Object{
322+
err = WaitForCRDs(env.Config, []client.Object{
323323
&v1beta1.CustomResourceDefinition{
324324
Spec: v1beta1.CustomResourceDefinitionSpec{
325325
Group: "qux.example.com",
@@ -377,7 +377,7 @@ var _ = Describe("Test", func() {
377377
Expect(err).NotTo(HaveOccurred())
378378
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
379379

380-
err = WaitForCRDs(env.Config, []runtime.Object{
380+
err = WaitForCRDs(env.Config, []client.Object{
381381
&apiextensionsv1.CustomResourceDefinition{
382382
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
383383
Group: "bar.example.com",
@@ -479,7 +479,7 @@ var _ = Describe("Test", func() {
479479
Expect(err).NotTo(HaveOccurred())
480480
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
481481

482-
err = WaitForCRDs(env.Config, []runtime.Object{
482+
err = WaitForCRDs(env.Config, []client.Object{
483483
&apiextensionsv1.CustomResourceDefinition{
484484
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
485485
Group: "bar.example.com",
@@ -570,7 +570,7 @@ var _ = Describe("Test", func() {
570570
// Store resource version for comparison later on
571571
firstRV := crd.ResourceVersion
572572

573-
err = WaitForCRDs(env.Config, []runtime.Object{
573+
err = WaitForCRDs(env.Config, []client.Object{
574574
&v1beta1.CustomResourceDefinition{
575575
Spec: v1beta1.CustomResourceDefinitionSpec{
576576
Group: "crew.example.com",
@@ -610,7 +610,7 @@ var _ = Describe("Test", func() {
610610
Expect(len(crd.Spec.Versions)).To(BeEquivalentTo(3))
611611
Expect(crd.ResourceVersion).NotTo(BeEquivalentTo(firstRV))
612612

613-
err = WaitForCRDs(env.Config, []runtime.Object{
613+
err = WaitForCRDs(env.Config, []client.Object{
614614
&v1beta1.CustomResourceDefinition{
615615
Spec: v1beta1.CustomResourceDefinitionSpec{
616616
Group: "crew.example.com",
@@ -678,7 +678,7 @@ var _ = Describe("Test", func() {
678678
Expect(err).NotTo(HaveOccurred())
679679
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
680680

681-
err = WaitForCRDs(env.Config, []runtime.Object{
681+
err = WaitForCRDs(env.Config, []client.Object{
682682
&apiextensionsv1.CustomResourceDefinition{
683683
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
684684
Group: "bar.example.com",

pkg/envtest/helper.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
66
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
77
"k8s.io/apimachinery/pkg/runtime"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
89
)
910

1011
var (
@@ -38,15 +39,15 @@ func mergePaths(s1, s2 []string) []string {
3839

3940
// mergeCRDs merges two CRD slices using their names.
4041
// This function makes no guarantees about order of the merged slice.
41-
func mergeCRDs(s1, s2 []runtime.Object) []runtime.Object {
42+
func mergeCRDs(s1, s2 []client.Object) []client.Object {
4243
m := make(map[string]*unstructured.Unstructured)
4344
for _, obj := range runtimeCRDListToUnstructured(s1) {
4445
m[obj.GetName()] = obj
4546
}
4647
for _, obj := range runtimeCRDListToUnstructured(s2) {
4748
m[obj.GetName()] = obj
4849
}
49-
merged := make([]runtime.Object, len(m))
50+
merged := make([]client.Object, len(m))
5051
i := 0
5152
for _, obj := range m {
5253
merged[i] = obj
@@ -55,7 +56,7 @@ func mergeCRDs(s1, s2 []runtime.Object) []runtime.Object {
5556
return merged
5657
}
5758

58-
func runtimeCRDListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
59+
func runtimeCRDListToUnstructured(l []client.Object) []*unstructured.Unstructured {
5960
res := []*unstructured.Unstructured{}
6061
for _, obj := range l {
6162
u := &unstructured.Unstructured{}

pkg/envtest/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"strings"
2424
"time"
2525

26-
"k8s.io/apimachinery/pkg/runtime"
2726
"k8s.io/client-go/rest"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
2828
"sigs.k8s.io/controller-runtime/pkg/client/config"
2929
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration"
3030

@@ -106,7 +106,7 @@ type Environment struct {
106106
// CRDs is a list of CRDs to install.
107107
// If both this field and CRDs field in CRDInstallOptions are specified, the
108108
// values are merged.
109-
CRDs []runtime.Object
109+
CRDs []client.Object
110110

111111
// CRDDirectoryPaths is a list of paths containing CRD yaml or json configs.
112112
// If both this field and Paths field in CRDInstallOptions are specified, the

pkg/envtest/webhook.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ type WebhookInstallOptions struct {
4444
Paths []string
4545

4646
// MutatingWebhooks is a list of MutatingWebhookConfigurations to install
47-
MutatingWebhooks []runtime.Object
47+
MutatingWebhooks []client.Object
4848

4949
// ValidatingWebhooks is a list of ValidatingWebhookConfigurations to install
50-
ValidatingWebhooks []runtime.Object
50+
ValidatingWebhooks []client.Object
5151

5252
// IgnoreErrorIfPathMissing will ignore an error if a DirectoryPath does not exist when set to true
5353
IgnoreErrorIfPathMissing bool
@@ -195,8 +195,8 @@ func (o *WebhookInstallOptions) Cleanup() error {
195195

196196
// WaitForWebhooks waits for the Webhooks to be available through API server
197197
func WaitForWebhooks(config *rest.Config,
198-
mutatingWebhooks []runtime.Object,
199-
validatingWebhooks []runtime.Object,
198+
mutatingWebhooks []client.Object,
199+
validatingWebhooks []client.Object,
200200
options WebhookInstallOptions) error {
201201

202202
waitingFor := map[schema.GroupVersionKind]*sets.String{}
@@ -293,7 +293,7 @@ func (o *WebhookInstallOptions) setupCA() ([]byte, error) {
293293
return certData, nil
294294
}
295295

296-
func createWebhooks(config *rest.Config, mutHooks []runtime.Object, valHooks []runtime.Object) error {
296+
func createWebhooks(config *rest.Config, mutHooks []client.Object, valHooks []client.Object) error {
297297
cs, err := client.New(config, client.Options{})
298298
if err != nil {
299299
return err
@@ -360,7 +360,7 @@ func parseWebhook(options *WebhookInstallOptions) error {
360360

361361
// readWebhooks reads the Webhooks from files and Unmarshals them into structs
362362
// returns slice of mutating and validating webhook configurations
363-
func readWebhooks(path string) ([]runtime.Object, []runtime.Object, error) {
363+
func readWebhooks(path string) ([]client.Object, []client.Object, error) {
364364
// Get the webhook files
365365
var files []os.FileInfo
366366
var err error
@@ -380,8 +380,8 @@ func readWebhooks(path string) ([]runtime.Object, []runtime.Object, error) {
380380
// file extensions that may contain Webhooks
381381
resourceExtensions := sets.NewString(".json", ".yaml", ".yml")
382382

383-
var mutHooks []runtime.Object
384-
var valHooks []runtime.Object
383+
var mutHooks []client.Object
384+
var valHooks []client.Object
385385
for _, file := range files {
386386
// Only parse allowlisted file types
387387
if !resourceExtensions.Has(filepath.Ext(file.Name())) {
@@ -433,7 +433,7 @@ func readWebhooks(path string) ([]runtime.Object, []runtime.Object, error) {
433433
return mutHooks, valHooks, nil
434434
}
435435

436-
func runtimeListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
436+
func runtimeListToUnstructured(l []client.Object) []*unstructured.Unstructured {
437437
res := []*unstructured.Unstructured{}
438438
for _, obj := range l {
439439
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj.DeepCopyObject())

0 commit comments

Comments
 (0)