forked from apache/camel-k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
411 lines (355 loc) · 14.6 KB
/
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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 trait
import (
"fmt"
"path"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
serving "knative.dev/serving/pkg/apis/serving/v1"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/defaults"
"github.com/apache/camel-k/pkg/util/envvar"
"github.com/apache/camel-k/pkg/util/kubernetes"
)
const (
defaultContainerName = "integration"
defaultContainerPort = 8080
defaultContainerPortName = "http"
defaultServicePort = 80
containerTraitID = "container"
)
// The Container trait can be used to configure properties of the container where the integration will run.
//
// It also provides configuration for Services associated to the container.
//
// +camel-k:trait=container
// nolint: tagliatelle
type containerTrait struct {
BaseTrait `property:",squash"`
Auto *bool `property:"auto" json:"auto,omitempty"`
// The minimum amount of CPU required.
RequestCPU string `property:"request-cpu" json:"requestCPU,omitempty"`
// The minimum amount of memory required.
RequestMemory string `property:"request-memory" json:"requestMemory,omitempty"`
// The maximum amount of CPU required.
LimitCPU string `property:"limit-cpu" json:"limitCPU,omitempty"`
// The maximum amount of memory required.
LimitMemory string `property:"limit-memory" json:"limitMemory,omitempty"`
// Can be used to enable/disable exposure via kubernetes Service.
Expose *bool `property:"expose" json:"expose,omitempty"`
// To configure a different port exposed by the container (default `8080`).
Port int `property:"port" json:"port,omitempty"`
// To configure a different port name for the port exposed by the container. It defaults to `http` only when the `expose` parameter is true.
PortName string `property:"port-name" json:"portName,omitempty"`
// To configure under which service port the container port is to be exposed (default `80`).
ServicePort int `property:"service-port" json:"servicePort,omitempty"`
// To configure under which service port name the container port is to be exposed (default `http`).
ServicePortName string `property:"service-port-name" json:"servicePortName,omitempty"`
// The main container name. It's named `integration` by default.
Name string `property:"name" json:"name,omitempty"`
// The main container image
Image string `property:"image" json:"image,omitempty"`
// The pull policy: Always|Never|IfNotPresent
ImagePullPolicy corev1.PullPolicy `property:"image-pull-policy" json:"imagePullPolicy,omitempty"`
// DeprecatedProbesEnabled enable/disable probes on the container (default `false`)
// Deprecated: replaced by the health trait.
DeprecatedProbesEnabled *bool `property:"probes-enabled" json:"probesEnabled,omitempty"`
// Scheme to use when connecting. Defaults to HTTP. Applies to the liveness probe.
// Deprecated: replaced by the health trait.
DeprecatedLivenessScheme string `property:"liveness-scheme" json:"livenessScheme,omitempty"`
// Number of seconds after the container has started before liveness probes are initiated.
// Deprecated: replaced by the health trait.
DeprecatedLivenessInitialDelay int32 `property:"liveness-initial-delay" json:"livenessInitialDelay,omitempty"`
// Number of seconds after which the probe times out. Applies to the liveness probe.
// Deprecated: replaced by the health trait.
DeprecatedLivenessTimeout int32 `property:"liveness-timeout" json:"livenessTimeout,omitempty"`
// How often to perform the probe. Applies to the liveness probe.
// Deprecated: replaced by the health trait.
DeprecatedLivenessPeriod int32 `property:"liveness-period" json:"livenessPeriod,omitempty"`
// Minimum consecutive successes for the probe to be considered successful after having failed.
// Applies to the liveness probe.
// Deprecated: replaced by the health trait.
DeprecatedLivenessSuccessThreshold int32 `property:"liveness-success-threshold" json:"livenessSuccessThreshold,omitempty"`
// Minimum consecutive failures for the probe to be considered failed after having succeeded.
// Applies to the liveness probe.
// Deprecated: replaced by the health trait.
DeprecatedLivenessFailureThreshold int32 `property:"liveness-failure-threshold" json:"livenessFailureThreshold,omitempty"`
// Scheme to use when connecting. Defaults to HTTP. Applies to the readiness probe.
// Deprecated: replaced by the health trait.
DeprecatedReadinessScheme string `property:"readiness-scheme" json:"readinessScheme,omitempty"`
// Number of seconds after the container has started before readiness probes are initiated.
// Deprecated: replaced by the health trait.
DeprecatedReadinessInitialDelay int32 `property:"readiness-initial-delay" json:"readinessInitialDelay,omitempty"`
// Number of seconds after which the probe times out. Applies to the readiness probe.
// Deprecated: replaced by the health trait.
DeprecatedReadinessTimeout int32 `property:"readiness-timeout" json:"readinessTimeout,omitempty"`
// How often to perform the probe. Applies to the readiness probe.
// Deprecated: replaced by the health trait.
DeprecatedReadinessPeriod int32 `property:"readiness-period" json:"readinessPeriod,omitempty"`
// Minimum consecutive successes for the probe to be considered successful after having failed.
// Applies to the readiness probe.
// Deprecated: replaced by the health trait.
DeprecatedReadinessSuccessThreshold int32 `property:"readiness-success-threshold" json:"readinessSuccessThreshold,omitempty"`
// Minimum consecutive failures for the probe to be considered failed after having succeeded.
// Applies to the readiness probe.
// Deprecated: replaced by the health trait.
DeprecatedReadinessFailureThreshold int32 `property:"readiness-failure-threshold" json:"readinessFailureThreshold,omitempty"`
}
func newContainerTrait() Trait {
return &containerTrait{
BaseTrait: NewBaseTrait(containerTraitID, 1600),
Port: defaultContainerPort,
ServicePort: defaultServicePort,
ServicePortName: defaultContainerPortName,
Name: defaultContainerName,
DeprecatedProbesEnabled: BoolP(false),
DeprecatedLivenessScheme: string(corev1.URISchemeHTTP),
DeprecatedReadinessScheme: string(corev1.URISchemeHTTP),
}
}
func (t *containerTrait) Configure(e *Environment) (bool, error) {
if IsFalse(t.Enabled) {
return false, nil
}
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() {
return false, nil
}
if IsNilOrTrue(t.Auto) {
if t.Expose == nil {
e := e.Resources.GetServiceForIntegration(e.Integration) != nil
t.Expose = &e
}
}
if !isValidPullPolicy(t.ImagePullPolicy) {
return false, fmt.Errorf("unsupported pull policy %s", t.ImagePullPolicy)
}
return true, nil
}
func isValidPullPolicy(policy corev1.PullPolicy) bool {
return policy == "" || policy == corev1.PullAlways || policy == corev1.PullIfNotPresent || policy == corev1.PullNever
}
func (t *containerTrait) Apply(e *Environment) error {
if err := t.configureImageIntegrationKit(e); err != nil {
return err
}
return t.configureContainer(e)
}
// IsPlatformTrait overrides base class method.
func (t *containerTrait) IsPlatformTrait() bool {
return true
}
func (t *containerTrait) configureImageIntegrationKit(e *Environment) error {
if t.Image != "" {
if e.Integration.Spec.IntegrationKit != nil {
return fmt.Errorf(
"unsupported configuration: a container image has been set in conjunction with an IntegrationKit %v",
e.Integration.Spec.IntegrationKit)
}
kitName := fmt.Sprintf("kit-%s", e.Integration.Name)
kit := v1.NewIntegrationKit(e.Integration.Namespace, kitName)
kit.Spec.Image = t.Image
// Add some information for post-processing, this may need to be refactored
// to a proper data structure
kit.Labels = map[string]string{
v1.IntegrationKitTypeLabel: v1.IntegrationKitTypeExternal,
kubernetes.CamelCreatorLabelKind: v1.IntegrationKind,
kubernetes.CamelCreatorLabelName: e.Integration.Name,
kubernetes.CamelCreatorLabelNamespace: e.Integration.Namespace,
kubernetes.CamelCreatorLabelVersion: e.Integration.ResourceVersion,
}
if kit.Annotations == nil {
kit.Annotations = make(map[string]string)
}
if v, ok := e.Integration.Annotations[v1.PlatformSelectorAnnotation]; ok {
kit.Annotations[v1.PlatformSelectorAnnotation] = v
}
operatorID := defaults.OperatorID()
if operatorID != "" {
kit.Annotations[v1.OperatorIDAnnotation] = operatorID
}
t.L.Infof("image %s", kit.Spec.Image)
e.Resources.Add(kit)
e.Integration.SetIntegrationKit(kit)
}
return nil
}
func (t *containerTrait) configureContainer(e *Environment) error {
if e.ApplicationProperties == nil {
e.ApplicationProperties = make(map[string]string)
}
container := corev1.Container{
Name: t.Name,
Image: e.Integration.Status.Image,
Env: make([]corev1.EnvVar, 0),
}
if t.ImagePullPolicy != "" {
container.ImagePullPolicy = t.ImagePullPolicy
}
// combine Environment of integration with platform, kit, integration
for _, env := range e.collectConfigurationPairs("env") {
envvar.SetVal(&container.Env, env.Name, env.Value)
}
envvar.SetVal(&container.Env, "CAMEL_K_DIGEST", e.Integration.Status.Digest)
envvar.SetVal(&container.Env, "CAMEL_K_CONF", path.Join(camel.BasePath, "application.properties"))
envvar.SetVal(&container.Env, "CAMEL_K_CONF_D", camel.ConfDPath)
e.addSourcesProperties()
if props, err := e.computeApplicationProperties(); err != nil {
return err
} else if props != nil {
e.Resources.Add(props)
}
t.configureResources(e, &container)
if IsTrue(t.Expose) {
t.configureService(e, &container)
}
t.configureCapabilities(e)
var containers *[]corev1.Container
visited := false
// Deployment
if err := e.Resources.VisitDeploymentE(func(deployment *appsv1.Deployment) error {
for _, envVar := range e.EnvVars {
envvar.SetVar(&container.Env, envVar)
}
containers = &deployment.Spec.Template.Spec.Containers
visited = true
return nil
}); err != nil {
return err
}
// Knative Service
if err := e.Resources.VisitKnativeServiceE(func(service *serving.Service) error {
for _, env := range e.EnvVars {
switch {
case env.ValueFrom == nil:
envvar.SetVar(&container.Env, env)
case env.ValueFrom.FieldRef != nil && env.ValueFrom.FieldRef.FieldPath == "metadata.namespace":
envvar.SetVar(&container.Env, corev1.EnvVar{Name: env.Name, Value: e.Integration.Namespace})
case env.ValueFrom.FieldRef != nil:
t.L.Infof("Skipping environment variable %s (fieldRef)", env.Name)
case env.ValueFrom.ResourceFieldRef != nil:
t.L.Infof("Skipping environment variable %s (resourceFieldRef)", env.Name)
default:
envvar.SetVar(&container.Env, env)
}
}
containers = &service.Spec.ConfigurationSpec.Template.Spec.Containers
visited = true
return nil
}); err != nil {
return err
}
// CronJob
if err := e.Resources.VisitCronJobE(func(cron *v1beta1.CronJob) error {
for _, envVar := range e.EnvVars {
envvar.SetVar(&container.Env, envVar)
}
containers = &cron.Spec.JobTemplate.Spec.Template.Spec.Containers
visited = true
return nil
}); err != nil {
return err
}
if visited {
*containers = append(*containers, container)
}
return nil
}
func (t *containerTrait) configureService(e *Environment, container *corev1.Container) {
service := e.Resources.GetServiceForIntegration(e.Integration)
if service == nil {
return
}
name := t.PortName
if name == "" {
name = defaultContainerPortName
}
containerPort := corev1.ContainerPort{
Name: name,
ContainerPort: int32(t.Port),
Protocol: corev1.ProtocolTCP,
}
servicePort := corev1.ServicePort{
Name: t.ServicePortName,
Port: int32(t.ServicePort),
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(name),
}
e.Integration.Status.SetCondition(
v1.IntegrationConditionServiceAvailable,
corev1.ConditionTrue,
v1.IntegrationConditionServiceAvailableReason,
// service -> container
fmt.Sprintf("%s(%s/%d) -> %s(%s/%d)",
service.Name, servicePort.Name, servicePort.Port,
container.Name, containerPort.Name, containerPort.ContainerPort),
)
container.Ports = append(container.Ports, containerPort)
service.Spec.Ports = append(service.Spec.Ports, servicePort)
// Mark the service as a user service
service.Labels["camel.apache.org/service.type"] = v1.ServiceTypeUser
}
func (t *containerTrait) configureResources(_ *Environment, container *corev1.Container) {
// Requests
if container.Resources.Requests == nil {
container.Resources.Requests = make(corev1.ResourceList)
}
if t.RequestCPU != "" {
v, err := resource.ParseQuantity(t.RequestCPU)
if err != nil {
t.L.Error(err, "unable to parse quantity", "request-cpu", t.RequestCPU)
} else {
container.Resources.Requests[corev1.ResourceCPU] = v
}
}
if t.RequestMemory != "" {
v, err := resource.ParseQuantity(t.RequestMemory)
if err != nil {
t.L.Error(err, "unable to parse quantity", "request-memory", t.RequestMemory)
} else {
container.Resources.Requests[corev1.ResourceMemory] = v
}
}
// Limits
if container.Resources.Limits == nil {
container.Resources.Limits = make(corev1.ResourceList)
}
if t.LimitCPU != "" {
v, err := resource.ParseQuantity(t.LimitCPU)
if err != nil {
t.L.Error(err, "unable to parse quantity", "limit-cpu", t.LimitCPU)
} else {
container.Resources.Limits[corev1.ResourceCPU] = v
}
}
if t.LimitMemory != "" {
v, err := resource.ParseQuantity(t.LimitMemory)
if err != nil {
t.L.Error(err, "unable to parse quantity", "limit-memory", t.LimitMemory)
} else {
container.Resources.Limits[corev1.ResourceMemory] = v
}
}
}
func (t *containerTrait) configureCapabilities(e *Environment) {
if util.StringSliceExists(e.Integration.Status.Capabilities, v1.CapabilityRest) {
e.ApplicationProperties["camel.context.rest-configuration.component"] = "platform-http"
}
}