Skip to content

Commit

Permalink
Merge branch 'xc-114350' into 'main'
Browse files Browse the repository at this point in the history
create Service account only if it does not exist

See merge request weblogic-cloud/weblogic-kubernetes-operator!4542
  • Loading branch information
rjeberhard committed Jan 19, 2024
2 parents 54e54ac + bc4d01d commit 8afd42b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
// Copyright (c) 2020, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package oracle.weblogic.kubernetes;
Expand Down Expand Up @@ -311,6 +311,7 @@ void testDeleteOperatorButNotDomain() {
+ "after the operator was deleted");
assertTrue(checkManagedServerConfiguration(domain1Namespace, domain1Uid));
} finally {
cleanUpSA(opNamespace);
if (!isDomain1Running) {
cleanUpDomainSecrets(domain1Namespace);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) 2020, 2021, Oracle and/or its affiliates.
// Copyright (c) 2020, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package oracle.weblogic.kubernetes.actions.impl;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1ServiceAccount;
import io.kubernetes.client.openapi.models.V1ServiceAccountList;
import oracle.weblogic.kubernetes.actions.impl.primitive.Kubernetes;

public class ServiceAccount {
Expand All @@ -31,4 +32,22 @@ public static boolean create(V1ServiceAccount serviceAccount) throws ApiExceptio
public static boolean delete(String name, String namespace) {
return Kubernetes.deleteServiceAccount(name, namespace);
}

/**
* Verify whether the service account exists in the namespace.
* @param name name of the service account
* @param namespace namespace where the service account exits
* @return true if the service account exists, false otherwise
*/
public static boolean serviceAccountExists(String name, String namespace) {
V1ServiceAccountList sas = Kubernetes.listServiceAccounts(namespace);
if (sas != null) {
for (V1ServiceAccount sa : sas.getItems()) {
if (sa.getMetadata().getName().equals(name)) {
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, 2023, Oracle and/or its affiliates.
// Copyright (c) 2021, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package oracle.weblogic.kubernetes.utils;
Expand All @@ -11,6 +11,7 @@
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1ServiceAccount;
import oracle.weblogic.kubernetes.actions.impl.OperatorParams;
import oracle.weblogic.kubernetes.actions.impl.ServiceAccount;
import oracle.weblogic.kubernetes.actions.impl.primitive.Command;
import oracle.weblogic.kubernetes.actions.impl.primitive.CommandParams;
import oracle.weblogic.kubernetes.actions.impl.primitive.HelmParams;
Expand Down Expand Up @@ -441,12 +442,14 @@ public static OperatorParams installAndVerifyOperator(String opNamespace,
LoggingFacade logger = getLogger();

// Create a service account for the unique opNamespace
logger.info("Creating service account");
assertDoesNotThrow(() -> createServiceAccount(new V1ServiceAccount()
.metadata(new V1ObjectMeta()
.namespace(opNamespace)
.name(opServiceAccount))));
logger.info("Created service account: {0}", opServiceAccount);
if (!ServiceAccount.serviceAccountExists(opServiceAccount, opNamespace)) {
logger.info("Creating service account");
assertDoesNotThrow(() -> createServiceAccount(new V1ServiceAccount()
.metadata(new V1ObjectMeta()
.namespace(opNamespace)
.name(opServiceAccount))));
logger.info("Created service account: {0}", opServiceAccount);
}

operatorImage = getOperatorImageName();
if (ARM) {
Expand Down Expand Up @@ -647,13 +650,14 @@ public static OperatorParams installAndVerifyOperator(String opNamespace,
LoggingFacade logger = getLogger();

// Create a service account for the unique opNamespace
logger.info("Creating service account");
assertDoesNotThrow(() -> createServiceAccount(new V1ServiceAccount()
.metadata(new V1ObjectMeta()
.namespace(opNamespace)
.name(opServiceAccount))));
logger.info("Created service account: {0}", opServiceAccount);

if (!ServiceAccount.serviceAccountExists(opServiceAccount, opNamespace)) {
logger.info("Creating service account");
assertDoesNotThrow(() -> createServiceAccount(new V1ServiceAccount()
.metadata(new V1ObjectMeta()
.namespace(opNamespace)
.name(opServiceAccount))));
logger.info("Created service account: {0}", opServiceAccount);
}

// get operator image name
String operatorImage = getOperatorImageName();
Expand Down

0 comments on commit 8afd42b

Please sign in to comment.