blob: 1b9b12cac6cff870712df6391c526bf80a538b5b [file] [log] [blame]
Lorenz Brun6e8f69c2019-11-18 10:44:24 +01001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package kubernetes
18
19import (
Lorenz Brun878f5f92020-05-12 16:15:39 +020020 "context"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010021 "encoding/pem"
22 "fmt"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010023 "os/exec"
24
Serge Bazanski31370b02021-01-07 16:31:14 +010025 "source.monogon.dev/metropolis/node/kubernetes/pki"
26 "source.monogon.dev/metropolis/pkg/fileargs"
27 "source.monogon.dev/metropolis/pkg/supervisor"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010028)
29
30type schedulerConfig struct {
31 kubeConfig []byte
32 serverCert []byte
33 serverKey []byte
Tim Windelschmidt90613af2023-07-20 14:26:18 +020034 rootCA []byte
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010035}
36
Serge Bazanski9411f7c2021-03-10 13:12:53 +010037func getPKISchedulerConfig(ctx context.Context, kpki *pki.PKI) (*schedulerConfig, error) {
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010038 var config schedulerConfig
39 var err error
Tim Windelschmidt90613af2023-07-20 14:26:18 +020040 config.rootCA, _, err = kpki.Certificate(ctx, pki.IdCA)
41 if err != nil {
42 return nil, fmt.Errorf("failed to get ID root CA: %w", err)
43 }
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020044 config.serverCert, config.serverKey, err = kpki.Certificate(ctx, pki.Scheduler)
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010045 if err != nil {
46 return nil, fmt.Errorf("failed to get scheduler serving certificate: %w", err)
47 }
Serge Bazanskie88ffe92023-03-21 13:38:46 +010048 config.kubeConfig, err = kpki.Kubeconfig(ctx, pki.SchedulerClient, pki.KubernetesAPIEndpointForController)
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010049 if err != nil {
50 return nil, fmt.Errorf("failed to get scheduler kubeconfig: %w", err)
51 }
52 return &config, nil
53}
54
Serge Bazanski967be212020-11-02 11:26:59 +010055func runScheduler(config schedulerConfig) supervisor.Runnable {
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020056 return func(ctx context.Context) error {
57 args, err := fileargs.New()
58 if err != nil {
59 panic(err) // If this fails, something is very wrong. Just crash.
60 }
61 defer args.Close()
62 cmd := exec.CommandContext(ctx, "/kubernetes/bin/kube", "kube-scheduler",
63 args.FileOpt("--kubeconfig", "kubeconfig", config.kubeConfig),
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020064 args.FileOpt("--tls-cert-file", "server-cert.pem",
65 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.serverCert})),
66 args.FileOpt("--tls-private-key-file", "server-key.pem",
67 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.serverKey})),
Tim Windelschmidt90613af2023-07-20 14:26:18 +020068 args.FileOpt("--client-ca-file", "root-ca.pem",
69 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.rootCA})),
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020070 )
71 if args.Error() != nil {
72 return fmt.Errorf("failed to use fileargs: %w", err)
73 }
Serge Bazanski05604292021-03-12 17:47:21 +010074 return supervisor.RunCommand(ctx, cmd, supervisor.ParseKLog())
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010075 }
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010076}