Repository navigation
Expand file tree
/
Copy pathfuncs.go
More file actions
310 lines (288 loc) · 9.59 KB
/
Copy pathfuncs.go
File metadata and controls
310 lines (288 loc) · 9.59 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
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 openstackclient provides functionality for managing OpenStack client resources
package openstackclient
import (
"context"
"fmt"
env "github.com/openstack-k8s-operators/lib-common/modules/common/env"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/pod"
"github.com/openstack-k8s-operators/lib-common/modules/common/tls"
"github.com/openstack-k8s-operators/lib-common/modules/users"
clientv1 "github.com/openstack-k8s-operators/openstack-operator/api/client/v1beta1"
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ClientPodSpec func
func ClientPodSpec(
ctx context.Context,
instance *clientv1.OpenStackClient,
helper *helper.Helper,
configHash string,
mcpTLSSvc *tls.Service,
) corev1.PodSpec {
envVars := map[string]env.Setter{}
envVars["OS_CLOUD"] = env.SetValue("default")
envVars["CONFIG_HASH"] = env.SetValue(configHash)
envVars["PROMETHEUS_HOST"] = env.SetValue(fmt.Sprintf("%s-prometheus.%s.svc",
telemetryv1.DefaultServiceName,
instance.Namespace))
envVars["PROMETHEUS_PORT"] = env.SetValue(fmt.Sprint(telemetryv1.DefaultPrometheusPort))
metricStorage := &telemetryv1.MetricStorage{}
err := helper.GetClient().Get(ctx, client.ObjectKey{
Namespace: instance.Namespace,
Name: telemetryv1.DefaultServiceName,
}, metricStorage)
if err == nil && metricStorage.Spec.PrometheusTLS.Enabled() {
envVars["PROMETHEUS_CA_CERT"] = env.SetValue(tls.DownstreamTLSCABundlePath)
}
if instance.Spec.Env != nil {
for idx := range instance.Spec.Env {
e := instance.Spec.Env[idx]
envVars[e.Name] = func(env *corev1.EnvVar) {
env.Value = e.Value
env.ValueFrom = e.ValueFrom
}
}
}
// create Volume and VolumeMounts
volumes := clientPodVolumes(instance)
volumeMounts := clientPodVolumeMounts()
// add CA cert if defined
if instance.Spec.CaBundleSecretName != "" {
volumes = append(volumes, instance.Spec.CreateVolume())
volumeMounts = append(volumeMounts, instance.Spec.CreateVolumeMounts(nil)...)
}
podSpec := corev1.PodSpec{
TerminationGracePeriodSeconds: ptr.To[int64](0),
ServiceAccountName: instance.RbacResourceName(),
SecurityContext: pod.RestrictivePodSecurityContext(users.CloudAdminUID, users.CloudAdminGID),
Volumes: volumes,
Containers: []corev1.Container{
{
Name: "openstackclient",
Image: instance.Spec.ContainerImage,
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{"/bin/sleep"},
Args: []string{"infinity"},
SecurityContext: pod.RestrictiveSecurityContext(users.CloudAdminUID, users.CloudAdminGID),
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: volumeMounts,
},
},
Tolerations: []corev1.Toleration{
{
Key: "node.kubernetes.io/not-ready",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &[]int64{120}[0],
},
{
Key: "node.kubernetes.io/unreachable",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &[]int64{120}[0],
},
},
}
if instance.Spec.MCP != nil && instance.Spec.MCP.Enabled {
mcpVolumeMounts := []corev1.VolumeMount{
{
Name: "mcp-config",
MountPath: "/home/cloud-admin/.config/openstack/clouds.yaml",
SubPath: "clouds.yaml",
ReadOnly: true,
},
{
Name: "openstack-config-secret",
MountPath: "/home/cloud-admin/.config/openstack/secure.yaml",
SubPath: "secure.yaml",
ReadOnly: true,
},
{
Name: "mcp-config",
MountPath: "/tmp/mcp-config/config.yaml",
SubPath: "config.yaml",
ReadOnly: true,
},
}
if instance.Spec.CaBundleSecretName != "" {
mcpVolumeMounts = append(mcpVolumeMounts, instance.Spec.CreateVolumeMounts(nil)...)
}
if mcpTLSSvc != nil {
mcpVolumeMounts = append(mcpVolumeMounts, mcpTLSSvc.CreateVolumeMounts("mcp")...)
}
podSpec.Volumes = append(podSpec.Volumes, corev1.Volume{
Name: "mcp-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: instance.Name + "-mcp-config",
},
},
},
})
if mcpTLSSvc != nil {
podSpec.Volumes = append(podSpec.Volumes, mcpTLSSvc.CreateVolume("mcp"))
}
mcpEnvVars := []corev1.EnvVar{
{Name: "HOME", Value: "/home/cloud-admin"},
{Name: "RHOS_MCPS_CONFIG", Value: "/tmp/mcp-config/config.yaml"},
{Name: "OS_CLOUD", Value: "default"},
{Name: "OS_CLIENT_CONFIG_FILE", Value: "/home/cloud-admin/.config/openstack/clouds.yaml"},
}
if instance.Spec.CaBundleSecretName != "" {
mcpEnvVars = append(mcpEnvVars,
corev1.EnvVar{Name: "OS_CACERT", Value: tls.DownstreamTLSCABundlePath},
corev1.EnvVar{Name: "REQUESTS_CA_BUNDLE", Value: tls.DownstreamTLSCABundlePath},
)
}
podSpec.Containers = append(podSpec.Containers, corev1.Container{
Name: "mcp-server",
Image: instance.Spec.MCPContainerImage,
Command: []string{"rhos-ls-mcps"},
Env: mcpEnvVars,
Ports: []corev1.ContainerPort{
{Name: "mcp", ContainerPort: 8080, Protocol: corev1.ProtocolTCP},
},
SecurityContext: pod.RestrictiveSecurityContext(users.CloudAdminUID, users.CloudAdminGID),
VolumeMounts: mcpVolumeMounts,
})
}
if instance.Spec.NodeSelector != nil {
podSpec.NodeSelector = *instance.Spec.NodeSelector
}
return podSpec
}
// MCPConfigYAML returns the rhos-mcps config.yaml content for the MCP sidecar.
// name and namespace identify the owning OpenStackClient and are used to scope
// mcp_transport_security to the MCP Service's own hostname (<name>-mcp.<namespace>.svc:8080),
// matching the URL OpenStackAssistant uses to reach it, instead of allowing any host/origin.
// When MetricStorage is present, its Prometheus endpoint is configured explicitly
// for RHOSO versions where it is not registered in the Keystone catalog.
func MCPConfigYAML(
name, namespace, caBundleSecretName string,
mcpTLSEnabled bool,
metricStorage *telemetryv1.MetricStorage,
) string {
caCert := ""
if caBundleSecretName != "" {
caCert = fmt.Sprintf("\n ca_cert: %s", tls.DownstreamTLSCABundlePath)
}
prometheusConfig := ""
if metricStorage != nil {
prometheusCACert := ""
if metricStorage.Spec.PrometheusTLS.Enabled() {
prometheusCACert = fmt.Sprintf("\n ca_cert: %s", tls.DownstreamTLSCABundlePath)
}
prometheusConfig = fmt.Sprintf(`
prometheus:
host: %s-prometheus.%s.svc
port: %d%s`,
telemetryv1.DefaultServiceName,
namespace,
telemetryv1.DefaultPrometheusPort,
prometheusCACert,
)
}
mcpTLS := ""
if mcpTLSEnabled {
mcpTLS = `
tls:
ssl_certfile: /etc/pki/tls/mcp/tls.crt
ssl_keyfile: /etc/pki/tls/mcp/tls.key`
}
mcpHost := fmt.Sprintf("%s-mcp.%s.svc:8080", name, namespace)
allowedOrigins := fmt.Sprintf(` - "http://%s"`, mcpHost)
if mcpTLSEnabled {
allowedOrigins = fmt.Sprintf(` - "http://%s"
- "https://%s"`, mcpHost, mcpHost)
}
return fmt.Sprintf(`ip: "0.0.0.0"
port: 8080
openstack:
enabled: true
allow_write: false%s%s
openshift:
enabled: false
mcp_transport_security:
enable_dns_rebinding_protection: true
allowed_hosts:
- "%s"
allowed_origins:
%s%s
`, caCert, prometheusConfig, mcpHost, allowedOrigins, mcpTLS)
}
// MCPCloudsYAML returns a clouds.yaml using the given auth URL for the MCP sidecar.
// When caBundleSecretName is set, a cacert path is included for TLS verification.
func MCPCloudsYAML(authURL, projectName, userName, region, caBundleSecretName string) string {
caCert := ""
if caBundleSecretName != "" {
caCert = fmt.Sprintf("\n cacert: %s", tls.DownstreamTLSCABundlePath)
}
return fmt.Sprintf(`clouds:
default:
auth:
auth_url: %s
project_name: %s
username: %s
user_domain_name: Default
project_domain_name: Default
region_name: %s%s
`, authURL, projectName, userName, region, caCert)
}
func clientPodVolumeMounts() []corev1.VolumeMount {
return []corev1.VolumeMount{
{
Name: "openstack-config",
MountPath: "/home/cloud-admin/.config/openstack/clouds.yaml",
SubPath: "clouds.yaml",
},
{
Name: "openstack-config-secret",
MountPath: "/home/cloud-admin/.config/openstack/secure.yaml",
SubPath: "secure.yaml",
},
{
Name: "openstack-config-secret",
MountPath: "/home/cloud-admin/cloudrc",
SubPath: "cloudrc",
},
}
}
func clientPodVolumes(
instance *clientv1.OpenStackClient,
) []corev1.Volume {
return []corev1.Volume{
{
Name: "openstack-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: *instance.Spec.OpenStackConfigMap,
},
},
},
},
{
Name: "openstack-config-secret",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: *instance.Spec.OpenStackConfigSecret,
},
},
},
}
}