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

Add input for setting docker registry for microk8s #63

Merged
merged 10 commits into from
Dec 14, 2023
2 changes: 2 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ inputs:
description: "microk8s addons to enable"
required: false
default: "storage dns rbac"
container-registry-url:
description: "Container registry to use"
runs:
using: "node16"
main: "dist/bootstrap/index.js"
Expand Down
79 changes: 76 additions & 3 deletions dist/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4317,6 +4317,55 @@ const validRange = (range, options) => {
module.exports = validRange


/***/ }),

/***/ 3604:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.dedent = void 0;
function dedent(templ) {
var values = [];
for (var _i = 1; _i < arguments.length; _i++) {
values[_i - 1] = arguments[_i];
}
var strings = Array.from(typeof templ === 'string' ? [templ] : templ);
strings[strings.length - 1] = strings[strings.length - 1].replace(/\r?\n([\t ]*)$/, '');
var indentLengths = strings.reduce(function (arr, str) {
var matches = str.match(/\n([\t ]+|(?!\s).)/g);
if (matches) {
return arr.concat(matches.map(function (match) { var _a, _b; return (_b = (_a = match.match(/[\t ]/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0; }));
}
return arr;
}, []);
if (indentLengths.length) {
var pattern_1 = new RegExp("\n[\t ]{" + Math.min.apply(Math, indentLengths) + "}", 'g');
strings = strings.map(function (str) { return str.replace(pattern_1, '\n'); });
}
strings[0] = strings[0].replace(/^\r?\n/, '');
var string = strings[0];
values.forEach(function (value, i) {
var endentations = string.match(/(?:^|\n)( *)$/);
var endentation = endentations ? endentations[1] : '';
var indentedValue = value;
if (typeof value === 'string' && value.includes('\n')) {
indentedValue = String(value)
.split('\n')
.map(function (str, i) {
return i === 0 ? str : "" + endentation + str;
})
.join('\n');
}
string += indentedValue + strings[i + 1];
});
return string;
}
exports.dedent = dedent;
exports.default = dedent;
//# sourceMappingURL=index.js.map

/***/ }),

/***/ 2467:
Expand Down Expand Up @@ -5443,8 +5492,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
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 os_release = () => __awaiter(void 0, void 0, void 0, function* () {
// Read os-release file into an object
Expand Down Expand Up @@ -5505,9 +5556,30 @@ function retry_until_rc(cmd, expected_rc = 0, maxRetries = 12, timeout = 10000)
return false;
});
}
function microk8s_init(addons) {
function microk8s_init(addons, container_registry_url) {
return __awaiter(this, void 0, void 0, function* () {
// microk8s needs some additional things done to ensure it's ready for Juju.
// Add container registry configuration if given.
if (container_registry_url) {
let hostname;
let port;
try {
const url = new URL(container_registry_url);
hostname = url.hostname;
port = url.port;
}
catch (err) {
core.setFailed(`Failed to parse URL of container registry for microk8s: ${err}`);
return false;
}
let content = ts_dedent_1.default `
server = "${container_registry_url}"

[host."${hostname}:${port}"]
capabilities = ["pull", "resolve"]
`;
fs.writeFileSync("/var/snap/microk8s/current/args/certs.d/docker.io/hosts.toml", content);
}
// Add the given addons if any were given.
yield exec_as_microk8s("microk8s status --wait-ready");
if (addons) {
Expand Down Expand Up @@ -5583,6 +5655,7 @@ function run() {
const microk8s_group = get_microk8s_group();
let bootstrap_constraints = core.getInput("bootstrap-constraints");
const microk8s_addons = core.getInput("microk8s-addons");
const container_registry_url = core.getInput("container-registry-url") || process.env["CONTAINER_REGISTRY_URL"];
let group = "";
try {
core.addPath('/snap/bin');
Expand Down Expand Up @@ -5650,7 +5723,7 @@ function run() {
core.endGroup();
core.startGroup("Initialize microk8s");
yield exec.exec('bash', ['-c', `sudo usermod -a -G ${microk8s_group} $USER`]);
if (!(yield microk8s_init(microk8s_addons))) {
if (!(yield microk8s_init(microk8s_addons, container_registry_url))) {
return;
}
group = microk8s_group;
Expand Down Expand Up @@ -5829,4 +5902,4 @@ module.exports = require("util");;
/******/ // Load entry module and return exports
/******/ return __nccwpck_require__(1098);
/******/ })()
;
;
25 changes: 25 additions & 0 deletions dist/bootstrap/licenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


ts-dedent
MIT
MIT License

Copyright (c) 2018 Tamino Martinius

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


ts-retry
MIT
MIT License
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@actions/glob": "^0.2.0",
"@types/semver": "^7.3.9",
"semver": "^7.3.7",
"ts-dedent": "^2.2.0",
"ts-retry": "^4.1.1"
},
"devDependencies": {
Expand Down
28 changes: 26 additions & 2 deletions src/bootstrap/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as fs from 'fs';
import { retryAsyncDecorator } from 'ts-retry/lib/cjs/retry/utils';
import semver from 'semver';
import dedent from 'ts-dedent';

declare var process : {
env: {
Expand Down Expand Up @@ -71,8 +73,29 @@ async function retry_until_rc(cmd: string, expected_rc=0, maxRetries=12, timeout
return false;
}

async function microk8s_init(addons) {
async function microk8s_init(addons, container_registry_url:string) {
// microk8s needs some additional things done to ensure it's ready for Juju.
// Add container registry configuration if given.
if (container_registry_url) {
let hostname;
let port;
try {
const url = new URL(container_registry_url);
hostname = url.hostname;
port = url.port;
} catch(err) {
core.setFailed(`Failed to parse URL of container registry for microk8s: ${err}`);
return false;
}
let content = dedent`
server = "${container_registry_url}"

[host."${hostname}:${port}"]
capabilities = ["pull", "resolve"]
`;
fs.writeFileSync("/var/snap/microk8s/current/args/certs.d/docker.io/hosts.toml", content)
}

// Add the given addons if any were given.
await exec_as_microk8s("microk8s status --wait-ready");
if (addons) {
Expand Down Expand Up @@ -151,6 +174,7 @@ async function run() {
const microk8s_group = get_microk8s_group();
let bootstrap_constraints = core.getInput("bootstrap-constraints");
const microk8s_addons = core.getInput("microk8s-addons")
const container_registry_url = core.getInput("container-registry-url") || process.env["CONTAINER_REGISTRY_URL"]
let group = "";
try {
core.addPath('/snap/bin');
Expand Down Expand Up @@ -218,7 +242,7 @@ async function run() {
core.endGroup();
core.startGroup("Initialize microk8s");
await exec.exec('bash', ['-c', `sudo usermod -a -G ${microk8s_group} $USER`]);
if(!await microk8s_init(microk8s_addons)) {
if(!await microk8s_init(microk8s_addons, container_registry_url)) {
return;
}
group = microk8s_group;
Expand Down
Loading