-
Notifications
You must be signed in to change notification settings - Fork 472
/
app.go
186 lines (162 loc) · 6.22 KB
/
app.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2022 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"
"fmt"
"net"
"net/http"
"os"
goruntime "runtime"
"strconv"
"time"
"github.com/go-logr/logr"
"github.com/spf13/cobra"
"go.uber.org/automaxprocs/maxprocs"
"k8s.io/component-base/version/verflag"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
controllerconfig "sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"github.com/gardener/gardener/cmd/gardener-controller-manager/app/bootstrappers"
"github.com/gardener/gardener/cmd/utils"
"github.com/gardener/gardener/pkg/api/indexer"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/controllermanager/apis/config"
"github.com/gardener/gardener/pkg/controllermanager/controller"
"github.com/gardener/gardener/pkg/controllerutils/routes"
"github.com/gardener/gardener/pkg/features"
gardenerhealthz "github.com/gardener/gardener/pkg/healthz"
)
// Name is a const for the name of this component.
const Name = "gardener-controller-manager"
// NewCommand creates a new cobra.Command for running gardener-controller-manager.
func NewCommand() *cobra.Command {
opts := &options{}
cmd := &cobra.Command{
Use: Name,
Short: "Launch the " + Name,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
log, err := utils.InitRun(cmd, opts, Name)
if err != nil {
return err
}
return run(cmd.Context(), log, opts.config)
},
}
flags := cmd.Flags()
verflag.AddFlags(flags)
opts.addFlags(flags)
return cmd
}
func run(ctx context.Context, log logr.Logger, cfg *config.ControllerManagerConfiguration) error {
log.Info("Feature Gates", "featureGates", features.DefaultFeatureGate)
// This is like importing the automaxprocs package for its init func (it will in turn call maxprocs.Set).
// Here we pass a custom logger, so that the result of the library gets logged to the same logger we use for the
// component itself.
if _, err := maxprocs.Set(maxprocs.Logger(func(s string, i ...interface{}) {
log.Info(fmt.Sprintf(s, i...)) //nolint:logcheck
})); err != nil {
log.Error(err, "Failed to set GOMAXPROCS")
}
log.Info("Getting rest config")
if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" {
cfg.GardenClientConnection.Kubeconfig = kubeconfig
}
restConfig, err := kubernetes.RESTConfigFromClientConnectionConfiguration(&cfg.GardenClientConnection, nil, kubernetes.AuthTokenFile)
if err != nil {
return err
}
var extraHandlers map[string]http.Handler
if cfg.Debugging != nil && cfg.Debugging.EnableProfiling {
extraHandlers = routes.ProfilingHandlers
if cfg.Debugging.EnableContentionProfiling {
goruntime.SetBlockProfileRate(1)
}
}
log.Info("Setting up manager")
mgr, err := manager.New(restConfig, manager.Options{
Logger: log,
Scheme: kubernetes.GardenScheme,
GracefulShutdownTimeout: ptr.To(5 * time.Second),
HealthProbeBindAddress: net.JoinHostPort(cfg.Server.HealthProbes.BindAddress, strconv.Itoa(cfg.Server.HealthProbes.Port)),
Metrics: metricsserver.Options{
BindAddress: net.JoinHostPort(cfg.Server.Metrics.BindAddress, strconv.Itoa(cfg.Server.Metrics.Port)),
ExtraHandlers: extraHandlers,
},
LeaderElection: cfg.LeaderElection.LeaderElect,
LeaderElectionResourceLock: cfg.LeaderElection.ResourceLock,
LeaderElectionID: cfg.LeaderElection.ResourceName,
LeaderElectionNamespace: cfg.LeaderElection.ResourceNamespace,
LeaderElectionReleaseOnCancel: true,
LeaseDuration: &cfg.LeaderElection.LeaseDuration.Duration,
RenewDeadline: &cfg.LeaderElection.RenewDeadline.Duration,
RetryPeriod: &cfg.LeaderElection.RetryPeriod.Duration,
Controller: controllerconfig.Controller{
RecoverPanic: ptr.To(true),
},
})
if err != nil {
return err
}
log.Info("Setting up health check endpoints")
if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
return err
}
if err := mgr.AddReadyzCheck("informer-sync", gardenerhealthz.NewCacheSyncHealthz(mgr.GetCache())); err != nil {
return err
}
log.Info("Adding field indexes to informers")
if err := addAllFieldIndexes(ctx, mgr.GetFieldIndexer()); err != nil {
return fmt.Errorf("failed adding indexes: %w", err)
}
log.Info("Adding garden bootstrapper to manager")
if err := mgr.Add(&bootstrappers.Bootstrapper{
Log: log.WithName("bootstrap"),
Client: mgr.GetClient(),
RESTConfig: mgr.GetConfig(),
}); err != nil {
return fmt.Errorf("failed adding garden cluster bootstrapper to manager: %w", err)
}
log.Info("Adding controllers to manager")
if err := controller.AddToManager(ctx, mgr, cfg); err != nil {
return fmt.Errorf("failed adding controllers to manager: %w", err)
}
log.Info("Starting manager")
return mgr.Start(ctx)
}
func addAllFieldIndexes(ctx context.Context, i client.FieldIndexer) error {
for _, fn := range []func(context.Context, client.FieldIndexer) error{
// core API group
indexer.AddProjectNamespace,
indexer.AddShootSeedName,
indexer.AddShootStatusSeedName,
indexer.AddBackupBucketSeedName,
indexer.AddBackupEntrySeedName,
indexer.AddControllerInstallationSeedRefName,
indexer.AddControllerInstallationRegistrationRefName,
// operations API group
indexer.AddBastionShootName,
// seedmanagement API group
indexer.AddManagedSeedShootName,
} {
if err := fn(ctx, i); err != nil {
return err
}
}
return nil
}