-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Owls 87956 - Generate shorter volume name when override secret name is too long #2257
Changes from all commits
49c2acb
3f53a3a
e641782
0fb1663
f634779
63810e3
73984f2
c1ccff8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import oracle.kubernetes.operator.calls.UnrecoverableErrorBuilder; | ||
import oracle.kubernetes.operator.logging.LoggingFacade; | ||
import oracle.kubernetes.operator.logging.LoggingFactory; | ||
import oracle.kubernetes.operator.utils.ChecksumUtils; | ||
import oracle.kubernetes.operator.work.NextAction; | ||
import oracle.kubernetes.operator.work.Packet; | ||
import oracle.kubernetes.operator.work.Step; | ||
|
@@ -44,6 +45,10 @@ public abstract class JobStepContext extends BasePodStepContext { | |
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator"); | ||
private static final String WEBLOGIC_OPERATOR_SCRIPTS_INTROSPECT_DOMAIN_SH = | ||
"/weblogic-operator/scripts/introspectDomain.sh"; | ||
private static final int MAX_ALLOWED_VOLUME_NAME_LENGTH = 63; | ||
public static final String VOLUME_NAME_SUFFIX = "-volume"; | ||
public static final String CONFIGMAP_TYPE = "cm"; | ||
public static final String SECRET_TYPE = "st"; | ||
private V1Job jobModel; | ||
|
||
JobStepContext(Packet packet) { | ||
|
@@ -313,14 +318,14 @@ protected V1PodSpec createPodSpec(TuningParameters tuningParameters) { | |
private void addConfigOverrideSecretVolume(V1PodSpec podSpec, String secretName) { | ||
podSpec.addVolumesItem( | ||
new V1Volume() | ||
.name(secretName + "-volume") | ||
.name(getVolumeName(secretName, SECRET_TYPE)) | ||
.secret(getOverrideSecretVolumeSource(secretName))); | ||
} | ||
|
||
private void addConfigOverrideVolume(V1PodSpec podSpec, String configOverrides) { | ||
podSpec.addVolumesItem( | ||
new V1Volume() | ||
.name(configOverrides + "-volume") | ||
.name(getVolumeName(configOverrides, CONFIGMAP_TYPE)) | ||
.configMap(getOverridesVolumeSource(configOverrides))); | ||
} | ||
|
||
|
@@ -331,7 +336,7 @@ private boolean isSourceWdt() { | |
private void addWdtConfigMapVolume(V1PodSpec podSpec, String configMapName) { | ||
podSpec.addVolumesItem( | ||
new V1Volume() | ||
.name(configMapName + "-volume") | ||
.name(getVolumeName(configMapName, CONFIGMAP_TYPE)) | ||
.configMap(getWdtConfigMapVolumeSource(configMapName))); | ||
} | ||
|
||
|
@@ -365,20 +370,20 @@ protected V1Container createPrimaryContainer(TuningParameters tuningParameters) | |
|
||
if (getConfigOverrides() != null && getConfigOverrides().length() > 0) { | ||
container.addVolumeMountsItem( | ||
readOnlyVolumeMount(getConfigOverrides() + "-volume", OVERRIDES_CM_MOUNT_PATH)); | ||
readOnlyVolumeMount(getVolumeName(getConfigOverrides(), CONFIGMAP_TYPE), OVERRIDES_CM_MOUNT_PATH)); | ||
} | ||
|
||
List<String> configOverrideSecrets = getConfigOverrideSecrets(); | ||
for (String secretName : configOverrideSecrets) { | ||
container.addVolumeMountsItem( | ||
readOnlyVolumeMount( | ||
secretName + "-volume", OVERRIDE_SECRETS_MOUNT_PATH + '/' + secretName)); | ||
getVolumeName(secretName, SECRET_TYPE), OVERRIDE_SECRETS_MOUNT_PATH + '/' + secretName)); | ||
} | ||
|
||
if (isSourceWdt()) { | ||
if (getWdtConfigMap() != null) { | ||
container.addVolumeMountsItem( | ||
readOnlyVolumeMount(getWdtConfigMap() + "-volume", WDTCONFIGMAP_MOUNT_PATH)); | ||
readOnlyVolumeMount(getVolumeName(getWdtConfigMap(), CONFIGMAP_TYPE), WDTCONFIGMAP_MOUNT_PATH)); | ||
} | ||
container.addVolumeMountsItem( | ||
readOnlyVolumeMount(RUNTIME_ENCRYPTION_SECRET_VOLUME, | ||
|
@@ -389,6 +394,22 @@ protected V1Container createPrimaryContainer(TuningParameters tuningParameters) | |
return container; | ||
} | ||
|
||
private String getVolumeName(String resourceName, String type) { | ||
return getName(resourceName, type); | ||
} | ||
|
||
private String getName(String resourceName, String type) { | ||
return resourceName.length() > (MAX_ALLOWED_VOLUME_NAME_LENGTH - VOLUME_NAME_SUFFIX.length()) | ||
? getShortName(resourceName, type) | ||
: resourceName + VOLUME_NAME_SUFFIX; | ||
} | ||
|
||
private String getShortName(String resourceName, String type) { | ||
String volumeSuffix = VOLUME_NAME_SUFFIX + "-" + type + "-" | ||
+ Optional.ofNullable(ChecksumUtils.getMD5Hash(resourceName)).orElse(""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would the result of getMD5Hash be null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be null when |
||
return resourceName.substring(0, MAX_ALLOWED_VOLUME_NAME_LENGTH - volumeSuffix.length()) + volumeSuffix; | ||
} | ||
|
||
protected String getContainerName() { | ||
return getJobName(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2021, Oracle and/or its affiliates. | ||
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. | ||
|
||
package oracle.kubernetes.operator.utils; | ||
|
||
import java.security.MessageDigest; | ||
import javax.xml.bind.DatatypeConverter; | ||
|
||
import oracle.kubernetes.operator.logging.LoggingFacade; | ||
import oracle.kubernetes.operator.logging.LoggingFactory; | ||
import oracle.kubernetes.operator.logging.MessageKeys; | ||
|
||
public class ChecksumUtils { | ||
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator"); | ||
|
||
/** | ||
* Gets the MD5 hash of a string. | ||
* | ||
* @param data input string | ||
* @return MD5 hash value of the data, null in case of an exception. | ||
*/ | ||
public static String getMD5Hash(String data) { | ||
try { | ||
return bytesToHex(MessageDigest.getInstance("MD5").digest(data.getBytes("UTF-8"))); | ||
} catch (Exception ex) { | ||
LOGGER.severe(MessageKeys.EXCEPTION, ex); | ||
return null; | ||
} | ||
} | ||
|
||
private static String bytesToHex(byte[] hash) { | ||
return DatatypeConverter.printHexBinary(hash).toLowerCase(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these methods do the same thing, why do you need two of them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was leftover from the previous implementation and I thought having methods with different names might provide better readability. I went ahead and removed it.