Skip to content

Commit

Permalink
Merge pull request #1080 from jiachengxu/webhook-install-file
Browse files Browse the repository at this point in the history
⚠️ Add support to read webhook configurations from files for WebhookInstallOptions
  • Loading branch information
k8s-ci-robot committed Aug 5, 2020
2 parents 97cfffd + 45bd578 commit 420cd15
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
24 changes: 16 additions & 8 deletions pkg/envtest/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
14 changes: 12 additions & 2 deletions pkg/envtest/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 420cd15

Please sign in to comment.