Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Add support to read webhook configurations from files for WebhookInstallOptions #1080

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming DirectoryPaths to Paths would be a breaking change right?
If so, we should probably mark this commit with a ⚠️ prefix.

Unless we want to avoid that and add a separate field e.g FilePaths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, you are right, it is a breaking change.

I prefer to not add FilePaths since I would like to keep it consistent with CRDInstallOptions.

WDYT? @vincepri @DirectXMan12

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for consistency, we can delay this change until v0.7


// 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