Skip to content

Commit

Permalink
✨ Add scaffolding for the webhook test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
prafull01 committed Oct 6, 2020
1 parent 70e9989 commit aee67b1
Show file tree
Hide file tree
Showing 9 changed files with 566 additions and 5 deletions.
7 changes: 5 additions & 2 deletions pkg/plugin/v3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import (
// TODO: remove this when a better solution for using addons is implemented.
const KbDeclarativePatternVersion = "v0.0.0-20200522144838-848d48e5b073"

// AddonPlugin is the name of the plugin for addons.
const AddonPlugin = "addon"

type createAPIPlugin struct {
config *config.Config

Expand Down Expand Up @@ -182,7 +185,7 @@ func (p *createAPIPlugin) GetScaffolder() (scaffold.Scaffolder, error) {
switch strings.ToLower(p.pattern) {
case "":
// Default pattern
case "addon":
case AddonPlugin:
plugins = append(plugins, &addon.Plugin{})
default:
return nil, fmt.Errorf("unknown pattern %q", p.pattern)
Expand All @@ -198,7 +201,7 @@ func (p *createAPIPlugin) PostScaffold() error {
switch strings.ToLower(p.pattern) {
case "":
// Default pattern
case "addon":
case AddonPlugin:
// Ensure that we are pinning sigs.k8s.io/kubebuilder-declarative-pattern version
// TODO: either find a better way to inject this version (ex. tools.go).
err := util.RunCmd("Get kubebuilder-declarative-pattern dependency", "go", "get",
Expand Down
194 changes: 194 additions & 0 deletions pkg/plugin/v3/scaffolds/internal/templates/config/api/webhook_suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package api

import (
"fmt"
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)

var _ file.Template = &WebhookSuite{}
var _ file.Inserter = &WebhookSuite{}

// WebhookSuite scaffolds the webhook_suite.go file to setup the webhook test
type WebhookSuite struct {
file.TemplateMixin
file.MultiGroupMixin
file.BoilerplateMixin
file.ResourceMixin

// BaseDirectoryRelativePath define the Path for the base directory when it is multigroup
BaseDirectoryRelativePath string
}

// SetTemplateDefaults implements file.Template
func (f *WebhookSuite) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup {
if f.Resource.Group != "" {
f.Path = filepath.Join("apis", "%[group]", "%[version]", "webhook_suite_test.go")
} else {
f.Path = filepath.Join("apis", "%[version]", "webhook_suite_test.go")
}
} else {
f.Path = filepath.Join("api", "%[version]", "webhook_suite_test.go")
}
}
f.Path = f.Resource.Replacer().Replace(f.Path)

f.TemplateBody = fmt.Sprintf(webhookTestSuiteTemplate,
file.NewMarkerFor(f.Path, addWebhookManagerMarker),
)

// If is multigroup the path needs to be ../../ since it has
// the group dir.
f.BaseDirectoryRelativePath = `"..", ".."`
if f.MultiGroup && f.Resource.Group != "" {
f.BaseDirectoryRelativePath = `"..", "..",".."`
}

return nil
}

const (
addWebhookManagerMarker = "webhook"
)

// GetMarkers implements file.Inserter
func (f *WebhookSuite) GetMarkers() []file.Marker {
return []file.Marker{
file.NewMarkerFor(f.Path, addWebhookManagerMarker),
}
}

const (
addWebhookManagerCodeFragment = `err = (&%s{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())
`
)

// GetCodeFragments implements file.Inserter
func (f *WebhookSuite) GetCodeFragments() file.CodeFragmentsMap {
fragments := make(file.CodeFragmentsMap, 2)

// Generate add webhookManager code fragments
addWebhookManager := make([]string, 0)
addWebhookManager = append(addWebhookManager, fmt.Sprintf(addWebhookManagerCodeFragment, f.Resource.Kind))

// Only store code fragments in the map if the slices are non-empty
if len(addWebhookManager) != 0 {
fragments[file.NewMarkerFor(f.Path, addWebhookManagerMarker)] = addWebhookManager
}

return fragments
}

const (
webhookTestSuiteTemplate = `
package {{ .Resource.Version }}
import (
"path/filepath"
"testing"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var stopCh = make(chan struct{})
func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecsWithDefaultAndCustomReporters(t,
"Webhook Suite",
[]Reporter{printer.NewlineReporter{}})
}
var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "crd", "bases")},
WebhookInstallOptions: envtest.WebhookInstallOptions{
DirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "webhook")},
},
}
var err error
cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())
scheme := runtime.NewScheme()
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())
// start webhook server using Manager
wio := &testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Host: wio.LocalServingHost,
Port: wio.LocalServingPort,
CertDir: wio.LocalServingCertDir,
LeaderElection: false,
MetricsBindAddress: "0",
})
Expect(err).ToNot(HaveOccurred())
%s
go func() {
err := mgr.Start(stopCh)
if err != nil {
panic(err)
}
}()
// wait for the webhook server to get ready
dialer := &net.Dialer{Timeout: time.Second}
addrPort := wio.LocalServingHost + ":" + fmt.Sprint(wio.LocalServingPort)
Eventually(func() error {
conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true})
if err != nil {
return err
}
conn.Close()
return nil
}).Should(Succeed())
close(done)
}, 60)
var _ = AfterSuite(func() {
close(stopCh)
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
`
)
17 changes: 15 additions & 2 deletions pkg/plugin/v3/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type webhookScaffolder struct {
config *config.Config
boilerplate string
resource *resource.Resource

// plugins is the list of plugins we should allow to transform our generated scaffolding
plugins []model.Plugin
// Webhook type options.
defaulting, validation, conversion bool
}
Expand All @@ -47,6 +48,7 @@ func NewWebhookScaffolder(
defaulting bool,
validation bool,
conversion bool,
plugins []model.Plugin,
) scaffold.Scaffolder {
return &webhookScaffolder{
config: config,
Expand All @@ -55,6 +57,7 @@ func NewWebhookScaffolder(
defaulting: defaulting,
validation: validation,
conversion: conversion,
plugins: plugins,
}
}

Expand All @@ -78,13 +81,23 @@ func (s *webhookScaffolder) scaffold() error {
You need to implement the conversion.Hub and conversion.Convertible interfaces for your CRD types.`)
}

if err := machinery.NewScaffold().Execute(
if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&api.Webhook{Defaulting: s.defaulting, Validating: s.validation},
&templates.MainUpdater{WireWebhook: true},
); err != nil {
return err
}


if s.defaulting || s.validation {
if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&api.WebhookSuite{},
); err != nil {
return err
}
}

return nil
}
21 changes: 20 additions & 1 deletion pkg/plugin/v3/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/plugins/addon"

"github.com/spf13/pflag"

Expand All @@ -36,6 +40,9 @@ type createWebhookPlugin struct {
// For help text.
commandName string

// pattern indicates that we should use a plugin to build according to a pattern
pattern string

resource *resource.Options
defaulting bool
validation bool
Expand Down Expand Up @@ -106,9 +113,21 @@ func (p *createWebhookPlugin) GetScaffolder() (scaffold.Scaffolder, error) {
return nil, fmt.Errorf("unable to load boilerplate: %v", err)
}

// Load the requested plugins
plugins := make([]model.Plugin, 0)
switch strings.ToLower(p.pattern) {
case "":
// Default pattern
case AddonPlugin:
plugins = append(plugins, &addon.Plugin{})
default:
return nil, fmt.Errorf("unknown pattern %q", p.pattern)
}

// Create the actual resource from the resource options
res := p.resource.NewResource(p.config, false)
return scaffolds.NewWebhookScaffolder(p.config, string(bp), res, p.defaulting, p.validation, p.conversion), nil
return scaffolds.NewWebhookScaffolder(p.config, string(bp), res, p.defaulting, p.validation, p.conversion, plugins),
nil
}

func (p *createWebhookPlugin) PostScaffold() error {
Expand Down
Loading

0 comments on commit aee67b1

Please sign in to comment.