Skip to content

Commit c7880ed

Browse files
committed
Merge branch 'develop' into owls-74699
2 parents db97763 + 04ff5c0 commit c7880ed

24 files changed

+675
-104
lines changed

model/src/main/java/oracle/kubernetes/weblogic/domain/model/Domain.java

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44

55
package oracle.kubernetes.weblogic.domain.model;
66

7+
import java.io.File;
8+
import java.util.ArrayList;
79
import java.util.Arrays;
810
import java.util.Comparator;
11+
import java.util.HashSet;
912
import java.util.List;
1013
import java.util.Map;
1114
import java.util.Optional;
15+
import java.util.Set;
1216
import javax.annotation.Nonnull;
17+
import javax.annotation.Nullable;
1318
import javax.validation.Valid;
1419

1520
import com.google.gson.annotations.Expose;
1621
import com.google.gson.annotations.SerializedName;
1722
import io.kubernetes.client.models.V1ObjectMeta;
1823
import io.kubernetes.client.models.V1SecretReference;
24+
import io.kubernetes.client.models.V1VolumeMount;
1925
import oracle.kubernetes.json.Description;
2026
import oracle.kubernetes.operator.LabelConstants;
2127
import oracle.kubernetes.operator.VersionConstants;
@@ -348,25 +354,33 @@ public String getDomainUid() {
348354
return Optional.ofNullable(spec.getDomainUid()).orElse(getMetadata().getName());
349355
}
350356

351-
public String getLogHome() {
357+
/**
358+
* Returns the path to the log home to be used by this domain. Null if the log home is disabled.
359+
* @return a path on a persistent volume, or null
360+
*/
361+
public @Nullable String getEffectiveLogHome() {
362+
return isLogHomeEnabled() ? getLogHome() : null;
363+
}
364+
365+
@Nonnull String getLogHome() {
352366
return Optional.ofNullable(spec.getLogHome())
353367
.orElse(String.format(LOG_HOME_DEFAULT_PATTERN, getDomainUid()));
354368
}
355369

356-
public boolean getLogHomeEnabled() {
357-
return spec.getLogHomeEnabled();
370+
boolean isLogHomeEnabled() {
371+
return spec.isLogHomeEnabled();
358372
}
359373

360374
public boolean isIncludeServerOutInPodLog() {
361375
return spec.getIncludeServerOutInPodLog();
362376
}
363377

364-
public boolean isDomainHomeInImage() {
378+
boolean isDomainHomeInImage() {
365379
return spec.isDomainHomeInImage();
366380
}
367381

368-
public boolean istioEnabled() {
369-
return spec.istioEnabled();
382+
public boolean isIstioEnabled() {
383+
return spec.isIstioEnabled();
370384
}
371385

372386
public int getIstioReadinessPort() {
@@ -399,7 +413,7 @@ public boolean isShuttingDown() {
399413
*
400414
* @return a list of names; may be empty
401415
*/
402-
public List<String> getAdminServerChannelNames() {
416+
List<String> getAdminServerChannelNames() {
403417
return getEffectiveConfigurationFactory().getAdminServerChannelNames();
404418
}
405419

@@ -460,4 +474,98 @@ public boolean equals(Object other) {
460474
.append(status, rhs.status)
461475
.isEquals();
462476
}
477+
478+
public List<String> getValidationFailures() {
479+
return new Validator().getValidationFailures();
480+
}
481+
482+
class Validator {
483+
static final String DUPLICATE_SERVER_NAME_FOUND
484+
= "More than one item under spec.managedServers in the domain resource has DNS-1123 name '%s'";
485+
static final String DUPLICATE_CLUSTER_NAME_FOUND
486+
= "More than one item under spec.clusters in the domain resource has DNS-1123 name '%s'";
487+
static final String LOG_HOME_PATH_NOT_MOUNTED
488+
= "No volume mount contains path for log home '%s', %s in the domain resource";
489+
static final String BAD_VOLUME_MOUNT_PATH
490+
= "The mount path '%s', in entry '%s' of domain resource additionalVolumeMounts, is not valid";
491+
492+
private List<String> failures = new ArrayList<>();
493+
private Set<String> clusterNames = new HashSet<>();
494+
private Set<String> serverNames = new HashSet<>();
495+
496+
List<String> getValidationFailures() {
497+
addDuplicateNames();
498+
addInvalidMountPaths();
499+
addUnmappedLogHome();
500+
501+
return failures;
502+
}
503+
504+
private void addDuplicateNames() {
505+
getSpec().getManagedServers()
506+
.stream()
507+
.map(ManagedServer::getServerName)
508+
.map(this::toDns1123LegalName)
509+
.forEach(this::checkDuplicateServerName);
510+
getSpec().getClusters()
511+
.stream()
512+
.map(Cluster::getClusterName)
513+
.map(this::toDns1123LegalName)
514+
.forEach(this::checkDuplicateClusterName);
515+
}
516+
517+
/**
518+
* Converts value to nearest DNS-1123 legal name, which can be used as a Kubernetes identifier.
519+
*
520+
* @param value Input value
521+
* @return nearest DNS-1123 legal name
522+
*/
523+
String toDns1123LegalName(String value) {
524+
return value.toLowerCase().replace('_', '-');
525+
}
526+
527+
private void checkDuplicateServerName(String s) {
528+
if (serverNames.contains(s))
529+
failures.add(String.format(DUPLICATE_SERVER_NAME_FOUND, s));
530+
else
531+
serverNames.add(s);
532+
}
533+
534+
private void checkDuplicateClusterName(String s) {
535+
if (clusterNames.contains(s))
536+
failures.add(String.format(DUPLICATE_CLUSTER_NAME_FOUND, s));
537+
else
538+
clusterNames.add(s);
539+
}
540+
541+
private void addInvalidMountPaths() {
542+
getSpec().getAdditionalVolumeMounts().forEach(this::checkValidMountPath);
543+
}
544+
545+
private void checkValidMountPath(V1VolumeMount mount) {
546+
if (!new File(mount.getMountPath()).isAbsolute())
547+
failures.add(String.format(BAD_VOLUME_MOUNT_PATH, mount.getMountPath(), mount.getName()));
548+
}
549+
550+
private void addUnmappedLogHome() {
551+
if (!isLogHomeEnabled()) return;
552+
553+
if (getSpec().getAdditionalVolumeMounts().stream().map(V1VolumeMount::getMountPath).noneMatch(this::mapsLogHome))
554+
failures.add(String.format(LOG_HOME_PATH_NOT_MOUNTED, getLogHome(), getLogHomeSource()));
555+
}
556+
557+
private String getLogHomeSource() {
558+
return getSpec().getLogHome() == null ? "implicit" : "specified";
559+
}
560+
561+
private boolean mapsLogHome(String mountPath) {
562+
return getLogHome().startsWith(separatorTerminated(mountPath));
563+
}
564+
565+
private String separatorTerminated(String path) {
566+
if (path.endsWith(File.separator)) return path;
567+
else return path + File.separator;
568+
}
569+
570+
}
463571
}

model/src/main/java/oracle/kubernetes/weblogic/domain/model/DomainSpec.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package oracle.kubernetes.weblogic.domain.model;
66

7+
import java.io.File;
78
import java.util.ArrayList;
89
import java.util.Collections;
910
import java.util.List;
@@ -379,7 +380,13 @@ String getLogHome() {
379380
}
380381

381382
public void setLogHome(String logHome) {
382-
this.logHome = logHome;
383+
this.logHome = Optional.ofNullable(logHome).map(this::validatePath).orElse(null);
384+
}
385+
386+
private String validatePath(String s) {
387+
if (s.isBlank()) return null;
388+
if (s.endsWith(File.separator)) return s;
389+
return s + File.separator;
383390
}
384391

385392
/**
@@ -388,7 +395,7 @@ public void setLogHome(String logHome) {
388395
* @since 2.0
389396
* @return log home enabled
390397
*/
391-
boolean getLogHomeEnabled() {
398+
boolean isLogHomeEnabled() {
392399
return Optional.ofNullable(logHomeEnabled).orElse(!isDomainHomeInImage());
393400
}
394401

@@ -510,10 +517,10 @@ public void setConfigOverrideSecrets(@Nullable List<String> overridesSecretNames
510517
*
511518
* @return istioEnabled
512519
*/
513-
boolean istioEnabled() {
520+
boolean isIstioEnabled() {
514521
return Optional.ofNullable(experimental)
515-
.map(e -> e.getIstio())
516-
.map(i -> i.getEnabled())
522+
.map(Experimental::getIstio)
523+
.map(Istio::getEnabled)
517524
.orElse(false);
518525
}
519526

@@ -524,8 +531,8 @@ boolean istioEnabled() {
524531
*/
525532
int getIstioReadinessPort() {
526533
return Optional.ofNullable(experimental)
527-
.map(e -> e.getIstio())
528-
.map(i -> i.getReadinessPort())
534+
.map(Experimental::getIstio)
535+
.map(Istio::getReadinessPort)
529536
.orElse(8888);
530537
}
531538

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.weblogic.domain.model;
6+
7+
/**
8+
* Describes a problem with a domain resource.
9+
*/
10+
public class DomainValidationFailure {
11+
private String reason;
12+
private String message;
13+
14+
DomainValidationFailure(String reason, String message) {
15+
this.reason = reason;
16+
this.message = message;
17+
}
18+
19+
/**
20+
* Returns the reason for the failure. This is a camel-cased string with no spaces.
21+
* @return the reason code
22+
*/
23+
public String getReason() {
24+
return reason;
25+
}
26+
27+
/**
28+
* Returns a human-readable description of the problem.
29+
* @return the descriptive message
30+
*/
31+
public String getMessage() {
32+
return message;
33+
}
34+
}

model/src/main/java/oracle/kubernetes/weblogic/domain/model/ManagedServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void setServerName(@Nonnull String serverName) {
3030
this.serverName = serverName;
3131
}
3232

33-
ManagedServer withServerName(@Nonnull String serverName) {
33+
public ManagedServer withServerName(@Nonnull String serverName) {
3434
setServerName(serverName);
3535
return this;
3636
}

model/src/test/java/oracle/kubernetes/weblogic/domain/DomainTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class DomainTestBase {
4242
private static final String VALUE2 = "value2";
4343
private static final V1SecretReference SECRET = new V1SecretReference().name("secret");
4444
private static final String NS = "test-namespace";
45-
private static final String DOMAIN_UID = "uid1";
45+
protected static final String DOMAIN_UID = "uid1";
4646
private static final String DOMAIN_V2_SAMPLE_YAML = "model/domain-sample.yaml";
4747
private static final String IMAGE = "myimage";
4848
private static final String PULL_SECRET_NAME = "pull-secret";

model/src/test/java/oracle/kubernetes/weblogic/domain/model/DomainV2Test.java

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,76 @@ protected DomainConfigurator configureDomain(Domain domain) {
6969
return new DomainCommonConfigurator(domain);
7070
}
7171

72+
@Test
73+
public void whenDomainOnPV_logHomeDefaultsToEnabled() {
74+
configureDomain(domain).withDomainHomeInImage(false);
75+
76+
assertThat(domain.isLogHomeEnabled(), is(true));
77+
}
78+
79+
@Test
80+
public void whenDomainOnPVAndLogHomeDisabled_returnOverride() {
81+
configureDomain(domain).withDomainHomeInImage(false).withLogHomeEnabled(false);
82+
83+
assertThat(domain.isLogHomeEnabled(), is(false));
84+
}
85+
86+
@Test
87+
public void whenDomainInImage_logHomeDefaultsToDisabled() {
88+
configureDomain(domain).withDomainHomeInImage(true);
89+
90+
assertThat(domain.isLogHomeEnabled(), is(false));
91+
}
92+
93+
@Test
94+
public void whenDomainInImageAndLogHomeEnabled_returnOverride() {
95+
configureDomain(domain).withDomainHomeInImage(true).withLogHomeEnabled(true);
96+
97+
assertThat(domain.isLogHomeEnabled(), is(true));
98+
}
99+
100+
@Test
101+
public void whenLogHomeSet_returnIt() {
102+
configureDomain(domain).withLogHome("/my/logs/");
103+
104+
assertThat(domain.getLogHome(), equalTo("/my/logs/"));
105+
}
106+
107+
@Test
108+
public void whenLogHomeSetWithoutTrailingSlash_appendOne() {
109+
configureDomain(domain).withLogHome("/my/logs");
110+
111+
assertThat(domain.getLogHome(), equalTo("/my/logs/"));
112+
}
113+
114+
@Test
115+
public void whenLogHomeSetNull_returnDefaultLogHome() {
116+
configureDomain(domain).withLogHome(null);
117+
118+
assertThat(domain.getLogHome(), equalTo("/shared/logs/" + DOMAIN_UID));
119+
}
120+
121+
@Test
122+
public void whenLogHomeSetToBlanks_returnDefaultLogHome() {
123+
configureDomain(domain).withLogHome(" ");
124+
125+
assertThat(domain.getLogHome(), equalTo("/shared/logs/" + DOMAIN_UID));
126+
}
127+
128+
@Test
129+
public void whenLogHomeDisabled_effectiveLogHomeIsNull() {
130+
configureDomain(domain).withLogHomeEnabled(false).withLogHome("/my/logs/");
131+
132+
assertThat(domain.getEffectiveLogHome(), nullValue());
133+
}
134+
135+
@Test
136+
public void whenLogHomeEnabled_effectiveLogHomeEqualsLogHome() {
137+
configureDomain(domain).withLogHomeEnabled(true).withLogHome("/my/logs/");
138+
139+
assertThat(domain.getEffectiveLogHome(), equalTo("/my/logs/"));
140+
}
141+
72142
@Test
73143
public void whenClusterNotConfiguredAndNoDomainReplicaCount_countIsZero() {
74144
assertThat(domain.getReplicaCount("nosuchcluster"), equalTo(0));
@@ -1363,30 +1433,30 @@ public void whenLogHomeNotSet_useDefault() {
13631433

13641434
@Test
13651435
public void whenLogHomeSet_useValue() {
1366-
configureDomain(domain).withLogHome("/custom/logs");
1436+
configureDomain(domain).withLogHome("/custom/logs/");
13671437

1368-
assertThat(domain.getLogHome(), equalTo("/custom/logs"));
1438+
assertThat(domain.getLogHome(), equalTo("/custom/logs/"));
13691439
}
13701440

13711441
@Test
13721442
public void whenDomainHomeInImage_logHomeNotEnabled() {
13731443
configureDomain(domain).withDomainHomeInImage(true);
13741444

1375-
assertThat(domain.getSpec().getLogHomeEnabled(), is(false));
1445+
assertThat(domain.getSpec().isLogHomeEnabled(), is(false));
13761446
}
13771447

13781448
@Test
13791449
public void whenDomainHomeNotInImage_logHomeEnabled() {
13801450
configureDomain(domain).withDomainHomeInImage(false);
13811451

1382-
assertThat(domain.getSpec().getLogHomeEnabled(), is(true));
1452+
assertThat(domain.getSpec().isLogHomeEnabled(), is(true));
13831453
}
13841454

13851455
@Test
13861456
public void whenLogHomeEnabledSet_useValue() {
13871457
configureDomain(domain).withLogHomeEnabled(true);
13881458

1389-
assertThat(domain.getSpec().getLogHomeEnabled(), is(true));
1459+
assertThat(domain.getSpec().isLogHomeEnabled(), is(true));
13901460
}
13911461

13921462
@Test

0 commit comments

Comments
 (0)