Skip to content

Commit ad139d6

Browse files
authored
Merge pull request #145 from mengqiy/fix_fs_cert_writer
fix issues when running on master
2 parents 28ba1f2 + 4692583 commit ad139d6

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

pkg/webhook/bootstrap.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ package webhook
1919
import (
2020
"errors"
2121
"fmt"
22+
"net"
2223
"net/http"
2324
"net/url"
2425
"os"
2526
"path"
27+
"strconv"
2628

2729
"github.com/ghodss/yaml"
2830

@@ -187,7 +189,7 @@ func (s *Server) getClientConfig() (*admissionregistration.WebhookClientConfig,
187189
if s.Host != nil {
188190
u := url.URL{
189191
Scheme: "https",
190-
Host: *s.Host,
192+
Host: net.JoinHostPort(*s.Host, strconv.Itoa(int(s.Port))),
191193
}
192194
urlString := u.String()
193195
cc.URL = &urlString
@@ -332,7 +334,7 @@ func (s *Server) admissionWebhook(path string, wh *admission.Webhook) (*admissio
332334
}
333335

334336
// service creates a corev1.service object fronting the admission server.
335-
func (s *Server) service() *corev1.Service {
337+
func (s *Server) service() runtime.Object {
336338
if s.Service == nil {
337339
return nil
338340
}

pkg/webhook/internal/cert/provisioner.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"errors"
2222
"fmt"
23+
"net"
2324
"net/url"
2425

2526
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
@@ -127,5 +128,12 @@ func dnsNameFromClientConfig(config *admissionregistrationv1beta1.WebhookClientC
127128
return generator.ServiceToCommonName(config.Service.Namespace, config.Service.Name), nil
128129
}
129130
u, err := url.Parse(*config.URL)
130-
return u.Host, err
131+
if err != nil {
132+
return "", err
133+
}
134+
host, _, err := net.SplitHostPort(u.Host)
135+
if err != nil {
136+
return u.Host, nil
137+
}
138+
return host, err
131139
}

pkg/webhook/internal/cert/provisioner_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ var _ = Describe("dnsNameFromClientConfig", func() {
191191
Expect(err).NotTo(HaveOccurred())
192192
Expect(dnsName).To(Equal("foo.example.com"))
193193
})
194+
195+
It("should return a DNS name w/o port", func() {
196+
urlStr := "https://foo.example.com:9876/webhookendpoint"
197+
cc := &admissionregistrationv1beta1.WebhookClientConfig{
198+
URL: &urlStr,
199+
}
200+
dnsName, err := dnsNameFromClientConfig(cc)
201+
Expect(err).NotTo(HaveOccurred())
202+
Expect(dnsName).To(Equal("foo.example.com"))
203+
})
194204
})
195205
})
196206

pkg/webhook/internal/cert/writer/fs.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package writer
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"io/ioutil"
2223
"os"
2324
"path"
@@ -91,21 +92,39 @@ func (f *fsCertWriter) doWrite() (*generator.Artifacts, error) {
9192
if err != nil {
9293
return nil, err
9394
}
95+
96+
// AtomicWriter's algorithm only manages files using symbolic link.
97+
// If a file is not a symbolic link, will ignore the update for it.
98+
// We want to cleanup for AtomicWriter by removing old files that are not symbolic links.
99+
err = prepareToWrite(f.Path)
100+
if err != nil {
101+
return nil, err
102+
}
103+
94104
aw, err := atomic.NewAtomicWriter(f.Path, log.WithName("atomic-writer").
95105
WithValues("task", "processing webhook"))
96106
if err != nil {
97107
return nil, err
98108
}
99-
// AtomicWriter's algorithm only manages files using symbolic link.
100-
// If a file is not a symbolic link, will ignore the update for it.
101-
// We want to cleanup for AtomicWriter by removing old files that are not symbolic links.
102-
prepareToWrite(f.Path)
103109
err = aw.Write(certToProjectionMap(certs))
104110
return certs, err
105111
}
106112

107113
// prepareToWrite ensures it directory is compatible with the atomic.Writer library.
108-
func prepareToWrite(dir string) {
114+
func prepareToWrite(dir string) error {
115+
_, err := os.Stat(dir)
116+
switch {
117+
case os.IsNotExist(err):
118+
log.Info(fmt.Sprintf("cert directory %v doesn't exist, creating", dir))
119+
// TODO: figure out if we can reduce the permission. (Now it's 0777)
120+
err = os.MkdirAll(dir, 0777)
121+
if err != nil {
122+
return fmt.Errorf("can't create dir: %v", dir)
123+
}
124+
case err != nil:
125+
return err
126+
}
127+
109128
filenames := []string{CACertName, ServerCertName, ServerKeyName}
110129
for _, f := range filenames {
111130
abspath := path.Join(dir, f)
@@ -124,6 +143,7 @@ func prepareToWrite(dir string) {
124143
}
125144
}
126145
}
146+
return nil
127147
}
128148

129149
func (f *fsCertWriter) read() (*generator.Artifacts, error) {

pkg/webhook/util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ func createOrReplaceHelper(c client.Client, obj runtime.Object, fn mutateFn) err
8484
// When replacing, it knows how to preserve existing fields in the object GET from the APIServer.
8585
// It currently only support MutatingWebhookConfiguration, ValidatingWebhookConfiguration and Service.
8686
func createOrReplace(c client.Client, obj runtime.Object) error {
87+
if obj == nil {
88+
return nil
89+
}
8790
switch obj.(type) {
8891
case *admissionregistration.MutatingWebhookConfiguration:
8992
return createOrReplaceHelper(c, obj, mutatingWebhookConfigFn)

0 commit comments

Comments
 (0)