Skip to content

Commit 1834a76

Browse files
xiancaorjeberhard
andauthored
Nginx mii new it (#1614)
* init commit * use kubernetes API call to create ingress * add more debug info for Jenkins run * add more debug info for Jenkins run * add more debug info for Jenkins run * remove debug info for Jenkins run * remove unused imports * uninstall nginx release for now * cleanup * more cleanup * cleanup * change DynamicClusterSize in sample wdt model file * resolve merge conflicts * fix with Mark's comments Co-authored-by: Ryan Eberhard <[email protected]>
1 parent 9adfe1a commit 1834a76

File tree

13 files changed

+1170
-41
lines changed

13 files changed

+1170
-41
lines changed

new-integration-tests/src/test/java/oracle/weblogic/kubernetes/ItSimpleNginxValidation.java

Lines changed: 620 additions & 0 deletions
Large diffs are not rendered by default.

new-integration-tests/src/test/java/oracle/weblogic/kubernetes/TestConstants.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
package oracle.weblogic.kubernetes;
55

6+
import java.net.InetAddress;
67
import java.util.Optional;
78

9+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
10+
811
public interface TestConstants {
912

1013
// operator constants
@@ -30,8 +33,14 @@ public interface TestConstants {
3033
.orElse("");
3134
public static final String BRANCH_NAME_FROM_JENKINS = Optional.ofNullable(System.getenv("BRANCH"))
3235
.orElse("");
36+
public static final String K8S_NODEPORT_HOST = Optional.ofNullable(System.getenv("K8S_NODEPORT_HOST"))
37+
.orElse(assertDoesNotThrow(() -> InetAddress.getLocalHost().getHostName()));
38+
public static final String GOOGLE_REPO_URL = "https://kubernetes-charts.storage.googleapis.com/";
3339
public static final String LOGS_DIR = System.getenv().getOrDefault("RESULT_ROOT",
3440
System.getProperty("java.io.tmpdir")) + "/diagnosticlogs";
3541
public static final String PV_ROOT = System.getenv().getOrDefault("PV_ROOT",
3642
System.getProperty("java.io.tmpdir")) + "/ittestspvroot";
43+
public static final String NGINX_RELEASE_NAME = "nginx-release" + BUILD_ID;
44+
public static final String STABLE_REPO_NAME = "stable";
45+
public static final String NGINX_CHART_NAME = "nginx-ingress";
3746
}

new-integration-tests/src/test/java/oracle/weblogic/kubernetes/actions/TestActions.java

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
import oracle.weblogic.kubernetes.actions.impl.Domain;
2626
import oracle.weblogic.kubernetes.actions.impl.Exec;
2727
import oracle.weblogic.kubernetes.actions.impl.Namespace;
28+
import oracle.weblogic.kubernetes.actions.impl.Nginx;
29+
import oracle.weblogic.kubernetes.actions.impl.NginxParams;
2830
import oracle.weblogic.kubernetes.actions.impl.Operator;
2931
import oracle.weblogic.kubernetes.actions.impl.OperatorParams;
3032
import oracle.weblogic.kubernetes.actions.impl.PersistentVolume;
3133
import oracle.weblogic.kubernetes.actions.impl.PersistentVolumeClaim;
3234
import oracle.weblogic.kubernetes.actions.impl.Secret;
3335
import oracle.weblogic.kubernetes.actions.impl.Service;
3436
import oracle.weblogic.kubernetes.actions.impl.ServiceAccount;
35-
import oracle.weblogic.kubernetes.actions.impl.Traefik;
36-
import oracle.weblogic.kubernetes.actions.impl.TraefikParams;
3737
import oracle.weblogic.kubernetes.actions.impl.primitive.Docker;
3838
import oracle.weblogic.kubernetes.actions.impl.primitive.Helm;
3939
import oracle.weblogic.kubernetes.actions.impl.primitive.HelmParams;
@@ -45,13 +45,12 @@
4545
// detail impl classes - tests would only ever call methods in here, never
4646
// directly call the methods in the impl classes
4747
public class TestActions {
48-
4948
// ---------------------- operator ---------------------------------
5049

5150
/**
5251
* Install WebLogic Kubernetes Operator.
5352
*
54-
* @param params operator parameters for helm values
53+
* @param params operator parameters for Helm values
5554
* @return true if the operator is successfully installed, false otherwise.
5655
*/
5756
public static boolean installOperator(OperatorParams params) {
@@ -61,7 +60,7 @@ public static boolean installOperator(OperatorParams params) {
6160
/**
6261
* Upgrade existing Operator release.
6362
*
64-
* @param params operator parameters for helm values
63+
* @param params operator parameters for Helm values
6564
* @return true if the operator is successfully upgraded, false otherwise.
6665
*/
6766
public static boolean upgradeOperator(OperatorParams params) {
@@ -83,7 +82,7 @@ public static boolean scaleDomain(String domainUid, String clusterName, int numO
8382
/**
8483
* Uninstall the Operator release.
8584
*
86-
* @param params the parameters to helm uninstall command, release name and namespace
85+
* @param params the parameters to Helm uninstall command, release name and namespace
8786
* @return true on success, false otherwise
8887
*/
8988

@@ -192,26 +191,52 @@ public static boolean patchDomainCustomResource(String domainUid, String namespa
192191
return Domain.patchDomainCustomResource(domainUid, namespace, patch, patchFormat);
193192
}
194193

195-
// ------------------------ ingress controller ----------------------
194+
// ------------------------ Ingress Controller ----------------------
196195

197196
/**
198-
* Install Traefik Operator.
197+
* Install NGINX ingress controller.
199198
*
200-
* @param params parameters for helm values
199+
* @param params the parameters to Helm install command, such as release name, namespace, repo url,
200+
* repo name and chart name
201201
* @return true on success, false otherwise
202202
*/
203-
public static boolean installTraefik(TraefikParams params) {
204-
return Traefik.install(params);
203+
public static boolean installNginx(NginxParams params) {
204+
return Nginx.install(params);
205205
}
206206

207207
/**
208-
* Create Treafik Ingress.
208+
* Create an ingress for the WebLogic domain with domainUid in the specified domain namespace.
209209
*
210-
* @param valuesYaml values yaml file to be used
210+
* @param domainNamespace the WebLogic domain namespace in which to create the ingress
211+
* @param domainUid WebLogic domainUid which is backend to the ingress
212+
* @param clusterName the name of the WebLogic domain cluster
213+
* @param managedServerPort the port number of the WebLogic domain managed servers
211214
* @return true on success, false otherwise
215+
* @throws ApiException if Kubernetes client API call fails
212216
*/
213-
public static boolean createIngress(String valuesYaml) {
214-
return Traefik.createIngress(valuesYaml);
217+
public static boolean createIngress(String domainNamespace, String domainUid, String clusterName,
218+
int managedServerPort) throws ApiException {
219+
return Nginx.createIngress(domainNamespace, domainUid, clusterName, managedServerPort);
220+
}
221+
222+
/**
223+
* Upgrade NGINX release.
224+
*
225+
* @param params the parameters to Helm upgrade command, such as release name and http/https nodeport
226+
* @return true on success, false otherwise
227+
*/
228+
public static boolean upgradeNginx(NginxParams params) {
229+
return Nginx.upgrade(params);
230+
}
231+
232+
/**
233+
* Uninstall the NGINX release.
234+
*
235+
* @param params the parameters to Helm uninstall command, such as release name and namespace
236+
* @return true on success, false otherwise
237+
*/
238+
public static boolean uninstallNginx(HelmParams params) {
239+
return Nginx.uninstall(params);
215240
}
216241

217242
// ------------------------- namespaces -------------------------------
@@ -454,7 +479,7 @@ public static boolean deleteClusterRoleBinding(String name) {
454479
return ClusterRoleBinding.delete(name);
455480
}
456481

457-
// ----------------------- helm -----------------------------------
482+
// ----------------------- Helm -----------------------------------
458483

459484
/**
460485
* List releases.
@@ -535,6 +560,19 @@ public static JsonObject createDockerConfigJson(String username, String password
535560
return Docker.createDockerConfigJson(username, password, email, registry);
536561
}
537562

563+
// ------------------------ Ingress -------------------------------------
564+
565+
/**
566+
* Get a list of ingress names in the specified namespace.
567+
*
568+
* @param namespace in which to list all the ingresses
569+
* @return list of ingress names in the specified namespace
570+
* @throws ApiException if Kubernetes client API call fails
571+
*/
572+
public static List<String> getIngressList(String namespace) throws ApiException {
573+
return Nginx.getIngressList(namespace);
574+
}
575+
538576
// ----------------------- Execute a Command ---------------------------
539577

540578
/**
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package oracle.weblogic.kubernetes.actions.impl;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
10+
import io.kubernetes.client.custom.IntOrString;
11+
import io.kubernetes.client.openapi.ApiException;
12+
import io.kubernetes.client.openapi.models.ExtensionsV1beta1HTTPIngressPath;
13+
import io.kubernetes.client.openapi.models.ExtensionsV1beta1HTTPIngressRuleValue;
14+
import io.kubernetes.client.openapi.models.ExtensionsV1beta1Ingress;
15+
import io.kubernetes.client.openapi.models.ExtensionsV1beta1IngressBackend;
16+
import io.kubernetes.client.openapi.models.ExtensionsV1beta1IngressList;
17+
import io.kubernetes.client.openapi.models.ExtensionsV1beta1IngressRule;
18+
import io.kubernetes.client.openapi.models.ExtensionsV1beta1IngressSpec;
19+
import io.kubernetes.client.openapi.models.V1ObjectMeta;
20+
import oracle.weblogic.kubernetes.actions.impl.primitive.Helm;
21+
import oracle.weblogic.kubernetes.actions.impl.primitive.HelmParams;
22+
import oracle.weblogic.kubernetes.actions.impl.primitive.Kubernetes;
23+
24+
import static oracle.weblogic.kubernetes.extensions.LoggedTest.logger;
25+
26+
/**
27+
* Utility class for NGINX ingress controller.
28+
*/
29+
public class Nginx {
30+
31+
private static final String INGRESS_API_VERSION = "extensions/v1beta1";
32+
private static final String INGRESS_KIND = "Ingress";
33+
private static final String INGRESS_NGINX_CLASS = "nginx";
34+
35+
/**
36+
* Install NGINX Helm chart.
37+
*
38+
* @param params the parameters to Helm install command such as release name, namespace, repo url or chart dir,
39+
* chart name and chart values
40+
* @return true on success, false otherwise
41+
*/
42+
public static boolean install(NginxParams params) {
43+
return Helm.install(params.getHelmParams(), params.getValues());
44+
}
45+
46+
/**
47+
* Upgrade NGINX Helm release.
48+
*
49+
* @param params the parameters to Helm upgrade command such as release name, namespace and chart values to override
50+
* @return true on success, false otherwise
51+
*/
52+
public static boolean upgrade(NginxParams params) {
53+
return Helm.upgrade(params.getHelmParams(), params.getValues());
54+
}
55+
56+
/**
57+
* Uninstall NGINX Helm release.
58+
*
59+
* @param params the parameters to Helm uninstall command such as release name and namespace
60+
* @return true on success, false otherwise
61+
*/
62+
public static boolean uninstall(HelmParams params) {
63+
return Helm.uninstall(params);
64+
}
65+
66+
/**
67+
* Create an ingress for the WebLogic domain with domainUid in the specified domain namespace.
68+
*
69+
* @param domainNamespace the WebLogic domain namespace in which the ingress will be created
70+
* @param domainUid the WebLogic domainUid which is backend to the ingress
71+
* @param clusterName the name of the WebLogic domain cluster
72+
* @param managedServerPort the port number of the WebLogic domain managed servers
73+
* @return true on success, false otherwise
74+
* @throws ApiException if Kubernetes client API call fails
75+
*/
76+
public static boolean createIngress(String domainNamespace,
77+
String domainUid,
78+
String clusterName,
79+
int managedServerPort) throws ApiException {
80+
81+
// set the annotation for kubernetes.io/ingress.class to "nginx"
82+
HashMap<String, String> annotation = new HashMap<>();
83+
annotation.put("kubernetes.io/ingress.class", INGRESS_NGINX_CLASS);
84+
85+
// set the http ingress paths
86+
ExtensionsV1beta1HTTPIngressPath httpIngressPath = new ExtensionsV1beta1HTTPIngressPath()
87+
.path(null)
88+
.backend(new ExtensionsV1beta1IngressBackend()
89+
.serviceName(domainUid + "-cluster-" + clusterName.toLowerCase().replace("_", "-"))
90+
.servicePort(new IntOrString(managedServerPort))
91+
);
92+
ArrayList<ExtensionsV1beta1HTTPIngressPath> httpIngressPaths = new ArrayList<>();
93+
httpIngressPaths.add(httpIngressPath);
94+
95+
// set the ingress rule
96+
ExtensionsV1beta1IngressRule ingressRule = new ExtensionsV1beta1IngressRule()
97+
.host(domainUid + ".test")
98+
.http(new ExtensionsV1beta1HTTPIngressRuleValue()
99+
.paths(httpIngressPaths)
100+
);
101+
ArrayList<ExtensionsV1beta1IngressRule> ingressRules = new ArrayList<>();
102+
ingressRules.add(ingressRule);
103+
104+
// set the ingress
105+
ExtensionsV1beta1Ingress ingress = new ExtensionsV1beta1Ingress()
106+
.apiVersion(INGRESS_API_VERSION)
107+
.kind(INGRESS_KIND)
108+
.metadata(new V1ObjectMeta()
109+
.name(domainUid + "-nginx")
110+
.namespace(domainNamespace)
111+
.annotations(annotation))
112+
.spec(new ExtensionsV1beta1IngressSpec()
113+
.rules(ingressRules));
114+
115+
// create the ingress
116+
try {
117+
Kubernetes.createIngress(domainNamespace, ingress);
118+
} catch (ApiException apex) {
119+
logger.warning(apex.getResponseBody());
120+
throw apex;
121+
}
122+
return true;
123+
}
124+
125+
/**
126+
* Get a list of ingress names in the specified namespace.
127+
*
128+
* @param namespace the namespace to which the ingresses belong
129+
* @return a list of ingress names in the namespace
130+
* @throws ApiException if Kubernetes client API call fails
131+
*/
132+
public static List<String> getIngressList(String namespace) throws ApiException {
133+
134+
List<String> ingressNames = new ArrayList<>();
135+
ExtensionsV1beta1IngressList ingressList = Kubernetes.listNamespacedIngresses(namespace);
136+
List<ExtensionsV1beta1Ingress> listOfIngress = ingressList.getItems();
137+
138+
listOfIngress.forEach(ingress -> {
139+
if (ingress.getMetadata() != null) {
140+
ingressNames.add(ingress.getMetadata().getName());
141+
}
142+
});
143+
144+
return ingressNames;
145+
}
146+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package oracle.weblogic.kubernetes.actions.impl;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Objects;
9+
10+
import oracle.weblogic.kubernetes.actions.impl.primitive.HelmParams;
11+
12+
/**
13+
* All parameters needed to install NGINX ingress controller.
14+
*/
15+
public class NginxParams {
16+
17+
// Only add the values which need to be updated here.
18+
// The default values can be found here:
19+
// https://github.com/helm/charts/blob/master/stable/nginx-ingress/values.yaml
20+
private static final String NODEPORTS_HTTP = "controller.service.nodePorts.http";
21+
private static final String NODEPORTS_HTTPS = "controller.service.nodePorts.https";
22+
23+
// Adding some of the most commonly used params for now
24+
private int nodePortsHttp;
25+
private int nodePortsHttps;
26+
private HelmParams helmParams;
27+
28+
public NginxParams nodePortsHttp(int nodePortsHttp) {
29+
this.nodePortsHttp = nodePortsHttp;
30+
return this;
31+
}
32+
33+
public NginxParams nodePortsHttps(int nodePortsHttps) {
34+
this.nodePortsHttps = nodePortsHttps;
35+
return this;
36+
}
37+
38+
public NginxParams helmParams(HelmParams helmParams) {
39+
this.helmParams = helmParams;
40+
return this;
41+
}
42+
43+
public HelmParams getHelmParams() {
44+
return helmParams;
45+
}
46+
47+
/**
48+
* Loads Helm values into a value map.
49+
*
50+
* @return Map of values
51+
*/
52+
public Map<String, Object> getValues() {
53+
Map<String, Object> values = new HashMap<>();
54+
55+
if (nodePortsHttp > 0) {
56+
values.put(NODEPORTS_HTTP, nodePortsHttp);
57+
}
58+
if (nodePortsHttps > 0) {
59+
values.put(NODEPORTS_HTTPS, nodePortsHttps);
60+
}
61+
62+
values.values().removeIf(Objects::isNull);
63+
return values;
64+
}
65+
}

0 commit comments

Comments
 (0)