From 45bd578b01bcd282f4f92c2623eb7cb0654c5823 Mon Sep 17 00:00:00 2001 From: Jiacheng Xu Date: Tue, 28 Jul 2020 15:57:21 +0800 Subject: [PATCH] Add support to read webhook from files for WebhookInstallOptions --- pkg/envtest/webhook.go | 24 ++++++++++++++++-------- pkg/envtest/webhook_test.go | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/pkg/envtest/webhook.go b/pkg/envtest/webhook.go index b64b7e09e5..2a5cb8f143 100644 --- a/pkg/envtest/webhook.go +++ b/pkg/envtest/webhook.go @@ -40,8 +40,8 @@ import ( // WebhookInstallOptions are the options for installing mutating or validating webhooks type WebhookInstallOptions struct { - // Paths is a list of paths to the directories containing the mutating or validating webhooks yaml or json configs. - DirectoryPaths []string + // Paths is a list of paths to the directories or files containing the mutating or validating webhooks yaml or json configs. + Paths []string // MutatingWebhooks is a list of MutatingWebhookConfigurations to install MutatingWebhooks []runtime.Object @@ -149,7 +149,7 @@ func (o *WebhookInstallOptions) Install(config *rest.Config) error { if err != nil { return err } - if err := parseWebhookDirs(o); err != nil { + if err := parseWebhook(o); err != nil { return err } @@ -319,10 +319,10 @@ func ensureCreated(cs client.Client, obj *unstructured.Unstructured) error { return nil } -// parseWebhookDirs reads the directories of Webhooks in options.DirectoryPaths and adds the Webhook structs to options -func parseWebhookDirs(options *WebhookInstallOptions) error { - if len(options.DirectoryPaths) > 0 { - for _, path := range options.DirectoryPaths { +// parseWebhook reads the directories or files of Webhooks in options.Paths and adds the Webhook structs to options +func parseWebhook(options *WebhookInstallOptions) error { + if len(options.Paths) > 0 { + for _, path := range options.Paths { _, err := os.Stat(path) if options.IgnoreErrorIfPathMissing && os.IsNotExist(err) { continue // skip this path @@ -348,9 +348,17 @@ func readWebhooks(path string) ([]runtime.Object, []runtime.Object, error) { var files []os.FileInfo var err error log.V(1).Info("reading Webhooks from path", "path", path) - if files, err = ioutil.ReadDir(path); err != nil { + info, err := os.Stat(path) + if err != nil { return nil, nil, err } + if !info.IsDir() { + path, files = filepath.Dir(path), []os.FileInfo{info} + } else { + if files, err = ioutil.ReadDir(path); err != nil { + return nil, nil, err + } + } // file extensions that may contain Webhooks resourceExtensions := sets.NewString(".json", ".yaml", ".yml") diff --git a/pkg/envtest/webhook_test.go b/pkg/envtest/webhook_test.go index 7ade969678..03a477e3db 100644 --- a/pkg/envtest/webhook_test.go +++ b/pkg/envtest/webhook_test.go @@ -75,11 +75,21 @@ var _ = Describe("Test", func() { close(done) }) + It("should load webhooks from directory", func() { + installOptions := WebhookInstallOptions{ + Paths: []string{filepath.Join("testdata", "webhooks")}, + } + err := parseWebhook(&installOptions) + Expect(err).NotTo(HaveOccurred()) + Expect(len(installOptions.MutatingWebhooks)).To(Equal(2)) + Expect(len(installOptions.ValidatingWebhooks)).To(Equal(2)) + }) + It("should load webhooks from files", func() { installOptions := WebhookInstallOptions{ - DirectoryPaths: []string{filepath.Join("testdata", "webhooks")}, + Paths: []string{filepath.Join("testdata", "webhooks", "manifests.yaml")}, } - err := parseWebhookDirs(&installOptions) + err := parseWebhook(&installOptions) Expect(err).NotTo(HaveOccurred()) Expect(len(installOptions.MutatingWebhooks)).To(Equal(2)) Expect(len(installOptions.ValidatingWebhooks)).To(Equal(2))