Skip to content

Commit f9967d1

Browse files
author
Tom Barnes
committed
Add domainUID labels to k8s that didn't already have them. Add a new general purpose delete-domain.sh script.
1 parent d73d9a8 commit f9967d1

File tree

7 files changed

+220
-16
lines changed

7 files changed

+220
-16
lines changed

kubernetes/delete-domain.sh

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/bin/bash
2+
# Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
3+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
#
5+
# Description:
6+
# Use this script to delete a set of given domains, or all domains.
7+
#
8+
# Alternatively, run the script in a test mode to show what would
9+
# be deleted without actually performing the deletes.
10+
#
11+
# Usage:
12+
# See "function usage" below or call this script with no parameters.
13+
#
14+
15+
# default when to stop retrying (settable via command line)
16+
default_maxwaitsecs=90
17+
18+
# optional test mode that lists what would be deleted without
19+
# actually deleting (settable via command line)
20+
test_mode=false
21+
22+
23+
function usage {
24+
cat << EOF
25+
Usage:
26+
27+
$0 -d domain-uid,domain-uid,... [-s max-seconds] [-t]
28+
$0 -d domain-uid [-s max-seconds] [-t]
29+
$0 -d all [-s max-seconds] [-t]
30+
$0 -h
31+
32+
Perform a best-effort delete of the k8s artifacts for
33+
the given domain(s), and retry until either max-seconds is reached
34+
or all artifacts were deleted (default $default_maxwaitsecs seconds).
35+
The domains can be specified as a comma-separated list of
36+
domain-uids (no spaces), or the keyword 'all'.
37+
38+
Specify '-t' to run the script in a test mode which will
39+
show the delete commands without actually performing them.
40+
41+
This script exits with a zero status on success, and a
42+
non-zero status on failure.
43+
EOF
44+
}
45+
46+
47+
#
48+
# getDomain
49+
# - get all k8s artifacts for domain $1 using label search weblogic.domainUID in $1
50+
# - if $1 has special value "all" then get the k8s artifacts for all domains
51+
#
52+
function getDomain {
53+
if [ "$1" = "all" ]; then
54+
local label_selector="weblogic.domainUID"
55+
else
56+
local label_selector="weblogic.domainUID in ($1)"
57+
fi
58+
59+
# get all namespaced types with -l $label_selector
60+
61+
local namespaced_types="pod,job,deploy,rs,service,pvc,ingress,cm,serviceaccount,role,rolebinding,secret"
62+
63+
# if domain crd exists, look for domains too:
64+
kubectl get crd domains.weblogic.oracle > /dev/null 2>&1
65+
if [ $? -eq 0 ]; then
66+
namespaced_types="domain,$namespaced_types"
67+
fi
68+
69+
kubectl get $namespaced_types \
70+
-l "$label_selector" \
71+
-o=jsonpath='{range .items[*]}{.kind}{" "}{.metadata.name}{" -n "}{.metadata.namespace}{"\n"}{end}'
72+
73+
# get all non-namespaced types with -l $label_selector
74+
75+
kubectl get pv,crd,clusterroles,clusterrolebindings \
76+
-l "$label_selector" \
77+
-o=jsonpath='{range .items[*]}{.kind}{" "}{.metadata.name}{"\n"}{end}'
78+
}
79+
80+
#
81+
# deleteDomain
82+
# - delete all k8s artifacts for domain $1 and retry up to $2 seconds
83+
# - if $1 has special value "all" then delete the k8s artifacts for all domains
84+
# - $2 is optional, default is $default_maxwaitsecs
85+
# - if $test_mode is true, show deletes but don't actually perform them
86+
function deleteDomain {
87+
88+
if [ "$test_mode" = "true" ]; then
89+
echo @@ Test mode. Delete commands for kubernetes artifacts with label weblogic.domainUID \'$1\'.
90+
else
91+
echo @@ Deleting kubernetes artifacts with label weblogic.domainUID \'$1\'.
92+
fi
93+
94+
local maxwaitsecs=${2:-$default_maxwaitsecs}
95+
local tempfile="/tmp/getdomain.tmp.$1.$$"
96+
local mstart=`date +%s`
97+
98+
while : ; do
99+
getDomain $1 > $tempfile
100+
local count=`wc -l $tempfile | awk '{ print $1 }'`
101+
102+
local mnow=`date +%s`
103+
104+
echo @@ $count objects remaining after $((mnow - mstart)) seconds. Max wait is $maxwaitsecs seconds.
105+
if [ $count -eq 0 ]; then
106+
echo @@ Success.
107+
rm -f $tempfile
108+
exit 0
109+
fi
110+
111+
if [ $((mnow - mstart)) -gt $maxwaitsecs ]; then
112+
echo @@ Error. Max wait of $maxwaitsecs seconds exceeded with $count objects remaining. giving up. Remaining objects:
113+
cat $tempfile
114+
rm -f $tempfile
115+
exit $count
116+
fi
117+
118+
cat $tempfile | while read line; do
119+
if [ "$test_mode" = "true" ]; then
120+
echo kubectl delete $line --ignore-not-found
121+
else
122+
kubectl delete $line --ignore-not-found
123+
fi
124+
done
125+
sleep 3
126+
done
127+
}
128+
129+
domains=""
130+
131+
# parse command line options
132+
while getopts ":d:s:th" opt; do
133+
case $opt in
134+
d) domains="${OPTARG}"
135+
;;
136+
137+
s) maxwaitsecs="${OPTARG}"
138+
;;
139+
140+
t) test_mode="true"
141+
;;
142+
143+
h) usage
144+
exit 0
145+
;;
146+
147+
*) usage
148+
exit 9999
149+
;;
150+
esac
151+
done
152+
153+
if [ "$domains" = "" ]; then
154+
usage
155+
exit 9999
156+
fi
157+
158+
if [ ! -x "$(command -v kubectl)" ]; then
159+
echo "@@ Error. kubectl is not installed."
160+
exit 9999
161+
fi
162+
163+
deleteDomain "${domains}" "${maxwaitsecs}"

kubernetes/internal/domain-custom-resource-template.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ kind: Domain
88
metadata:
99
name: %DOMAIN_UID%
1010
namespace: %NAMESPACE%
11+
labels:
12+
weblogic.domainUID: %DOMAIN_UID%
1113
spec:
1214
# The domainUID must be unique across the entire Kubernetes Cluster. Each WebLogic Domain must
1315
# have its own unique domainUID. This does not have to be the same as the Domain Name. It is allowed

kubernetes/internal/domain-job-template.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ kind: ConfigMap
55
metadata:
66
name: domain-%DOMAIN_UID%-scripts
77
namespace: %NAMESPACE%
8+
labels:
9+
weblogic.domainUID: %DOMAIN_UID%
810
data:
911
utility.sh: |-
1012
#!/bin/bash

kubernetes/internal/traefik-deployment-template.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ kind: ServiceAccount
44
metadata:
55
name: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
66
namespace: %NAMESPACE%
7+
labels:
8+
weblogic.domainUID: %DOMAIN_UID%
79
---
810
kind: Deployment
911
apiVersion: extensions/v1beta1
@@ -12,6 +14,7 @@ metadata:
1214
namespace: %NAMESPACE%
1315
labels:
1416
app: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
17+
weblogic.domainUID: %DOMAIN_UID%
1518
spec:
1619
replicas: 1
1720
selector:
@@ -21,6 +24,7 @@ spec:
2124
metadata:
2225
labels:
2326
app: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
27+
weblogic.domainUID: %DOMAIN_UID%
2428
spec:
2529
serviceAccountName: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
2630
terminationGracePeriodSeconds: 60
@@ -74,6 +78,7 @@ metadata:
7478
namespace: %NAMESPACE%
7579
labels:
7680
app: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
81+
weblogic.domainUID: %DOMAIN_UID%
7782
data:
7883
traefik.toml: |
7984
# traefik.toml
@@ -93,6 +98,8 @@ apiVersion: v1
9398
metadata:
9499
name: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
95100
namespace: %NAMESPACE%
101+
labels:
102+
weblogic.domainUID: %DOMAIN_UID%
96103
spec:
97104
selector:
98105
app: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
@@ -110,6 +117,7 @@ metadata:
110117
namespace: %NAMESPACE%
111118
labels:
112119
app: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
120+
weblogic.domainUID: %DOMAIN_UID%
113121
spec:
114122
selector:
115123
app: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik

kubernetes/internal/traefik-rbac-template.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ kind: ClusterRole
33
apiVersion: rbac.authorization.k8s.io/v1beta1
44
metadata:
55
name: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
6+
labels:
7+
weblogic.domainUID: %DOMAIN_UID%
68
rules:
79
- apiGroups:
810
- ""
@@ -28,6 +30,8 @@ kind: ClusterRoleBinding
2830
apiVersion: rbac.authorization.k8s.io/v1beta1
2931
metadata:
3032
name: %DOMAIN_UID%-%CLUSTER_NAME_LC%-traefik
33+
labels:
34+
weblogic.domainUID: %DOMAIN_UID%
3135
roleRef:
3236
apiGroup: rbac.authorization.k8s.io
3337
kind: ClusterRole

src/integration-tests/bash/run.sh

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -716,15 +716,15 @@ function run_create_domain_job {
716716
mkdir -p $tmp_dir
717717

718718
local CREDENTIAL_NAME="$DOMAIN_UID-weblogic-credentials"
719-
local CREDENTIAL_FILE="${tmp_dir}/$CREDENTIAL_NAME.yaml"
719+
local CREDENTIAL_FILE="${tmp_dir}/weblogic-credentials.yaml"
720720

721721
trace 'Create the secret with weblogic admin credentials'
722-
cp $CUSTOM_YAML/domain1-weblogic-credentials.yaml $CREDENTIAL_FILE
722+
cp $CUSTOM_YAML/weblogic-credentials-template.yaml $CREDENTIAL_FILE
723723

724-
sed -i -e "s|namespace: default|namespace: $NAMESPACE|g" $CREDENTIAL_FILE
725-
sed -i -e "s|name: domain1-weblogic-credentials|name: $CREDENTIAL_NAME|g" $CREDENTIAL_FILE
724+
sed -i -e "s|%NAMESPACE%|$NAMESPACE|g" $CREDENTIAL_FILE
725+
sed -i -e "s|%DOMAIN_UID%|$DOMAIN_UID|g" $CREDENTIAL_FILE
726726

727-
kubectl apply -f $CREDENTIAL_FILE -n $NAMESPACE
727+
kubectl apply -f $CREDENTIAL_FILE
728728

729729
trace 'Check secret'
730730
local ADMINSECRET=`kubectl get secret $CREDENTIAL_NAME -n $NAMESPACE | grep $CREDENTIAL_NAME | wc -l `
@@ -816,8 +816,8 @@ function deploy_webapp_via_REST {
816816
local ADMIN_PORT="`dom_get $1 ADMIN_PORT`"
817817
local TMP_DIR="`dom_get $1 TMP_DIR`"
818818

819-
local WLS_ADMIN_USERNAME="`get_wladmin_user`"
820-
local WLS_ADMIN_PASSWORD="`get_wladmin_pass`"
819+
local WLS_ADMIN_USERNAME="`get_wladmin_user $1`"
820+
local WLS_ADMIN_PASSWORD="`get_wladmin_pass $1`"
821821

822822
local AS_NAME="$DOMAIN_UID-admin-server"
823823

@@ -1098,8 +1098,8 @@ function verify_admin_server_ext_service {
10981098
local NAMESPACE="`dom_get $1 NAMESPACE`"
10991099
local DOMAIN_UID="`dom_get $1 DOMAIN_UID`"
11001100
local TMP_DIR="`dom_get $1 TMP_DIR`"
1101-
local WLS_ADMIN_USERNAME="`get_wladmin_user`"
1102-
local WLS_ADMIN_PASSWORD="`get_wladmin_pass`"
1101+
local WLS_ADMIN_USERNAME="`get_wladmin_user $1`"
1102+
local WLS_ADMIN_PASSWORD="`get_wladmin_pass $1`"
11031103

11041104
local ADMIN_SERVER_NODEPORT_SERVICE="$DOMAIN_UID-admin-server"
11051105

@@ -1480,23 +1480,30 @@ function check_pv {
14801480
}
14811481

14821482
function get_wladmin_cred {
1483-
if [ "$#" != 1 ]; then
1484-
fail "requires one parameter, keyword 'username' or 'password'."
1483+
if [ "$#" != 2 ]; then
1484+
fail "requires two parameters: domainKey and keyword 'username' or 'password'."
14851485
fi
14861486
# All domains use the same user/pass
1487-
if ! val=`grep "^ $1:" $CUSTOM_YAML/domain1-weblogic-credentials.yaml | awk '{ print $2 }' | base64 -d`
1487+
local TMP_DIR="`dom_get $1 TMP_DIR`"
1488+
if ! val=`grep "^ $2:" $TMP_DIR/weblogic-credentials.yaml | awk '{ print $2 }' | base64 -d`
14881489
then
14891490
fail "get_wladmin_cred: Could not determine $1"
14901491
fi
14911492
echo $val
14921493
}
14931494

14941495
function get_wladmin_pass {
1495-
get_wladmin_cred password
1496+
if [ "$#" != 1 ] ; then
1497+
fail "requires 1 parameter: domainKey"
1498+
fi
1499+
get_wladmin_cred $1 password
14961500
}
14971501

14981502
function get_wladmin_user {
1499-
get_wladmin_cred username
1503+
if [ "$#" != 1 ] ; then
1504+
fail "requires 1 parameter: domainKey"
1505+
fi
1506+
get_wladmin_cred $1 username
15001507
}
15011508

15021509
function verify_wlst_access {
@@ -1550,8 +1557,8 @@ function run_wlst_script {
15501557
local TMP_DIR="`dom_get $1 TMP_DIR`"
15511558
local AS_NAME="$DOMAIN_UID-admin-server"
15521559
local location="$2"
1553-
local username=`get_wladmin_user`
1554-
local password=`get_wladmin_pass`
1560+
local username=`get_wladmin_user $1`
1561+
local password=`get_wladmin_pass $1`
15551562
local pyfile_lcl="$3"
15561563
local pyfile_pod="/shared/`basename $pyfile_lcl`"
15571564
local t3url_lcl="t3://$NODEPORT_HOST:$ADMIN_WLST_PORT"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This is an example of how to set up the Secret with the WebLogic Admin credentials.
2+
# Please note that this method used encoded, but not encrypted data.
3+
# We recommend that you use the command line alternative from a secure environment instead,
4+
# i.e. kubectl create secret ...
5+
apiVersion: v1
6+
kind: Secret
7+
metadata:
8+
# Note that this MUST match the name defined in the Domain Custom Resource
9+
name: %DOMAIN_UID%-weblogic-credentials
10+
namespace: %NAMESPACE%
11+
labels:
12+
weblogic.domainUID: %DOMAIN_UID%
13+
type: Opaque
14+
data:
15+
# data is base64 encoded like this: `echo -n "weblogic" | base64`
16+
# In this example, username is weblogic, password is welcome1
17+
username: d2VibG9naWM=
18+
password: d2VsY29tZTE=

0 commit comments

Comments
 (0)