-
Notifications
You must be signed in to change notification settings - Fork 451
/
gardener_seed_admission_controller.go
156 lines (131 loc) · 5.29 KB
/
gardener_seed_admission_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"context"
"flag"
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/component-base/version"
"k8s.io/component-base/version/verflag"
"sigs.k8s.io/controller-runtime/pkg/client/config"
runtimelog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/seedadmissioncontroller/webhooks/admission/extensioncrds"
"github.com/gardener/gardener/pkg/seedadmissioncontroller/webhooks/admission/extensionresources"
"github.com/gardener/gardener/pkg/seedadmissioncontroller/webhooks/admission/podschedulername"
)
const (
// Name is a const for the name of this component.
Name = "gardener-seed-admission-controller"
)
var (
gracefulShutdownTimeout = 5 * time.Second
log = runtimelog.Log
)
// NewSeedAdmissionControllerCommand creates a new *cobra.Command able to run gardener-seed-admission-controller.
func NewSeedAdmissionControllerCommand() *cobra.Command {
opts := &Options{}
cmd := &cobra.Command{
Use: Name,
Short: "Launch the " + Name,
Long: Name + " serves validating and mutating webhook endpoints for resources in seed clusters.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
verflag.PrintAndExitIfRequested()
if err := opts.validate(); err != nil {
return err
}
cmd.SilenceUsage = true
log.Info("Starting "+Name+"...", "version", version.Get())
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
log.Info(fmt.Sprintf("FLAG: --%s=%s", flag.Name, flag.Value))
})
return opts.Run(cmd.Context())
},
}
flags := cmd.Flags()
flags.AddGoFlagSet(flag.CommandLine)
verflag.AddFlags(flags)
opts.AddFlags(flags)
return cmd
}
// Options has all the context and parameters needed to run gardener-seed-admission-controller.
type Options struct {
// BindAddress is the address the HTTP server should bind to.
BindAddress string
// Port is the port that should be opened by the HTTP server.
Port int
// ServerCertDir is the path to server TLS cert and key.
ServerCertDir string
// AllowInvalidExtensionResources causes the seed-admission-controller to allow invalid extension resources.
AllowInvalidExtensionResources bool
}
// AddFlags adds gardener-seed-admission-controller's flags to the specified FlagSet.
func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.BindAddress, "bind-address", "0.0.0.0", "address to bind to")
fs.IntVar(&o.Port, "port", 9443, "webhook server port")
fs.StringVar(&o.ServerCertDir, "tls-cert-dir", "", "directory with server TLS certificate and key (must contain a tls.crt and tls.key file)")
fs.BoolVar(&o.AllowInvalidExtensionResources, "allow-invalid-extension-resources", false, "Allow invalid extension resources")
}
// validate validates all the required options.
func (o *Options) validate() error {
if len(o.BindAddress) == 0 {
return fmt.Errorf("missing bind address")
}
if o.Port == 0 {
return fmt.Errorf("missing port")
}
if len(o.ServerCertDir) == 0 {
return fmt.Errorf("missing server tls cert path")
}
return nil
}
// Run runs gardener-seed-admission-controller using the specified options.
func (o *Options) Run(ctx context.Context) error {
log.Info("getting rest config")
restConfig, err := config.GetConfig()
if err != nil {
return err
}
log.Info("setting up manager")
mgr, err := manager.New(restConfig, manager.Options{
Scheme: kubernetes.SeedScheme,
LeaderElection: false,
MetricsBindAddress: "0", // disable for now, as we don't scrape the component
Host: o.BindAddress,
Port: o.Port,
CertDir: o.ServerCertDir,
GracefulShutdownTimeout: &gracefulShutdownTimeout,
})
if err != nil {
return err
}
log.Info("setting up webhook server")
server := mgr.GetWebhookServer()
server.Register(extensioncrds.WebhookPath, &webhook.Admission{Handler: extensioncrds.New(runtimelog.Log.WithName(extensioncrds.HandlerName))})
server.Register(podschedulername.WebhookPath, &webhook.Admission{Handler: admission.HandlerFunc(podschedulername.DefaultShootControlPlanePodsSchedulerName)})
server.Register(extensionresources.WebhookPath, &webhook.Admission{Handler: extensionresources.New(runtimelog.Log.WithName(extensionresources.HandlerName), o.AllowInvalidExtensionResources)})
log.Info("starting manager")
if err := mgr.Start(ctx); err != nil {
log.Error(err, "error running manager")
return err
}
return nil
}