-
Notifications
You must be signed in to change notification settings - Fork 222
/
main.go
154 lines (132 loc) · 6.05 KB
/
main.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
// Copyright 2022 Google LLC
//
// 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 main
import (
"context"
goflag "flag"
"fmt"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof" // Needed to allow pprof server to accept requests
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/kccmanager"
controllermetrics "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/metrics"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp/profiler"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/krmtotf"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/logging"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/metrics"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/ready"
flag "github.com/spf13/pflag"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
klog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
var logger = klog.Log.WithName("setup")
func main() {
ctx := context.Background()
stop := signals.SetupSignalHandler()
var (
prometheusScrapeEndpoint string
scopedNamespace string
userProjectOverride bool
billingProject string
enablePprof bool
pprofPort int
)
flag.StringVar(&prometheusScrapeEndpoint, "prometheus-scrape-endpoint", ":8888", "configure the Prometheus scrape endpoint; :8888 as default")
flag.BoolVar(&controllermetrics.ResourceNameLabel, "resource-name-label", false, "option to enable the resource name label on some Prometheus metrics; false by default")
flag.BoolVar(&userProjectOverride, "user-project-override", false, "option to use the resource project for preconditions, quota, and billing, instead of the project the credentials belong to; false by default")
flag.StringVar(&billingProject, "billing-project", "", "project to use for preconditions, quota, and billing if --user-project-override is enabled; empty by default; if this is left empty but --user-project-override is enabled, the resource's project will be used")
flag.StringVar(&scopedNamespace, "scoped-namespace", "", "scope controllers to only watch resources in the specified namespace; if unspecified, controllers will run in cluster scope")
flag.BoolVar(&enablePprof, "enable-pprof", false, "Enable the pprof server.")
flag.IntVar(&pprofPort, "pprof-port", 6060, "The port that the pprof server binds to if enabled.")
profiler.AddFlag(flag.CommandLine)
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
flag.Parse()
// Discard everything logged onto the Go standard logger. We do this since
// there are cases of Terraform logging sensitive data onto the Go standard
// logger.
log.SetOutput(ioutil.Discard)
logging.SetupLogger()
// Start pprof server if enabled
if enablePprof {
go func() {
if err := http.ListenAndServe(fmt.Sprintf(":%d", pprofPort), nil); err != nil {
logger.Error(err, "error while running pprof server")
}
}()
}
// Start Cloud Profiler agent if enabled
if err := profiler.StartIfEnabled(); err != nil {
logging.Fatal(err, "error starting Cloud Profiler agent")
}
// Get a config to talk to the apiserver
restCfg, err := config.GetConfig()
if err != nil {
logging.Fatal(err, "fatal getting configuration from APIServer.")
}
logger.Info("Creating the manager")
mgr, err := newManager(ctx, restCfg, scopedNamespace, userProjectOverride, billingProject)
if err != nil {
logging.Fatal(err, "error creating the manager")
}
// Register controller OpenCensus views
logger.Info("Registering controller OpenCensus views.")
if controllermetrics.ResourceNameLabel {
if err = metrics.RegisterControllerOpenCensusViewsWithResourceNameLabel(); err != nil {
logging.Fatal(err, "error registering controller OpenCensus views with resource name label.")
}
} else {
if err = metrics.RegisterControllerOpenCensusViews(); err != nil {
logging.Fatal(err, "error registering controller OpenCensus views.")
}
}
// Register the Prometheus exporter
logger.Info("Registering the Prometheus exporter")
if err = metrics.RegisterPrometheusExporter(prometheusScrapeEndpoint); err != nil {
logging.Fatal(err, "error registering the Prometheus exporter.")
}
// Record the process start time which will be used by prometheus-to-sd sidecar
if err = metrics.RecordProcessStartTime(); err != nil {
logging.Fatal(err, "error recording the process start time.")
}
// Set up the HTTP server for the readiness probe
logger.Info("Setting container as ready...")
ready.SetContainerAsReady()
logger.Info("Container is ready.")
logger.Info("Starting the Cmd.")
// Start the Cmd
logging.Fatal(mgr.Start(stop), "error during manager execution.")
}
func newManager(ctx context.Context, restCfg *rest.Config, scopedNamespace string, userProjectOverride bool, billingProject string) (manager.Manager, error) {
krmtotf.SetUserAgentForTerraformProvider()
controllersCfg := kccmanager.Config{
ManagerOptions: manager.Options{
Namespace: scopedNamespace,
},
}
controllersCfg.UserProjectOverride = userProjectOverride
controllersCfg.BillingProject = billingProject
// TODO(b/320784855): StateIntoSpecDefaultValue and StateIntoSpecUserOverride values should come from the flags.
controllersCfg.StateIntoSpecDefaultValue = k8s.StateIntoSpecDefaultValueV1Beta1
mgr, err := kccmanager.New(ctx, restCfg, controllersCfg)
if err != nil {
return nil, fmt.Errorf("error creating manager: %w", err)
}
return mgr, nil
}