Skip to content

Commit

Permalink
Changing nginx ingress to traefik, use Epinio CLI instead of APIs (
Browse files Browse the repository at this point in the history
…#65)

* removing ingress

* fix loop

* add missing ingress on DD

* updating Epinio to 1.11, update for info with json

* fix let

* add log

* bump epinio version
  • Loading branch information
enrichman authored Dec 5, 2023
1 parent 40c9d56 commit 520628e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 84 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ARG KUBECTL_CHECKSUM_LINUX_AMD64=aaa5ea3b3630730d2b8a8ef3cccb14b47754602c7207c7b
ARG KUBECTL_CHECKSUM_WINDOWS_AMD64=ed404eb0c3b74341d2ff799e78f9c0352e2bbd5c1b645652de2725ec77c0a78e

# https://github.com/epinio/epinio/releases
ARG EPINIO_VERSION=1.10.0
ARG EPINIO_VERSION=1.11.0

# /darwin amd64
RUN wget -nv https://get.helm.sh/helm-v${HELM_VERSION}-darwin-amd64.tar.gz && \
Expand Down Expand Up @@ -113,7 +113,7 @@ ARG KUBECTL_CHECKSUM_DARWIN_ARM64=4166d293b4f58e5293363f1f91a285d929a54557bf0c1a
ARG KUBECTL_CHECKSUM_LINUX_ARM64=741e65b681a22074aaf9459b57dbcef6a9e993472b3019a87f57c191bc68575f

# https://github.com/epinio/epinio/releases
ARG EPINIO_VERSION=1.10.0
ARG EPINIO_VERSION=1.11.0

# /darwin arm64
RUN wget -nv https://get.helm.sh/helm-v${HELM_VERSION}-darwin-arm64.tar.gz && \
Expand Down
9 changes: 7 additions & 2 deletions ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ function App() {
</Modal>
</div>

<Credentials enabled={hasKubernetes} credentials={credentials} onCredentialsChanged={setCredentials} installation={installation} />
<Credentials
enabled={hasKubernetes}
credentials={credentials}
onCredentialsChanged={setCredentials}
installation={installation}
domain={domain} />

<Box sx={{ width: '100%' }}>
<Typography variant="subtitle1" component="div" gutterBottom>
Expand Down Expand Up @@ -133,7 +138,7 @@ function App() {
<BottomNavigation showLabels sx={{ gridTemplateColumns: 'repeat(4, 1fr)' }}>
<Info apiDomain={uiDomain} enabled={hasKubernetes} credentials={credentials} info={epinioInfo} onInfoChanged={setEpinioInfo} />
<BottomNavigationAction label="epinio.io" icon={<HomeIcon />} onClick={openURL} url="https://epinio.io" />
<BottomNavigationAction label="CLI" icon={<DownloadIcon />} onClick={openURL} url="https://github.com/epinio/epinio/releases/tag/v1.10.0" />
<BottomNavigationAction label="CLI" icon={<DownloadIcon />} onClick={openURL} url="https://github.com/epinio/epinio/releases/tag/v1.11.0" />
</BottomNavigation>
</Paper>

Expand Down
63 changes: 35 additions & 28 deletions ui/src/epinio/API.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
import { Buffer } from 'buffer'

export default function EpinioClient({
apiDomain,
credentials: {
username,
password
}
apiDomain
}) {
const login = async (username, password) => {
console.info('EpinioClient.login')

await epinio([
'login', '-u', username, '-p', password, '--trust-ca', `epinio.${apiDomain}`
])
}

const logout = async () => {
console.info('EpinioClient.logout')

await epinio(['logout'])
}

const info = async () => {
console.log('EpinioClient.info')
return doFetch(`http://${apiDomain}/api/v1/info`)

const result = await epinio([
'info', '--output', 'json'
])

return JSON.parse(result)
}

const listApplications = async (namespace) => {
console.log('EpinioClient.listApplications')
return doFetch(`http://${apiDomain}/api/v1/namespaces/${namespace}/applications`)
}

const authHeaders = () => {
const encodedUserPass = Buffer.from(`${username}:${password}`).toString('base64')
const authHeader = `Basic ${encodedUserPass}`
const headers = new Headers()
headers.set('Authorization', authHeader)
return headers
}
const result = await epinio([
'app', 'list', '--output', 'json'
])

const doFetch = async (url) => {
console.debug('EpinioClient.call', 'executing fetch', url)
return JSON.parse(result)
}

const epinio = async (args) => {
try {
const resp = await fetch(url, { headers: authHeaders() })
console.debug('EpinioClient.call', 'response', resp)

const json = await resp.json()
console.debug('EpinioClient.call', 'response JSON', json)

return json
const result = await window.ddClient.extension.host.cli.exec('epinio', args)
return result.stdout
} catch (error) {
console.error('EpinioClient.call', error)
return Error(error)
if (error.stderr) {
throw Error(error.stderr)
}
throw error
}
}

return {
login,
logout,
info,
listApplications
}
Expand Down
50 changes: 26 additions & 24 deletions ui/src/epinio/Credentials.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import EpinioClient from './API'

function credsChanged(creds, update) {
return creds.username !== update.username || creds.password !== update.password
Expand All @@ -11,35 +12,36 @@ export function credentialsOK(creds) {
// Credentials will fetch the default user, when props.enabled is true
function Credentials(props) {
React.useEffect(() => {
const getCredentials = async () => {
const epinioClient = EpinioClient({ apiDomain: props.domain })

const login = async () => {
let u = { username: '-', password: '-' }

try {
// note: `-l` returns a list, hence the `.items...`, even if only a single secret matches
const result = await window.ddClient.extension.host.cli.exec(
'kubectl',
['get', 'secret', '-n', 'epinio', '-l', 'epinio.io/role=admin', '-o', 'jsonpath={.items[0].data}']
)
result.parseJsonObject()
// Retrieval above as check that epinio is present.
// Creds hardwired, unchanged from defaults
const u = { username: 'admin', password: 'password' }
if (credsChanged(props.credentials, u)) {
props.onCredentialsChanged(u)
}
await epinioClient.login('admin', 'password')
u = { username: 'admin', password: 'password' }
} catch (error) {
// for debugging:
// if (error instanceof Error) {
// console.error(error);
// } else {
// console.log(JSON.stringify(error));
// }
const u = { username: '-', password: '-' }
if (credsChanged(props.credentials, u)) {
props.onCredentialsChanged(u)
}
console.error(error)
}

if (credsChanged(props.credentials, u)) {
props.onCredentialsChanged(u)
}
}

const logout = async () => {
await epinioClient.logout()

const u = { username: '-', password: '-' }
if (credsChanged(props.credentials, u)) {
props.onCredentialsChanged(u)
}
}

if (props.enabled) {
getCredentials()
login()
} else {
logout()
}
}, [props])

Expand Down
85 changes: 57 additions & 28 deletions ui/src/epinio/Installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ export default function EpinioInstaller({
return result?.stdout
}

const kubectl = async (args) => {
const result = await window.ddClient.extension.host.cli.exec('kubectl', args)
console.debug(JSON.stringify(result))

if (result.stderr) {
throw Error(result?.stderr)
}

return result.parseJsonObject()
}

const isEpinioInstalled = async () => {
console.debug('check Epinio installation')

Expand Down Expand Up @@ -52,10 +63,13 @@ export default function EpinioInstaller({
async function install() {
try {
setProgress(10)
await installNginx()
setProgress(25)

const isInstalled = await checkTraefik()
if (!isInstalled) {
await installTraefik()
}
setProgress(30)

setProgress(40)
await installCertManager()
setProgress(50)

Expand All @@ -69,8 +83,12 @@ export default function EpinioInstaller({
setInstalled(false)
onInstallationChanged(false)

let message = 'Error installing Epinio'
if (error.stderr) {
message = error.stderr
}
console.error(error)
const message = `If the nginx service is stuck in pending state, you might need to restart docker desktop. \n ${JSON.stringify(error)}`

onError(message)
} finally {
setProgress(0)
Expand All @@ -87,8 +105,8 @@ export default function EpinioInstaller({
await uninstallCertManager()
setProgress(50)

setProgress(75)
await uninstallNginx()
setProgress(60)
await uninstallTraefik()
setProgress(100)

onInstallationChanged(true)
Expand All @@ -101,18 +119,31 @@ export default function EpinioInstaller({
}
}

const installNginx = async () => {
console.log('installing nginx chart')
const checkTraefik = async () => {
console.log('checking traefik installation')

const result = await kubectl([
'get', 'svc', '-A',
'-l', 'app.kubernetes.io/name=traefik',
'-o', 'json'
])

const isInstalled = result.items.length > 0
console.log(`traefik already installed: ${isInstalled}`)

return isInstalled
}

const installTraefik = async () => {
console.log('installing traefik chart')

await helm([
'upgrade', '--install', '--atomic', 'ingress-nginx',
'--create-namespace', '--namespace', 'ingress-nginx',
'https://github.com/kubernetes/ingress-nginx/releases/download/helm-chart-4.7.1/ingress-nginx-4.7.1.tgz'
'upgrade', '--install', '--atomic', 'traefik',
'--create-namespace', '--namespace', 'ingress-traefik',
'https://traefik.github.io/charts/traefik/traefik-19.0.3.tgz'
])

// https://github.com/docker/for-mac/issues/4903
console.log('installed: nginx')
console.log("you might need to restart docker-desktop if localhost:443 doesn't forward to nginx")
console.log('installed: traefik')
}

const installCertManager = async () => {
Expand All @@ -137,14 +168,23 @@ export default function EpinioInstaller({
'--create-namespace', '--namespace', 'epinio',
'--atomic',
'--set', 'global.domain=' + domain,
'--set', 'ingress.ingressClassName=nginx',
'--set', 'ingress.nginxSSLRedirect=false',
'https://github.com/epinio/helm-charts/releases/download/epinio-1.10.0/epinio-1.10.0.tgz'
'https://github.com/epinio/helm-charts/releases/download/epinio-1.11.0/epinio-1.11.0.tgz'
])

console.log('installed: epinio')
}

const uninstallTraefik = async () => {
console.log('uninstalling traefik chart')

await helm([
'uninstall', '--namespace', 'ingress-traefik',
'--wait', 'traefik'
])

console.log('uninstalled: traefik')
}

const uninstallEpinio = async () => {
console.log('uninstalling epinio chart')

Expand All @@ -167,17 +207,6 @@ export default function EpinioInstaller({
console.log('uninstalled: cert-manager')
}

const uninstallNginx = async () => {
console.log('uninstalling nginx chart')

await helm([
'uninstall', '--namespace', 'ingress-nginx',
'--wait', 'ingress-nginx'
])

console.log('uninstalled: nginx')
}

// spawn epinio status check only once
useEffect(() => {
checkEpinioStatus()
Expand Down

0 comments on commit 520628e

Please sign in to comment.