Skip to content

Commit f56b62b

Browse files
Adding extension class to build and push Operator image and more (#1589)
* script to build and push operator image to ocir for jenkins to work * Revert "script to build and push operator image to ocir for jenkins to work" This reverts commit a83f700. * script to build and push operator image to ocir for jenkins to work * Synchronization for one-time initialization logic * Add try finally * moving constants to a interface * Adding imagebuilder * adding imagebuilder class * build and push operator image in extension * fix command run * use BRANCH env var for JENKINS * fix image name for Jenkins * remove double REPO_NAME * remove double REPO_NAME * fix domain image name * fix domain image name * fix domain image name * checking operator helm release status * workaround for image check * adding assertions for checking docker image and helm release status * address review comments Co-authored-by: Ryan Eberhard <[email protected]>
1 parent d75f1e3 commit f56b62b

File tree

15 files changed

+535
-194
lines changed

15 files changed

+535
-194
lines changed

new-integration-tests/pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,6 @@
176176
<skip>${skipITs}</skip>
177177
</configuration>
178178
</execution>
179-
<execution>
180-
<id>buildoperator</id>
181-
<phase>pre-integration-test</phase>
182-
<goals>
183-
<goal>exec</goal>
184-
</goals>
185-
<configuration>
186-
<skip>${skipITs}</skip>
187-
<executable>../buildDockerImage.sh</executable>
188-
</configuration>
189-
</execution>
190179
</executions>
191180
</plugin>
192181
<plugin>

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

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.text.DateFormat;
77
import java.text.SimpleDateFormat;
88
import java.util.Arrays;
9-
import java.util.Base64;
109
import java.util.Collections;
1110
import java.util.Date;
1211
import java.util.HashMap;
@@ -48,11 +47,21 @@
4847

4948
import static java.util.concurrent.TimeUnit.MINUTES;
5049
import static java.util.concurrent.TimeUnit.SECONDS;
50+
import static oracle.weblogic.kubernetes.TestConstants.OPERATOR_CHART_DIR;
51+
import static oracle.weblogic.kubernetes.TestConstants.OPERATOR_RELEASE_NAME;
52+
import static oracle.weblogic.kubernetes.TestConstants.REPO_DUMMY_VALUE;
53+
import static oracle.weblogic.kubernetes.TestConstants.REPO_EMAIL;
54+
import static oracle.weblogic.kubernetes.TestConstants.REPO_NAME;
55+
import static oracle.weblogic.kubernetes.TestConstants.REPO_PASSWORD;
56+
import static oracle.weblogic.kubernetes.TestConstants.REPO_REGISTRY;
57+
import static oracle.weblogic.kubernetes.TestConstants.REPO_SECRET_NAME;
58+
import static oracle.weblogic.kubernetes.TestConstants.REPO_USERNAME;
5159
import static oracle.weblogic.kubernetes.actions.ActionConstants.ARCHIVE_DIR;
5260
import static oracle.weblogic.kubernetes.actions.ActionConstants.MODEL_DIR;
5361
import static oracle.weblogic.kubernetes.actions.ActionConstants.WDT_VERSION;
5462
import static oracle.weblogic.kubernetes.actions.ActionConstants.WIT_BUILD_DIR;
5563
import static oracle.weblogic.kubernetes.actions.TestActions.buildAppArchive;
64+
import static oracle.weblogic.kubernetes.actions.TestActions.createDockerConfigJson;
5665
import static oracle.weblogic.kubernetes.actions.TestActions.createDomainCustomResource;
5766
import static oracle.weblogic.kubernetes.actions.TestActions.createMiiImage;
5867
import static oracle.weblogic.kubernetes.actions.TestActions.createSecret;
@@ -65,18 +74,20 @@
6574
import static oracle.weblogic.kubernetes.actions.TestActions.deleteServiceAccount;
6675
import static oracle.weblogic.kubernetes.actions.TestActions.dockerLogin;
6776
import static oracle.weblogic.kubernetes.actions.TestActions.dockerPush;
68-
import static oracle.weblogic.kubernetes.actions.TestActions.helmList;
77+
import static oracle.weblogic.kubernetes.actions.TestActions.getOperatorImageName;
6978
import static oracle.weblogic.kubernetes.actions.TestActions.installOperator;
7079
import static oracle.weblogic.kubernetes.actions.TestActions.uninstallOperator;
71-
import static oracle.weblogic.kubernetes.assertions.TestAssertions.dockerImageExists;
80+
import static oracle.weblogic.kubernetes.assertions.TestAssertions.doesImageExist;
7281
import static oracle.weblogic.kubernetes.assertions.TestAssertions.domainExists;
82+
import static oracle.weblogic.kubernetes.assertions.TestAssertions.isHelmReleaseDeployed;
7383
import static oracle.weblogic.kubernetes.assertions.TestAssertions.operatorIsRunning;
7484
import static oracle.weblogic.kubernetes.assertions.TestAssertions.podExists;
7585
import static oracle.weblogic.kubernetes.assertions.TestAssertions.podReady;
7686
import static oracle.weblogic.kubernetes.assertions.TestAssertions.serviceExists;
7787
import static oracle.weblogic.kubernetes.utils.FileUtils.checkDirectory;
7888
import static org.awaitility.Awaitility.with;
7989
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
90+
import static org.junit.jupiter.api.Assertions.assertFalse;
8091
import static org.junit.jupiter.api.Assertions.assertNotNull;
8192
import static org.junit.jupiter.api.Assertions.assertTrue;
8293

@@ -86,14 +97,6 @@
8697
@IntegrationTest
8798
class ItMiiDomain implements LoggedTest {
8899

89-
// operator constants
90-
private static final String OPERATOR_RELEASE_NAME = "weblogic-operator";
91-
private static final String OPERATOR_CHART_DIR =
92-
"../kubernetes/charts/weblogic-operator";
93-
private static final String OPERATOR_IMAGE =
94-
"oracle/weblogic-kubernetes-operator:3.0.0";
95-
//"phx.ocir.io/weblogick8s/weblogic-kubernetes-operator:develop";
96-
97100
// mii constants
98101
private static final String WDT_MODEL_FILE = "model1-wls.yaml";
99102
private static final String MII_IMAGE_NAME = "mii-image";
@@ -108,14 +111,10 @@ class ItMiiDomain implements LoggedTest {
108111
private static String opNamespace = null;
109112
private static String domainNamespace = null;
110113
private static ConditionFactory withStandardRetryPolicy = null;
114+
private static String dockerConfigJson = "";
111115

112116
private String domainUid = "domain1";
113-
private String repoSecretName = "reposecret";
114117
private String miiImage = null;
115-
private String repoRegistry = "dummy";
116-
private String repoUserName = "dummy";
117-
private String repoPassword = "dummy";
118-
private String repoEmail = "dummy";
119118

120119
/**
121120
* Install Operator.
@@ -148,6 +147,33 @@ public static void initAll(@Namespaces(2) List<String> namespaces) {
148147
.name(serviceAccountName))));
149148
logger.info("Created service account: {0}", serviceAccountName);
150149

150+
// get Operator image name
151+
String operatorImage = getOperatorImageName();
152+
assertFalse(operatorImage.isEmpty(), "Operator image name can not be empty");
153+
logger.info("Operator image name {0}", operatorImage);
154+
155+
// Create docker registry secret in the operator namespace to pull the image from repository
156+
logger.info("Creating docker registry secret in namespace {0}", opNamespace);
157+
JsonObject dockerConfigJsonObject = createDockerConfigJson(
158+
REPO_USERNAME, REPO_PASSWORD, REPO_EMAIL, REPO_REGISTRY);
159+
dockerConfigJson = dockerConfigJsonObject.toString();
160+
161+
// Create the V1Secret configuration
162+
V1Secret repoSecret = new V1Secret()
163+
.metadata(new V1ObjectMeta()
164+
.name(REPO_SECRET_NAME)
165+
.namespace(opNamespace))
166+
.type("kubernetes.io/dockerconfigjson")
167+
.putDataItem(".dockerconfigjson", dockerConfigJson.getBytes());
168+
169+
boolean secretCreated = assertDoesNotThrow(() -> createSecret(repoSecret),
170+
String.format("createSecret failed for %s", REPO_SECRET_NAME));
171+
assertTrue(secretCreated, String.format("createSecret failed while creating secret %s in namespace",
172+
REPO_SECRET_NAME, opNamespace));
173+
174+
// map with secret
175+
Map<String, Object> secretNameMap = new HashMap<String, Object>();
176+
secretNameMap.put("name", REPO_SECRET_NAME);
151177
// helm install parameters
152178
opHelmParams = new HelmParams()
153179
.releaseName(OPERATOR_RELEASE_NAME)
@@ -158,7 +184,8 @@ public static void initAll(@Namespaces(2) List<String> namespaces) {
158184
OperatorParams opParams =
159185
new OperatorParams()
160186
.helmParams(opHelmParams)
161-
.image(OPERATOR_IMAGE)
187+
.image(operatorImage)
188+
.imagePullSecrets(secretNameMap)
162189
.domainNamespaces(Arrays.asList(domainNamespace))
163190
.serviceAccount(serviceAccountName);
164191

@@ -168,9 +195,14 @@ public static void initAll(@Namespaces(2) List<String> namespaces) {
168195
String.format("Operator install failed in namespace %s", opNamespace));
169196
logger.info("Operator installed in namespace {0}", opNamespace);
170197

171-
// list helm releases
172-
logger.info("List helm releases in namespace {0}", opNamespace);
173-
helmList(opHelmParams);
198+
// list helm releases matching Operator release name in operator namespace
199+
logger.info("Checking Operator release {0} status in namespace {1}",
200+
OPERATOR_RELEASE_NAME, opNamespace);
201+
assertTrue(isHelmReleaseDeployed(OPERATOR_RELEASE_NAME, opNamespace),
202+
String.format("Operator release %s is not in deployed status in namespace %s",
203+
OPERATOR_RELEASE_NAME, opNamespace));
204+
logger.info("Operator release {0} status is deployed in namespace {1}",
205+
OPERATOR_RELEASE_NAME, opNamespace);
174206

175207
// check operator is running
176208
logger.info("Check Operator pod is running in namespace {0}", opNamespace);
@@ -200,36 +232,25 @@ public void testCreateMiiDomain() {
200232
miiImage = createImageAndVerify();
201233

202234
// push the image to OCIR to make the test work in multi node cluster
203-
if (System.getenv("REPO_REGISTRY") != null && System.getenv("REPO_USERNAME") != null
204-
&& System.getenv("REPO_PASSWORD") != null && System.getenv("REPO_EMAIL") != null) {
205-
repoRegistry = System.getenv("REPO_REGISTRY");
206-
repoUserName = System.getenv("REPO_USERNAME");
207-
repoPassword = System.getenv("REPO_PASSWORD");
208-
repoEmail = System.getenv("REPO_EMAIL");
209-
235+
if (!REPO_USERNAME.equals(REPO_DUMMY_VALUE)) {
210236
logger.info("docker login");
211-
assertTrue(dockerLogin(repoRegistry, repoUserName, repoPassword), "docker login failed");
237+
assertTrue(dockerLogin(REPO_REGISTRY, REPO_USERNAME, REPO_PASSWORD), "docker login failed");
212238

213239
logger.info("docker push image {0} to OCIR", miiImage);
214240
assertTrue(dockerPush(miiImage), String.format("docker push failed for image %s", miiImage));
215241
}
216242

217-
// create docker registry secret in the domain namespace to pull the image from OCIR
218-
JsonObject dockerConfigJsonObject = getDockerConfigJson(
219-
repoUserName, repoPassword, repoEmail, repoRegistry);
220-
String dockerConfigJson = dockerConfigJsonObject.toString();
221-
222243
// Create the V1Secret configuration
223244
V1Secret repoSecret = new V1Secret()
224245
.metadata(new V1ObjectMeta()
225-
.name(repoSecretName)
246+
.name(REPO_SECRET_NAME)
226247
.namespace(domainNamespace))
227248
.type("kubernetes.io/dockerconfigjson")
228249
.putDataItem(".dockerconfigjson", dockerConfigJson.getBytes());
229250

230251
boolean secretCreated = assertDoesNotThrow(() -> createSecret(repoSecret),
231-
String.format("createSecret failed for %s", repoSecretName));
232-
assertTrue(secretCreated, String.format("createSecret failed while creating secret %s", repoSecretName));
252+
String.format("createSecret failed for %s", REPO_SECRET_NAME));
253+
assertTrue(secretCreated, String.format("createSecret failed while creating secret %s", REPO_SECRET_NAME));
233254

234255
// create secret for admin credentials
235256
logger.info("Create secret for admin credentials");
@@ -269,7 +290,7 @@ public void testCreateMiiDomain() {
269290
.domainHomeSourceType("FromModel")
270291
.image(miiImage)
271292
.addImagePullSecretsItem(new V1LocalObjectReference()
272-
.name(repoSecretName))
293+
.name(REPO_SECRET_NAME))
273294
.webLogicCredentialsSecret(new V1SecretReference()
274295
.name(adminSecretName)
275296
.namespace(domainNamespace))
@@ -287,7 +308,7 @@ public void testCreateMiiDomain() {
287308
.adminService(new AdminService()
288309
.addChannelsItem(new Channel()
289310
.channelName("default")
290-
.nodePort(30711))))
311+
.nodePort(0))))
291312
.addClustersItem(new Cluster()
292313
.clusterName("cluster-1")
293314
.replicas(replicaCount)
@@ -413,6 +434,9 @@ private String createImageAndVerify() {
413434
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
414435
Date date = new Date();
415436
final String imageTag = dateFormat.format(date) + "-" + System.currentTimeMillis();
437+
// Add repository name in image name for Jenkins runs
438+
final String imageName = REPO_USERNAME.equals(REPO_DUMMY_VALUE) ? MII_IMAGE_NAME : REPO_NAME + MII_IMAGE_NAME;
439+
final String image = imageName + ":" + imageTag;
416440

417441
// build the model file list
418442
final List<String> modelList = Collections.singletonList(MODEL_DIR + "/" + WDT_MODEL_FILE);
@@ -431,25 +455,29 @@ private String createImageAndVerify() {
431455
env.put("WLSIMG_BLDDIR", WIT_BUILD_DIR);
432456

433457
// build an image using WebLogic Image Tool
434-
logger.info("Create image {0}:{1} using model directory {2}",
435-
MII_IMAGE_NAME, imageTag, MODEL_DIR);
458+
logger.info("Create image {0} using model directory {1}", image, MODEL_DIR);
436459
boolean result = createMiiImage(
437460
defaultWitParams()
438-
.modelImageName(MII_IMAGE_NAME)
461+
.modelImageName(imageName)
439462
.modelImageTag(imageTag)
440463
.modelFiles(modelList)
441464
.modelArchiveFiles(archiveList)
442465
.wdtVersion(WDT_VERSION)
443466
.env(env)
444467
.redirect(true));
445468

446-
assertTrue(result, String.format("Failed to create the image %s using WebLogic Image Tool", MII_IMAGE_NAME));
469+
assertTrue(result, String.format("Failed to create the image %s using WebLogic Image Tool", image));
447470

448-
// check image exists
449-
assertTrue(dockerImageExists(MII_IMAGE_NAME, imageTag),
450-
String.format("Image %s doesn't exist", MII_IMAGE_NAME + ":" + imageTag));
471+
/* Check image exists using docker images | grep image tag.
472+
* Tag name is unique as it contains date and timestamp.
473+
* This is a workaround for the issue on Jenkins machine
474+
* as docker images imagename:imagetag is not working and
475+
* the test fails even though the image exists.
476+
*/
477+
assertTrue(doesImageExist(imageTag),
478+
String.format("Image %s doesn't exist", image));
451479

452-
return MII_IMAGE_NAME + ":" + imageTag;
480+
return image;
453481
}
454482

455483

@@ -498,19 +526,4 @@ private void checkServiceCreated(String serviceName) {
498526

499527
}
500528

501-
private static JsonObject getDockerConfigJson(String username, String password, String email, String registry) {
502-
JsonObject authObject = new JsonObject();
503-
authObject.addProperty("username", username);
504-
authObject.addProperty("password", password);
505-
authObject.addProperty("email", email);
506-
String auth = username + ":" + password;
507-
String authEncoded = Base64.getEncoder().encodeToString(auth.getBytes());
508-
System.out.println("auth encoded: " + authEncoded);
509-
authObject.addProperty("auth", authEncoded);
510-
JsonObject registryObject = new JsonObject();
511-
registryObject.add(registry, authObject);
512-
JsonObject configJsonObject = new JsonObject();
513-
configJsonObject.add("auths", registryObject);
514-
return configJsonObject;
515-
}
516529
}

0 commit comments

Comments
 (0)