in pkg/controller/unnamedwatches/controller.go [91:157]
func (wr *WResource) Init(mgr ctrl.Manager, options *pc.Options) {
if !options.EnableWebHook {
klog.Infof("wresource init not enable and WebHook not enable.")
return
}
cfg := mgr.GetConfig()
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Errorf("wresource init build clientset from mgr failed, err=%s", err.Error())
os.Exit(1)
}
ws := &WatchSecret{
client: clientset,
Name: WatchSecretWebhookName,
NamespaceName: types.NamespacedName{
Name: options.SecretName,
Namespace: options.Namespace,
},
WebhookService: options.WebhookService,
Type: &corev1.Secret{},
MutatingWebhookConfigurationName: DefaultMutatingWebhookConfigurationName,
ValidatingWebhookConfigurationName: DefaultValidatingWebhookConfigurationName,
}
wv := &WatchValidatingWebhookConfiguration{
client: clientset,
Name: WatchValidatingWebhookConfigurationName,
SecretNamespaceName: types.NamespacedName{
Name: options.SecretName,
Namespace: options.Namespace,
},
ValidatingWebhookConfigurationName: DefaultValidatingWebhookConfigurationName,
Type: &v1.ValidatingWebhookConfiguration{},
}
wm := &WatchMutationWebhookConfiguration{
client: clientset,
Name: WatchMutatingWebhookConfigurationName,
SecretNamespaceName: types.NamespacedName{
Name: options.SecretName,
Namespace: options.Namespace,
},
MutationWebhookConfigurationName: DefaultMutatingWebhookConfigurationName,
Type: &v1.MutatingWebhookConfiguration{},
}
wr.watches = append(wr.watches, ws, wv, wm)
//force a first reconciliation to create the resources
if err := wr.ReconcileResource(context.Background()); err != nil {
klog.Errorf("first reconciliation failed to reconcile resource, err=%s", err.Error())
}
c, err := controller.New(options.Name, mgr, controller.Options{Reconciler: wr})
if err != nil {
klog.Errorf("wresource init new controller failed, err=%s", err.Error())
}
for _, w := range wr.watches {
if err := c.Watch(source.Kind(mgr.GetCache(), w.GetType(), w)); err != nil {
klog.Errorf("wresource init build clientset from mgr failed, err=%s", err.Error())
os.Exit(1)
}
}
}