Skip to content

Commit

Permalink
Ensure that .kube/config is written out once microk8s is active
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Sep 25, 2024
1 parent b93f710 commit 860b870
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 34 deletions.
44 changes: 27 additions & 17 deletions dist/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5493,36 +5493,35 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const os = __importStar(__nccwpck_require__(2087));
const fs = __importStar(__nccwpck_require__(5747));
const utils_1 = __nccwpck_require__(2828);
const semver_1 = __importDefault(__nccwpck_require__(1383));
const ts_dedent_1 = __importDefault(__nccwpck_require__(3604));
const ignoreFail = { "ignoreReturnCode": true };
const user = os.userInfo().username;
const os_release = () => __awaiter(void 0, void 0, void 0, function* () {
// Read os-release file into an object
const checkOutput = (cmd, args, options) => __awaiter(void 0, void 0, void 0, function* () {
let stdout_buf = '';
const options = {
listeners: {
stdout: (data) => { stdout_buf += data.toString(); }
}
options = options || {};
options.listeners = {
stdout: (data) => { stdout_buf += data.toString(); }
};
yield exec.exec('cat', ['/etc/os-release'], options);
yield exec.exec(cmd, args, options);
return stdout_buf;
});
const os_release = () => __awaiter(void 0, void 0, void 0, function* () {
// Read os-release file into an object
const output = yield checkOutput('cat', ['/etc/os-release']);
let data = {};
stdout_buf.split('\n').forEach(function (line) {
output.split('\n').forEach(function (line) {
let [key, value] = line.split("=", 2);
data[key] = value;
});
return data;
});
const snap_version = (snap_name) => __awaiter(void 0, void 0, void 0, function* () {
let stdout_buf = '';
const options = {
listeners: {
stdout: (data) => { stdout_buf += data.toString(); }
}
};
yield exec.exec("snap", ["list", snap_name], options);
const lines = stdout_buf.split('\n');
// Get the version of a snap
const output = yield checkOutput("snap", ["list", snap_name, "--color=never"]);
const lines = output.split('\n');
if (lines.length < 2) {
throw new Error(`snap ${snap_name} not found`);
}
Expand All @@ -5532,6 +5531,17 @@ const snap_version = (snap_name) => __awaiter(void 0, void 0, void 0, function*
}
return snap_line[1];
});
const microk8sKubeConfig = () => __awaiter(void 0, void 0, void 0, function* () {
// Get kubeconfig from microk8s
let kubeconfig = "";
let options = {
listeners: {
stdout: (data) => { kubeconfig += data.toString(); }
}
};
yield exec_as_microk8s("microk8s config", options);
fs.writeFileSync(`${os.homedir()}/.kube/config`, kubeconfig, { encoding: 'utf8' });
});
const docker_lxd_clash = () => __awaiter(void 0, void 0, void 0, function* () {
// Work-around clash between docker and lxd on jammy
// https://github.com/docker/for-linux/issues/1034
Expand Down Expand Up @@ -5871,7 +5881,7 @@ function run() {
yield snap("install kubectl --classic");
yield exec.exec("mkdir", ["-p", `${HOME}/.kube`]);
if (provider === "microk8s") {
yield exec_as_microk8s(`microk8s config > ${HOME}/.kube/config`);
yield microk8sKubeConfig();
}
core.endGroup();
}
Expand Down
46 changes: 29 additions & 17 deletions src/bootstrap/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as os from 'os'
import * as fs from 'fs';
import { retryAsyncDecorator } from 'ts-retry/lib/cjs/retry/utils';
import semver from 'semver';
import dedent from 'ts-dedent';
Expand All @@ -14,32 +15,31 @@ declare var process : {
const ignoreFail: exec.ExecOptions = {"ignoreReturnCode": true}
const user = os.userInfo().username

const os_release = async () => {
// Read os-release file into an object
const checkOutput = async (cmd: string, args?: string[], options?: exec.ExecOptions) => {
let stdout_buf = '';
const options = {
listeners: {
stdout: (data: Buffer) => { stdout_buf += data.toString() }
}
options = options || {};
options.listeners = {
stdout: (data: Buffer) => { stdout_buf += data.toString() }
};
await exec.exec('cat', ['/etc/os-release'], options);
await exec.exec(cmd, args, options);
return stdout_buf
}

const os_release = async () => {
// Read os-release file into an object
const output = await checkOutput('cat', ['/etc/os-release']);
let data: { [name:string]: string} = {};
stdout_buf.split('\n').forEach(function(line){
output.split('\n').forEach(function(line){
let [key, value] = line.split("=", 2);
data[key] = value
})
return data
}

const snap_version = async (snap_name: string) => {
let stdout_buf = '';
const options = {
listeners: {
stdout: (data: Buffer) => { stdout_buf += data.toString() }
}
};
await exec.exec("snap", ["list", snap_name], options);
const lines = stdout_buf.split('\n');
// Get the version of a snap
const output = await checkOutput("snap", ["list", snap_name, "--color=never"]);
const lines = output.split('\n');
if (lines.length < 2) {
throw new Error(`snap ${snap_name} not found`)
}
Expand All @@ -50,6 +50,18 @@ const snap_version = async (snap_name: string) => {
return snap_line[1]
}

const microk8sKubeConfig = async () => {
// Get kubeconfig from microk8s
let kubeconfig = ""
let options = {
listeners: {
stdout: (data: Buffer) => { kubeconfig += data.toString() }
}
};
await exec_as_microk8s("microk8s config", options);
fs.writeFileSync(`${os.homedir()}/.kube/config`, kubeconfig, {encoding: 'utf8'});
}

const docker_lxd_clash = async () => {
// Work-around clash between docker and lxd on jammy
// https://github.com/docker/for-linux/issues/1034
Expand Down Expand Up @@ -394,7 +406,7 @@ async function run() {
await snap("install kubectl --classic");
await exec.exec("mkdir", ["-p", `${HOME}/.kube`]);
if (provider === "microk8s") {
await exec_as_microk8s(`microk8s config > ${HOME}/.kube/config`);
await microk8sKubeConfig();
}
core.endGroup();
} catch(error: any) {
Expand Down
7 changes: 7 additions & 0 deletions tests/test_deploy_k8s.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import pytest
from subprocess import check_output


@pytest.mark.abort_on_fail
async def test_build(ops_test):
await ops_test.model.deploy("ch:minio")
await ops_test.model.wait_for_idle()


@pytest.mark.usefixtures("ops_test")
async def test_kubectl():
# Confirms that kubectl has access to a functional kubeconfig @ $HOME/.kube/config
check_output(["kubectl", "get", "pods", "-A"])

0 comments on commit 860b870

Please sign in to comment.