-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-default-configmaps.sh
executable file
·73 lines (58 loc) · 2.21 KB
/
create-default-configmaps.sh
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
#!/usr/bin/env bash
source determine-cmd.sh
cat <<EOF | ${cmd} apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: k8s-info-configuration
data:
application.properties: |
k8s.info.enabled=true
EOF
cat <<EOF | ${cmd} apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: k8s-info-mutating-script
data:
mutator.groovy: |
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import io.fabric8.kubernetes.api.model.EnvVar
import io.fabric8.kubernetes.client.KubernetesClient
import io.fabric8.openshift.client.OpenShiftClient
import me.snowdrop.kubernetes.info.webhook.script.ScriptRunner
//read the script's input
final ScriptRunner.Input input = binding.getVariable("input")
final Map<String, Object> originalObjectCopy = input.originalObjectCopy
final KubernetesClient kubernetesClient = input.kubernetesClient
final ObjectMapper objectMapper = input.objectMapper
//add KUBERNETES_GIT_VERSION and KUBERNETES_DISTRO to each one of the containers of
//the originalObjectCopy is assumed to be a pod
originalObjectCopy.spec.containers.each { container ->
final List<EnvVar> containerEnv = containerEnv(container, objectMapper)
containerEnv.add(
createNameValueEnvVar("KUBERNETES_GIT_VERSION", kubernetesClient.version.gitVersion)
)
containerEnv.add(
createNameValueEnvVar(
"KUBERNETES_DISTRO",
kubernetesClient.isAdaptable(OpenShiftClient.class) ? "openshift" : "vanilla"
)
)
}
//return the updated object - pod in this case
return originalObjectCopy
private List<EnvVar> containerEnv(Map<String, Object> container, ObjectMapper objectMapper) {
if (container.containsKey("env")) {
return objectMapper.convertValue(container.env, new TypeReference<List<EnvVar>>(){})
}
return container.env = new ArrayList<>()
}
private EnvVar createNameValueEnvVar(String name, value) {
final kubernetesVersionEnvVar = new EnvVar()
kubernetesVersionEnvVar.name = name
kubernetesVersionEnvVar.value = value
return kubernetesVersionEnvVar
}
EOF