Skip to content

Commit

Permalink
Cleanup Webhook server setup
Browse files Browse the repository at this point in the history
- Called the webhook server cleanup function

- Only ignore controlplane cleanup when using existing cluster
  • Loading branch information
Ayush Rangwala committed May 3, 2021
1 parent a17ac06 commit 9cee39f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
12 changes: 6 additions & 6 deletions pkg/envtest/envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ var _ = Describe("Test", func() {
Name: crd.GetName(),
}
var placeholder v1beta1.CustomResourceDefinition
err := c.Get(context.TODO(), crdObjectKey, &placeholder)
err = c.Get(context.TODO(), crdObjectKey, &placeholder)
if err != nil && apierrors.IsNotFound(err) {
// CRD doesn't need to be deleted.
continue
}
Expect(err).NotTo(HaveOccurred())
Expect(c.Delete(context.TODO(), crd)).To(Succeed())
Eventually(func() bool {
err := c.Get(context.TODO(), crdObjectKey, &placeholder)
err = c.Get(context.TODO(), crdObjectKey, &placeholder)
return apierrors.IsNotFound(err)
}, 5*time.Second).Should(BeTrue())
}
Expand Down Expand Up @@ -830,16 +830,16 @@ var _ = Describe("Test", func() {

Describe("Start", func() {
It("should raise an error on invalid dir when flag is enabled", func(done Done) {
env := &Environment{ErrorIfCRDPathMissing: true, CRDDirectoryPaths: []string{invalidDirectory}}
_, err := env.Start()
env = &Environment{ErrorIfCRDPathMissing: true, CRDDirectoryPaths: []string{invalidDirectory}}
_, err = env.Start()
Expect(err).To(HaveOccurred())
Expect(env.Stop()).To(Succeed())
close(done)
}, 30)

It("should not raise an error on invalid dir when flag is disabled", func(done Done) {
env := &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
_, err := env.Start()
env = &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
_, err = env.Start()
Expect(err).NotTo(HaveOccurred())
Expect(env.Stop()).To(Succeed())
close(done)
Expand Down
10 changes: 6 additions & 4 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ func (te *Environment) Stop() error {
return err
}
}

if err := te.WebhookInstallOptions.Cleanup(); err != nil {
return err
}

if te.useExistingCluster() {
return nil
}
err := te.WebhookInstallOptions.Cleanup()
if err != nil {
return err
}

return te.ControlPlane.Stop()
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/envtest/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (o *WebhookInstallOptions) PrepWithoutInstalling() error {
if err != nil {
return err
}
if err := parseWebhook(o); err != nil {
if err = parseWebhook(o); err != nil {
return err
}

Expand Down Expand Up @@ -292,10 +292,10 @@ func (o *WebhookInstallOptions) setupCA() ([]byte, error) {
return nil, fmt.Errorf("unable to marshal webhook serving certs: %v", err)
}

if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil {
if err = ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil {
return nil, fmt.Errorf("unable to write webhook serving cert to disk: %v", err)
}
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil {
if err = ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil {
return nil, fmt.Errorf("unable to write webhook serving key to disk: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/envtest/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ var _ = Describe("Test", func() {
type rejectingValidator struct {
}

func (v *rejectingValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
func (v *rejectingValidator) Handle(_ context.Context, _ admission.Request) admission.Response {
return admission.Denied(fmt.Sprint("Always denied"))
}
2 changes: 1 addition & 1 deletion pkg/webhook/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var _ = Describe("Webhook Server", func() {

BeforeEach(func() {
ctx, ctxCancel = context.WithCancel(context.Background())
// closed in indivual tests differently
// closed in individual tests differently

servingOpts = envtest.WebhookInstallOptions{}
Expect(servingOpts.PrepWithoutInstalling()).To(Succeed())
Expand Down

0 comments on commit 9cee39f

Please sign in to comment.