-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[issues-k8s-support] - Adding supported execution targets and conditi…
…onal OpenShift/Kubernetes JUnit5 extension behavior
- Loading branch information
Showing
12 changed files
with
557 additions
and
18 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
core/src/main/java/org/jboss/intersmash/application/k8s/KubernetesApplication.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,28 @@ | ||
/** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* 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 org.jboss.intersmash.application.k8s; | ||
|
||
import org.jboss.intersmash.application.Application; | ||
|
||
/** | ||
* Interface representing the Application on Kubernetes. | ||
* | ||
* This interface is not supposed to be implemented by user Applications. See the "Mapping of implemented provisioners" | ||
* section of Intersmash README.md file for the up-to-date list of supported end users Applications. | ||
*/ | ||
public interface KubernetesApplication extends Application { | ||
|
||
} |
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
71 changes: 71 additions & 0 deletions
71
core/src/main/java/org/jboss/intersmash/junit5/IntersmashExtensionHelper.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,71 @@ | ||
/** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* 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 org.jboss.intersmash.junit5; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.jboss.intersmash.annotations.Intersmash; | ||
import org.jboss.intersmash.application.k8s.KubernetesApplication; | ||
import org.jboss.intersmash.application.openshift.OpenShiftApplication; | ||
import org.jboss.intersmash.provision.Provisioner; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
public class IntersmashExtensionHelper { | ||
|
||
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create("org", "jboss", "intersmash", | ||
"IntersmashExtension"); | ||
private static final String INTERSMASH_SERVICES = "INTERSMASH_SERVICES"; | ||
private static final String INTERSMASH = "INTERSMASH"; | ||
|
||
public static Map<String, Provisioner> getProvisioners(ExtensionContext extensionContext) { | ||
ExtensionContext.Store store = extensionContext.getStore(NAMESPACE); | ||
Map<String, Provisioner> provisioners = (Map<String, Provisioner>) store.get(INTERSMASH_SERVICES); | ||
if (provisioners != null) { | ||
return provisioners; | ||
} else { | ||
store.put(INTERSMASH_SERVICES, new HashMap<String, Provisioner>()); | ||
return (Map<String, Provisioner>) store.get(INTERSMASH_SERVICES); | ||
} | ||
} | ||
|
||
public static Intersmash getIntersmash(ExtensionContext extensionContext) { | ||
ExtensionContext.Store store = extensionContext.getStore(NAMESPACE); | ||
Intersmash result = (Intersmash) store.get(INTERSMASH); | ||
if (result != null) { | ||
return result; | ||
} else { | ||
Intersmash[] intersmashes = extensionContext.getRequiredTestClass().getAnnotationsByType(Intersmash.class); | ||
Intersmash intersmash; | ||
if (intersmashes.length > 0) { | ||
store.put(INTERSMASH, intersmashes[0]); | ||
return (Intersmash) store.get(INTERSMASH); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public static Boolean isIntersmashTargetingOpenShift(ExtensionContext extensionContext) { | ||
return Arrays.stream(getIntersmash(extensionContext).value()) | ||
.anyMatch(app -> OpenShiftApplication.class.isAssignableFrom(app.value())); | ||
} | ||
|
||
public static Boolean isIntersmashTargetingKubernetes(ExtensionContext extensionContext) { | ||
return Arrays.stream(getIntersmash(extensionContext).value()) | ||
.anyMatch(app -> KubernetesApplication.class.isAssignableFrom(app.value())); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
core/src/main/java/org/jboss/intersmash/provision/k8s/KubernetesProvisioner.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,87 @@ | ||
/** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* 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 org.jboss.intersmash.provision.k8s; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import org.jboss.intersmash.application.k8s.KubernetesApplication; | ||
import org.jboss.intersmash.application.openshift.HasConfigMaps; | ||
import org.jboss.intersmash.application.openshift.HasSecrets; | ||
import org.jboss.intersmash.provision.Provisioner; | ||
import org.jboss.intersmash.provision.openshift.HasPods; | ||
import org.jboss.intersmash.provision.openshift.Scalable; | ||
|
||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.ConfigBuilder; | ||
import io.fabric8.kubernetes.client.DefaultKubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
|
||
/** | ||
* Provisioner that is supposed to deploy an application on Kubernetes. | ||
*/ | ||
public interface KubernetesProvisioner<T extends KubernetesApplication> extends Provisioner<T>, Scalable, HasPods { | ||
|
||
// TODO - check for aq new class of statics like XTF OpenShifts? | ||
KubernetesClient kubernetes = newKubernetesClient(); | ||
|
||
static KubernetesClient newKubernetesClient() { | ||
Config config = new ConfigBuilder() | ||
.build(); | ||
return new DefaultKubernetesClient(config); | ||
} | ||
|
||
@Override | ||
default void preDeploy() { | ||
// create secrets | ||
if (HasSecrets.class.isAssignableFrom(getApplication().getClass())) { | ||
((HasSecrets) getApplication()).getSecrets().forEach(s -> kubernetes.secrets().create(s)); | ||
} | ||
// create configMaps | ||
if (HasConfigMaps.class.isAssignableFrom(getApplication().getClass())) { | ||
((HasConfigMaps) getApplication()).getConfigMaps().forEach(c -> kubernetes.configMaps().create(c)); | ||
} | ||
} | ||
|
||
@Override | ||
default void postUndeploy() { | ||
// delete secrets | ||
if (HasSecrets.class.isAssignableFrom(getApplication().getClass())) { | ||
((HasSecrets) getApplication()).getSecrets().forEach(s -> kubernetes.secrets().delete(s)); | ||
} | ||
// delete configMaps | ||
if (HasConfigMaps.class.isAssignableFrom(getApplication().getClass())) { | ||
((HasConfigMaps) getApplication()).getConfigMaps().forEach(c -> kubernetes.configMaps().delete(c)); | ||
} | ||
} | ||
|
||
// TODO - check (use a static class method like XTF OpenShift::generateHostName ?) | ||
default String getUrl(String routeName, boolean secure) { | ||
String protocol = secure ? "https" : "http"; | ||
return protocol + "://" + kubernetes.getMasterUrl() + "-" + routeName + "-" | ||
+ kubernetes.getConfiguration().getNamespace(); | ||
} | ||
|
||
@Override | ||
default URL getURL() { | ||
try { | ||
return new URL(getUrl(getApplication().getName(), false)); | ||
} catch (MalformedURLException ex) { | ||
throw new RuntimeException( | ||
String.format("Failed to get an URL for the \"%s\" route", this.getClass().getSimpleName()), ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.