mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
improve script readability
This commit is contained in:
@@ -30,32 +30,37 @@
|
|||||||
# At time of writing, its 'call point' was in
|
# At time of writing, its 'call point' was in
|
||||||
# https://github.com/kubernetes/test-infra/blob/master/jobs/config.json
|
# https://github.com/kubernetes/test-infra/blob/master/jobs/config.json
|
||||||
|
|
||||||
function exit_with {
|
function exitWith {
|
||||||
local msg=$1
|
local msg=$1
|
||||||
echo >&2 ${msg}
|
echo >&2 ${msg}
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
export -f exit_with
|
export -f exitWith
|
||||||
|
|
||||||
repo=kubernetes-sigs/kustomize
|
function expectCommand {
|
||||||
if [[ `pwd` != */$repo ]]; then
|
command -v $1 >/dev/null 2>&1 || \
|
||||||
exit_with "Script must be run from $repo"
|
{ exitWith "Expected $1 on PATH."; }
|
||||||
|
}
|
||||||
|
|
||||||
|
function setUpEnv {
|
||||||
|
local repo=$(git rev-parse --show-toplevel)
|
||||||
|
cd $repo
|
||||||
|
[[ $? -eq 0 ]] || "Failed to cd to $repo"
|
||||||
|
echo "pwd is " `pwd`
|
||||||
|
|
||||||
|
local expectedRepo=kubernetes-sigs/kustomize
|
||||||
|
if [[ `pwd` != */$expectedRepo ]]; then
|
||||||
|
exitWith "Script must be run from $expectedRepo"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo pwd is `pwd`
|
|
||||||
echo GOPATH is $GOPATH
|
|
||||||
echo PATH is $PATH
|
|
||||||
|
|
||||||
go install . || \
|
go install . || \
|
||||||
{ exit_with "Failed to install kustomize."; }
|
{ exitWith "Failed to install kustomize."; }
|
||||||
|
|
||||||
export PATH=$GOPATH/bin:$PATH
|
PATH=$GOPATH/bin:$PATH
|
||||||
|
|
||||||
command -v kustomize >/dev/null 2>&1 || \
|
expectCommand kustomize
|
||||||
{ exit_with "Require kustomize but it's not installed."; }
|
expectCommand kubectl
|
||||||
|
}
|
||||||
command -v kubectl >/dev/null 2>&1 || \
|
|
||||||
{ exit_with "Require kubectl but it's not installed."; }
|
|
||||||
|
|
||||||
function runTest {
|
function runTest {
|
||||||
local script=$1
|
local script=$1
|
||||||
@@ -63,16 +68,17 @@ function runTest {
|
|||||||
local args=$@
|
local args=$@
|
||||||
|
|
||||||
if [ ! -x "$script" ]; then
|
if [ ! -x "$script" ]; then
|
||||||
exit_with "Unable to run $script"
|
exitWith "Unable to run $script"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
$script "$args"
|
$script "$args"
|
||||||
if [ $? -ne 0 ]; then
|
[[ $? -eq 0 ]] || exitWith "Failed: $script $args"
|
||||||
exit_with "Failed: $script $args"
|
|
||||||
fi
|
|
||||||
echo "$script passed."
|
echo "$script passed."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setUpEnv
|
||||||
|
|
||||||
pushd demos
|
pushd demos
|
||||||
runTest ldap/integration_test.sh ldap/base
|
runTest ldap/integration_test.sh ldap/base
|
||||||
popd
|
popd
|
||||||
|
|||||||
@@ -30,68 +30,105 @@ set -x
|
|||||||
|
|
||||||
target=$1
|
target=$1
|
||||||
|
|
||||||
echo pwd is `pwd`
|
echo Kustomizing: \"$target\"
|
||||||
echo Kustomizing \"$target\"
|
|
||||||
ls $target
|
ls $target
|
||||||
kustomize build $target > generatedResources.yaml
|
|
||||||
[[ $? -eq 0 ]] || { exit_with "Failed to kustomize build"; }
|
|
||||||
|
|
||||||
cat generatedResources.yaml
|
tmpDir=$(mktemp -d)
|
||||||
|
|
||||||
|
function configureCluster {
|
||||||
|
kustomize build $target > $tmpDir/my.yaml
|
||||||
|
[[ $? -eq 0 ]] || { exitWith "Failed to kustomize build"; }
|
||||||
|
|
||||||
|
cat $tmpDir/my.yaml
|
||||||
|
|
||||||
# Setting the namespace this way is a test-infra thing?
|
# Setting the namespace this way is a test-infra thing?
|
||||||
kubectl config set-context \
|
kubectl config set-context \
|
||||||
$(kubectl config current-context) --namespace=default
|
$(kubectl config current-context) --namespace=default
|
||||||
|
|
||||||
kubectl apply -f generatedResources.yaml
|
kubectl apply -f $tmpDir/my.yaml
|
||||||
[[ $? -eq 0 ]] || { exit_with "Failed to run kubectl apply"; }
|
[[ $? -eq 0 ]] || { exitWith "Failed to run kubectl apply"; }
|
||||||
|
|
||||||
sleep 20
|
sleep 20
|
||||||
|
}
|
||||||
|
|
||||||
# get the pod and namespace
|
function tearDownCluster {
|
||||||
pod=$(kubectl get pods -l app=ldap -o jsonpath='{.items[0].metadata.name}')
|
kubectl delete -f $tmpDir/my.yaml
|
||||||
namespace=$(kubectl get pods -l app=ldap -o jsonpath='{.items[0].metadata.namespace}')
|
rm -rf $tmpDir
|
||||||
container="ldap"
|
}
|
||||||
[[ -z ${pod} ]] && { exit_with "Pod is not started successfully"; }
|
|
||||||
[[ -z ${namespace} ]] && { exit_with "Couldn't get namespace for Pod ${pod}"; }
|
|
||||||
|
|
||||||
# create a user ldif file locally
|
function getPodField {
|
||||||
ldiffile="user.ldif"
|
echo $(kubectl get pods -l app=ldap -o jsonpath=$1)
|
||||||
cat <<EOF >$ldiffile
|
}
|
||||||
|
|
||||||
|
function podExec {
|
||||||
|
kubectl exec $podName -c $containerName -- "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
function addUser {
|
||||||
|
local namespace=`getPodField '{.items[0].metadata.namespace}'`
|
||||||
|
[[ -z $namespace ]] && { exitWith "Unable to get namespace"; }
|
||||||
|
|
||||||
|
# Create a user ldif file
|
||||||
|
local userFile="user.ldif"
|
||||||
|
cat <<EOF >$tmpDir/$userFile
|
||||||
dn: cn=The Postmaster,dc=example,dc=org
|
dn: cn=The Postmaster,dc=example,dc=org
|
||||||
objectClass: organizationalRole
|
objectClass: organizationalRole
|
||||||
cn: The Postmaster
|
cn: The Postmaster
|
||||||
EOF
|
EOF
|
||||||
[[ -f ${ldiffile} ]] || { exit_with "Failed to create ldif file locally"; }
|
[[ -f $tmpDir/$userFile ]] || { exitWith "Failed to create $tmpDir/$userFile"; }
|
||||||
|
|
||||||
# add a user
|
kubectl cp $tmpDir/$userFile $namespace/$podName:/tmp/$userFile || \
|
||||||
pod_ldiffile="/tmp/user.ldif"
|
{ exitWith "Failed to copy ldif file to Pod $podName"; }
|
||||||
kubectl cp $ldiffile ${namespace}/${pod}:${pod_ldiffile} || \
|
|
||||||
{ exit_with "Failed to copy ldif file to Pod ${pod}"; }
|
|
||||||
|
|
||||||
kubectl exec ${pod} -c ${container} -- \
|
rm $tmpDir/$userFile
|
||||||
ldapadd -x -H ldap://localhost -D "cn=admin,dc=example,dc=org" -w admin \
|
|
||||||
-f ${pod_ldiffile} || { exit_with "Failed to add a user"; }
|
|
||||||
|
|
||||||
# query the added user
|
podExec \
|
||||||
r=$(kubectl exec ${pod} -c ${container} -- \
|
ldapadd \
|
||||||
ldapsearch -x -H ldap://localhost -b dc=example,dc=org \
|
-x \
|
||||||
-D "cn=admin,dc=example,dc=org" -w admin)
|
-w admin \
|
||||||
|
-H ldap://localhost \
|
||||||
|
-D "cn=admin,dc=example,dc=org" \
|
||||||
|
-f /tmp/$userFile
|
||||||
|
}
|
||||||
|
|
||||||
user_count=$(echo ${r} | grep "cn: The Postmaster" | wc -l)
|
function getUserCount {
|
||||||
[[ ${user_count} -eq 0 ]] && { exit_with "Couldn't find the new added user"; }
|
local result=$(\
|
||||||
|
podExec \
|
||||||
|
ldapsearch \
|
||||||
|
-x \
|
||||||
|
-w admin \
|
||||||
|
-H ldap://localhost \
|
||||||
|
-D "cn=admin,dc=example,dc=org" \
|
||||||
|
-b dc=example,dc=org \
|
||||||
|
)
|
||||||
|
return $(echo $result | grep "cn: The Postmaster" | wc -l)
|
||||||
|
}
|
||||||
|
|
||||||
# delete the added user
|
function deleteAddedUser {
|
||||||
kubectl exec ${pod} -c ${container} -- \
|
podExec \
|
||||||
ldapdelete -v -x -H ldap://localhost "cn=The Postmaster,dc=example,dc=org" \
|
ldapdelete \
|
||||||
-D "cn=admin,dc=example,dc=org" -w admin || \
|
-v \
|
||||||
{ exit_with "Failed to delete the user"; }
|
-x \
|
||||||
|
-w admin \
|
||||||
|
-H ldap://localhost \
|
||||||
|
-D "cn=admin,dc=example,dc=org" \
|
||||||
|
"cn=The Postmaster,dc=example,dc=org"
|
||||||
|
}
|
||||||
|
|
||||||
r=$(kubectl exec ${pod} -c ${container} -- \
|
configureCluster
|
||||||
ldapsearch -x -H ldap://localhost -b dc=example,dc=org \
|
|
||||||
-D "cn=admin,dc=example,dc=org" -w admin)
|
|
||||||
user_count=$(echo ${r} | grep "cn: The Postmaster" | wc -l)
|
|
||||||
[[ ${user_count} -ne 0 ]] && { exit_with "The user hasn't been deleted."; }
|
|
||||||
|
|
||||||
# kubectl delete
|
podName=`getPodField '{.items[0].metadata.name}'`
|
||||||
kubectl delete -f generatedResources.yaml
|
[[ -z $podName ]] && { exitWith "Unable to get pod name"; }
|
||||||
rm $ldiffile
|
|
||||||
|
echo pod is $podName
|
||||||
|
containerName="ldap"
|
||||||
|
|
||||||
|
getUserCount; [[ $? -eq 0 ]] || { exitWith "Expected no users."; }
|
||||||
|
|
||||||
|
addUser || { exitWith "Failed to add a user"; }
|
||||||
|
getUserCount; [[ $? -eq 1 ]] || { exitWith "Couldn't find the new added user"; }
|
||||||
|
|
||||||
|
deleteAddedUser || { exitWith "Failed to delete the user"; }
|
||||||
|
getUserCount; [[ $? -eq 0 ]] || { exitWith "User has not been deleted."; }
|
||||||
|
|
||||||
|
tearDownCluster
|
||||||
|
|||||||
Reference in New Issue
Block a user