|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + |
| 7 | + "sigs.k8s.io/kubebuilder/pkg/model/file" |
| 8 | +) |
| 9 | + |
| 10 | +var _ file.Template = &WebhookSuite{} |
| 11 | +var _ file.Inserter = &WebhookSuite{} |
| 12 | + |
| 13 | +// WebhookSuite scaffolds the webhook_suite.go file to setup the webhook test |
| 14 | +type WebhookSuite struct { |
| 15 | + file.TemplateMixin |
| 16 | + file.MultiGroupMixin |
| 17 | + file.BoilerplateMixin |
| 18 | + file.ResourceMixin |
| 19 | + |
| 20 | + // BaseDirectoryRelativePath define the Path for the base directory when it is multigroup |
| 21 | + BaseDirectoryRelativePath string |
| 22 | +} |
| 23 | + |
| 24 | +// SetTemplateDefaults implements file.Template |
| 25 | +func (f *WebhookSuite) SetTemplateDefaults() error { |
| 26 | + if f.Path == "" { |
| 27 | + if f.MultiGroup { |
| 28 | + if f.Resource.Group != "" { |
| 29 | + f.Path = filepath.Join("apis", "%[group]", "%[version]", "webhook_suite_test.go") |
| 30 | + } else { |
| 31 | + f.Path = filepath.Join("apis", "%[version]", "webhook_suite_test.go") |
| 32 | + } |
| 33 | + } else { |
| 34 | + f.Path = filepath.Join("api", "%[version]", "webhook_suite_test.go") |
| 35 | + } |
| 36 | + } |
| 37 | + f.Path = f.Resource.Replacer().Replace(f.Path) |
| 38 | + |
| 39 | + f.TemplateBody = fmt.Sprintf(webhookTestSuiteTemplate, |
| 40 | + file.NewMarkerFor(f.Path, importMarker), |
| 41 | + file.NewMarkerFor(f.Path, addSchemeMarker), |
| 42 | + file.NewMarkerFor(f.Path, addWebhookManagerMarker), |
| 43 | + "%s", |
| 44 | + "%d", |
| 45 | + ) |
| 46 | + |
| 47 | + // If is multigroup the path needs to be ../../.. since it has the group dir. |
| 48 | + f.BaseDirectoryRelativePath = `"..", ".."` |
| 49 | + if f.MultiGroup && f.Resource.Group != "" { |
| 50 | + f.BaseDirectoryRelativePath = `"..", "..",".."` |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +const ( |
| 57 | + // TODO: admission webhook versions should be based on the input of the user. For More Info #1664 |
| 58 | + admissionImportAlias = "admissionv1beta1" |
| 59 | + admissionPath = "k8s.io/api/admission/v1beta1" |
| 60 | + importMarker = "imports" |
| 61 | + addWebhookManagerMarker = "webhook" |
| 62 | + addSchemeMarker = "scheme" |
| 63 | +) |
| 64 | + |
| 65 | +// GetMarkers implements file.Inserter |
| 66 | +func (f *WebhookSuite) GetMarkers() []file.Marker { |
| 67 | + return []file.Marker{ |
| 68 | + file.NewMarkerFor(f.Path, importMarker), |
| 69 | + file.NewMarkerFor(f.Path, addSchemeMarker), |
| 70 | + file.NewMarkerFor(f.Path, addWebhookManagerMarker), |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +const ( |
| 75 | + apiImportCodeFragment = `%s "%s" |
| 76 | +` |
| 77 | + addschemeCodeFragment = `err = %s.AddToScheme(scheme ) |
| 78 | +Expect(err).NotTo(HaveOccurred()) |
| 79 | +
|
| 80 | +` |
| 81 | + addWebhookManagerCodeFragment = `err = (&%s{}).SetupWebhookWithManager(mgr) |
| 82 | +Expect(err).NotTo(HaveOccurred()) |
| 83 | +
|
| 84 | +` |
| 85 | +) |
| 86 | + |
| 87 | +// GetCodeFragments implements file.Inserter |
| 88 | +func (f *WebhookSuite) GetCodeFragments() file.CodeFragmentsMap { |
| 89 | + fragments := make(file.CodeFragmentsMap, 3) |
| 90 | + |
| 91 | + // Generate import code fragments |
| 92 | + imports := make([]string, 0) |
| 93 | + imports = append(imports, fmt.Sprintf(apiImportCodeFragment, admissionImportAlias, admissionPath)) |
| 94 | + |
| 95 | + // Generate add scheme code fragments |
| 96 | + addScheme := make([]string, 0) |
| 97 | + addScheme = append(addScheme, fmt.Sprintf(addschemeCodeFragment, admissionImportAlias)) |
| 98 | + |
| 99 | + // Generate add webhookManager code fragments |
| 100 | + addWebhookManager := make([]string, 0) |
| 101 | + addWebhookManager = append(addWebhookManager, fmt.Sprintf(addWebhookManagerCodeFragment, f.Resource.Kind)) |
| 102 | + |
| 103 | + // Only store code fragments in the map if the slices are non-empty |
| 104 | + if len(addWebhookManager) != 0 { |
| 105 | + fragments[file.NewMarkerFor(f.Path, addWebhookManagerMarker)] = addWebhookManager |
| 106 | + } |
| 107 | + if len(imports) != 0 { |
| 108 | + fragments[file.NewMarkerFor(f.Path, importMarker)] = imports |
| 109 | + } |
| 110 | + if len(addScheme) != 0 { |
| 111 | + fragments[file.NewMarkerFor(f.Path, addSchemeMarker)] = addScheme |
| 112 | + } |
| 113 | + |
| 114 | + return fragments |
| 115 | +} |
| 116 | + |
| 117 | +const ( |
| 118 | + webhookTestSuiteTemplate = ` |
| 119 | +package {{ .Resource.Version }} |
| 120 | +
|
| 121 | +import ( |
| 122 | + "path/filepath" |
| 123 | + "testing" |
| 124 | + "fmt" |
| 125 | +
|
| 126 | + . "github.com/onsi/ginkgo" |
| 127 | + . "github.com/onsi/gomega" |
| 128 | + %s |
| 129 | + "k8s.io/client-go/kubernetes/scheme" |
| 130 | + "k8s.io/client-go/rest" |
| 131 | + "k8s.io/apimachinery/pkg/runtime" |
| 132 | + ctrl "sigs.k8s.io/controller-runtime" |
| 133 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 134 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 135 | + "sigs.k8s.io/controller-runtime/pkg/envtest/printer" |
| 136 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 137 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 138 | +) |
| 139 | +
|
| 140 | +// These tests use Ginkgo (BDD-style Go testing framework). Refer to |
| 141 | +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. |
| 142 | +
|
| 143 | +var cfg *rest.Config |
| 144 | +var k8sClient client.Client |
| 145 | +var testEnv *envtest.Environment |
| 146 | +var stopCh = make(chan struct{}) |
| 147 | +
|
| 148 | +func TestAPIs(t *testing.T) { |
| 149 | + RegisterFailHandler(Fail) |
| 150 | +
|
| 151 | + RunSpecsWithDefaultAndCustomReporters(t, |
| 152 | + "Webhook Suite", |
| 153 | + []Reporter{printer.NewlineReporter{}}) |
| 154 | +} |
| 155 | +
|
| 156 | +var _ = BeforeSuite(func(done Done) { |
| 157 | + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) |
| 158 | +
|
| 159 | + By("bootstrapping test environment") |
| 160 | + testEnv = &envtest.Environment{ |
| 161 | + CRDDirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "crd", "bases")}, |
| 162 | + WebhookInstallOptions: envtest.WebhookInstallOptions{ |
| 163 | + DirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "webhook")}, |
| 164 | + }, |
| 165 | + } |
| 166 | +
|
| 167 | + var err error |
| 168 | + cfg, err = testEnv.Start() |
| 169 | + Expect(err).NotTo(HaveOccurred()) |
| 170 | + Expect(cfg).NotTo(BeNil()) |
| 171 | +
|
| 172 | + scheme := runtime.NewScheme() |
| 173 | + err = AddToScheme(scheme) |
| 174 | + Expect(err).NotTo(HaveOccurred()) |
| 175 | +
|
| 176 | + %s |
| 177 | + |
| 178 | + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) |
| 179 | + Expect(err).NotTo(HaveOccurred()) |
| 180 | + Expect(k8sClient).NotTo(BeNil()) |
| 181 | +
|
| 182 | + // start webhook server using Manager |
| 183 | + webhookInstallOptions := &testEnv.WebhookInstallOptions |
| 184 | + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ |
| 185 | + Scheme: scheme, |
| 186 | + Host: webhookInstallOptions.LocalServingHost, |
| 187 | + Port: webhookInstallOptions.LocalServingPort, |
| 188 | + CertDir: webhookInstallOptions.LocalServingCertDir, |
| 189 | + LeaderElection: false, |
| 190 | + MetricsBindAddress: "0", |
| 191 | + }) |
| 192 | + Expect(err).NotTo(HaveOccurred()) |
| 193 | +
|
| 194 | + %s |
| 195 | +
|
| 196 | + go func() { |
| 197 | + err = mgr.Start(stopCh) |
| 198 | + if err != nil { |
| 199 | + Expect(err).NotTo(HaveOccurred()) |
| 200 | + } |
| 201 | + }() |
| 202 | +
|
| 203 | + // wait for the webhook server to get ready |
| 204 | + dialer := &net.Dialer{Timeout: time.Second} |
| 205 | + addrPort := fmt.Sprintf("%s:%s", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort) |
| 206 | + Eventually(func() error { |
| 207 | + conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true}) |
| 208 | + if err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + conn.Close() |
| 212 | + return nil |
| 213 | + }).Should(Succeed()) |
| 214 | +
|
| 215 | + close(done) |
| 216 | +}, 60) |
| 217 | +
|
| 218 | +var _ = AfterSuite(func() { |
| 219 | + close(stopCh) |
| 220 | + By("tearing down the test environment") |
| 221 | + err := testEnv.Stop() |
| 222 | + Expect(err).NotTo(HaveOccurred()) |
| 223 | +}) |
| 224 | +` |
| 225 | +) |
0 commit comments