forked from cloud-ark/kubeplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrieve application and pod statuses (cloud-ark#1332)
- Loading branch information
Showing
6 changed files
with
251 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import subprocess | ||
import sys | ||
import json | ||
from crmetrics import CRBase | ||
|
||
|
||
class AppStatusFinder(CRBase): | ||
|
||
def get_app_instance_status(self, kind, instance, kubeconfig): | ||
cmd = 'kubectl get %s %s -o json %s' % (kind, instance, kubeconfig) | ||
out, err = self.run_command(cmd) | ||
if err != "": | ||
print("Something went wrong while getting app instance status.") | ||
print(err) | ||
exit(1) | ||
|
||
deployed = False | ||
ns = None | ||
response = json.loads(out) | ||
if 'status' in response: | ||
if 'helmrelease' in response['status']: | ||
helm_release = response['status']['helmrelease'].strip('\n') | ||
ns, name = helm_release.split(':') | ||
deployed = True | ||
return name, ns, deployed | ||
else: | ||
# an error has occurred | ||
status = response['status'] | ||
return status, ns, deployed | ||
|
||
else: | ||
return 'Application not deployed properly', ns, deployed | ||
|
||
|
||
def get_app_pods(self, namespace, kubeconfig): | ||
cmd = 'kubectl get pods -n %s %s -o json' % (namespace, kubeconfig) | ||
out, err = self.run_command(cmd) | ||
# format? | ||
response = json.loads(out) | ||
pods = [] | ||
for pod in response['items']: | ||
name = pod['metadata']['name'] | ||
typ = pod['kind'] | ||
ns = pod['metadata']['namespace'] | ||
phase = pod['status']['phase'] | ||
pods.append((name, typ, ns, phase)) | ||
return pods | ||
|
||
|
||
if __name__ == '__main__': | ||
appStatusFinder = AppStatusFinder() | ||
kind = sys.argv[1] | ||
instance = sys.argv[2] | ||
kubeconfig = sys.argv[3] | ||
|
||
valid_consumer_api = appStatusFinder.verify_kind_is_consumerapi(kind, kubeconfig) | ||
if not valid_consumer_api: | ||
print(("{} is not a valid Consumer API.").format(kind)) | ||
exit(0) | ||
|
||
res_exists, ns, err = appStatusFinder.check_res_exists(kind, instance, kubeconfig) | ||
if not res_exists: | ||
print(err) | ||
exit(0) | ||
|
||
working, error = appStatusFinder.validate_kind_and_instance(kind, instance, ns) | ||
if working == False: | ||
print(err) | ||
exit(1) | ||
|
||
release_name_or_status, release_ns, deployed = appStatusFinder.get_app_instance_status(kind, instance, kubeconfig) | ||
|
||
if deployed: | ||
deploy_str = 'Deployed' | ||
else: | ||
print(release_name_or_status) | ||
exit(1) | ||
|
||
pods = appStatusFinder.get_app_pods(instance, kubeconfig) | ||
|
||
print("{:<55} {:<55} {:<55} {:<55}".format("NAME", "TYPE", "NAMESPACE", "STATUS")) | ||
print("{:<55} {:<55} {:<55} {:<55}".format(release_name_or_status, 'helmrelease', release_ns, deploy_str)) | ||
for pod_name, typ, pod_ns, phase in pods: | ||
print("{:<55} {:<55} {:<55} {:<55}".format(pod_name, typ, pod_ns, phase)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/bin/bash | ||
|
||
source utils.sh | ||
|
||
print_help () { | ||
echo "NAME" | ||
echo " kubectl appstatus" | ||
echo "" | ||
echo "SYNOPSIS" | ||
echo " kubectl appstatus <Kind> <Instance> -k <Absolute path to kubeconfig>" | ||
echo "" | ||
echo "DESCRIPTION" | ||
echo " kubectl appstatus shows the status of the application instance and its pods" | ||
exit 0 | ||
} | ||
|
||
if (( $# < 2 )); then | ||
print_help | ||
fi | ||
|
||
kind=$1 | ||
instance=$2 | ||
kubeconfig="" | ||
|
||
shift; | ||
shift; | ||
|
||
while getopts ":k:" opt; do | ||
case ${opt} in | ||
k ) | ||
kubeconfig1=$OPTARG | ||
if [ ! -f $kubeconfig1 ]; then | ||
echo "Kubeconfig $kubeconfig1 does not exist." | ||
exit 0 | ||
fi;; | ||
? ) | ||
echo "Invalid option: ${1} " 1>&2 | ||
print_help | ||
exit 0 | ||
;; | ||
esac | ||
done | ||
|
||
kubeconfig="--kubeconfig="$kubeconfig1 | ||
|
||
canonicalKind=$(get_canonical_kind $kind) | ||
|
||
if [[ $canonicalKind == *"Unknown"* ]]; then | ||
echo "$canonicalKind" | ||
exit 0 | ||
fi | ||
|
||
python3 /$KUBEPLUS_HOME/plugins/appstatus.py $canonicalKind $instance $kubeconfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters