blob: a6b4b5d7d08fce4283901c80c8fabf0a3b53ee22 [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 "net"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010025 "os/exec"
26
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010027 "go.etcd.io/etcd/clientv3"
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010028
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020029 "git.monogon.dev/source/nexantic.git/core/internal/common/supervisor"
Serge Bazanski5a091422020-06-22 14:01:45 +020030 "git.monogon.dev/source/nexantic.git/core/internal/kubernetes/pki"
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010031 "git.monogon.dev/source/nexantic.git/core/pkg/fileargs"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010032)
33
34type controllerManagerConfig struct {
35 clusterNet net.IPNet
36 // All PKI-related things are in DER
37 kubeConfig []byte
38 rootCA []byte
39 serviceAccountPrivKey []byte // In PKCS#8 form
40 serverCert []byte
41 serverKey []byte
42}
43
44func getPKIControllerManagerConfig(consensusKV clientv3.KV) (*controllerManagerConfig, error) {
45 var config controllerManagerConfig
46 var err error
Serge Bazanski5a091422020-06-22 14:01:45 +020047 config.rootCA, _, err = pki.GetCert(consensusKV, "id-ca")
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010048 if err != nil {
49 return nil, fmt.Errorf("failed to get ID root CA: %w", err)
50 }
Serge Bazanski5a091422020-06-22 14:01:45 +020051 config.serverCert, config.serverKey, err = pki.GetCert(consensusKV, "controller-manager")
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010052 if err != nil {
53 return nil, fmt.Errorf("failed to get controller-manager serving certificate: %w", err)
54 }
Serge Bazanski5a091422020-06-22 14:01:45 +020055 config.serviceAccountPrivKey, err = pki.GetSingle(consensusKV, "service-account-privkey.der")
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010056 if err != nil {
57 return nil, fmt.Errorf("failed to get serviceaccount privkey: %w", err)
58 }
Serge Bazanski5a091422020-06-22 14:01:45 +020059 config.kubeConfig, err = pki.GetSingle(consensusKV, "controller-manager.kubeconfig")
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010060 if err != nil {
61 return nil, fmt.Errorf("failed to get controller-manager kubeconfig: %w", err)
62 }
63 return &config, nil
64}
65
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020066func runControllerManager(config controllerManagerConfig, output io.Writer) supervisor.Runnable {
67 return func(ctx context.Context) error {
68 args, err := fileargs.New()
69 if err != nil {
70 panic(err) // If this fails, something is very wrong. Just crash.
71 }
72 defer args.Close()
73
74 cmd := exec.CommandContext(ctx, "/kubernetes/bin/kube", "kube-controller-manager",
75 args.FileOpt("--kubeconfig", "kubeconfig", config.kubeConfig),
76 args.FileOpt("--service-account-private-key-file", "service-account-privkey.pem",
77 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.serviceAccountPrivKey})),
78 args.FileOpt("--root-ca-file", "root-ca.pem",
79 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.rootCA})),
80 "--port=0", // Kill insecure serving
81 "--use-service-account-credentials=true", // Enables things like PSP enforcement
82 fmt.Sprintf("--cluster-cidr=%v", config.clusterNet.String()),
83 args.FileOpt("--tls-cert-file", "server-cert.pem",
84 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.serverCert})),
85 args.FileOpt("--tls-private-key-file", "server-key.pem",
86 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.serverKey})),
87 )
88 if args.Error() != nil {
89 return fmt.Errorf("failed to use fileargs: %w", err)
90 }
91 cmd.Stdout = output
92 cmd.Stderr = output
93 supervisor.Signal(ctx, supervisor.SignalHealthy)
94 err = cmd.Run()
95 fmt.Fprintf(output, "controller-manager stopped: %v\n", err)
96 return err
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010097 }
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010098}