Skip to content

Commit d517737

Browse files
feat(*): automate signed certificates (#81)
1 parent fc98614 commit d517737

File tree

33 files changed

+1228
-369
lines changed

33 files changed

+1228
-369
lines changed

generate/.conform.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ tasks:
3030
COPY --from=dianemo/kernel:{{ .Docker.Image.Tag }} /tmp/vmlinuz /rootfs/boot/vmlinuz
3131
COPY --from=dianemo/kernel:{{ .Docker.Image.Tag }} /tmp/lib/modules /rootfs/lib/modules
3232
COPY --from=dianemo/initramfs:{{ .Docker.Image.Tag }} /tmp/osd /rootfs/bin/osd
33+
COPY --from=dianemo/initramfs:{{ .Docker.Image.Tag }} /tmp/rotd /rootfs/bin/rotd
3334
COPY --from=dianemo/initramfs:{{ .Docker.Image.Tag }} /tmp/initramfs.xz /rootfs/boot/initramfs.xz
3435
COPY --from=dianemo/initramfs:{{ .Docker.Image.Tag }} /tmp/init /rootfs/bin/init
3536
WORKDIR /rootfs

initramfs/.conform.yaml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,35 @@ pipeline:
77
stages:
88
build:
99
artifacts:
10-
- source: /tmp/osctl
11-
destination: ../build/osctl
10+
- source: /tmp/osctl-linux-amd64
11+
destination: ../build/osctl-linux-amd64
12+
- source: /tmp/osctl-darwin-amd64
13+
destination: ../build/osctl-darwin-amd64
1214
tasks:
1315
- src
1416
- test
1517
- osd
18+
- rotd
1619
- osctl
1720
- init
1821
- image
1922
generate:
2023
artifacts:
21-
- source: /go/src/github.com/autonomy/dianemo/proto
24+
- source: /go/src/github.com/autonomy/dianemo/initramfs/cmd/osd/proto
2225
destination: ./cmd/osd
26+
- source: /go/src/github.com/autonomy/dianemo/initramfs/cmd/rotd/proto
27+
destination: ./cmd/rotd
2328
tasks:
2429
- proto
2530
tasks:
2631
image:
2732
template: |
2833
FROM scratch
2934
WORKDIR /tmp
30-
COPY --from=src /osctl osctl
35+
COPY --from=src /osctl-linux-amd64 osctl-linux-amd64
36+
COPY --from=src /osctl-darwin-amd64 osctl-darwin-amd64
3137
COPY --from=src /osd osd
38+
COPY --from=src /rotd rotd
3239
COPY --from=src /initramfs/init init
3340
COPY --from=src /initramfs/initramfs.xz initramfs.xz
3441
CMD false
@@ -42,26 +49,36 @@ tasks:
4249
osctl:
4350
template: |
4451
WORKDIR $GOPATH/src/github.com/autonomy/dianemo/initramfs/cmd/{{ .Docker.CurrentStage }}
45-
RUN GOOS=darwin go build -o /{{ .Docker.CurrentStage }}
46-
RUN chmod +x /{{ .Docker.CurrentStage }}
52+
RUN GOOS=linux GOARCH=amd64 go build -o /{{ .Docker.CurrentStage }}-linux-amd64
53+
RUN chmod +x /{{ .Docker.CurrentStage }}-linux-amd64
54+
RUN GOOS=darwin GOARCH=amd64 go build -o /{{ .Docker.CurrentStage }}-darwin-amd64
55+
RUN chmod +x /{{ .Docker.CurrentStage }}-darwin-amd64
4756
osd:
4857
template: |
4958
WORKDIR $GOPATH/src/github.com/autonomy/dianemo/initramfs/cmd/{{ .Docker.CurrentStage }}
5059
RUN go build -o /{{ .Docker.CurrentStage }}
5160
RUN chmod +x /{{ .Docker.CurrentStage }}
61+
rotd:
62+
template: |
63+
WORKDIR $GOPATH/src/github.com/autonomy/dianemo/initramfs/cmd/{{ .Docker.CurrentStage }}
64+
RUN go build -o /{{ .Docker.CurrentStage }}
65+
RUN chmod +x /{{ .Docker.CurrentStage }}
5266
proto:
5367
template: |
5468
FROM golang:1.10.0 AS {{ .Docker.CurrentStage }}
55-
WORKDIR /go/src/github.com/autonomy/dianemo
5669
RUN apt-get update
5770
RUN apt-get -y install bsdtar
5871
RUN go get github.com/golang/protobuf/protoc-gen-go
5972
RUN curl -L https://github.com/google/protobuf/releases/download/v3.5.1/protoc-3.5.1-linux-x86_64.zip | bsdtar -xf - -C /tmp \
6073
&& mv /tmp/bin/protoc /bin \
6174
&& mv /tmp/include/* /usr/local/include \
6275
&& chmod +x /bin/protoc
76+
WORKDIR $GOPATH/src/github.com/autonomy/dianemo/initramfs/cmd/osd
6377
COPY ./cmd/osd/proto ./proto
6478
RUN protoc -I/usr/local/include -I./proto --go_out=plugins=grpc:proto proto/api.proto
79+
WORKDIR $GOPATH/src/github.com/autonomy/dianemo/initramfs/cmd/rotd
80+
COPY ./cmd/rotd/proto ./proto
81+
RUN protoc -I/usr/local/include -I./proto --go_out=plugins=grpc:proto proto/api.proto
6582
src:
6683
template: |
6784
FROM dianemo/tools:{{ .Docker.Image.Tag }} AS {{ .Docker.CurrentStage }}

initramfs/cmd/init/main.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@ import (
1414
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/rootfs"
1515
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/service"
1616
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/switchroot"
17-
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/userdata"
17+
"github.com/autonomy/dianemo/initramfs/pkg/userdata"
1818
)
1919

2020
var (
2121
switchRoot *bool
2222
)
2323

24-
func hang() {
25-
if rec := recover(); rec != nil {
26-
err, ok := rec.(error)
27-
if ok {
28-
log.Printf("%s\n", err.Error())
29-
}
24+
func recovery() {
25+
if r := recover(); r != nil {
26+
log.Printf("recovered from: %v\n", r)
3027
}
31-
// Hang forever to avoid a kernel panic.
32-
select {}
3328
}
3429

3530
func init() {
@@ -44,23 +39,28 @@ func init() {
4439

4540
func initram() (err error) {
4641
// Read the block devices and populate the mount point definitions.
42+
log.Println("initializing mount points")
4743
if err = mount.Init(constants.NewRoot); err != nil {
4844
return
4945
}
5046
// Download the user data.
47+
log.Println("downloading the user data")
5148
data, err := userdata.Download()
5249
if err != nil {
5350
return
5451
}
5552
// Prepare the necessary files in the rootfs.
53+
log.Println("preparing the root filesystem")
5654
if err = rootfs.Prepare(constants.NewRoot, data); err != nil {
5755
return
5856
}
5957
// Unmount the ROOT and DATA block devices.
58+
log.Println("unmounting the ROOT and DATA partitions")
6059
if err = mount.Unmount(); err != nil {
6160
return
6261
}
6362
// Perform the equivalent of switch_root.
63+
log.Println("entering the new root")
6464
if err = switchroot.Switch(constants.NewRoot); err != nil {
6565
return
6666
}
@@ -69,20 +69,26 @@ func initram() (err error) {
6969
}
7070

7171
func root() (err error) {
72-
// Download the user data.
73-
data, err := userdata.Download()
72+
// Read the user data.
73+
log.Println("reading the user data")
74+
data, err := userdata.Open(constants.UserDataPath)
7475
if err != nil {
7576
return
7677
}
7778

7879
services := &service.Manager{
79-
UserData: data,
80+
UserData: *data,
8081
}
8182

82-
// Start the OSD gRPC service.
83+
// Start the services essential to managing the node.
84+
log.Println("starting OS services")
8385
services.Start(&service.OSD{})
86+
if data.Kubernetes.Init {
87+
services.Start(&service.ROTD{})
88+
}
8489

8590
// Start the services essential to running Kubernetes.
91+
log.Println("starting Kubernetes services")
8692
switch data.Kubernetes.ContainerRuntime {
8793
case constants.ContainerRuntimeDocker:
8894
services.Start(&service.Docker{})
@@ -98,17 +104,19 @@ func root() (err error) {
98104
}
99105

100106
func main() {
101-
defer hang()
107+
defer recovery()
102108

103109
if *switchRoot {
104110
if err := root(); err != nil {
105111
panic(err)
106112
}
113+
select {}
107114
}
108115

109116
if err := initram(); err != nil {
110117
panic(err)
111118
}
112119

120+
// We should only reach this point if something within initram() fails.
113121
select {}
114122
}

initramfs/cmd/init/pkg/etc/etc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"path"
88
"text/template"
99

10-
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/userdata"
10+
"github.com/autonomy/dianemo/initramfs/pkg/userdata"
1111
)
1212

1313
const hostsTemplate = `

initramfs/cmd/init/pkg/mount/mount.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package mount
44

55
import (
66
"fmt"
7-
"log"
87
"os"
98
"path"
109
"sync"
@@ -165,7 +164,6 @@ func mountSpecialDevices() (err error) {
165164
if err = unix.Mount(mountpoint.source, mountpoint.target, mountpoint.fstype, mountpoint.flags, mountpoint.data); err != nil {
166165
return fmt.Errorf("mount %s: %s", mountpoint.target, err.Error())
167166
}
168-
log.Printf("mounted %s", mountpoint.target)
169167
}
170168

171169
return nil
@@ -199,8 +197,6 @@ func mountBlockDevices(s string) (err error) {
199197
return fmt.Errorf("mount %s: %s", mountpoint.target, err.Error())
200198
}
201199

202-
log.Printf("mounted %s", mountpoint.target)
203-
204200
instance.blockdevices[b.LABEL] = mountpoint
205201
}
206202

initramfs/cmd/init/pkg/rootfs/rootfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/constants"
1010
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/etc"
11-
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/userdata"
11+
"github.com/autonomy/dianemo/initramfs/pkg/userdata"
1212
yaml "gopkg.in/yaml.v2"
1313
)
1414

initramfs/cmd/init/pkg/service/crio.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"io/ioutil"
66

77
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/service/conditions"
8-
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/userdata"
8+
"github.com/autonomy/dianemo/initramfs/pkg/userdata"
99
)
1010

1111
const crioConf = `

initramfs/cmd/init/pkg/service/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package service
22

33
import (
44
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/service/conditions"
5-
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/userdata"
5+
"github.com/autonomy/dianemo/initramfs/pkg/userdata"
66
)
77

88
// Docker implements the Service interface. It serves as the concrete type with

initramfs/cmd/init/pkg/service/kubeadm.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package service
22

33
import (
4-
"encoding/base64"
54
"fmt"
65
"io/ioutil"
76
"os"
87
"path"
98

109
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/constants"
1110
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/service/conditions"
12-
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/userdata"
11+
"github.com/autonomy/dianemo/initramfs/pkg/userdata"
1312
)
1413

1514
// Kubeadm implements the Service interface. It serves as the concrete type with
@@ -78,26 +77,18 @@ func writeKubeadmManifest(data string) (err error) {
7877
return nil
7978
}
8079

81-
func writeKubeadmPKIFiles(data *userdata.CertificateAndKeyPaths) (err error) {
82-
caCrtBytes, err := base64.StdEncoding.DecodeString(data.Crt)
83-
if err != nil {
84-
return err
85-
}
80+
func writeKubeadmPKIFiles(data *userdata.PEMEncodedCertificateAndKey) (err error) {
8681
if err = os.MkdirAll(path.Dir(constants.KubeadmCACert), 0600); err != nil {
8782
return err
8883
}
89-
if err = ioutil.WriteFile(constants.KubeadmCACert, caCrtBytes, 0400); err != nil {
84+
if err = ioutil.WriteFile(constants.KubeadmCACert, data.Crt, 0400); err != nil {
9085
return fmt.Errorf("write %s: %s", constants.KubeadmCACert, err.Error())
9186
}
9287

93-
caKeyBytes, err := base64.StdEncoding.DecodeString(data.Key)
94-
if err != nil {
95-
return err
96-
}
9788
if err = os.MkdirAll(path.Dir(constants.KubeadmCAKey), 0600); err != nil {
9889
return err
9990
}
100-
if err = ioutil.WriteFile(constants.KubeadmCAKey, caKeyBytes, 0400); err != nil {
91+
if err = ioutil.WriteFile(constants.KubeadmCAKey, data.Key, 0400); err != nil {
10192
return fmt.Errorf("write %s: %s", constants.KubeadmCAKey, err.Error())
10293
}
10394

initramfs/cmd/init/pkg/service/kubelet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/constants"
88
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/service/conditions"
9-
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/userdata"
9+
"github.com/autonomy/dianemo/initramfs/pkg/userdata"
1010
)
1111

1212
// Kubelet implements the Service interface. It serves as the concrete type with

0 commit comments

Comments
 (0)