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