blob: 75dea971e53cdac63b75907a14806e22c3272569 [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
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010025 "go.etcd.io/etcd/clientv3"
Lorenz Brun878f5f92020-05-12 16:15:39 +020026 "go.uber.org/zap"
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010027
28 "git.monogon.dev/source/nexantic.git/core/pkg/fileargs"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010029)
30
31type schedulerConfig struct {
32 kubeConfig []byte
33 serverCert []byte
34 serverKey []byte
35}
36
37func getPKISchedulerConfig(consensusKV clientv3.KV) (*schedulerConfig, error) {
38 var config schedulerConfig
39 var err error
40 config.serverCert, config.serverKey, err = getCert(consensusKV, "scheduler")
41 if err != nil {
42 return nil, fmt.Errorf("failed to get scheduler serving certificate: %w", err)
43 }
44 config.kubeConfig, err = getSingle(consensusKV, "scheduler.kubeconfig")
45 if err != nil {
46 return nil, fmt.Errorf("failed to get scheduler kubeconfig: %w", err)
47 }
48 return &config, nil
49}
50
Lorenz Brun878f5f92020-05-12 16:15:39 +020051func (s *Service) runScheduler(ctx context.Context, config schedulerConfig) error {
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010052 args, err := fileargs.New()
53 if err != nil {
54 panic(err) // If this fails, something is very wrong. Just crash.
55 }
56 defer args.Close()
Lorenz Brun878f5f92020-05-12 16:15:39 +020057 cmd := exec.CommandContext(ctx, "/kubernetes/bin/kube", "kube-scheduler",
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010058 args.FileOpt("--kubeconfig", "kubeconfig", config.kubeConfig),
59 "--port=0", // Kill insecure serving
60 args.FileOpt("--tls-cert-file", "server-cert.pem",
61 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.serverCert})),
62 args.FileOpt("--tls-private-key-file", "server-key.pem",
63 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.serverKey})),
64 )
65 if args.Error() != nil {
66 return fmt.Errorf("failed to use fileargs: %w", err)
67 }
Lorenz Brun878f5f92020-05-12 16:15:39 +020068 cmd.Stdout = s.schedulerLogs
69 cmd.Stderr = s.schedulerLogs
70 err = cmd.Run()
71 fmt.Fprintf(s.schedulerLogs, "scheduler stopped: %v\n", err)
72 if ctx.Err() == context.Canceled {
73 s.logger.Info("scheduler stopped", zap.Error(err))
74 } else {
75 s.logger.Warn("scheduler stopped unexpectedly", zap.Error(err))
76 }
77 return err
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010078}