NewWebhook creates a new instance of a mutating webhook for automatic sidecar injection.
(p WebhookParameters)
| 200 | |
| 201 | // NewWebhook creates a new instance of a mutating webhook for automatic sidecar injection. |
| 202 | func NewWebhook(p WebhookParameters) (*Webhook, error) { |
| 203 | if p.Mux == nil { |
| 204 | return nil, errors.New("expected mux to be passed, but was not passed") |
| 205 | } |
| 206 | |
| 207 | wh := &Webhook{ |
| 208 | watcher: p.Watcher, |
| 209 | meshConfig: p.Env.Mesh(), |
| 210 | env: p.Env, |
| 211 | revision: p.Revision, |
| 212 | } |
| 213 | |
| 214 | if p.MultiCluster != nil { |
| 215 | if platform.IsOpenShift() { |
| 216 | wh.namespaces = multicluster.BuildMultiClusterKclientComponent[*corev1.Namespace](p.MultiCluster, kubetypes.Filter{}) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if features.EnableNativeSidecars != features.NativeSidecarModeDisabled { |
| 221 | wh.nodes = multicluster.BuildMultiClusterKclientComponent[*corev1.Node](p.MultiCluster, kubetypes.Filter{ |
| 222 | ObjectTransform: kube.StripNodeUnusedFields, |
| 223 | }) |
| 224 | } |
| 225 | |
| 226 | mc := NewMulticast(p.Watcher, wh.GetConfig) |
| 227 | mc.AddHandler(wh.updateConfig) |
| 228 | wh.MultiCast = mc |
| 229 | sidecarConfig, valuesConfig, err := p.Watcher.Get() |
| 230 | if err != nil { |
| 231 | return nil, fmt.Errorf("failed to get initial configuration: %v", err) |
| 232 | } |
| 233 | if err := wh.updateConfig(sidecarConfig, valuesConfig); err != nil { |
| 234 | return nil, fmt.Errorf("failed to process webhook config: %v", err) |
| 235 | } |
| 236 | |
| 237 | p.Mux.HandleFunc("/inject", wh.serveInject) |
| 238 | p.Mux.HandleFunc("/inject/", wh.serveInject) |
| 239 | |
| 240 | p.Env.Watcher.AddMeshHandler(func() { |
| 241 | wh.mu.Lock() |
| 242 | wh.meshConfig = p.Env.Mesh() |
| 243 | wh.mu.Unlock() |
| 244 | }) |
| 245 | |
| 246 | return wh, nil |
| 247 | } |
| 248 | |
| 249 | // Run implements the webhook server |
| 250 | func (wh *Webhook) Run(stop <-chan struct{}) { |
searching dependent graphs…