|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package config |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "net" |
| 24 | + "net/url" |
| 25 | + |
| 26 | + lru "github.com/hashicorp/golang-lru" |
| 27 | + "k8s.io/api/admissionregistration/v1alpha1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + utilerrors "k8s.io/apimachinery/pkg/util/errors" |
| 30 | + "k8s.io/client-go/rest" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + defaultCacheSize = 200 |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + ErrNeedServiceOrURL = errors.New("webhook configuration must have either service or URL") |
| 39 | +) |
| 40 | + |
| 41 | +// ClientManager builds REST clients to talk to webhooks. It caches the clients |
| 42 | +// to avoid duplicate creation. |
| 43 | +type ClientManager struct { |
| 44 | + authInfoResolver AuthenticationInfoResolver |
| 45 | + serviceResolver ServiceResolver |
| 46 | + negotiatedSerializer runtime.NegotiatedSerializer |
| 47 | + cache *lru.Cache |
| 48 | +} |
| 49 | + |
| 50 | +// NewClientManager creates a ClientManager. |
| 51 | +func NewClientManager() (ClientManager, error) { |
| 52 | + cache, err := lru.New(defaultCacheSize) |
| 53 | + if err != nil { |
| 54 | + return ClientManager{}, err |
| 55 | + } |
| 56 | + return ClientManager{ |
| 57 | + cache: cache, |
| 58 | + }, nil |
| 59 | +} |
| 60 | + |
| 61 | +// SetAuthenticationInfoResolverWrapper sets the |
| 62 | +// AuthenticationInfoResolverWrapper. |
| 63 | +func (cm *ClientManager) SetAuthenticationInfoResolverWrapper(wrapper AuthenticationInfoResolverWrapper) { |
| 64 | + if wrapper != nil { |
| 65 | + cm.authInfoResolver = wrapper(cm.authInfoResolver) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// SetAuthenticationInfoResolver sets the AuthenticationInfoResolver. |
| 70 | +func (cm *ClientManager) SetAuthenticationInfoResolver(resolver AuthenticationInfoResolver) { |
| 71 | + cm.authInfoResolver = resolver |
| 72 | +} |
| 73 | + |
| 74 | +// SetServiceResolver sets the ServiceResolver. |
| 75 | +func (cm *ClientManager) SetServiceResolver(sr ServiceResolver) { |
| 76 | + if sr != nil { |
| 77 | + cm.serviceResolver = sr |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// SetNegotiatedSerializer sets the NegotiatedSerializer. |
| 82 | +func (cm *ClientManager) SetNegotiatedSerializer(n runtime.NegotiatedSerializer) { |
| 83 | + cm.negotiatedSerializer = n |
| 84 | +} |
| 85 | + |
| 86 | +// Validate checks if ClientManager is properly set up. |
| 87 | +func (cm *ClientManager) Validate() error { |
| 88 | + var errs []error |
| 89 | + if cm.negotiatedSerializer == nil { |
| 90 | + errs = append(errs, fmt.Errorf("the ClientManager requires a negotiatedSerializer")) |
| 91 | + } |
| 92 | + if cm.serviceResolver == nil { |
| 93 | + errs = append(errs, fmt.Errorf("the ClientManager requires a serviceResolver")) |
| 94 | + } |
| 95 | + if cm.authInfoResolver == nil { |
| 96 | + errs = append(errs, fmt.Errorf("the ClientManager requires an authInfoResolver")) |
| 97 | + } |
| 98 | + return utilerrors.NewAggregate(errs) |
| 99 | +} |
| 100 | + |
| 101 | +// HookClient get a RESTClient from the cache, or constructs one based on the |
| 102 | +// webhook configuration. |
| 103 | +func (cm *ClientManager) HookClient(h *v1alpha1.Webhook) (*rest.RESTClient, error) { |
| 104 | + cacheKey, err := json.Marshal(h.ClientConfig) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + if client, ok := cm.cache.Get(string(cacheKey)); ok { |
| 109 | + return client.(*rest.RESTClient), nil |
| 110 | + } |
| 111 | + |
| 112 | + complete := func(cfg *rest.Config) (*rest.RESTClient, error) { |
| 113 | + cfg.TLSClientConfig.CAData = h.ClientConfig.CABundle |
| 114 | + cfg.ContentConfig.NegotiatedSerializer = cm.negotiatedSerializer |
| 115 | + cfg.ContentConfig.ContentType = runtime.ContentTypeJSON |
| 116 | + client, err := rest.UnversionedRESTClientFor(cfg) |
| 117 | + if err == nil { |
| 118 | + cm.cache.Add(string(cacheKey), client) |
| 119 | + } |
| 120 | + return client, err |
| 121 | + } |
| 122 | + |
| 123 | + if svc := h.ClientConfig.Service; svc != nil { |
| 124 | + serverName := svc.Name + "." + svc.Namespace + ".svc" |
| 125 | + restConfig, err := cm.authInfoResolver.ClientConfigFor(serverName) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + cfg := rest.CopyConfig(restConfig) |
| 130 | + host := serverName + ":443" |
| 131 | + cfg.Host = "https://" + host |
| 132 | + if svc.Path != nil { |
| 133 | + cfg.APIPath = *svc.Path |
| 134 | + } |
| 135 | + cfg.TLSClientConfig.ServerName = serverName |
| 136 | + |
| 137 | + delegateDialer := cfg.Dial |
| 138 | + if delegateDialer == nil { |
| 139 | + delegateDialer = net.Dial |
| 140 | + } |
| 141 | + cfg.Dial = func(network, addr string) (net.Conn, error) { |
| 142 | + if addr == host { |
| 143 | + u, err := cm.serviceResolver.ResolveEndpoint(svc.Namespace, svc.Name) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + addr = u.Host |
| 148 | + } |
| 149 | + return delegateDialer(network, addr) |
| 150 | + } |
| 151 | + |
| 152 | + return complete(cfg) |
| 153 | + } |
| 154 | + |
| 155 | + if h.ClientConfig.URL == nil { |
| 156 | + return nil, &ErrCallingWebhook{WebhookName: h.Name, Reason: ErrNeedServiceOrURL} |
| 157 | + } |
| 158 | + |
| 159 | + u, err := url.Parse(*h.ClientConfig.URL) |
| 160 | + if err != nil { |
| 161 | + return nil, &ErrCallingWebhook{WebhookName: h.Name, Reason: fmt.Errorf("Unparsable URL: %v", err)} |
| 162 | + } |
| 163 | + |
| 164 | + restConfig, err := cm.authInfoResolver.ClientConfigFor(u.Host) |
| 165 | + if err != nil { |
| 166 | + return nil, err |
| 167 | + } |
| 168 | + |
| 169 | + cfg := rest.CopyConfig(restConfig) |
| 170 | + cfg.Host = u.Host |
| 171 | + cfg.APIPath = u.Path |
| 172 | + |
| 173 | + return complete(cfg) |
| 174 | +} |
0 commit comments