-
Notifications
You must be signed in to change notification settings - Fork 55
/
impl.go
148 lines (120 loc) · 3.98 KB
/
impl.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
package job
import (
"strings"
crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1"
batchv1 "k8s.io/api/batch/v1"
core "k8s.io/api/core/v1"
deployProvider "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/deployment"
provutils "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/utils"
"k8s.io/apimachinery/pkg/types"
"github.com/RedHatInsights/rhc-osdk-utils/utils"
)
// applyJob build the k8s job resource and applies it from the Job config
// defined in the ClowdApp
func CreateJobResource(cji *crd.ClowdJobInvocation, env *crd.ClowdEnvironment, app *crd.ClowdApp, nn types.NamespacedName, job *crd.Job, j *batchv1.Job) error {
labels := cji.GetLabels()
cji.SetObjectMeta(j, crd.Name(nn.Name), crd.Labels(labels))
j.ObjectMeta.Labels = labels
j.ObjectMeta.Labels["job"] = job.Name
j.Spec.Template.ObjectMeta.Labels = labels
j.Spec.ActiveDeadlineSeconds = job.ActiveDeadlineSeconds
pod := job.PodSpec
if job.RestartPolicy == "" {
j.Spec.Template.Spec.RestartPolicy = core.RestartPolicyNever
} else {
j.Spec.Template.Spec.RestartPolicy = job.RestartPolicy
}
if job.Parallelism != nil {
j.Spec.Parallelism = job.Parallelism
}
if job.Completions != nil {
j.Spec.Completions = job.Completions
}
envvar := pod.Env
envvar = append(envvar, core.EnvVar{Name: "ACG_CONFIG", Value: "/cdapp/cdappconfig.json"})
var livenessProbe core.Probe
var readinessProbe core.Probe
if pod.LivenessProbe != nil {
livenessProbe = *pod.LivenessProbe
} else {
livenessProbe = core.Probe{}
}
if pod.ReadinessProbe != nil {
readinessProbe = *pod.ReadinessProbe
} else {
readinessProbe = core.Probe{}
}
c := core.Container{
Name: nn.Name,
Image: pod.Image,
Command: pod.Command,
Args: pod.Args,
Env: envvar,
Resources: deployProvider.ProcessResources(&pod, env),
VolumeMounts: pod.VolumeMounts,
Ports: []core.ContainerPort{{
Name: "metrics",
ContainerPort: env.Spec.Providers.Metrics.Port,
Protocol: core.ProtocolTCP,
}},
TerminationMessagePath: "/dev/termination-log",
TerminationMessagePolicy: core.TerminationMessageReadFile,
ImagePullPolicy: core.PullIfNotPresent,
}
if pod.MachinePool != "" {
j.Spec.Template.Spec.Tolerations = []core.Toleration{{
Key: pod.MachinePool,
Effect: core.TaintEffectNoSchedule,
Operator: core.TolerationOpEqual,
Value: "true",
}}
} else {
j.Spec.Template.Spec.Tolerations = []core.Toleration{}
}
if !env.Spec.Providers.Deployment.OmitPullPolicy {
c.ImagePullPolicy = core.PullIfNotPresent
} else {
imageComponents := strings.Split(c.Image, ":")
if len(imageComponents) > 1 {
if imageComponents[1] == "latest" {
c.ImagePullPolicy = core.PullAlways
} else {
c.ImagePullPolicy = core.PullIfNotPresent
}
} else {
c.ImagePullPolicy = core.PullIfNotPresent
}
}
if (core.Probe{}) != livenessProbe {
c.LivenessProbe = &livenessProbe
}
if (core.Probe{}) != readinessProbe {
c.ReadinessProbe = &readinessProbe
}
j.Spec.Template.Spec.ServiceAccountName = app.GetClowdSAName()
c.VolumeMounts = append(c.VolumeMounts, core.VolumeMount{
Name: "config-secret",
MountPath: "/cdapp/",
})
j.Spec.Template.Spec.Containers = []core.Container{c}
ics, err := deployProvider.ProcessInitContainers(nn, &c, pod.InitContainers)
if err != nil {
return err
}
j.Spec.Template.Spec.InitContainers = ics
j.Spec.Template.Spec.Volumes = pod.Volumes
j.Spec.Template.Spec.Volumes = append(j.Spec.Template.Spec.Volumes, core.Volume{
Name: "config-secret",
VolumeSource: core.VolumeSource{
Secret: &core.SecretVolumeSource{
SecretName: cji.Spec.AppName,
},
},
})
if env.Spec.Providers.Web.TLS.Enabled {
provutils.AddCertVolume(&j.Spec.Template.Spec, nn.Name)
}
utils.UpdateAnnotations(&j.Spec.Template, provutils.KubeLinterAnnotations, cji.Annotations)
utils.UpdateAnnotations(j, provutils.KubeLinterAnnotations, app.ObjectMeta.Annotations)
return nil
}