/
deployment_init_container.go
193 lines (171 loc) · 5.13 KB
/
deployment_init_container.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gateways
import (
"bytes"
"strconv"
"strings"
"text/template"
corev1 "k8s.io/api/core/v1"
meshv2beta1 "github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1"
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
"github.com/hashicorp/consul-k8s/control-plane/namespaces"
)
const (
injectInitContainerName = "consul-mesh-init"
initContainersUserAndGroupID = 5996
)
var tpl = template.Must(template.New("root").Parse(strings.TrimSpace(initContainerCommandTpl)))
type initContainerCommandData struct {
ServiceName string
ServiceAccountName string
AuthMethod string
// Log settings for the connect-init command.
LogLevel string
LogJSON bool
}
// initContainer returns the init container spec for connect-init that polls for the service and the connect proxy service to be registered
// so that it can save the proxy service id to the shared volume and boostrap Envoy with the proxy-id.
func (b *gatewayBuilder[T]) initContainer() (corev1.Container, error) {
data := initContainerCommandData{
AuthMethod: b.config.AuthMethod,
LogLevel: b.logLevelForInitContainer(),
LogJSON: b.config.LogJSON,
ServiceName: b.gateway.GetName(),
ServiceAccountName: b.serviceAccountName(),
}
// Render the command
var buf bytes.Buffer
if err := tpl.Execute(&buf, &data); err != nil {
return corev1.Container{}, err
}
// Create expected volume mounts
volMounts := []corev1.VolumeMount{
{
Name: volumeName,
MountPath: constants.MeshV2VolumePath,
},
}
var bearerTokenFile string
if b.config.AuthMethod != "" {
bearerTokenFile = defaultBearerTokenFile
}
consulNamespace := namespaces.ConsulNamespace(b.gateway.GetNamespace(), b.config.ConsulTenancyConfig.EnableConsulNamespaces, b.config.ConsulTenancyConfig.ConsulDestinationNamespace, b.config.ConsulTenancyConfig.EnableConsulNamespaces, b.config.ConsulTenancyConfig.NSMirroringPrefix)
initContainerName := injectInitContainerName
container := corev1.Container{
Name: initContainerName,
Image: b.config.ImageConsulK8S,
Env: []corev1.EnvVar{
{
Name: envPodName,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.name"},
},
},
{
Name: envPodNamespace,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.namespace"},
},
},
{
Name: envNodeName,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "spec.nodeName",
},
},
},
{
Name: envConsulAddresses,
Value: b.config.ConsulConfig.Address,
},
{
Name: envConsulGRPCPort,
Value: strconv.Itoa(b.config.ConsulConfig.GRPCPort),
},
{
Name: envConsulHTTPPort,
Value: strconv.Itoa(b.config.ConsulConfig.HTTPPort),
},
{
Name: envConsulAPITimeout,
Value: b.config.ConsulConfig.APITimeout.String(),
},
{
Name: envConsulNodeName,
Value: "$(NODE_NAME)-virtual",
},
},
VolumeMounts: volMounts,
Command: []string{"/bin/sh", "-ec", buf.String()},
Resources: initContainerResourcesOrDefault(b.gcc),
}
if b.config.AuthMethod != "" {
container.Env = append(container.Env,
corev1.EnvVar{
Name: envConsulLoginAuthMethod,
Value: b.config.AuthMethod,
},
corev1.EnvVar{
Name: envConsulLoginBearerTokenFile,
Value: bearerTokenFile,
},
corev1.EnvVar{
Name: envConsulLoginMeta,
Value: "pod=$(POD_NAMESPACE)/$(POD_NAME)",
})
if b.config.ConsulTenancyConfig.ConsulPartition != "" {
container.Env = append(container.Env, corev1.EnvVar{
Name: envConsulLoginPartition,
Value: b.config.ConsulTenancyConfig.ConsulPartition,
})
}
}
container.Env = append(container.Env,
corev1.EnvVar{
Name: envConsulNamespace,
Value: consulNamespace,
})
if b.config.TLSEnabled {
container.Env = append(container.Env,
corev1.EnvVar{
Name: constants.UseTLSEnvVar,
Value: "true",
},
corev1.EnvVar{
Name: constants.CACertPEMEnvVar,
Value: b.config.ConsulCACert,
},
corev1.EnvVar{
Name: constants.TLSServerNameEnvVar,
Value: b.config.ConsulTLSServerName,
})
}
if b.config.ConsulTenancyConfig.ConsulPartition != "" {
container.Env = append(container.Env,
corev1.EnvVar{
Name: envConsulPartition,
Value: b.config.ConsulTenancyConfig.ConsulPartition,
})
}
return container, nil
}
func initContainerResourcesOrDefault(gcc *meshv2beta1.GatewayClassConfig) corev1.ResourceRequirements {
if gcc != nil && gcc.Spec.Deployment.InitContainer != nil && gcc.Spec.Deployment.InitContainer.Resources != nil {
return *gcc.Spec.Deployment.InitContainer.Resources
}
return corev1.ResourceRequirements{}
}
// initContainerCommandTpl is the template for the command executed by
// the init container.
// TODO @GatewayManagement parametrize gateway kind.
const initContainerCommandTpl = `
consul-k8s-control-plane mesh-init \
-proxy-name=${POD_NAME} \
-namespace=${POD_NAMESPACE} \
{{- with .LogLevel }}
-log-level={{ . }} \
{{- end }}
-log-json={{ .LogJSON }}
`