Skip to content

Commit 420cd15

Browse files
authored
Merge pull request #1080 from jiachengxu/webhook-install-file
⚠️ Add support to read webhook configurations from files for WebhookInstallOptions
2 parents 97cfffd + 45bd578 commit 420cd15

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

pkg/envtest/webhook.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ import (
4040

4141
// WebhookInstallOptions are the options for installing mutating or validating webhooks
4242
type WebhookInstallOptions struct {
43-
// Paths is a list of paths to the directories containing the mutating or validating webhooks yaml or json configs.
44-
DirectoryPaths []string
43+
// Paths is a list of paths to the directories or files containing the mutating or validating webhooks yaml or json configs.
44+
Paths []string
4545

4646
// MutatingWebhooks is a list of MutatingWebhookConfigurations to install
4747
MutatingWebhooks []runtime.Object
@@ -149,7 +149,7 @@ func (o *WebhookInstallOptions) Install(config *rest.Config) error {
149149
if err != nil {
150150
return err
151151
}
152-
if err := parseWebhookDirs(o); err != nil {
152+
if err := parseWebhook(o); err != nil {
153153
return err
154154
}
155155

@@ -319,10 +319,10 @@ func ensureCreated(cs client.Client, obj *unstructured.Unstructured) error {
319319
return nil
320320
}
321321

322-
// parseWebhookDirs reads the directories of Webhooks in options.DirectoryPaths and adds the Webhook structs to options
323-
func parseWebhookDirs(options *WebhookInstallOptions) error {
324-
if len(options.DirectoryPaths) > 0 {
325-
for _, path := range options.DirectoryPaths {
322+
// parseWebhook reads the directories or files of Webhooks in options.Paths and adds the Webhook structs to options
323+
func parseWebhook(options *WebhookInstallOptions) error {
324+
if len(options.Paths) > 0 {
325+
for _, path := range options.Paths {
326326
_, err := os.Stat(path)
327327
if options.IgnoreErrorIfPathMissing && os.IsNotExist(err) {
328328
continue // skip this path
@@ -348,9 +348,17 @@ func readWebhooks(path string) ([]runtime.Object, []runtime.Object, error) {
348348
var files []os.FileInfo
349349
var err error
350350
log.V(1).Info("reading Webhooks from path", "path", path)
351-
if files, err = ioutil.ReadDir(path); err != nil {
351+
info, err := os.Stat(path)
352+
if err != nil {
352353
return nil, nil, err
353354
}
355+
if !info.IsDir() {
356+
path, files = filepath.Dir(path), []os.FileInfo{info}
357+
} else {
358+
if files, err = ioutil.ReadDir(path); err != nil {
359+
return nil, nil, err
360+
}
361+
}
354362

355363
// file extensions that may contain Webhooks
356364
resourceExtensions := sets.NewString(".json", ".yaml", ".yml")

pkg/envtest/webhook_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,21 @@ var _ = Describe("Test", func() {
7575
close(done)
7676
})
7777

78+
It("should load webhooks from directory", func() {
79+
installOptions := WebhookInstallOptions{
80+
Paths: []string{filepath.Join("testdata", "webhooks")},
81+
}
82+
err := parseWebhook(&installOptions)
83+
Expect(err).NotTo(HaveOccurred())
84+
Expect(len(installOptions.MutatingWebhooks)).To(Equal(2))
85+
Expect(len(installOptions.ValidatingWebhooks)).To(Equal(2))
86+
})
87+
7888
It("should load webhooks from files", func() {
7989
installOptions := WebhookInstallOptions{
80-
DirectoryPaths: []string{filepath.Join("testdata", "webhooks")},
90+
Paths: []string{filepath.Join("testdata", "webhooks", "manifests.yaml")},
8191
}
82-
err := parseWebhookDirs(&installOptions)
92+
err := parseWebhook(&installOptions)
8393
Expect(err).NotTo(HaveOccurred())
8494
Expect(len(installOptions.MutatingWebhooks)).To(Equal(2))
8595
Expect(len(installOptions.ValidatingWebhooks)).To(Equal(2))

0 commit comments

Comments
 (0)