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