Skip to content

Commit 925f842

Browse files
author
Eric Stroczynski
authored
docs/olm-integration/generation.md: fix example Go code to get webhooks working with OLM < 0.17 (#4945)
Signed-off-by: Eric Stroczynski <[email protected]>
1 parent b55e81a commit 925f842

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

website/content/en/docs/olm-integration/generation.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,32 +101,28 @@ import (
101101
func main() {
102102
...
103103

104-
// Standard Manager setup.
105-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
106-
Host: <some host>, // May not be configured explicitly.
107-
Port: <some port>, // May not be configured explicitly.
108-
})
109-
110-
...
111-
112-
// Before any webhooks are registered:
113-
var certDir, certName, keyName string
114-
if info, err := os.Stat("/apiserver.local.config/certificates"); err == nil && info.IsDir() {
115-
certDir = "/apiserver.local.config/certificates"
116-
certName = "apiserver.crt"
117-
keyName = "apiserver.key"
118-
}
119-
if err = mgr.Add(webhook.Server{
120-
Host: <some host>, // Set this only if set in ctrl.Options above.
121-
Port: <some port>, // Set this only if set in ctrl.Options above.
122-
CertDir: certDir, // Defaults to the correct path if unset.
123-
CertName: certName, // Defaults to the correct path if unset.
124-
KeyName: keyName, // Defaults to the correct path if unset.
125-
}); err != nil {
126-
setupLog.Error(err, "unable to add webhook server")
127-
os.Exit(1)
104+
// Configure a webhook.Server with the correct path and file names.
105+
// If webhookServer is nil, which will be the case of OLM >= 0.17 is available,
106+
// the manager will create a server for you using Host, Port,
107+
// and the default CertDir, KeyName, and CertName.
108+
var webhookServer *webhook.Server
109+
const legacyOLMCertDir = "/apiserver.local.config/certificates"
110+
if info, err := os.Stat(legacyOLMCertDir); err == nil && info.IsDir() {
111+
webhookServer = &webhook.Server{
112+
Host: <some host>, // Set this only if normally set in ctrl.Options below.
113+
Port: <some port>, // Set this only if normally set in ctrl.Options below.
114+
CertDir: legacyOLMCertDir,
115+
CertName: "apiserver.crt",
116+
KeyName: "apiserver.key",
117+
}
128118
}
129119

120+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
121+
Host: <some host>,
122+
Port: <some port>,
123+
WebhookServer: webhookServer, // Host/Port will not be used if webhookServer is nil.
124+
})
125+
130126
// Now you can register webhooks.
131127
...
132128
}

0 commit comments

Comments
 (0)