blob: d3ee20b33258d2ea008e29c733ed8075c9e9c435 [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 Brun8e3b8fc2020-05-19 14:29:40 +020023 "io"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010024 "os/exec"
25
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010026 "go.etcd.io/etcd/clientv3"
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010027
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020028 "git.monogon.dev/source/nexantic.git/core/internal/common/supervisor"
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010029 "git.monogon.dev/source/nexantic.git/core/pkg/fileargs"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010030)
31
32type schedulerConfig struct {
33 kubeConfig []byte
34 serverCert []byte
35 serverKey []byte
36}
37
38func getPKISchedulerConfig(consensusKV clientv3.KV) (*schedulerConfig, error) {
39 var config schedulerConfig
40 var err error
Serge Bazanski71f7a562020-06-22 16:37:28 +020041 config.serverCert, config.serverKey, err = getCert(consensusKV, "scheduler")
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010042 if err != nil {
43 return nil, fmt.Errorf("failed to get scheduler serving certificate: %w", err)
44 }
Serge Bazanski71f7a562020-06-22 16:37:28 +020045 config.kubeConfig, err = getSingle(consensusKV, "scheduler.kubeconfig")
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010046 if err != nil {
47 return nil, fmt.Errorf("failed to get scheduler kubeconfig: %w", err)
48 }
49 return &config, nil
50}
51
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020052func runScheduler(config schedulerConfig, output io.Writer) supervisor.Runnable {
53 return func(ctx context.Context) error {
54 args, err := fileargs.New()
55 if err != nil {
56 panic(err) // If this fails, something is very wrong. Just crash.
57 }
58 defer args.Close()
59 cmd := exec.CommandContext(ctx, "/kubernetes/bin/kube", "kube-scheduler",
60 args.FileOpt("--kubeconfig", "kubeconfig", config.kubeConfig),
61 "--port=0", // Kill insecure serving
62 args.FileOpt("--tls-cert-file", "server-cert.pem",
63 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.serverCert})),
64 args.FileOpt("--tls-private-key-file", "server-key.pem",
65 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.serverKey})),
66 )
67 if args.Error() != nil {
68 return fmt.Errorf("failed to use fileargs: %w", err)
69 }
70 cmd.Stdout = output
71 cmd.Stderr = output
72 supervisor.Signal(ctx, supervisor.SignalHealthy)
73 err = cmd.Run()
74 fmt.Fprintf(output, "scheduler stopped: %v\n", err)
75 return err
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010076 }
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010077}