-
Notifications
You must be signed in to change notification settings - Fork 65
/
microk8s.ts
139 lines (129 loc) · 4.17 KB
/
microk8s.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Copyright (c) 2019-2021 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
import { Command } from '@oclif/command'
import * as commandExists from 'command-exists'
import * as execa from 'execa'
import * as Listr from 'listr'
import { VersionHelper } from '../../api/version'
import { CommonPlatformTasks } from './common-platform-tasks'
export class MicroK8sTasks {
/**
* Returns tasks list which perform preflight platform checks.
*/
preflightCheckTasks(flags: any, command: Command): Listr {
return new Listr([
{
title: 'Verify if kubectl is installed',
task: () => {
if (!commandExists.sync('kubectl')) {
command.error('E_REQUISITE_NOT_FOUND')
}
},
},
{
title: 'Verify if microk8s is installed',
task: () => {
if (!commandExists.sync('microk8s.status')) {
command.error('E_REQUISITE_NOT_FOUND', { code: 'E_REQUISITE_NOT_FOUND' })
}
},
},
{
title: 'Verify if microk8s is running',
task: async (ctx: any) => {
ctx.isMicroK8sRunning = await this.isMicroK8sRunning()
},
},
{
title: 'Start microk8s',
skip: (ctx: any) => {
if (ctx.isMicroK8sRunning) {
return 'MicroK8s is already running.'
}
},
task: () => {
// microk8s.start requires sudo permissions
// this.startMicroK8s()
command.error('MicroK8s is not running.', { code: 'E_REQUISITE_NOT_RUNNING' })
},
},
VersionHelper.getK8sCheckVersionTask(flags),
{
title: 'Verify if microk8s ingress and storage addons is enabled',
task: async (ctx: any) => {
ctx.enabledAddons = await this.enabledAddons()
},
},
{
title: 'Enable microk8s ingress addon',
skip: (ctx: any) => {
if (ctx.enabledAddons.ingress) {
return 'Ingress addon is already enabled.'
}
},
task: () => this.enableIngressAddon(),
},
{
title: 'Enable microk8s storage addon',
skip: (ctx: any) => {
if (ctx.enabledAddons.storage) {
return 'Storage addon is already enabled.'
}
},
task: () => {
// Enabling storage requires sudo permissions
// this.enableStorageAddon()
return command.error('The storage addon hasn\'t been enabled in microk8s', { code: 'E_REQUISITE_NOT_FOUND' })
},
},
{
title: 'Retrieving microk8s IP and domain for ingress URLs',
enabled: () => !flags.domain,
task: async (_ctx: any, task: any) => {
const ip = await this.getMicroK8sIP()
flags.domain = ip + '.nip.io'
task.title = `${task.title}...${flags.domain}.`
},
},
CommonPlatformTasks.getPingClusterTask(flags),
], { renderer: flags['listr-renderer'] as any })
}
async isMicroK8sRunning(): Promise<boolean> {
const { exitCode } = await execa('microk8s.status', { timeout: 10000, reject: false })
if (exitCode === 0) {
return true
} else {
return false
}
}
async startMicroK8s() {
execa('microk8s.start', { timeout: 180000 })
}
async enabledAddons(): Promise<object> {
const { stdout } = await execa('microk8s.status', ['--format', 'short'], { timeout: 10000 })
return {
ingress: stdout.includes('ingress: enabled'),
storage: stdout.includes('storage: enabled'),
}
}
async enableIngressAddon() {
await execa('microk8s.enable', ['ingress'], { timeout: 10000 })
}
async enableStorageAddon() {
await execa('microk8s.enable', ['storage'], { timeout: 10000 })
}
async getMicroK8sIP(): Promise<string> {
const { stdout } = await execa('microk8s.config', { timeout: 10000 })
const regMatch = /server:\s*https?:\/\/([\d.]+)/.exec(stdout)
return regMatch ? regMatch[1] : ''
}
}