Skip to content

Commit 4a48c01

Browse files
committed
Add sample for creating weblogic credentials secret
Signed-off-by: doxiao <[email protected]>
1 parent acb6e08 commit 4a48c01

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

kubernetes/samples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Sample scripts
44

5+
* [Sample Secret for WebLogic Amin Credentials](scripts/create-weblogic-domain/create-weblogic-credentials.sh) for creating a Kubernetes secret that contains the admin server credentials. This secret can then be used in creating a WebLogic domain custom resource.
56
* [Sample PV and PVC](scripts/create-weblogic-domain-pv-pvc/README.md) for creating a PV or PVC that can be used by a domain custom resource as the persistent storage for the WebLogic domain home or log files.
67
* [Sample domain home on a persistent volume](scripts/create-weblogic-domain/domain-home-on-pv/README.md) for creating a WebLogic domain home on an existing PV or PVC, and the domain customer resource YAML file for deploying the generated WebLogic domain.
78
* [Sample Elasticsearch and Kibana configuration](scripts/elasticsearch_and_kibana.yaml) for configuring the Elasticsearch and Kibana deployments and services for the operator's logs.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env 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+
# This sample script creates a Kubernetes secret for WebLogic domain admin credentials.
7+
#
8+
# The following pre-requisites must be handled prior to running this script:
9+
# * The kubernetes namespace must already be created
10+
#
11+
12+
# Initialize
13+
script="${BASH_SOURCE[0]}"
14+
scriptDir="$( cd "$( dirname "${script}" )" && pwd )"
15+
# source ${scriptDir}/../common/utility.sh
16+
# source ${scriptDir}/../common/validate.sh
17+
18+
#
19+
# Function to exit and print an error message
20+
# $1 - text of message
21+
function fail {
22+
echo [ERROR] $*
23+
exit 1
24+
}
25+
26+
# try to execute kubectl to see whether kubectl is available
27+
function validateKubectlAvailable {
28+
if ! [ -x "$(command -v kubectl)" ]; then
29+
fail "kubectl is not installed"
30+
fi
31+
}
32+
33+
function usage {
34+
echo usage: ${script} -u userName -p password [-d domainUID] [-n name] [-h]
35+
echo " -u username, must be specified."
36+
echo " -p password, must be specified."
37+
echo " -n namespace, optional."
38+
echo " -d domainUID, optional."
39+
echo " -h Help"
40+
exit $1
41+
}
42+
43+
#
44+
# Parse the command line options
45+
#
46+
domainUID=domain1
47+
namespace=default
48+
while getopts "hu:p:n:d:" opt; do
49+
case $opt in
50+
u) username="${OPTARG}"
51+
;;
52+
p) password="${OPTARG}"
53+
;;
54+
n) namespace="${OPTARG}"
55+
;;
56+
d) domainUID="${OPTARG}"
57+
;;
58+
h) usage 0
59+
;;
60+
*) usage 1
61+
;;
62+
esac
63+
done
64+
secretName=$domainUID-weblogic-credentials
65+
66+
if [ -z ${username} ]; then
67+
echo "${script}: -u must be specified."
68+
missingRequiredOption="true"
69+
fi
70+
71+
if [ -z ${password} ]; then
72+
echo "${script}: -p must be specified."
73+
missingRequiredOption="true"
74+
fi
75+
76+
if [ "${missingRequiredOption}" == "true" ]; then
77+
usage 1
78+
fi
79+
80+
#
81+
# Function to validate the domain secret
82+
#
83+
function validateDomainSecret {
84+
# Verify the secret exists
85+
local SECRET=`kubectl get secret ${secretName} -n ${namespace} | grep ${secretName} | wc | awk ' { print $1; }'`
86+
if [ "${SECRET}" != "1" ]; then
87+
fail "The secret ${secretName} was not found in namespace ${namespace}"
88+
fi
89+
90+
# Verify the secret contains a username
91+
SECRET=`kubectl get secret ${secretName} -n ${namespace} -o jsonpath='{.data}'| grep username: | wc | awk ' { print $1; }'`
92+
if [ "${SECRET}" != "1" ]; then
93+
fail "The domain secret ${secretName} in namespace ${namespace} does contain a username"
94+
fi
95+
96+
# Verify the secret contains a password
97+
SECRET=`kubectl get secret ${secretName} -n ${namespace} -o jsonpath='{.data}'| grep password: | wc | awk ' { print $1; }'`
98+
if [ "${SECRET}" != "1" ]; then
99+
fail "The domain secret ${secretName} in namespace ${namespace} does contain a password"
100+
fi
101+
echo "The secret ${secretName} has been successfully created in namespace ${namespace}"
102+
}
103+
104+
#
105+
# Perform the following sequence of steps to create a domain
106+
#
107+
108+
result=`kubectl get secret ${secretName} -n ${namespace} --ignore-not-found=true | grep ${secretName} | wc | awk ' { print $1; }'`
109+
if [ "${result:=Error}" != "0" ]; then
110+
fail "The secret ${secretName} already exists in namespace ${namespace}."
111+
fi
112+
113+
kubectl -n $namespace create secret generic $secretName \
114+
--from-literal=username=$username \
115+
--from-literal=password=$password
116+
117+
kubectl label secret ${secretName} -n $namespace weblogic.domainUID=$domainUID weblogic.domainName=$domainUID
118+
119+
validateDomainSecret
120+
121+
echo
122+
echo Completed
123+
124+

0 commit comments

Comments
 (0)