-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathmain.go
More file actions
177 lines (161 loc) · 5.9 KB
/
Copy pathmain.go
File metadata and controls
177 lines (161 loc) · 5.9 KB
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
// 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
//
// https://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"
"errors"
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"cloud.google.com/go/compute/metadata"
"github.com/oklog/run"
versioninfo "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap/zapcore"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
// Blank import required to register GCP auth handlers to talk to GKE clusters.
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"github.com/GoogleCloudPlatform/prometheus-engine/pkg/operator"
)
const (
defaultTLSDir = "/etc/tls/private"
)
func main() {
ctx := context.Background()
var (
defaultProjectID string
defaultCluster string
defaultLocation string
)
errList := []error{}
if metadata.OnGCE() {
var err error
defaultProjectID, err = metadata.ProjectIDWithContext(ctx)
errList = append(errList, err)
defaultCluster, err = metadata.InstanceAttributeValueWithContext(ctx, "cluster-name")
errList = append(errList, err)
defaultLocation, err = metadata.InstanceAttributeValueWithContext(ctx, "cluster-location")
errList = append(errList, err)
}
var (
logVerbosity = flag.Int("v", 0, "Logging verbosity")
projectID = flag.String("project-id", defaultProjectID, "Project ID of the cluster. May be left empty on GKE.")
location = flag.String("location", defaultLocation, "Google Cloud region or zone where your data will be stored. May be left empty on GKE.")
cluster = flag.String("cluster", defaultCluster, "Name of the cluster the operator acts on. May be left empty on GKE.")
operatorNamespace = flag.String("operator-namespace", operator.DefaultOperatorNamespace,
"Namespace in which the operator manages its resources.")
publicNamespace = flag.String("public-namespace", operator.DefaultPublicNamespace,
"Namespace in which the operator reads user-provided resources.")
tlsCert = flag.String("tls-cert-base64", "", "The base64-encoded TLS certificate.")
tlsKey = flag.String("tls-key-base64", "", "The base64-encoded TLS key.")
caCert = flag.String("ca-cert-base64", "", "The base64-encoded certificate authority.")
certDir = flag.String("cert-dir", defaultTLSDir, "The directory which contains TLS certificates for webhook server.")
webhookAddr = flag.String("webhook-addr", ":10250",
"Address to listen to for incoming kube admission webhook connections.")
probeAddr = flag.String("probe-addr", ":18081", "Address to outputs probe statuses (e.g. /readyz and /healthz)")
metricsAddr = flag.String("metrics-addr", ":18080", "Address to emit metrics on.")
// Permit the operator to cleanup previously-managed resources that
// are missing the provided annotation. An empty string disables this
// feature.
cleanupAnnotKey = flag.String("cleanup-unless-annotation-key", "",
"Clean up operator-managed workloads without the provided annotation key.")
)
flag.Parse()
logger := zap.New(zap.Level(zapcore.Level(-*logVerbosity)))
ctrl.SetLogger(logger)
if err := errors.Join(errList...); err != nil {
logger.Error(err, "unable to fetch Google Cloud metadata")
}
cfg, err := ctrl.GetConfig()
if err != nil {
logger.Error(err, "loading kubeconfig failed")
os.Exit(1)
}
// controller-runtime creates a registry against which its metrics are registered globally.
// Using it as our non-global registry is the easiest way to combine metrics into a single
// /metrics endpoint.
// It already has the GoCollector and ProcessCollector metrics installed.
metrics := ctrlmetrics.Registry
metrics.MustRegister(versioninfo.NewCollector("operator")) // Add build_info metric.
op, err := operator.New(logger, cfg, operator.Options{
ProjectID: *projectID,
Location: *location,
Cluster: *cluster,
OperatorNamespace: *operatorNamespace,
PublicNamespace: *publicNamespace,
ProbeAddr: *probeAddr,
TLSCert: *tlsCert,
TLSKey: *tlsKey,
CACert: *caCert,
CertDir: *certDir,
ListenAddr: *webhookAddr,
CleanupAnnotKey: *cleanupAnnotKey,
})
if err != nil {
logger.Error(err, "instantiating operator failed")
os.Exit(1)
}
var g run.Group
// Termination handler.
{
term := make(chan os.Signal, 1)
cancel := make(chan struct{})
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
g.Add(
func() error {
select {
case <-term:
logger.Info("received SIGTERM, exiting gracefully...")
case <-cancel:
}
return nil
},
func(error) {
close(cancel)
},
)
}
// Operator monitoring.
{
server := &http.Server{Addr: *metricsAddr}
http.Handle("/metrics", promhttp.HandlerFor(metrics, promhttp.HandlerOpts{Registry: metrics}))
g.Add(func() error {
return server.ListenAndServe()
}, func(error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
if err := server.Shutdown(ctx); err != nil {
logger.Error(err, "Server failed to shut down gracefully.")
}
cancel()
})
}
// Main operator loop.
{
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return op.Run(ctx, metrics)
}, func(error) {
cancel()
})
}
if err := g.Run(); err != nil {
logger.Error(err, "exit with error")
os.Exit(1)
}
}