/
is-running.go
162 lines (142 loc) · 5.36 KB
/
is-running.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package apiserver
import (
"encoding/json"
"errors"
"fmt"
httpproberapi "github.com/kubermatic/kubermatic/api/cmd/http-prober/api"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
"github.com/kubermatic/kubermatic/api/pkg/resources"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
tag = "v0.3.1"
emptyDirVolumeName = "http-prober-bin"
initContainerName = "copy-http-prober"
)
// IsRunningInitContainer returns a init container which will wait until the apiserver is reachable via its ClusterIP
type isRunningInitContainerData interface {
ImageRegistry(string) string
Cluster() *kubermaticv1.Cluster
}
// IsRunningWrapper wraps the named containers in the pod with a check if the API server is reachable.
// This is achieved by copying a `http-prober` binary via an init container into an emptyDir volume,
// then mounting that volume onto all named containers and replacing the command with a call to
// the `http-prober` binary. The http prober binary gets the original command as serialized string
// and does an syscall.Exec onto it once the apiserver became reachable
func IsRunningWrapper(data isRunningInitContainerData, spec corev1.PodSpec, containersToWrap sets.String, crdsToWaitFor ...string) (*corev1.PodSpec, error) {
if containersToWrap.Len() == 0 {
return nil, errors.New("no containers to wrap passed")
}
for _, containerToWrap := range containersToWrap.List() {
if !hasContainerNamed(spec, containerToWrap) {
return nil, fmt.Errorf("pod has no container named %q", containerToWrap)
}
}
var newVolumes []corev1.Volume
for _, volume := range spec.Volumes {
if volume.Name == emptyDirVolumeName {
continue
}
newVolumes = append(newVolumes, volume)
}
newVolumes = append(newVolumes, corev1.Volume{
Name: emptyDirVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
})
spec.Volumes = newVolumes
var newInitContainers []corev1.Container
for _, container := range spec.InitContainers {
if container.Name == initContainerName {
continue
}
newInitContainers = append(newInitContainers, container)
}
copyContainer := corev1.Container{
Name: initContainerName,
Image: data.ImageRegistry(resources.RegistryQuay) + "/kubermatic/http-prober:" + tag,
Command: []string{"/bin/cp", "/usr/local/bin/http-prober", "/http-prober-bin/http-prober"},
VolumeMounts: []corev1.VolumeMount{{
Name: emptyDirVolumeName,
MountPath: "/http-prober-bin",
}},
}
// We must come first in case an initContainer gets wrapped
spec.InitContainers = append([]corev1.Container{copyContainer}, newInitContainers...)
for idx := range spec.InitContainers {
if !containersToWrap.Has(spec.InitContainers[idx].Name) {
continue
}
wrappedContainer, err := wrapContainer(data, spec.InitContainers[idx], crdsToWaitFor...)
if err != nil {
return nil, fmt.Errorf("failed to wrap initContainer %q: %v", spec.InitContainers[idx].Name, err)
}
spec.InitContainers[idx] = *wrappedContainer
}
for idx := range spec.Containers {
if !containersToWrap.Has(spec.Containers[idx].Name) {
continue
}
wrappedContainer, err := wrapContainer(data, spec.Containers[idx], crdsToWaitFor...)
if err != nil {
return nil, fmt.Errorf("failed to wrap container %q: %v", spec.Containers[idx].Name, err)
}
spec.Containers[idx] = *wrappedContainer
}
return &spec, nil
}
func hasContainerNamed(spec corev1.PodSpec, name string) bool {
for _, container := range append(spec.InitContainers, spec.Containers...) {
if container.Name == name {
return true
}
}
return false
}
func wrapContainer(data isRunningInitContainerData, container corev1.Container, crdsToWaitFor ...string) (*corev1.Container, error) {
commandWithArgs := append(container.Command, container.Args...)
if len(commandWithArgs) == 0 {
return nil, fmt.Errorf("container %q has no command or args set", container.Name)
}
command := httpproberapi.Command{
Command: commandWithArgs[0],
}
if len(commandWithArgs) > 1 {
command.Args = commandWithArgs[1:]
}
serializedCommand, err := json.Marshal(command)
if err != nil {
return nil, fmt.Errorf("failed to serialize command: %v", err)
}
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: emptyDirVolumeName,
MountPath: "/http-prober-bin",
})
container.Command = []string{"/http-prober-bin/http-prober"}
container.Args = []string{
"-endpoint", fmt.Sprintf("https://%s:%d/healthz", data.Cluster().Address.InternalName, data.Cluster().Address.Port),
"-insecure",
"-retries", "100",
"-retry-wait", "2",
"-timeout", "1",
"-command", string(serializedCommand),
}
for _, crdToWaitFor := range crdsToWaitFor {
container.Args = append(container.Args, "--crd-to-wait-for", crdToWaitFor)
}
return &container, nil
}