3
3
4
4
package oracle .kubernetes .operator .helpers ;
5
5
6
+ import java .util .ArrayList ;
7
+ import java .util .Arrays ;
8
+ import java .util .Collections ;
6
9
import java .util .HashMap ;
7
10
import java .util .List ;
8
11
import java .util .Map ;
9
12
import java .util .Optional ;
13
+ import java .util .StringJoiner ;
14
+ import java .util .regex .Pattern ;
15
+ import java .util .stream .Collectors ;
10
16
17
+ import io .kubernetes .client .custom .Quantity ;
11
18
import io .kubernetes .client .openapi .models .V1Container ;
19
+ import io .kubernetes .client .openapi .models .V1EmptyDirVolumeSource ;
12
20
import io .kubernetes .client .openapi .models .V1EnvVar ;
13
21
import io .kubernetes .client .openapi .models .V1EnvVarSource ;
14
22
import io .kubernetes .client .openapi .models .V1Pod ;
15
23
import io .kubernetes .client .openapi .models .V1PodSpec ;
16
24
import io .kubernetes .client .openapi .models .V1Toleration ;
25
+ import io .kubernetes .client .openapi .models .V1Volume ;
26
+ import io .kubernetes .client .openapi .models .V1VolumeMount ;
17
27
import oracle .kubernetes .operator .KubernetesConstants ;
18
28
import oracle .kubernetes .operator .TuningParameters ;
29
+ import oracle .kubernetes .weblogic .domain .model .CommonMount ;
30
+ import oracle .kubernetes .weblogic .domain .model .CommonMountEnvVars ;
31
+ import oracle .kubernetes .weblogic .domain .model .CommonMountVolume ;
19
32
import oracle .kubernetes .weblogic .domain .model .ServerSpec ;
20
33
34
+ import static oracle .kubernetes .operator .helpers .LegalNames .toDns1123LegalName ;
35
+ import static oracle .kubernetes .weblogic .domain .model .CommonMount .COMMON_MOUNT_INIT_CONTAINER_NAME_PREFIX ;
36
+ import static oracle .kubernetes .weblogic .domain .model .CommonMount .COMMON_MOUNT_INIT_CONTAINER_WRAPPER_SCRIPT ;
37
+ import static oracle .kubernetes .weblogic .domain .model .CommonMount .COMMON_MOUNT_TARGET_PATH ;
38
+ import static oracle .kubernetes .weblogic .domain .model .CommonMount .COMMON_MOUNT_VOLUME_NAME_PREFIX ;
39
+
21
40
public abstract class BasePodStepContext extends StepContextBase {
22
41
23
42
BasePodStepContext (DomainPresenceInfo info ) {
@@ -32,6 +51,34 @@ final <T> T updateForDeepSubstitution(V1PodSpec podSpec, T target) {
32
51
33
52
abstract ServerSpec getServerSpec ();
34
53
54
+ String getMountPath (CommonMount commonMount , List <CommonMountVolume > commonMountVolumes ) {
55
+ return commonMountVolumes .stream ().filter (
56
+ commonMountVolume -> hasMatchingVolumeName (commonMountVolume , commonMount )).findFirst ()
57
+ .map (CommonMountVolume ::getMountPath ).orElse (null );
58
+ }
59
+
60
+ private boolean hasMatchingVolumeName (CommonMountVolume commonMountVolume , CommonMount commonMount ) {
61
+ return commonMount .getVolume ().equals (commonMountVolume .getName ());
62
+ }
63
+
64
+ protected void addVolumeMount (V1Container container , CommonMount cm ) {
65
+ Optional .ofNullable (getMountPath (cm , info .getDomain ().getCommonMountVolumes ())).ifPresent (mountPath ->
66
+ addVolumeMountIfMissing (container , cm , mountPath ));
67
+ }
68
+
69
+ protected void addVolumeMountIfMissing (V1Container container , CommonMount cm , String mountPath ) {
70
+ if (Optional .ofNullable (container .getVolumeMounts ()).map (volumeMounts -> volumeMounts .stream ().noneMatch (
71
+ volumeMount -> hasMatchingVolumeMountName (volumeMount , cm ))).orElse (true )) {
72
+ container .addVolumeMountsItem (
73
+ new V1VolumeMount ().name (getDNS1123CommonMountVolumeName (cm .getVolume ()))
74
+ .mountPath (mountPath ));
75
+ }
76
+ }
77
+
78
+ private boolean hasMatchingVolumeMountName (V1VolumeMount volumeMount , CommonMount commonMount ) {
79
+ return getDNS1123CommonMountVolumeName (commonMount .getVolume ()).equals (volumeMount .getName ());
80
+ }
81
+
35
82
abstract String getContainerName ();
36
83
37
84
abstract List <String > getContainerCommand ();
@@ -49,6 +96,60 @@ protected V1Container createPrimaryContainer(TuningParameters tuningParameters)
49
96
.securityContext (getServerSpec ().getContainerSecurityContext ());
50
97
}
51
98
99
+ protected V1Volume createEmptyDirVolume (CommonMountVolume cmv ) {
100
+ V1EmptyDirVolumeSource emptyDirVolumeSource = new V1EmptyDirVolumeSource ();
101
+ Optional .ofNullable (cmv .getMedium ()).ifPresent (emptyDirVolumeSource ::medium );
102
+ Optional .ofNullable (cmv .getSizeLimit ())
103
+ .ifPresent (sl -> emptyDirVolumeSource .sizeLimit (Quantity .fromString ((String ) sl )));
104
+ return new V1Volume ().name (getDNS1123CommonMountVolumeName (cmv .getName ())).emptyDir (emptyDirVolumeSource );
105
+ }
106
+
107
+ public String getDNS1123CommonMountVolumeName (String name ) {
108
+ return toDns1123LegalName (COMMON_MOUNT_VOLUME_NAME_PREFIX + name );
109
+ }
110
+
111
+ protected V1Container createInitContainerForCommonMount (CommonMount commonMount , int index ) {
112
+ return new V1Container ().name (getName (index ))
113
+ .image (commonMount .getImage ())
114
+ .imagePullPolicy (commonMount .getImagePullPolicy ())
115
+ .command (Collections .singletonList (COMMON_MOUNT_INIT_CONTAINER_WRAPPER_SCRIPT ))
116
+ .env (createEnv (commonMount , info .getDomain ().getCommonMountVolumes (), getName (index )))
117
+ .volumeMounts (Arrays .asList (
118
+ new V1VolumeMount ().name (getDNS1123CommonMountVolumeName (commonMount .getVolume ()))
119
+ .mountPath (COMMON_MOUNT_TARGET_PATH ),
120
+ new V1VolumeMount ().name (SCRIPTS_VOLUME ).mountPath (SCRIPTS_MOUNTS_PATH )));
121
+ }
122
+
123
+ private String getName (int index ) {
124
+ return COMMON_MOUNT_INIT_CONTAINER_NAME_PREFIX + (index + 1 );
125
+ }
126
+
127
+ protected List <V1EnvVar > createEnv (CommonMount commonMount , List <CommonMountVolume > commonMountVolumes , String name ) {
128
+ List <V1EnvVar > vars = new ArrayList <>();
129
+ addEnvVar (vars , CommonMountEnvVars .COMMON_MOUNT_PATH , getMountPath (commonMount , commonMountVolumes ));
130
+ addEnvVar (vars , CommonMountEnvVars .COMMON_MOUNT_TARGET_PATH , COMMON_MOUNT_TARGET_PATH );
131
+ addEnvVar (vars , CommonMountEnvVars .COMMON_MOUNT_COMMAND , commonMount .getCommand ());
132
+ addEnvVar (vars , CommonMountEnvVars .COMMON_MOUNT_CONTAINER_IMAGE , commonMount .getImage ());
133
+ addEnvVar (vars , CommonMountEnvVars .COMMON_MOUNT_CONTAINER_NAME , name );
134
+ return vars ;
135
+ }
136
+
137
+ protected void addEmptyDirVolume (V1PodSpec podSpec , List <CommonMountVolume > commonMountVolumes ) {
138
+ Optional .ofNullable (commonMountVolumes ).ifPresent (volumes -> volumes .forEach (commonMountVolume ->
139
+ addVolumeIfMissing (podSpec , commonMountVolume )));
140
+ }
141
+
142
+ private void addVolumeIfMissing (V1PodSpec podSpec , CommonMountVolume commonMountVolume ) {
143
+ if (Optional .ofNullable (podSpec .getVolumes ()).map (volumes -> volumes .stream ().noneMatch (
144
+ volume -> podHasMatchingVolumeName (volume , commonMountVolume ))).orElse (true )) {
145
+ podSpec .addVolumesItem (createEmptyDirVolume (commonMountVolume ));
146
+ }
147
+ }
148
+
149
+ private boolean podHasMatchingVolumeName (V1Volume volume , CommonMountVolume commonMountVolume ) {
150
+ return volume .getName ().equals (commonMountVolume .getName ());
151
+ }
152
+
52
153
protected V1PodSpec createPodSpec (TuningParameters tuningParameters ) {
53
154
return new V1PodSpec ()
54
155
.containers (getContainers ())
@@ -208,4 +309,16 @@ protected boolean isK8sContainer(V1Container c) {
208
309
protected String getMainContainerName () {
209
310
return KubernetesConstants .CONTAINER_NAME ;
210
311
}
312
+
313
+ protected String getCommonMountPaths (List <CommonMount > commonMounts , List <CommonMountVolume > commonMountVolumes ) {
314
+ return Optional .ofNullable (commonMounts ).map (cmList -> createCommonMountPathsEnv (cmList , commonMountVolumes ))
315
+ .orElse (null );
316
+ }
317
+
318
+ private String createCommonMountPathsEnv (List <CommonMount > commonMounts , List <CommonMountVolume > commonMountVolumes ) {
319
+ StringJoiner commonMountPaths = new StringJoiner ("," ,"" ,"" );
320
+ commonMounts .forEach (commonMount -> commonMountPaths .add (getMountPath (commonMount , commonMountVolumes )));
321
+ return Arrays .stream (commonMountPaths .toString ().split (Pattern .quote ("," ))).distinct ()
322
+ .filter (st -> !st .isEmpty ()).collect (Collectors .joining ("," ));
323
+ }
211
324
}
0 commit comments