-
Notifications
You must be signed in to change notification settings - Fork 328
/
start.go
315 lines (281 loc) · 12 KB
/
start.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package cmd
import (
"context"
"crypto/tls"
"encoding/base64"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"regexp"
"syscall"
"time"
hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1"
"github.com/openshift/hypershift/hypershift-operator/controllers/nodepool"
"github.com/openshift/hypershift/ignition-server/controllers"
"github.com/openshift/hypershift/pkg/version"
hyperapi "github.com/openshift/hypershift/support/api"
"github.com/openshift/hypershift/support/releaseinfo"
"github.com/openshift/hypershift/support/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cobra"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
const namespaceEnvVariableName = "MY_NAMESPACE"
var (
// We only match /ignition
ignPathPattern = regexp.MustCompile("^/ignition[^/ ]*$")
payloadStore = controllers.NewPayloadStore()
getRequestsPerNodePool = prometheus.NewCounterVec(prometheus.CounterOpts{Name: "ign_server_get_request"}, []string{"nodePool"})
TokenSecretIgnitionReachedAnnotation = "hypershift.openshift.io/ignition-reached"
)
func init() {
metrics.Registry.MustRegister(
getRequestsPerNodePool,
)
}
type Options struct {
Addr string
CertFile string
KeyFile string
RegistryOverrides map[string]string
Platform string
WorkDir string
MetricsAddr string
FeatureGateManifest string
}
// This is a https server that enable us to satisfy
// 1 - 1 relation between clusters and ign endpoints.
// It runs a token Secret controller.
// The token Secret controller uses an IgnitionProvider provider implementation
// (e.g. machineConfigServerIgnitionProvider) to keep up to date a payload store in memory.
// The payload store has the structure "NodePool token": "payload".
// A token represents a given cluster version (and in the future also a machine Config) at any given point in time.
// For a request to succeed a token needs to be passed in the Header.
func NewStartCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "ignition-server",
Short: "Starts the ignition server",
}
opts := Options{
Addr: "0.0.0.0:9090",
MetricsAddr: "0.0.0.0:8080",
CertFile: "/var/run/secrets/ignition/serving-cert/tls.crt",
KeyFile: "/var/run/secrets/ignition/serving-cert/tls.key",
WorkDir: "/payloads",
RegistryOverrides: map[string]string{},
}
cmd.Flags().StringVar(&opts.Addr, "addr", opts.Addr, "Listen address")
cmd.Flags().StringVar(&opts.CertFile, "cert-file", opts.CertFile, "Path to the serving cert")
cmd.Flags().StringVar(&opts.KeyFile, "key-file", opts.KeyFile, "Path to the serving key")
cmd.Flags().StringToStringVar(&opts.RegistryOverrides, "registry-overrides", map[string]string{}, "registry-overrides contains the source registry string as a key and the destination registry string as value. Images before being applied are scanned for the source registry string and if found the string is replaced with the destination registry string. Format is: sr1=dr1,sr2=dr2")
cmd.Flags().StringVar(&opts.Platform, "platform", "", "The cloud provider platform name")
cmd.Flags().StringVar(&opts.WorkDir, "work-dir", opts.WorkDir, "Directory in which to store transient working data")
cmd.Flags().StringVar(&opts.MetricsAddr, "metrics-addr", opts.MetricsAddr, "The address the metric endpoint binds to.")
cmd.Flags().StringVar(&opts.FeatureGateManifest, "feature-gate-manifest", opts.FeatureGateManifest, "Path to a rendered featuregates.config.openshift.io/v1 file")
cmd.Run = func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
go func() {
<-sigs
cancel()
}()
// TODO: Add an fsnotify watcher to cancel the context and trigger a restart
// if any of the secret data has changed.
if err := run(ctx, opts); err != nil {
log.Fatal(err)
}
}
return cmd
}
// setUpPayloadStoreReconciler sets up manager with a TokenSecretReconciler controller
// to keep the PayloadStore up to date.
func setUpPayloadStoreReconciler(ctx context.Context, registryOverrides map[string]string, cloudProvider hyperv1.PlatformType, cacheDir string, metricsAddr string, featureGateManifest string) (ctrl.Manager, error) {
if os.Getenv(namespaceEnvVariableName) == "" {
return nil, fmt.Errorf("environment variable %s is empty, this is not supported", namespaceEnvVariableName)
}
restConfig := ctrl.GetConfigOrDie()
restConfig.UserAgent = "ignition-server-manager"
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: hyperapi.Scheme,
WebhookServer: webhook.NewServer(webhook.Options{
Port: 9443,
}),
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{os.Getenv(namespaceEnvVariableName): {}},
},
})
if err != nil {
return nil, fmt.Errorf("unable to start manager: %w", err)
}
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
return nil, fmt.Errorf("unable to set up health check: %w", err)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
return nil, fmt.Errorf("unable to set up ready check: %w", err)
}
imageFileCache, err := controllers.NewImageFileCache(cacheDir)
if err != nil {
return nil, fmt.Errorf("unable to create image file cache: %w", err)
}
imageMetaDataProvider := &util.RegistryClientImageMetadataProvider{
OpenShiftImageRegistryOverrides: util.ConvertImageRegistryOverrideStringToMap(os.Getenv("OPENSHIFT_IMG_OVERRIDES")),
}
if err = (&controllers.TokenSecretReconciler{
Client: mgr.GetClient(),
PayloadStore: payloadStore,
IgnitionProvider: &controllers.LocalIgnitionProvider{
ReleaseProvider: &releaseinfo.ProviderWithOpenShiftImageRegistryOverridesDecorator{
Delegate: &releaseinfo.RegistryMirrorProviderDecorator{
Delegate: &releaseinfo.CachedProvider{
Inner: &releaseinfo.RegistryClientProvider{},
Cache: map[string]*releaseinfo.ReleaseImage{},
},
RegistryOverrides: registryOverrides,
},
OpenShiftImageRegistryOverrides: util.ConvertImageRegistryOverrideStringToMap(os.Getenv("OPENSHIFT_IMG_OVERRIDES")),
},
Client: mgr.GetClient(),
Namespace: os.Getenv(namespaceEnvVariableName),
CloudProvider: cloudProvider,
WorkDir: cacheDir,
ImageFileCache: imageFileCache,
FeatureGateManifest: featureGateManifest,
ImageMetadataProvider: imageMetaDataProvider,
},
}).SetupWithManager(ctx, mgr); err != nil {
return nil, fmt.Errorf("unable to create controller: %w", err)
}
return mgr, nil
}
func run(ctx context.Context, opts Options) error {
logger := zap.New(zap.UseDevMode(true), zap.JSONEncoder(func(o *zapcore.EncoderConfig) {
o.EncodeTime = zapcore.RFC3339TimeEncoder
}))
ctrl.SetLogger(logger)
logger.Info("Starting ignition-server", "version", version.String())
certWatcher, err := certwatcher.New(opts.CertFile, opts.KeyFile)
if err != nil {
return fmt.Errorf("failed to load serving cert: %w", err)
}
mgr, err := setUpPayloadStoreReconciler(ctx, opts.RegistryOverrides, hyperv1.PlatformType(opts.Platform), opts.WorkDir, opts.MetricsAddr, opts.FeatureGateManifest)
if err != nil {
return fmt.Errorf("error setting up manager: %w", err)
}
if err := mgr.Add(certWatcher); err != nil {
return fmt.Errorf("failed to add certWatcher to manager: %w", err)
}
go mgr.Start(ctx)
mgr.GetLogger().Info("Using opts", "opts", fmt.Sprintf("%+v", opts))
eventRecorder := mgr.GetEventRecorderFor("ignition-server")
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("User Agent: %s. Requested: %s", r.Header.Get("User-Agent"), r.URL.Path)
tokenSecret := nodepool.TokenSecret(os.Getenv(namespaceEnvVariableName),
util.ParseNamespacedName(r.Header.Get("NodePool")).Name,
r.Header.Get("TargetConfigVersionHash"))
if !ignPathPattern.MatchString(r.URL.Path) {
// No pattern matched; send 404 response.
log.Printf("Path not found: %s", r.URL.Path)
http.NotFound(w, r)
return
}
// Authorize the request against the token
const bearerPrefix = "Bearer "
auth := r.Header.Get("Authorization")
n := len(bearerPrefix)
if len(auth) < n || auth[:n] != bearerPrefix {
log.Printf("Invalid Authorization header value prefix")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
eventRecorder.Event(tokenSecret, corev1.EventTypeWarning, "GetPayloadFailed", "Bad header")
return
}
encodedToken := auth[n:]
decodedToken, err := base64.StdEncoding.DecodeString(encodedToken)
if err != nil {
log.Printf("Invalid token value")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
eventRecorder.Event(tokenSecret, corev1.EventTypeWarning, "GetPayloadFailed", "Token invalid")
return
}
value, ok := payloadStore.Get(string(decodedToken))
if !ok {
// We return a 5xx here to give ignition the chance to backoff and retry if the machine request happens
// before the content is cached for this token.
// https://coreos.github.io/ignition/operator-notes/#http-backoff-and-retry
log.Printf("Token not found")
http.Error(w, "Token not found", http.StatusNetworkAuthenticationRequired)
eventRecorder.Event(tokenSecret, corev1.EventTypeWarning, "GetPayloadFailed", "Token not found in cache")
return
}
if err := util.SanitizeIgnitionPayload(value.Payload); err != nil {
log.Printf("Invalid ignition payload: %s", err)
http.Error(w, "Invalid ignition payload", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(value.Payload)
eventRecorder.Event(tokenSecret, corev1.EventTypeNormal, "GetPayload", "")
getRequestsPerNodePool.WithLabelValues(r.Header.Get("NodePool")).Inc()
// Annotate tokenSecret so NodePool controller can set a conditions based on it.
if err := mgr.GetClient().Get(ctx, client.ObjectKeyFromObject(tokenSecret), tokenSecret); err != nil {
log.Printf("Failed to get tokenSecret resource: %q: %s", client.ObjectKeyFromObject(tokenSecret).String(), err)
} else {
tokenSecret.Annotations[TokenSecretIgnitionReachedAnnotation] = "True"
if err := mgr.GetClient().Update(ctx, tokenSecret); err != nil {
log.Printf("Failed to update tokenSecret: %q: %s", tokenSecret.Name, err)
}
}
})
mux.HandleFunc("/healthz", func(http.ResponseWriter, *http.Request) {})
server := http.Server{
Addr: opts.Addr,
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
TLSConfig: &tls.Config{GetCertificate: certWatcher.GetCertificate,
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
//TLS 1.3 ciphers from openshift tls modern profile
tls.TLS_AES_128_GCM_SHA256,
tls.TLS_AES_256_GCM_SHA384,
tls.TLS_CHACHA20_POLY1305_SHA256,
//TLS 1.2 subset from openshift intermediate tls profile
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
},
},
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
go func() {
<-ctx.Done()
if err := server.Shutdown(ctx); err != nil {
log.Printf("error shutting down server: %s", err)
}
}()
log.Printf("Listening on %s", opts.Addr)
if err := server.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}