Skip to content

Commit

Permalink
Improve logging process (nephio-project#247)
Browse files Browse the repository at this point in the history
* Drop unused assertion

Signed-off-by: Victor Morales <[email protected]>

* Reduce log entries

Signed-off-by: Victor Morales <[email protected]>

* Simplify kubectl logs instruction

Signed-off-by: Victor Morales <[email protected]>

* Fix shell format

Signed-off-by: Victor Morales <[email protected]>

* Use kubectl wait instruction for checking packagerev lifecycles

Signed-off-by: Victor Morales <[email protected]>

* Improve kubeconfig files creation

Signed-off-by: Victor Morales <[email protected]>

* Add porch logs

Signed-off-by: Victor Morales <[email protected]>

* Ignore Normal events in logs

Signed-off-by: Victor Morales <[email protected]>

* Change dependency order for OAI SMF NF

* Change dependency order in OAI RAN NFs

Signed-off-by: Victor Morales <[email protected]>

* Remove unnecessary porch waiting entries

Signed-off-by: Victor Morales <[email protected]>

* Delete porch_wait_log_entry function

Signed-off-by: Victor Morales <[email protected]>

* Propagate kpt_wait_pkg timeout value to _wait_for_user_repo func

Signed-off-by: Victor Morales <[email protected]>

* Wait for nfdeployment resources

Signed-off-by: Victor Morales <[email protected]>

* Refactor OAI waiting RAN functions

Signed-off-by: Victor Morales <[email protected]>

* Fix jsonpath in porch_wait_published_packagerev function

Signed-off-by: Victor Morales <[email protected]>

---------

Signed-off-by: Victor Morales <[email protected]>
  • Loading branch information
electrocucaracha authored Feb 16, 2024
1 parent fb1c08d commit e175645
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 173 deletions.
13 changes: 0 additions & 13 deletions e2e/lib/_assertions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ fi
# shellcheck source=e2e/lib/kpt.sh
source "${E2EDIR:-$HOME/test-infra/e2e}/lib/kpt.sh"

# assert_non_empty() - This assertion checks if the expected value is not empty
function assert_non_empty {
local input=$1
local error_msg=$2

if [[ $DEBUG == "true" ]]; then
debug "NonEmpty Assertion - value: $1"
fi
if [ -z "$input" ]; then
error "$error_msg"
fi
}

# assert_are_equal() - This assertion checks if the inputs are equal
function assert_are_equal {
local input=$1
Expand Down
73 changes: 33 additions & 40 deletions e2e/lib/k8s.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,25 @@ function k8s_wait_exists {
local kubeconfig=${3:-"$HOME/.kube/config"}
local resource_namespace=${4:-default}
local timeout=${5:-600}
lapse=$timeout

# should validate the params...
[ -f $kubeconfig ] || error "Kubeconfig file doesn't exist"

info "looking for $resource_type $resource_namespace/$resource_name using $kubeconfig"
local found=""
while [[ -z $found && $timeout -gt 0 ]]; do
while [[ $lapse -gt 0 ]]; do
found=$(kubectl --kubeconfig $kubeconfig -n $resource_namespace get $resource_type $resource_name -o jsonpath='{.metadata.name}' --ignore-not-found)
timeout=$((timeout - 5))
if [[ -z $found && $timeout -gt 0 ]]; then
sleep 5
if [[ $found ]]; then
[ $((timeout * 2 / 3)) -lt $lapse ] || warn "$resource_namespace/$resource_name $resource_type took $lapse seconds to exist"
return
fi
lapse=$((lapse - 5))
sleep 5
done
debug "timeout: $timeout"

if [[ $found != "$resource_name" ]]; then
kubectl --kubeconfig $kubeconfig -n $resource_namespace get $resource_type
error "Timed out waiting for $resource_type $resource_namespace/$resource_name"
fi

info "Found $resource_type $resource_namespace/$resource_name"
kubectl --kubeconfig $kubeconfig -n $resource_namespace get $resource_type
error "Timed out waiting for $resource_type $resource_namespace/$resource_name"
}

# k8s_wait_ready() - Waits for the readiness of a given kubernetes resource
Expand All @@ -60,6 +58,7 @@ function k8s_wait_ready {
local kubeconfig=${3:-"$HOME/.kube/config"}
local resource_namespace=${4:-default}
local timeout=${5:-600}
lapse=$timeout

# should validate the params...
[ -f $kubeconfig ] || error "Kubeconfig file doesn't exist"
Expand All @@ -68,31 +67,28 @@ function k8s_wait_ready {

info "checking readiness of $resource_type $resource_namespace/$resource_name using $kubeconfig"
local ready=""
while [[ $ready != "True" && $timeout -gt 0 ]]; do
while [[ $lapse -gt 0 ]]; do
ready=$(kubectl --kubeconfig $kubeconfig -n $resource_namespace get $resource_type $resource_name -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}' || echo)
timeout=$((timeout - 5))
if [[ $ready != "True" && $timeout -gt 0 ]]; then
sleep 5
if [[ $ready == "True" ]]; then
[ $((timeout * 2 / 3)) -lt $lapse ] || warn "$resource_namespace/$resource_name $resource_type took $lapse seconds to be ready"
return
fi
lapse=$((lapse - 5))
sleep 5
done
debug "status: $ready"
debug "timeout: $timeout"

if [[ $ready != "True" ]]; then
kubectl --kubeconfig $kubeconfig -n $resource_namespace describe $resource_type $resource_name
error "Timed out waiting for $resource_type $resource_namespace/$resource_name to be ready"
fi

info "$resource_type $resource_namespace/$resource_name is ready"
kubectl --kubeconfig $kubeconfig -n $resource_namespace describe $resource_type $resource_name
error "Timed out waiting for $resource_type $resource_namespace/$resource_name to be ready"
}

# k8s_wait_ready_replicas() - Waits for the readiness of a minimun number of replicas
# k8s_wait_ready_replicas() - Waits for the readiness of a minimum number of replicas
function k8s_wait_ready_replicas {
local resource_type=$1
local resource_name=$2
local kubeconfig=${3:-"$HOME/.kube/config"}
local resource_namespace=${4:-default}
local timeout=${5:-600}
lapse=$timeout
local min_ready=${6:-1}
local status_field=${7:-readyReplicas}
status_field=readyReplicas
Expand All @@ -105,32 +101,29 @@ function k8s_wait_ready_replicas {

info "checking readiness of $resource_type $resource_namespace/$resource_name using $kubeconfig"
local ready=""
while [[ $ready -lt $min_ready && $timeout -gt 0 ]]; do
while [[ $lapse -gt 0 ]]; do
ready=$(kubectl --kubeconfig $kubeconfig -n $resource_namespace get $resource_type $resource_name -o jsonpath="{.status.$status_field}" || echo)
timeout=$((timeout - 5))
if [[ $ready -lt $min_ready && $timeout -gt 0 ]]; then
sleep 5
if [[ $ready -ge $min_ready ]]; then
[ $((timeout * 2 / 3)) -lt $lapse ] || warn "$resource_namespace/$resource_name $resource_type took $lapse seconds to have minimum number of replicas"
return
fi
lapse=$((lapse - 5))
sleep 5
done
debug "status: $ready (want $min_ready)"
debug "timeout: $timeout"

if [[ $ready -lt $min_ready ]]; then
kubectl --kubeconfig $kubeconfig -n $resource_namespace describe $resource_type $resource_name
error "Timed out waiting for $resource_type $resource_namespace/$resource_name to be ready"
fi

info "$resource_type $resource_namespace/$resource_name is ready"
kubectl --kubeconfig $kubeconfig -n $resource_namespace describe $resource_type $resource_name
error "Timed out waiting for $resource_type $resource_namespace/$resource_name to be ready"
}

# k8s_get_capi_kubeconfig() - Gets the Kubeconfig file for a given Cluster API cluster
function k8s_get_capi_kubeconfig {
local cluster=$1
local file="/tmp/${cluster}-kubeconfig"

# mktemp is supported on both ubuntu and fedora
local file=$(mktemp --suffix "_kubeconfig-$cluster")
k8s_wait_exists "secret" "${cluster}-kubeconfig" >/dev/null 2>&1
kubectl --kubeconfig "$HOME/.kube/config" get secret "${cluster}-kubeconfig" -o jsonpath='{.data.value}' | base64 -d >"$file"
if [ ! -f "$file" ]; then
k8s_wait_exists "secret" "${cluster}-kubeconfig" >/dev/null 2>&1
kubectl --kubeconfig "$HOME/.kube/config" get secret "${cluster}-kubeconfig" -o jsonpath='{.data.value}' | base64 -d >"$file"
fi
echo "$file"
}

Expand Down
36 changes: 17 additions & 19 deletions e2e/lib/kpt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,22 @@ function _wait_for_user_repo {
local repo=$1
local user=${2:-nephio}
local timeout=${3:-600}
lapse=$timeout

info "looking for $repo repository of $user user"
local found="False"
while [[ $timeout -gt 0 ]]; do
while [[ $lapse -gt 0 ]]; do
found=$(curl_gitea_api "repos/$user/$repo" 'import json; import sys; print("full_name" in json.loads(sys.stdin.read()))')
if [[ $found == "True" ]]; then
debug "timeout: $timeout"
break
[ $((timeout * 2 / 3)) -lt $lapse ] || warn "$user user took $lapse seconds to exist in $repo repository"
return
fi
timeout=$((timeout - 5))
lapse=$((lapse - 5))
sleep 5
done

if [[ $found != "True" ]]; then
curl_gitea_api "users/$user/repos" 'import json; import sys; print("\n".join(repo["full_name"] for repo in json.loads(sys.stdin.read())))'
error "Timed out waiting for $repo repository"
fi
info "Found $user/$repo repository"
curl_gitea_api "users/$user/repos" 'import json; import sys; print("\n".join(repo["full_name"] for repo in json.loads(sys.stdin.read())))'
error "Timed out waiting for $repo repository"
}

# kpt_wait_pkg() - Wait for a given kpt package to exist in a given repository
Expand All @@ -51,23 +49,23 @@ function kpt_wait_pkg {
local pkg=$2
local user=${3:-nephio}
local timeout=${4:-600}
lapse=$timeout

_wait_for_user_repo "$repo" "$user"
_wait_for_user_repo "$repo" "$user" "$timeout"
info "looking for $pkg kpt package on $user/$repo repository"
local found="False"
while [[ $timeout -gt 0 ]]; do
while [[ $lapse -gt 0 ]]; do
found=$(curl_gitea_api "repos/$user/$repo/contents" "import json; import sys; print('$pkg' in [dir['path'] for dir in json.loads(sys.stdin.read()) if dir['type'] == 'dir' ])")
if [[ $found == "True" ]]; then
debug "timeout: $timeout"
break
[ $((timeout * 2 / 3)) -lt $lapse ] || warn "$pkg kpt package took $lapse seconds to exist"
return
fi
timeout=$((timeout - 5))
lapse=$((lapse - 5))
sleep 5
done

if [[ $found != "True" ]]; then
curl_gitea_api "repos/$user/$repo/contents" 'import json; import sys; print("\n".join(dir["path"] for dir in json.loads(sys.stdin.read()) if dir["type"] == "dir" ))'
error "Timed out waiting for $pkg kpt package"
fi
info "Found $pkg kpt package in $user/$repo repository"
porchctl rpkg get --name "$pkg"
kubectl logs -l fn.kptgen.dev/controller=nephio-controller -n nephio-system -c controller --tail -1 | grep ".*\"packageName\": \"$pkg\", \"repository\": \"$repo\", \"status\": \"False\","
curl_gitea_api "repos/$user/$repo/contents" 'import json; import sys; print("\n".join(dir["path"] for dir in json.loads(sys.stdin.read()) if dir["type"] == "dir" ))'
error "Timed out waiting for $pkg kpt package"
}
33 changes: 5 additions & 28 deletions e2e/lib/porch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,33 @@
# shellcheck source=e2e/lib/_utils.sh
source "${E2EDIR:-$HOME/test-infra/e2e}/lib/_utils.sh"

# porch_wait_log_entry() - Waits for the entry log in Porch server
function porch_wait_log_entry {
local pattern="$1"
local timeout=${2:-600}

info "looking for $pattern log entry in porch server($(kubectl get pods -n porch-system -l app=porch-server -o jsonpath='{.items[*].metadata.name}'))"
local found=""
while [[ $timeout -gt 0 ]]; do
found=$(kubectl logs -n porch-system "$(kubectl get pods -n porch-system -l app=porch-server -o jsonpath='{.items[*].metadata.name}')" | { grep "$pattern" || :; })
if [[ $found ]]; then
debug "timeout: $timeout"
break
fi
timeout=$((timeout - 5))
sleep 5
done

if [[ -z $found ]]; then
kubectl logs -n porch-system "$(kubectl get pods -n porch-system -l app=porch-server -o jsonpath='{.items[*].metadata.name}')"
error "Timed out waiting for $pattern"
fi
info "Found $pattern log entry in porch server"
}

# porch_wait_published_packagerev() - Waits for a kpt package revision gets published
function porch_wait_published_packagerev {
local pkg_name="$1"
local repository="$2"
local revision="${3:-main}"
local timeout=${4:-900}
lapse=$timeout

info "looking for package published revision on $pkg_name"
local found=""
while [[ $timeout -gt 0 ]]; do
while [[ $lapse -gt 0 ]]; do
for pkg_rev in $(kubectl get packagerevisions -o jsonpath="{range .items[?(@.spec.packageName==\"$pkg_name\")]}{.metadata.name}{\"\\n\"}{end}"); do
if [ "$(kubectl get packagerevision "$pkg_rev" -o jsonpath='{.spec.repository}/{.spec.revision}')" == "$repository/$revision" ]; then
found=$pkg_rev
break
fi
done
if [[ $found ]]; then
debug "timeout: $timeout"
[ $((timeout * 2 / 3)) -lt $lapse ] || warn "$pkg_name package took $lapse seconds to be published"
break
fi
timeout=$((timeout - 5))
lapse=$((lapse - 5))
sleep 5
done

if [[ -z $found ]]; then
kubectl get packagerevisions -o jsonpath="{range .items[?(@.spec.packageName==\"$pkg_name\")]}{.metadata.name}{\"\\n\\\"}{end}"
kubectl get packagerevisions -o jsonpath="{range .items[?(@.spec.packageName==\"$pkg_name\")]}{.metadata.name}{\"\\n\"}{end}"
error "Timed out waiting for revisions on $pkg_name package"
fi
kubectl wait --for jsonpath='{.spec.lifecycle}'=Published packagerevisions $found --timeout=10m
Expand Down
6 changes: 4 additions & 2 deletions e2e/provision/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ function get_status {
awk -v low="$(grep low /proc/zoneinfo | awk '{k+=$2}END{print k}')" '{a[$1]=$2} END{ print a["MemFree:"]+a["Active(file):"]+a["Inactive(file):"]+a["SReclaimable:"]-(12*low);}' /proc/meminfo
fi
if command -v kubectl >/dev/null; then
echo "Draft Porch Package Revisions"
kubectl get packagerevision -o jsonpath='{range .items[?(@.spec.lifecycle=="Draft")]}{.metadata.name}{"\n"}{end}' || :
KUBECONFIG=$HOME/.kube/config
for kubeconfig in $(sudo find /tmp/ -name "kubeconfig-*"); do
for kubeconfig in /tmp/*-kubeconfig; do
KUBECONFIG+=":$kubeconfig"
done
export KUBECONFIG
for context in $(kubectl config get-contexts --no-headers --output name); do
echo "Kubernetes Events ($context):"
kubectl get events --sort-by='.lastTimestamp' -A --context "$context"
kubectl get events --sort-by='.lastTimestamp' -A --context "$context" --field-selector type!=Normal
echo "Kubernetes Resources ($context):"
kubectl get all -A -o wide --context "$context"
done
Expand Down
1 change: 0 additions & 1 deletion e2e/provision/install_sandbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,4 @@ fi
echo "$ansible_cmd"
eval "$ansible_cmd" | tee ~/cluster.log


echo "Done installing Nephio Sandbox Environment"
10 changes: 3 additions & 7 deletions e2e/tests/free5gc/001.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ regional_pkg_rev=$(porchctl rpkg clone -n default "https://github.com/nephio-pro
k8s_wait_exists "packagerev" "$regional_pkg_rev"

# Draft creation
porch_wait_log_entry "Creating packagerev default/mgmt-"
assert_lifecycle_equals "$regional_pkg_rev" "Draft"
kubectl wait --for jsonpath='{.spec.lifecycle}'=Draft packagerevisions "$regional_pkg_rev" --timeout="600s"
assert_branch_exists "drafts/regional/v1"
assert_commit_msg_in_branch "Intermediate commit" "drafts/regional/v1"
assert_workload_resource_contains "drafts/regional/v1" "clusterName: regional" "Workload cluster has not been transformed properly"
Expand All @@ -62,20 +61,17 @@ kpt fn eval --image "gcr.io/kpt-fn/set-labels:v0.2.0" regional -- "nephio.org/si
assert_contains "$(cat regional/workload-cluster.yaml)" "nephio.org/region: us-west1" "Workload cluster doesn't have region label"

porchctl rpkg push -n default "$regional_pkg_rev" regional
porch_wait_log_entry "Update.*packagerevisionresources/${regional_pkg_rev},"

# Proposal
porchctl rpkg propose -n default "$regional_pkg_rev"
porch_wait_log_entry "Update.*packagerevisions/${regional_pkg_rev},"
assert_lifecycle_equals "$regional_pkg_rev" "Proposed"
kubectl wait --for jsonpath='{.spec.lifecycle}'=Proposed packagerevisions "$regional_pkg_rev" --timeout="600s"
assert_branch_exists "proposed/regional/v1"
assert_commit_msg_in_branch "Intermediate commit" "proposed/regional/v1"
assert_workload_resource_contains "proposed/regional/v1" "nephio.org/site-type: regional" "Workload cluster has not been transformed properly to proposed"

# Approval
porchctl rpkg approve -n default "$regional_pkg_rev"
porch_wait_log_entry "Update.*/${regional_pkg_rev}.*/approval"
assert_lifecycle_equals "$regional_pkg_rev" "Published"
kubectl wait --for jsonpath='{.spec.lifecycle}'=Published packagerevisions "$regional_pkg_rev" --timeout="600s"
assert_workload_resource_contains "main" "nephio.org/site-type: regional" "Workload cluster has not been successfully merged into main branch"

k8s_wait_exists "workloadcluster" "regional"
Expand Down
9 changes: 3 additions & 6 deletions e2e/tests/free5gc/003.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,17 @@ source "${LIBDIR}/_assertions.sh"
pkg_rev=$(porchctl rpkg clone -n default "https://github.com/nephio-project/catalog.git/workloads/free5gc/free5gc-cp@$REVISION" --repository regional free5gc-cp | cut -f 1 -d ' ')
k8s_wait_exists "packagerev" "$pkg_rev"

porch_wait_log_entry "Creating packagerev default/regional-"
assert_lifecycle_equals "$pkg_rev" "Draft"
kubectl wait --for jsonpath='{.spec.lifecycle}'=Draft packagerevisions "$pkg_rev" --timeout="600s"
assert_branch_exists "drafts/free5gc-cp/v1" "nephio/regional"
assert_commit_msg_in_branch "Intermediate commit" "drafts/free5gc-cp/v1" "nephio/regional"

porchctl rpkg propose -n default "$pkg_rev"
porch_wait_log_entry "Update.*packagerevisions/${pkg_rev},"
assert_lifecycle_equals "$pkg_rev" "Proposed"
kubectl wait --for jsonpath='{.spec.lifecycle}'=Proposed packagerevisions "$pkg_rev" --timeout="600s"
assert_branch_exists "proposed/free5gc-cp/v1" "nephio/regional"
assert_commit_msg_in_branch "Intermediate commit" "proposed/free5gc-cp/v1" "nephio/regional"

porchctl rpkg approve -n default "$pkg_rev"
porch_wait_log_entry "Update.*/${pkg_rev}.*/approval"
assert_lifecycle_equals "$pkg_rev" "Published"
kubectl wait --for jsonpath='{.spec.lifecycle}'=Published packagerevisions "$pkg_rev" --timeout="600s"

kpt_wait_pkg "regional" "free5gc-cp"
k8s_wait_ready_replicas "statefulset" "mongodb" "$(k8s_get_capi_kubeconfig "regional")" "free5gc-cp"
2 changes: 1 addition & 1 deletion e2e/tests/free5gc/004.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ k8s_apply "$TESTDIR/004-free5gc-operator.yaml"
kubeconfig="$HOME/.kube/config"
for cluster in $(kubectl get cl -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' --kubeconfig "$kubeconfig"); do
k8s_wait_exists "packagevariant" "free5gc-operator-$cluster-free5gc-operator"
porch_wait_log_entry "refs/heads/proposed/free5gc-operator/packagevariant-1 :refs/heads/drafts/free5gc-operator/packagevariant-1"
porch_wait_published_packagerev "free5gc-operator" "$cluster"
kpt_wait_pkg "$cluster" "free5gc-operator"
k8s_wait_ready_replicas "deployment" "free5gc-operator" "$(k8s_get_capi_kubeconfig "$cluster")" "free5gc"
done
1 change: 0 additions & 1 deletion e2e/tests/free5gc/005.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ k8s_apply "$TESTDIR/005-edge-free5gc-upf.yaml"

for cluster in "edge01" "edge02"; do
k8s_wait_exists "packagevariant" "edge-free5gc-upf-${cluster}-free5gc-upf"
porch_wait_log_entry 'updated to "free5gc-upf"'
porch_wait_published_packagerev "free5gc-upf" "$cluster"
kpt_wait_pkg "$cluster" "free5gc-upf"
k8s_wait_ready_replicas "deployment" "upf-${cluster}" "$(k8s_get_capi_kubeconfig "$cluster")" "free5gc-upf"
Expand Down
1 change: 0 additions & 1 deletion e2e/tests/free5gc/006.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ cluster_kubeconfig=$(k8s_get_capi_kubeconfig "regional")
# check the NFs
for nf in amf smf; do
k8s_wait_exists "packagevariant" "regional-free5gc-$nf-regional-free5gc-$nf"
porch_wait_log_entry "\"$nf-example\" updated to \"free5gc-cp\""
porch_wait_published_packagerev "free5gc-$nf" "regional"
kpt_wait_pkg "regional" "free5gc-$nf"
k8s_wait_ready_replicas "deployment" "$nf-regional" "$cluster_kubeconfig" "free5gc-cp"
Expand Down
1 change: 0 additions & 1 deletion e2e/tests/oai/001a-infra.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ source "${LIBDIR}/porch.sh"
# shellcheck source=e2e/lib/_assertions.sh
source "${LIBDIR}/_assertions.sh"


declare -A clusters
clusters=(
["core"]="172.18.16.0/20"
Expand Down
Loading

0 comments on commit e175645

Please sign in to comment.