Skip to content

Commit 8f6723d

Browse files
authored
Merge pull request #111 from A-Hilaly/register-webhooks
Setup webhooks on controller startup
2 parents 138195a + 5b07308 commit 8f6723d

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

templates/cmd/controller/main.go.tpl

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config"
99
ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"
10+
ackrtutil "github.com/aws-controllers-k8s/runtime/pkg/util"
11+
ackrtwebhook "github.com/aws-controllers-k8s/runtime/pkg/webhook"
1012
flag "github.com/spf13/pflag"
1113
"k8s.io/apimachinery/pkg/runtime"
1214
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -16,7 +18,7 @@ import (
1618
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
1719
svcresource "github.com/aws-controllers-k8s/{{ .ServiceIDClean }}-controller/pkg/resource"
1820
svctypes "github.com/aws-controllers-k8s/{{ .ServiceIDClean }}-controller/apis/{{ .APIVersion }}"
19-
21+
{{/* TODO(a-hilaly): import apis/* packages to register webhooks */}}
2022
{{ $serviceIDClean := .ServiceIDClean }} {{range $crdName := .SnakeCasedCRDNames }}_ "github.com/aws-controllers-k8s/{{ $serviceIDClean }}-controller/pkg/resource/{{ $crdName }}"
2123
{{end}}
2224
)
@@ -30,6 +32,7 @@ var (
3032

3133
func init() {
3234
_ = clientgoscheme.AddToScheme(scheme)
35+
{{/* TODO(a-hilaly): register all the apis/* schemes */}}
3336
_ = svctypes.AddToScheme(scheme)
3437
_ = ackv1alpha1.AddToScheme(scheme)
3538
}
@@ -48,13 +51,23 @@ func main() {
4851
os.Exit(1)
4952
}
5053

54+
host, port, err := ackrtutil.GetHostPort(ackCfg.WebhookServerAddr)
55+
if err != nil {
56+
setupLog.Error(
57+
err, "Unable to parse webhook server address.",
58+
"aws.service", awsServiceAlias,
59+
)
60+
os.Exit(1)
61+
}
62+
5163
mgr, err := ctrlrt.NewManager(ctrlrt.GetConfigOrDie(), ctrlrt.Options{
52-
Scheme: scheme,
53-
Port: ackCfg.BindPort,
64+
Scheme: scheme,
65+
Port: port,
66+
Host: host,
5467
MetricsBindAddress: ackCfg.MetricsAddr,
55-
LeaderElection: ackCfg.EnableLeaderElection,
68+
LeaderElection: ackCfg.EnableLeaderElection,
5669
LeaderElectionID: awsServiceAPIGroup,
57-
Namespace: ackCfg.WatchNamespace,
70+
Namespace: ackCfg.WatchNamespace,
5871
})
5972
if err != nil {
6073
setupLog.Error(
@@ -80,6 +93,20 @@ func main() {
8093
).WithPrometheusRegistry(
8194
ctrlrtmetrics.Registry,
8295
)
96+
97+
if ackCfg.EnableWebhookServer {
98+
webhooks := ackrtwebhook.GetWebhooks()
99+
for _, webhook := range webhooks {
100+
if err := webhook.Setup(mgr); err != nil {
101+
setupLog.Error(
102+
err, "unable to register webhook "+webhook.UID(),
103+
"aws.service", awsServiceAlias,
104+
)
105+
106+
}
107+
}
108+
}
109+
83110
if err = sc.BindControllerManager(mgr, ackCfg); err != nil {
84111
setupLog.Error(
85112
err, "unable bind to controller manager to service controller",

0 commit comments

Comments
 (0)