-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix issue when webhook server refreshing cert
- Loading branch information
Mengqi Yu
committed
Dec 19, 2018
1 parent
25aca14
commit 6c8993d
Showing
3 changed files
with
232 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/cert" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/cert/generator" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/cert/writer" | ||
) | ||
|
||
type fakeCertWriter struct { | ||
changed bool | ||
} | ||
|
||
func (cw *fakeCertWriter) EnsureCert(dnsName string) (*generator.Artifacts, bool, error) { | ||
return &generator.Artifacts{}, cw.changed, nil | ||
} | ||
|
||
func (cw *fakeCertWriter) Inject(objs ...runtime.Object) error { | ||
return nil | ||
} | ||
|
||
var _ = Describe("webhook server", func() { | ||
Describe("run", func() { | ||
var stop chan struct{} | ||
var s *Server | ||
var cn = "example.com" | ||
|
||
BeforeEach(func() { | ||
s = &Server{ | ||
sMux: http.NewServeMux(), | ||
ServerOptions: ServerOptions{ | ||
BootstrapOptions: &BootstrapOptions{ | ||
Host: &cn, | ||
}, | ||
}, | ||
} | ||
|
||
var err error | ||
cg := &generator.SelfSignedCertGenerator{} | ||
s.CertDir, err = ioutil.TempDir("/tmp", "controller-runtime-") | ||
Expect(err).NotTo(HaveOccurred()) | ||
certWriter, err := writer.NewFSCertWriter(writer.FSCertWriterOptions{CertGenerator: cg, Path: s.CertDir}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
_, _, err = certWriter.EnsureCert(cn) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
stop = make(chan struct{}) | ||
}) | ||
|
||
It("should stop if the stop channel is closed", func() { | ||
close(stop) | ||
Expect(s.run(stop)).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should exit if the server encounter an unexpected error", func() { | ||
var e error | ||
|
||
go func() { | ||
defer GinkgoRecover() | ||
e = s.run(stop) | ||
}() | ||
|
||
Eventually(func() *http.Server { | ||
return s.httpServer | ||
}).ShouldNot(BeNil()) | ||
|
||
err := s.httpServer.Shutdown(context.Background()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Eventually(func() error { | ||
return e | ||
}).Should(Equal(http.ErrServerClosed)) | ||
|
||
close(stop) | ||
}) | ||
|
||
It("should be able to keep existing valid cert when timer fires", func() { | ||
var e error | ||
defaultCertRefreshInterval = time.Second | ||
|
||
s.certProvisioner = &cert.Provisioner{ | ||
CertWriter: &fakeCertWriter{changed: false}, | ||
} | ||
|
||
go func() { | ||
defer GinkgoRecover() | ||
e = s.run(stop) | ||
}() | ||
|
||
// Wait for multiple cycles of timer firing | ||
time.Sleep(3 * time.Second) | ||
Expect(e).NotTo(HaveOccurred()) | ||
|
||
close(stop) | ||
}) | ||
|
||
It("should be able to rotate the cert when timer fires", func() { | ||
var e error | ||
defaultCertRefreshInterval = time.Second | ||
s.certProvisioner = &cert.Provisioner{ | ||
CertWriter: &fakeCertWriter{changed: true}, | ||
} | ||
|
||
go func() { | ||
defer GinkgoRecover() | ||
e = s.run(stop) | ||
}() | ||
|
||
Eventually(func() *http.Server { | ||
return s.httpServer | ||
}).ShouldNot(BeNil()) | ||
|
||
// Wait for multiple cycles of timer firing | ||
time.Sleep(3 * time.Second) | ||
Expect(e).NotTo(HaveOccurred()) | ||
|
||
close(stop) | ||
}) | ||
|
||
AfterEach(func() { | ||
defaultCertRefreshInterval = 3 * 30 * 24 * time.Hour | ||
}, 60) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
) | ||
|
||
func TestSource(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecsWithDefaultAndCustomReporters(t, "Webhook Integration Suite", []Reporter{envtest.NewlineReporter{}}) | ||
} | ||
|
||
var testenv *envtest.Environment | ||
var cfg *rest.Config | ||
var clientset *kubernetes.Clientset | ||
|
||
var _ = BeforeSuite(func(done Done) { | ||
logf.SetLogger(logf.ZapLoggerTo(GinkgoWriter, true)) | ||
|
||
testenv = &envtest.Environment{} | ||
|
||
var err error | ||
cfg, err = testenv.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
clientset, err = kubernetes.NewForConfig(cfg) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
close(done) | ||
}, 60) | ||
|
||
var _ = AfterSuite(func() { | ||
testenv.Stop() | ||
}) |