blob: ac21588f0887d220b247d81e6e9b6ac1beea60d9 [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 (
20 "encoding/pem"
21 "fmt"
22 "os"
23 "os/exec"
24
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010025 "go.etcd.io/etcd/clientv3"
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010026
27 "git.monogon.dev/source/nexantic.git/core/pkg/fileargs"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010028)
29
30type schedulerConfig struct {
31 kubeConfig []byte
32 serverCert []byte
33 serverKey []byte
34}
35
36func getPKISchedulerConfig(consensusKV clientv3.KV) (*schedulerConfig, error) {
37 var config schedulerConfig
38 var err error
39 config.serverCert, config.serverKey, err = getCert(consensusKV, "scheduler")
40 if err != nil {
41 return nil, fmt.Errorf("failed to get scheduler serving certificate: %w", err)
42 }
43 config.kubeConfig, err = getSingle(consensusKV, "scheduler.kubeconfig")
44 if err != nil {
45 return nil, fmt.Errorf("failed to get scheduler kubeconfig: %w", err)
46 }
47 return &config, nil
48}
49
50func runScheduler(config schedulerConfig) error {
51 args, err := fileargs.New()
52 if err != nil {
53 panic(err) // If this fails, something is very wrong. Just crash.
54 }
55 defer args.Close()
56 cmd := exec.Command("/bin/kube-controlplane", "kube-scheduler",
57 args.FileOpt("--kubeconfig", "kubeconfig", config.kubeConfig),
58 "--port=0", // Kill insecure serving
59 args.FileOpt("--tls-cert-file", "server-cert.pem",
60 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.serverCert})),
61 args.FileOpt("--tls-private-key-file", "server-key.pem",
62 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.serverKey})),
63 )
64 if args.Error() != nil {
65 return fmt.Errorf("failed to use fileargs: %w", err)
66 }
67 cmd.Stdout = os.Stdout
68 cmd.Stderr = os.Stderr
69 return cmd.Run()
70}