forked from dekorateio/dekorate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split the route decorator to allow custom resources
Relates quarkusio/quarkus#29899
- Loading branch information
Showing
13 changed files
with
419 additions
and
39 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...ft-annotations/src/main/java/io/dekorate/openshift/decorator/AddHostToRouteDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright 2018 The original authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.dekorate.openshift.decorator; | ||
|
||
import static io.dekorate.openshift.decorator.AddRouteDecorator.KIND_ROUTE; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import io.dekorate.ConfigReference; | ||
import io.dekorate.WithConfigReferences; | ||
import io.dekorate.doc.Description; | ||
import io.dekorate.kubernetes.decorator.Decorator; | ||
import io.dekorate.kubernetes.decorator.NamedResourceDecorator; | ||
import io.dekorate.openshift.config.OpenshiftConfig; | ||
import io.dekorate.utils.Strings; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.openshift.api.model.RouteSpecFluent; | ||
|
||
@Description("Add the host to the Route resource.") | ||
public class AddHostToRouteDecorator extends NamedResourceDecorator<RouteSpecFluent<?>> implements WithConfigReferences { | ||
|
||
private final OpenshiftConfig config; | ||
|
||
public AddHostToRouteDecorator(OpenshiftConfig config) { | ||
super(KIND_ROUTE, config.getName()); | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public void andThenVisit(RouteSpecFluent<?> spec, ObjectMeta resourceMeta) { | ||
if (!spec.hasHost() && config.getRoute() != null && Strings.isNotNullOrEmpty(config.getRoute().getHost())) { | ||
spec.withHost(config.getRoute().getHost()); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<? extends Decorator>[] after() { | ||
return new Class[] { AddRouteDecorator.class }; | ||
} | ||
|
||
@Override | ||
public List<ConfigReference> getConfigReferences() { | ||
return Arrays.asList(buildConfigReferenceHost()); | ||
} | ||
|
||
private ConfigReference buildConfigReferenceHost() { | ||
String property = "host"; | ||
String path = "(kind == Route && metadata.name == " + getName() + ").spec.host"; | ||
return new ConfigReference(property, path); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...ft-annotations/src/main/java/io/dekorate/openshift/decorator/AddPortToRouteDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright 2018 The original authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.dekorate.openshift.decorator; | ||
|
||
import static io.dekorate.utils.Ports.getHttpPort; | ||
import static io.dekorate.utils.Ports.getPortByFilter; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import io.dekorate.ConfigReference; | ||
import io.dekorate.WithConfigReferences; | ||
import io.dekorate.doc.Description; | ||
import io.dekorate.kubernetes.config.Port; | ||
import io.dekorate.kubernetes.decorator.Decorator; | ||
import io.dekorate.kubernetes.decorator.NamedResourceDecorator; | ||
import io.dekorate.openshift.config.OpenshiftConfig; | ||
import io.dekorate.utils.Strings; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.openshift.api.model.RouteSpecFluent; | ||
|
||
@Description("Add the port to the Route resource.") | ||
public class AddPortToRouteDecorator extends NamedResourceDecorator<RouteSpecFluent<?>> implements WithConfigReferences { | ||
|
||
private final OpenshiftConfig config; | ||
|
||
public AddPortToRouteDecorator(OpenshiftConfig config) { | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public void andThenVisit(RouteSpecFluent<?> spec, ObjectMeta resourceMeta) { | ||
Optional<Port> port = getNamedHttpPort(config); | ||
if (!port.isPresent()) { | ||
return; | ||
} | ||
|
||
if (!spec.hasPath()) { | ||
spec.withPath(port.get().getPath()); | ||
} | ||
|
||
if (!spec.hasPort()) { | ||
spec.editOrNewPort() | ||
.withNewTargetPort(port.get().getName()) | ||
.endPort(); | ||
} | ||
} | ||
|
||
private Optional<Port> getNamedHttpPort(OpenshiftConfig config) { | ||
String namedPortName = config.getRoute().getTargetPort(); | ||
if (Strings.isNotNullOrEmpty(namedPortName)) { | ||
Optional<Port> port = getPortByFilter(p -> Strings.equals(p.getName(), namedPortName), config); | ||
if (port.isPresent()) { | ||
return port; | ||
} | ||
|
||
// Set the named port to the one provided by the user even though it was not found in the configured ports. | ||
return Optional.of(Port.newBuilder().withName(namedPortName).build()); | ||
} | ||
|
||
return getHttpPort(config); | ||
} | ||
|
||
@Override | ||
public Class<? extends Decorator>[] after() { | ||
return new Class[] { AddRouteDecorator.class }; | ||
} | ||
|
||
@Override | ||
public List<ConfigReference> getConfigReferences() { | ||
return Arrays.asList(buildConfigReferencePath()); | ||
} | ||
|
||
private ConfigReference buildConfigReferencePath() { | ||
String property = "path"; | ||
String path = "(kind == Route && metadata.name == " + getName() + ").spec.path"; | ||
return new ConfigReference(property, path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...annotations/src/main/java/io/dekorate/openshift/decorator/AddServiceToRouteDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright 2018 The original authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.dekorate.openshift.decorator; | ||
|
||
import static io.dekorate.openshift.decorator.AddRouteDecorator.KIND_ROUTE; | ||
|
||
import io.dekorate.doc.Description; | ||
import io.dekorate.kubernetes.decorator.Decorator; | ||
import io.dekorate.kubernetes.decorator.NamedResourceDecorator; | ||
import io.dekorate.openshift.config.OpenshiftConfig; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.openshift.api.model.RouteSpecFluent; | ||
|
||
@Description("Add the service to the Route resource.") | ||
public class AddServiceToRouteDecorator extends NamedResourceDecorator<RouteSpecFluent<?>> { | ||
|
||
private final OpenshiftConfig config; | ||
|
||
public AddServiceToRouteDecorator(OpenshiftConfig config) { | ||
super(KIND_ROUTE, config.getName()); | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public void andThenVisit(RouteSpecFluent<?> spec, ObjectMeta resourceMeta) { | ||
if (!spec.hasTo()) { | ||
spec.withNewTo() | ||
.withKind("Service") | ||
.withName(config.getName()) | ||
.endTo(); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<? extends Decorator>[] after() { | ||
return new Class[] { AddRouteDecorator.class }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>dekorate-tests</artifactId> | ||
<groupId>io.dekorate</groupId> | ||
<version>3.1-SNAPSHOT</version> | ||
<relativePath>../</relativePath> | ||
</parent> | ||
|
||
<groupId>io.dekorate</groupId> | ||
<artifactId>issue-existitng-route-resource</artifactId> | ||
<name>Dekorate :: Tests :: Annotations :: Existing route resources</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.dekorate</groupId> | ||
<artifactId>openshift-annotations</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.dekorate</groupId> | ||
<artifactId>dekorate-spring-boot</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<version>${version.spring-boot}</version> | ||
</dependency> | ||
|
||
<!-- Testing --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>${version.junit-jupiter}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>${version.junit-jupiter}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<inherited>true</inherited> | ||
<configuration> | ||
<useSystemClassLoader>false</useSystemClassLoader> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<version>${version.spring-boot}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Oops, something went wrong.