lib/sct/commands/cluster.rb in sct-0.1.13 vs lib/sct/commands/cluster.rb in sct-0.1.14
- old
+ new
@@ -3,57 +3,116 @@
module Sct
class ClusterCommand
IS_PUBLIC_COMMAND = true
- SYNTAX = 'sct cluster up | sct cluster down | sct cluster update-config'
- SUMMARY = 'Starts/stops or updates the config of the minikube cluster'
- EXAMPLE = 'sct cluster up | sct cluster down | sct cluster update-config'
- EXAMPLE_DESCRIPTION = 'Starts/stops or updates the config of the minikube cluster'
- DESCRIPTION = "Sct cluster allows you to start, stop or update the config of the Spend Cloud minikube cluster"
+ SYNTAX = 'sct cluster up | sct cluster down | sct cluster reset | sct cluster update-config'
+ SUMMARY = 'Starts/stops/resets or updates the config of the minikube cluster.'
+ EXAMPLE = 'sct cluster up | sct cluster down | sct cluster reset | sct cluster update-config'
+ EXAMPLE_DESCRIPTION = 'Starts/stops/resets or updates the config of the minikube cluster.'
+ DESCRIPTION = "sct cluster allows you to start, stop, reset or update the config of the Spend Cloud minikube cluster."
OPTIONS = []
def execute(args, options)
return puts "SCT has not been initialized. Run 'sct init' first.".red unless Sct::Config.exists
- if Sct::Helpers.operatingSystem == Sct::Helpers::WINDOWS
- minikube = "minikube.exe"
- else
- minikube = "minikube"
- end
-
case args[0]
when "up"
- system("#{minikube} start")
- system("#{minikube} ssh -- 'sudo su -c \"echo 10048576 > /proc/sys/fs/inotify/max_user_watches\"'")
- update_config
- system("kubectl config use-context minikube")
- system("kubectl delete pod -n kube-system $(kubectl get pods -n kube-system | grep registry-creds | awk '{print $1}')")
- system("sudo sct hostfile")
- puts "\nāļø You can now visit your environment at š https://spend-cloud.spend.cloud.local š"
+ up
when "down"
- system("#{minikube} stop")
+ down
when "update-config"
update_config
+ when "reset"
+ reset
else
puts "Unknown or missing argument. Please run 'sct cluster up','sct cluster down' or 'sct cluster update-config'".red
end
end
+ def up
+ start
+ system "kubectl delete pod -n kube-system $(kubectl get pods -n kube-system | grep registry-creds | awk '{print $1}')"
+ system "kubectl rollout status -n kube-system deployment/registry-creds"
+ post_start
+ end
+
+ def down
+ system "#{minikube} stop"
+ end
+
def update_config
- return puts "Skipping minikube config (only needed for Windows)".yellow unless Sct::Helpers.operatingSystem == Sct::Helpers::WINDOWS
+ if Sct::Helpers.operatingSystem == Sct::Helpers::WINDOWS
+ windows_home_path = Sct::Helpers.windowsHomePath
+ kube_file_path = windows_home_path+"/.kube/config"
- windows_home_path = Sct::Helpers.windowsHomePath
- kube_file_path = windows_home_path+"/.kube/config"
+ if !File.exists?(kube_file_path)
+ return puts "#{kube_file_path} doesn't exist".red
+ end
- if !File.exists?(kube_file_path)
- return puts "#{kube_file_path} doesn't exist".red
+ system("sed -e 's~\\\\~/~g' -e 's~C:~/mnt/c~g' < #{kube_file_path} > ~/.kube/minikube-config")
+
+ puts "#{kube_file_path} copied to ~/.kube/minikube-config".green
end
- system("sed -e 's~\\\\~/~g' -e 's~C:~/mnt/c~g' < #{kube_file_path} > ~/.kube/minikube-config")
+ system "kubectl config use-context minikube"
+ end
- return puts "Windows minikube config copied to ~/.kube/minikube-config".green
+ def reset
+ system "#{minikube} delete"
+ start
+ system "kubectl create secret generic gcloud-credentials --from-file=\"$(echo ~)/.config/gcloud/application_default_credentials.json\""
+ system "kubectl create secret generic -n kube-system registry-creds-dpr --from-literal DOCKER_PRIVATE_REGISTRY_PASSWORD=changeme --from-literal DOCKER_PRIVATE_REGISTRY_SERVER=changeme --from-literal DOCKER_PRIVATE_REGISTRY_USER=changeme"
+ system "kubectl patch secret -n kube-system registry-creds-dpr -p='{\"metadata\": {\"labels\": { \"app\": \"registry-creds\", \"cloud\": \"dpr\", \"kubernetes.io/minikube-addons\": \"registry-creds\"}}}'"
+ system "kubectl create secret generic -n kube-system registry-creds-ecr --from-literal AWS_ACCESS_KEY_ID=changeme --from-literal AWS_SECRET_ACCESS_KEY=changeme --from-literal AWS_SESSION_TOKEN=\"\" --from-literal aws-account=changeme --from-literal aws-assume-role=changeme --from-literal aws-region=changeme"
+ system "kubectl patch secret -n kube-system registry-creds-ecr -p='{\"metadata\": {\"labels\": { \"app\": \"registry-creds\", \"cloud\": \"ecr\", \"kubernetes.io/minikube-addons\": \"registry-creds\"}}}'"
+ system "kubectl create secret generic -n kube-system registry-creds-gcr --from-file=\"$(echo ~)/.config/gcloud/application_default_credentials.json\" --from-literal=gcrurl=\"https://eu.gcr.io\""
+ system "kubectl patch secret -n kube-system registry-creds-gcr -p='{\"metadata\": {\"labels\": { \"app\": \"registry-creds\", \"cloud\": \"gcr\", \"kubernetes.io/minikube-addons\": \"registry-creds\"}}}'"
+ system "kubectl create secret generic -n kube-system registry-creds-acr --from-literal ACR_PASSWORD=changeme --from-literal ACR_CLIENT_ID=changeme --from-literal ACR_URL=changeme"
+ system "kubectl patch secret -n kube-system registry-creds-acr -p='{\"metadata\": {\"labels\": { \"app\": \"registry-creds\", \"cloud\": \"acr\", \"kubernetes.io/minikube-addons\": \"registry-creds\"}}}'"
+ system "#{minikube} addons enable registry-creds"
+ system "#{minikube} addons enable ingress"
+ system "kubectl rollout status -n kube-system deployment/registry-creds"
+ system "kubectl rollout status -n kube-system deployment/nginx-ingress-controller"
+ wait_for_gcr_secret
+ system "kubectl apply -f ~/development/spend-cloud/k8s/"
+ post_start
+ end
+
+ def start
+ system "#{minikube} start --cpus=$(cat /proc/cpuinfo | grep processor | wc -l) --memory=16G"
+ system "#{minikube} ssh -- 'sudo su -c \"echo 10048576 > /proc/sys/fs/inotify/max_user_watches\"'"
+ update_config
+ end
+
+ def post_start
+ system "kubectl rollout status deployment/spend-cloud-api"
+ system "sudo sct hostfile"
+ puts "\nāļø You can visit your environment at š https://spend-cloud.spend.cloud.local š (might still take a few minutes)"
+ end
+
+ def wait_for_gcr_secret
+ puts "Waiting for Google Cloud Registry secret to become available...".yellow
+
+ while true
+ secrets = `kubectl get secrets`
+
+ if secrets.include? "gcr-secret"
+ puts "Google Cloud Registry secret is now available.".green
+ break
+ else
+ sleep 1
+ end
+ end
+ end
+
+ def minikube
+ if Sct::Helpers.operatingSystem == Sct::Helpers::WINDOWS
+ return "minikube.exe"
+ else
+ return "minikube"
+ end
end
implements CommandInterface
end