-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.go
65 lines (53 loc) · 1.45 KB
/
extension.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package extension
import (
"context"
"errors"
"net"
"net/http"
"strings"
eirinix "github.com/SUSE/eirinix"
"github.com/docker/libnetwork/resolvconf"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
type Extension struct {
DNSServiceHost string
}
func (ext *Extension) Handle(
ctx context.Context,
eiriniManager eirinix.Manager,
pod *corev1.Pod,
req admission.Request,
) admission.Response {
if pod == nil {
return admission.Errored(http.StatusBadRequest, errors.New("failed to decode pod"))
}
podCopy := pod.DeepCopy()
podCopy.Spec.DNSPolicy = corev1.DNSNone
rc, err := resolvconf.Get()
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
strOptions := resolvconf.GetOptions(rc.Content)
options := make([]corev1.PodDNSConfigOption, 0, len(strOptions))
for _, strOption := range strOptions {
split := strings.Split(strOption, ":")
option := corev1.PodDNSConfigOption{
Name: split[0],
Value: &split[1],
}
options = append(options, option)
}
searches := resolvconf.GetSearchDomains(rc.Content)
searches = append(searches, "service.cf.internal")
nameservers, err := net.LookupHost(ext.DNSServiceHost)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
podCopy.Spec.DNSConfig = &corev1.PodDNSConfig{
Nameservers: nameservers,
Options: options,
Searches: searches,
}
return eiriniManager.PatchFromPod(req, podCopy)
}