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

Ensure that .kube/config is written once microk8s is active #85

Merged
merged 1 commit into from
Sep 25, 2024
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
47 changes: 29 additions & 18 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,18 @@ 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 = {
silent: true,
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 @@ -5635,7 +5646,7 @@ function microk8s_init(channel, addons, container_registry_url) {
;
if (semver_1.default.lt(mk8s_ver, '1.24.0')) {
yield exec_as_microk8s("microk8s kubectl create serviceaccount test-sa");
if (!(yield retry_until_rc("microk8s kubectl get secrets | grep -q test-sa-token-"))) {
if (!(yield retry_until_rc("bash -c 'microk8s kubectl get secrets | grep -q test-sa-token-'"))) {
core.setFailed("Timed out waiting for test SA token");
return false;
}
Expand Down Expand Up @@ -5871,7 +5882,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
49 changes: 31 additions & 18 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,19 @@ const snap_version = async (snap_name: string) => {
return snap_line[1]
}

const microk8sKubeConfig = async () => {
// Get kubeconfig from microk8s
let kubeconfig = ""
let options = {
silent: true,
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 @@ -158,7 +171,7 @@ async function microk8s_init(channel, addons, container_registry_url:string) {
};
if (semver.lt(mk8s_ver, '1.24.0')) {
await exec_as_microk8s("microk8s kubectl create serviceaccount test-sa");
if (! await retry_until_rc("microk8s kubectl get secrets | grep -q test-sa-token-")) {
if (! await retry_until_rc("bash -c 'microk8s kubectl get secrets | grep -q test-sa-token-'")) {
core.setFailed("Timed out waiting for test SA token");
return false;
};
Expand Down Expand Up @@ -394,7 +407,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
8 changes: 8 additions & 0 deletions tests/test_deploy_k8s.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
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
out = check_output(["kubectl", "get", "pods", "-A"], text=True)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

assert "kube-system" in out, "should see some kube-system pods"
Loading