Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update troubleshootscript for MacOS #318

Merged
merged 4 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions troubleshoot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This script checks the following scenarios:
- Tenant is reachable from the operator pod using the same options as the `dynatrace-operator` (proxy, certificate, ...)
- Image (OneAgent and ActiveGate)
- registry is accessible
- image is accessible from the operator pod using registry from (custom) pull secret or docker hub
- image is accessible from the operator pod using registry from the tenant or (custom) pull secret

## Requirements

Expand All @@ -29,6 +29,18 @@ The script has the following dependencies:
- `jq`
- `curl`

In addition, macOS has the following requirements:
- GNU implementation of `getopt`
- please install gnu-based version and reopen terminal:
```bash
brew install gnu-getopt && brew link --force gnu-getopt
```
- GNU implementation of `cut`
- please install `gcut`:
```bash
brew install coreutils
```

## Usage

Run the following command to run the script.
Expand Down Expand Up @@ -60,7 +72,3 @@ Specify options by appending them to the command, e.g: `./troubleshoot.sh --dyna
`-c` or`--oc`
- changes CLI to `oc`
- default: `kubectl`

`-r` or`--openshift`
- changes the default image to `registry.connect.redhat.com/dynatrace/oneagent`
- default: `docker.io/dynatrace/oneagent`
121 changes: 56 additions & 65 deletions troubleshoot/troubleshoot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,26 @@ set -eu
selected_dynakube="dynakube"
selected_namespace="dynatrace"
cli="kubectl"
default_oneagent_image="docker.io/dynatrace/oneagent"

missing_value="<no value>"
api_url=""
paas_token=""
log_section=""

cut_command="cut"
if [[ $OSTYPE == 'darwin'* ]]; then
cut_command="gcut"
fi

function usage {
echo "Usage: $(basename "${0}") [options]" 2>&1
echo " [ -d | --dynakube DYNAKUBE ] Specify a different Dynakube name, Default: 'dynakube'."
echo " [ -n | --namespace NAMESPACE ] Specify a different Namespace, Default: 'dynatrace'."
echo " [ -c | --oc ] Use 'oc' instead of 'kubectl' to access cluster."
echo " [ -r | --openshift ] Check the OneAgent image in the RedHat registry."
echo " [ -h | --help ] Display usage information."
exit 1
}

options="hd:n:cr"
long_options="help,dynakube:,namespace:,oc,openshift"
eval set -- "$(getopt --options="$options" --longoptions="$long_options" --name "$0" -- "$@")"

while true; do
case "${1}" in
-h | --help)
usage
;;
-d | --dynakube)
selected_dynakube="${2}"
shift 2
;;
-n | --namespace)
selected_namespace="${2}"
shift 2
;;
-c | --oc)
cli="oc"
shift
;;
-r | --openshift)
default_oneagent_image="registry.connect.redhat.com/dynatrace/oneagent"
shift
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done

function log {
printf "[%10s] %s\n" "$log_section" "$1"
}
Expand All @@ -68,7 +35,7 @@ function error {
}

function checkDependencies {
dependencies=("jq" "curl")
dependencies=("jq" "curl" "getopt" "${cut_command}")
if [[ "${cli}" == "oc" ]] ; then
dependencies+=("oc")
else
Expand All @@ -81,6 +48,45 @@ function checkDependencies {
error "${dependency} is required to run this script!"
fi
done

if [[ $(getopt 2>&1) != *"getopt"* ]]; then
error "GNU implementation of 'getopt' required."
fi
}

function parseArguments {
options="hd:n:cr"
long_options="help,dynakube:,namespace:,oc,openshift"

eval set -- "$(getopt --options="$options" --longoptions="$long_options" --name "$0" -- "$@")"

while true; do
case "${1}" in
-h | --help)
usage
;;
-d | --dynakube)
selected_dynakube="${2}"
shift 2
;;
-n | --namespace)
selected_namespace="${2}"
shift 2
;;
-c | --oc)
cli="oc"
shift
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
}

function checkNamespace {
Expand Down Expand Up @@ -199,8 +205,12 @@ function getImage {
type="$1"

if [[ "${type}" == "oneAgent" ]] ; then
# oneagent uses docker.io by default
image="${default_oneagent_image}"
# oneagent immutable image is not published and uses the cluster registry by default
api_url=$("${cli}" get dynakube "${selected_dynakube}" \
--namespace "${selected_namespace}" \
--template="{{.spec.apiUrl}}")
image="${api_url#*//}"
image="${image%/*}/linux/oneagent"
else
# activegate is not published and uses the cluster registry by default
api_url=$("${cli}" get dynakube "${selected_dynakube}" \
Expand Down Expand Up @@ -259,14 +269,15 @@ function checkImagePullable {
oneagent_image="${dynakube_oneagent_image##"$oneagent_registry/"}"

# check if image has version set
image_version="$(cut --delimiter ':' --only-delimited --fields=2 <<< "${oneagent_image}")"
image_version="$(${cut_command} --delimiter ':' --only-delimited --fields=2 <<< "${oneagent_image}")"

if [[ -z "$image_version" ]] ; then
# no version set, default to latest
oneagent_version="latest"

log "using latest image version"
else
oneagent_image="$(cut --delimiter ':' --fields=1 <<< "${oneagent_image}")"
oneagent_image="$(${cut_command} --delimiter ':' --fields=1 <<< "${oneagent_image}")"
oneagent_version="$image_version"

log "using custom image version"
Expand Down Expand Up @@ -331,28 +342,7 @@ function checkImagePullable {
if [[ "$oneagent_image_works" == "true" ]] ; then
log "oneagent image '$dynakube_oneagent_image' found"
else
if [[ "$oneagent_registry" == "docker.io" ]] ; then
# get auth token with pull access for docker hub registry
token="$(
curl --silent \
"https://auth.docker.io/token?service=registry.docker.io&scope=repository:$oneagent_image:pull" \
| jq --raw-output '.token'
)"

# check selected image exists on docker hub
dockerio_image_request="$run_container_command 'curl --head \
--header \"Authorization: Bearer ${token}\" \
https://registry-1.docker.io/v2/$oneagent_image/manifests/$oneagent_version \
--silent --output /dev/null --write-out %{http_code}'"

if [[ "$(eval "$dockerio_image_request")" == "200" ]] ; then
log "oneagent image '$oneagent_image' with version '$oneagent_version' exists on docker.io registry"
else
error "oneagent image '$oneagent_image' with version '$oneagent_version' not found on docker.io registry"
fi
else
error "oneagent image '$dynakube_oneagent_image' with version '$oneagent_version' missing."
fi
error "oneagent image '$dynakube_activegate_image' missing"
fi

if [[ "$activegate_image_works" == "true" ]] ; then
Expand Down Expand Up @@ -448,6 +438,7 @@ function checkDTClusterConnection {

####### MAIN #######
checkDependencies
parseArguments "$@"

checkNamespace
checkDynakube
Expand Down