<commit_before>#!/bin/bash

# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# "---------------------------------------------------------"
# "-                                                       -"
# "-  Uninstalls all k8s resources and deletes             -"
# "-  the GKE cluster                                      -"
# "-                                                       -"
# "---------------------------------------------------------"

# Do not set errexit as it makes partial deletes impossible
set -o errexit

PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"

# shellcheck source=k8s.env
source "$PROJECT_ROOT"/k8s.env

context="${STAGING_ON_PREM_GKE_CONTEXT}"
# try to set context to on-prem cluster. if we can't set that context, we can't do anything else
# in this file, so we can exit

if [[ ! $(kubectl config use-context "$context") ]]; then
	echo "on-prem cluster was not found; skipping cluster teardown"
	exit 1
fi

# You have to wait the default pod grace period before you can delete the pvcs
GRACE=$(kubectl --namespace default get sts -l component=elasticsearch,role=data -o jsonpath='{..terminationGracePeriodSeconds}')
PADDING=30

kubectl --namespace default delete -f "$PROJECT_ROOT"/elasticsearch/manifests/ || true
kubectl --namespace default delete -f "$PROJECT_ROOT"/policy/on-prem-network-policy.yaml || true

echo "Sleeping $(( GRACE + PADDING )) seconds before deleting PVCs. The default pod grace period."
sleep "$(( GRACE + PADDING ))"

# Deleting and/or scaling a StatefulSet down will not delete the volumes associated with the StatefulSet.
# This is done to ensure data safety, which is generally more valuable
# than an automatic purge of all related StatefulSet resources.
kubectl --namespace default delete pvc -l component=elasticsearch,role=data || true
kubectl --namespace default delete pvc -l component=elasticsearch,role=master || true

timeout=100

while [ $(kubectl get pvc --all-namespaces | wc -l)  -gt 0 ]  &&  [ $(kubectl get pv --all-namespaces | wc -l ) -gt 0 ] && [ $timeout -gt 0 ]; do
	echo "kubectl get pvc --all-namespaces"
	kubectl get pvc --all-namespaces
	echo "kubectl get pv --all-namespaces"
	kubectl get pv --all-namespaces
	sleep 10<commit_msg>Add more comments to script<commit_after>#!/bin/bash

# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# "---------------------------------------------------------"
# "-                                                       -"
# "-  Uninstalls all k8s resources and deletes             -"
# "-  the GKE cluster                                      -"
# "-                                                       -"
# "---------------------------------------------------------"

# Do not set errexit as it makes partial deletes impossible
set -o errexit

PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"

# shellcheck source=k8s.env
source "$PROJECT_ROOT"/k8s.env

context="${STAGING_ON_PREM_GKE_CONTEXT}"
# try to set context to on-prem cluster. if we can't set that context, we can't do anything else
# in this file, so we can exit

if [[ ! $(kubectl config use-context "$context") ]]; then
	echo "on-prem cluster was not found; skipping cluster teardown"
	exit 1
fi

# You have to wait the default pod grace period before you can delete the pvcs
GRACE=$(kubectl --namespace default get sts -l component=elasticsearch,role=data -o jsonpath='{..terminationGracePeriodSeconds}')
PADDING=30

kubectl --namespace default delete -f "$PROJECT_ROOT"/elasticsearch/manifests/ || true
kubectl --namespace default delete -f "$PROJECT_ROOT"/policy/on-prem-network-policy.yaml || true

echo "Sleeping $(( GRACE + PADDING )) seconds before deleting PVCs. The default pod grace period."
sleep "$(( GRACE + PADDING ))"

# Deleting and/or scaling a StatefulSet down will not delete the volumes associated with the StatefulSet.
# This is done to ensure data safety, which is generally more valuable
# than an automatic purge of all related StatefulSet resources.
kubectl --namespace default delete pvc -l component=elasticsearch,role=data || true
kubectl --namespace default delete pvc -l component=elasticsearch,role=master || true

timeout=100

# Attempt to wait for the PV to be destroyed before proceeding. Most of the time the PV's are deleted immediately,
# but in the odd chance there are PV's left behind, premature teardown of the GKE clusters (in the terraform code)
# will result in orphaned disks
while [ $(kubectl get pvc --all-namespaces | wc -l)  -gt 0 ]  &&  [ $(kubectl get pv --all-namespaces | wc -l ) -gt 0 ] && [ $timeout -gt 0 ]; do
	echo "kubectl get pvc --all-namespaces"
	kubectl get pvc --all-namespaces
	echo "kubectl get pv --all-namespaces"
	kubectl get pv --all-namespaces
	sleep 10
{'fraction_non_alphanumeric': 0.13335630599586493, 'fraction_numerical': 0.007925568573397657, 'mean_word_length': 2.6927480916030535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7527310', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1604, 'n_words': 662}