-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a script to deploy minikube on a VM. (#459)
* Create a script to deploy minikube on a VM. * This is the first step in adding E2E testing on minikube. * We add a function call to test_deploy that will deploy minikube on a VM. * Also provide a function to teardown the VM. * The script copies the relevant kubeconfig information and certificates to a directory. * In a follow on PR we will incorporate this new command into our E2E workflow in order to create an E2E test that runs on minikube. * Related to #6 * Adress comments. * Address comments.
- Loading branch information
1 parent
94d3d29
commit febb21d
Showing
4 changed files
with
315 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
# | ||
# A helper script to run on a VM to install minikube. | ||
|
||
set -ex | ||
|
||
# Install Docker. | ||
sudo apt-get update -y | ||
sudo apt-get install -y \ | ||
apt-transport-https \ | ||
ca-certificates \ | ||
curl \ | ||
software-properties-common | ||
|
||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | ||
|
||
sudo add-apt-repository \ | ||
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \ | ||
$(lsb_release -cs) \ | ||
stable" | ||
|
||
sudo apt-get update -y | ||
sudo apt-get install docker-ce -y | ||
|
||
# Install kubectl | ||
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl | ||
chmod +x ./kubectl | ||
sudo mv kubectl /usr/local/bin/ | ||
|
||
# Install minikube | ||
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.25.0/minikube-linux-amd64 | ||
chmod +x minikube | ||
sudo mv minikube /usr/local/bin/ | ||
|
||
sudo chmod -R a+rw ~/.kube | ||
sudo chmod -R a+rw ~/.minikube |
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,60 @@ | ||
import tempfile | ||
import unittest | ||
import yaml | ||
|
||
from testing import test_deploy | ||
|
||
class TestDeploy(unittest.TestCase): | ||
|
||
def testModifyMinikubeConfig(self): | ||
"""Test modeify_minikube_config""" | ||
|
||
config_path = None | ||
with tempfile.NamedTemporaryFile(delete=False) as hf: | ||
config_path = hf.name | ||
hf.write("""apiVersion: v1 | ||
clusters: | ||
- cluster: | ||
certificate-authority: /home/jlewi/.minikube/ca.crt | ||
server: https://10.240.0.18:8443 | ||
name: minikube | ||
contexts: | ||
- context: | ||
cluster: minikube | ||
user: minikube | ||
name: minikube | ||
current-context: minikube | ||
kind: Config | ||
preferences: {} | ||
users: | ||
- name: minikube | ||
user: | ||
as-user-extra: {} | ||
client-certificate: /home/jlewi/.minikube/client.crt | ||
client-key: /home/jlewi/.minikube/client.key | ||
""") | ||
|
||
test_deploy.modify_minikube_config(config_path, "/test/.minikube") | ||
|
||
# Load the output. | ||
with open(config_path) as hf: | ||
config = yaml.load(hf) | ||
|
||
expected = {"apiVersion": "v1", | ||
"clusters": [{"cluster": {"certificate-authority": "/test/.minikube/ca.crt", | ||
"server": "https://10.240.0.18:8443"}, | ||
"name": "minikube"}], | ||
"contexts": [{"context": {"cluster": "minikube", "user": "minikube"}, | ||
"name": "minikube"}], | ||
"current-context": "minikube", | ||
"kind": "Config", | ||
"preferences": {}, | ||
"users": [{"name": "minikube", | ||
"user": {"as-user-extra": {}, | ||
"client-certificate": "/test/.minikube/client.crt", | ||
"client-key": "/test/.minikube/client.key"}}]} | ||
|
||
self.assertDictEqual(expected, config) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,56 @@ | ||
"""Utilities for working with VMs as part of our tests.""" | ||
|
||
import datetime | ||
import logging | ||
import os | ||
import subprocess | ||
import time | ||
import uuid | ||
|
||
from kubeflow.testing import util | ||
|
||
# TODO(jlewi): Should we move this to kubeflow/testing | ||
|
||
def wait_for_vm(project, zone, vm, timeout=datetime.timedelta(minutes=5), | ||
polling_interval=datetime.timedelta(seconds=10)): | ||
"""Wait for the VM to be ready. This is measured by trying to ssh into the VM. | ||
timeout: A datetime.timedelta expressing the amount of time to wait before | ||
giving up. | ||
polling_interval: A datetime.timedelta to represent the amount of time to | ||
wait between requests polling for the operation status. | ||
Raises: | ||
TimeoutError: if we timeout waiting for the operation to complete. | ||
""" | ||
endtime = datetime.datetime.now() + timeout | ||
while True: | ||
try: | ||
util.run(["gcloud", "compute", "--project=" + project, "ssh", | ||
"--zone=" + zone, vm, "--", "echo hello world"]) | ||
logging.info("VM is ready") | ||
return | ||
except subprocess.CalledProcessError: | ||
pass | ||
|
||
if datetime.datetime.now() > endtime: | ||
raise util.TimeoutError( | ||
"Timed out waiting for op: {0} to complete.".format(op_id)) | ||
time.sleep(polling_interval.total_seconds()) | ||
|
||
def execute(project, zone, vm, commands): | ||
"""Execute the supplied commands on the VM.""" | ||
util.run(["gcloud", "compute", "--project=" + project, "ssh", | ||
"--zone=" + zone, vm, "--", " && ".join(commands)]) | ||
|
||
def execute_script(project, zone, vm, script): | ||
"""Execute the specified script on the VM.""" | ||
|
||
target_path = os.path.join("/tmp", os.path.basename(script) + "." + uuid.uuid4().hex[0:4]) | ||
|
||
target = "{0}:{1}".format(vm, target_path) | ||
logging.info("Copying %s to %s", script, target) | ||
util.run(["gcloud", "compute", "--project=" + project, "scp", | ||
script, target, "--zone=" + zone]) | ||
|
||
util.run(["gcloud", "compute", "--project=" + project, "ssh", | ||
"--zone=" + zone, vm, "--", "chmod a+rx " + target_path + " && " + target_path]) |