Skip to content

Commit 30a51ce

Browse files
authored
Owls 87209 - Domain lifecycle scripts fix in cases where domain/cluster specs are overridden at the individual managed server level (#2222)
* Fix to start and stop servers in cases where there are pre-existing details under spec.managedServers. * Changes to remove server element when unsetting serverStartPolicy if spec has only serverStartPolicy
1 parent f9588f0 commit 30a51ce

File tree

1 file changed

+37
-7
lines changed
  • kubernetes/samples/scripts/domain-lifecycle

1 file changed

+37
-7
lines changed

kubernetes/samples/scripts/domain-lifecycle/helper.sh

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,24 @@ function createServerStartPolicyPatch {
111111
local policy=$3
112112
local __result=$4
113113
local currentStartPolicy=""
114+
local serverStartPolicyPatch=""
114115

115116
# Get server start policy for this server
116117
getServerPolicy "${domainJson}" "${serverName}" currentStartPolicy
117-
if [ -z "${currentStartPolicy}" ]; then
118+
managedServers=$(echo ${domainJson} | jq -cr '(.spec.managedServers)')
119+
if [[ -z "${currentStartPolicy}" && "${managedServers}" == "null" ]]; then
118120
# Server start policy doesn't exist, add a new policy
119121
addPolicyCmd=".[.| length] |= . + {\"serverName\":\"${serverName}\", \
120122
\"serverStartPolicy\":\"${policy}\"}"
121123
serverStartPolicyPatch=$(echo ${domainJson} | jq .spec.managedServers | jq -c "${addPolicyCmd}")
124+
elif [ "${managedServers}" != "null" ]; then
125+
extractSpecCmd="(.spec.managedServers)"
126+
mapCmd="\
127+
. |= (map(.serverName) | index (\"${serverName}\")) as \$idx | \
128+
if \$idx then \
129+
.[\$idx][\"serverStartPolicy\"] = \"${policy}\" \
130+
else .+ [{serverName: \"${serverName}\" , serverStartPolicy: \"${policy}\"}] end"
131+
serverStartPolicyPatch=$(echo ${domainJson} | jq "${extractSpecCmd}" | jq "${mapCmd}")
122132
else
123133
# Server start policy exists, replace policy value
124134
replacePolicyCmd="(.spec.managedServers[] \
@@ -142,9 +152,7 @@ function createPatchJsonToUnsetPolicyAndUpdateReplica {
142152
local replicaPatch=$3
143153
local __result=$4
144154

145-
replacePolicyCmd="[(.spec.managedServers[] \
146-
| select (.serverName != \"${serverName}\"))]"
147-
serverStartPolicyPatch=$(echo ${domainJson} | jq "${replacePolicyCmd}")
155+
unsetServerStartPolicy "${domainJson}" "${serverName}" serverStartPolicyPatch
148156
patchJson="{\"spec\": {\"clusters\": "${replicaPatch}",\"managedServers\": "${serverStartPolicyPatch}"}}"
149157
eval $__result="'${patchJson}'"
150158
}
@@ -199,13 +207,35 @@ function createPatchJsonToUnsetPolicy {
199207
local serverName=$2
200208
local __result=$3
201209

202-
replacePolicyCmd="[(.spec.managedServers[] \
203-
| select (.serverName != \"${serverName}\"))]"
204-
serverStartPolicyPatch=$(echo ${domainJson} | jq "${replacePolicyCmd}")
210+
unsetServerStartPolicy "${domainJson}" "${serverName}" serverStartPolicyPatch
205211
patchJson="{\"spec\": {\"managedServers\": "${serverStartPolicyPatch}"}}"
206212
eval $__result="'${patchJson}'"
207213
}
208214

215+
#
216+
# Function to create patch string with server start policy unset
217+
# $1 - Domain resource in json format
218+
# $2 - Name of server whose policy will be unset
219+
# $3 - Return value containing patch string with server start policy unset
220+
#
221+
function unsetServerStartPolicy {
222+
local domainJson=$1
223+
local serverName=$2
224+
local __result=$3
225+
local unsetStartPolicyPatch=""
226+
227+
unsetCmd="(.spec.managedServers[] | select (.serverName == \"${serverName}\") | del (.serverStartPolicy))"
228+
replacePolicyCmd=$(echo ${domainJson} | jq -cr "${unsetCmd}")
229+
replacePolicyCmdLen=$(echo "${replacePolicyCmd}" | jq -e keys_unsorted | jq length)
230+
if [ ${replacePolicyCmdLen} == 1 ]; then
231+
mapCmd=". |= map(if .serverName == \"${serverName}\" then del(.) else . end)"
232+
else
233+
mapCmd=". |= map(if .serverName == \"${serverName}\" then . = ${replacePolicyCmd} else . end)"
234+
fi
235+
unsetStartPolicyPatch=$(echo ${domainJson} | jq "(.spec.managedServers)" | jq "${mapCmd}")
236+
eval $__result="'${unsetStartPolicyPatch}'"
237+
}
238+
209239
#
210240
# Function to create patch json to update cluster server start policy
211241
# $1 - Domain resource in json format

0 commit comments

Comments
 (0)