Skip to content

Commit 29efdcc

Browse files
author
Luke Goodfellow
authored
On Prem Support (#2)
* added support for on prem installs * updated requirements with helm * updated requirements about Codefresh Tokens
1 parent 76df7e1 commit 29efdcc

File tree

5 files changed

+106
-15
lines changed

5 files changed

+106
-15
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Luke Goodfellow
3+
Copyright (c) 2024 Codefresh Support
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
# Codefresh Support Package
22

3-
This project is designed to gather data from Hybrid Runtimes for Codefresh SaaS platform and OnPrem Platforms. It collects information about various Kubernetes resources such as Pods, Nodes, Configmaps, Services, and Events. It also fetches logs for each Pod.
3+
This project is designed to gather data from Hybrid Runtimes for Codefresh SaaS platform, and Hybrid Runtimes and OnPrem isntallation on the OnPrem Platform. It collects information about various Kubernetes resources such as Pods, Nodes, Configmaps, Services, and Events. For Classic and OnPrem we gather some informtion from the platform itself.
44

55
## Prereqs
66

7-
- `kubectl config current-context` must be the context of the cluster where the Hybrid Runner is installed.
7+
- `kubectl`
8+
- Current Context must be the context of the cluster where the Codefresh is installed.
89
- Codefresh
910
- CLI installed and configured
1011
- Or the following ENV vars set
1112
- `CF_API_KEY`: Codefresh API Token
1213
- `CF_URL`: URL of the platform (ex: `https://g.codefresh.io`)
14+
- Need an Account Admin Token for Claasic Hybrid Runtime
15+
- Need a System Admin Token for the OnPrem Installation.
16+
- Helm
17+
- Version 3
18+
- Used to get the helm release version of the installation
1319

1420
## Usage
1521

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.0
1+
2.0.0

src/codefresh.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,31 @@ class Codefresh {
4545
}
4646
}
4747

48-
getRuntimeInfo(name) {
48+
async getOnPremAccounts() {
4949
try {
50-
const runtime = this.runtimes.find((re) => re.metadata.name === name);
51-
return runtime;
50+
const response = await fetch(`${this.baseURL}/admin/accounts`, {
51+
method: 'GET',
52+
headers: this.headers,
53+
});
54+
const accounts = await response.json();
55+
return accounts;
56+
} catch (error) {
57+
console.error(error);
58+
return error;
59+
}
60+
}
61+
62+
async getOnPremRuntimes() {
63+
try {
64+
const response = await fetch(`${this.baseURL}/admin/runtime-environments`, {
65+
method: 'GET',
66+
headers: this.headers,
67+
});
68+
const onPremRuntimes = await response.json();
69+
return onPremRuntimes;
5270
} catch (error) {
5371
console.error(error);
72+
return error;
5473
}
5574
}
5675
}

src/main.js

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Codefresh } from './codefresh.js';
44
import { autoDetectClient } from 'https://deno.land/x/[email protected]/mod.ts';
5+
import { AppsV1Api } from "https://deno.land/x/[email protected]/builtin/apps@v1/mod.ts";
56
import { BatchV1Api } from 'https://deno.land/x/[email protected]/builtin/batch@v1/mod.ts';
67
import { CoreV1Api } from 'https://deno.land/x/[email protected]/builtin/core@v1/mod.ts';
78
import { StorageV1Api } from 'https://deno.land/x/[email protected]/builtin/storage.k8s.io@v1/mod.ts';
@@ -11,17 +12,27 @@ import { stringify as toYaml } from 'https://deno.land/[email protected]/yaml/mod.ts';
1112

1213
console.log('Initializing \n');
1314
const kubeConfig = await autoDetectClient();
15+
const appsApi = new AppsV1Api(kubeConfig);
1416
const coreApi = new CoreV1Api(kubeConfig);
1517
const storageApi = new StorageV1Api(kubeConfig);
1618
const batchApi = new BatchV1Api(kubeConfig);
1719
const argoProj = new ArgoprojIoV1alpha1Api(kubeConfig);
1820
const timestamp = new Date().getTime();
1921
const dirPath = `./codefresh-support-${timestamp}`;
2022

23+
function selectRuntimeType() {
24+
const reTypes = ['classic', 'gitops', 'onprem'];
25+
reTypes.forEach((reType, index) => {
26+
console.log(`${index + 1}. ${reType}`);
27+
});
28+
const typeSelected = prompt('\nWhich Type Of Runtime Are We Using? (Number):');
29+
return reTypes[typeSelected - 1];
30+
}
31+
2132
async function saveItems(resources, dir) {
2233
await Deno.mkdir(`${dirPath}/${dir}/`, { recursive: true });
2334
return Promise.all(resources.map((item) => {
24-
return Deno.writeTextFile(`${dirPath}/${dir}/${item.metadata.name}.yaml`, toYaml(item, {skipInvalid: true}));
35+
return Deno.writeTextFile(`${dirPath}/${dir}/${item.metadata.name}.yaml`, toYaml(item, { skipInvalid: true }));
2536
}));
2637
}
2738

@@ -39,6 +50,10 @@ async function gatherClassic() {
3950

4051
console.log(`\nGathering Data For ${reSpec.metadata.name}.`);
4152

53+
const helmList = new Deno.Command('helm', { args: ['list', '-n', namespace, '-o', 'json'] });
54+
const output = await helmList.output();
55+
const helmReleases = JSON.parse(new TextDecoder().decode(output.stdout));
56+
4257
const dataFetchers = {
4358
'Cron': () => batchApi.namespace(namespace).getCronJobList(),
4459
'Jobs': () => batchApi.namespace(namespace).getJobList(),
@@ -65,7 +80,8 @@ async function gatherClassic() {
6580
}
6681
}
6782

68-
Deno.writeTextFile(`${dirPath}/runtimeSpec.yaml`, toYaml(reSpec, {skipInvalid: true}));
83+
Deno.writeTextFile(`${dirPath}/runtimeSpec.yaml`, toYaml(reSpec, { skipInvalid: true }));
84+
Deno.writeTextFile(`${dirPath}/classicReleases.yaml`, toYaml(helmReleases, { skipInvalid: true }));
6985
}
7086

7187
async function gatherGitOps() {
@@ -89,6 +105,10 @@ async function gatherGitOps() {
89105

90106
console.log(`\nGathering Data In ${namespace} For The GitOps Runtime.`);
91107

108+
const helmList = new Deno.Command('helm', { args: ['list', '-n', namespace, '-o', 'json'] });
109+
const output = await helmList.output();
110+
const helmReleases = JSON.parse(new TextDecoder().decode(output.stdout));
111+
92112
const dataFetchers = {
93113
'Apps': () => argoProj.namespace(namespace).getApplicationList(),
94114
'Nodes': () => coreApi.getNodeList(),
@@ -110,15 +130,58 @@ async function gatherGitOps() {
110130
await saveItems(resources.items, dir);
111131
}
112132
}
133+
134+
Deno.writeTextFile(`${dirPath}/gitopsReleases.yaml`, toYaml(helmReleases, { skipInvalid: true }));
113135
}
114136

115-
function selectRuntimeType() {
116-
const reTypes = ['classic', 'gitops'];
117-
reTypes.forEach((reType, index) => {
118-
console.log(`${index + 1}. ${reType}`);
137+
async function gatherOnPrem() {
138+
const cf = new Codefresh();
139+
await cf.init();
140+
const accounts = await cf.getOnPremAccounts();
141+
const runtimes = await cf.getOnPremRuntimes();
142+
143+
const namespaceList = await coreApi.getNamespaceList();
144+
console.log('');
145+
namespaceList.items.forEach((ns, index) => {
146+
console.log(`${index + 1}. ${ns.metadata.name}`);
119147
});
120-
const typeSelected = prompt('\nWhich Type Of Runtime Are We Using? (Number):');
121-
return reTypes[typeSelected - 1];
148+
const selection = prompt('\nWhich Namespace Is Codefresh OnPrem Installed In? (Number): ');
149+
const namespace = namespaceList.items[selection - 1].metadata.name;
150+
151+
console.log(`\nGathering Data For On Prem.`);
152+
153+
const helmList = new Deno.Command('helm', { args: ['list', '-n', namespace, '-o', 'json'] });
154+
const output = await helmList.output();
155+
const helmReleases = JSON.parse(new TextDecoder().decode(output.stdout));
156+
157+
const dataFetchers = {
158+
'Deployments': () => appsApi.namespace(namespace).getDeploymentList(),
159+
'Daemonsets': () => appsApi.namespace(namespace).getDaemonSetList(),
160+
'Nodes': () => coreApi.getNodeList(),
161+
'Volumes': () => coreApi.getPersistentVolumeList({ labelSelector: 'io.codefresh.accountName' }),
162+
'Volumeclaims': () => coreApi.namespace(namespace).getPersistentVolumeClaimList({ labelSelector: 'io.codefresh.accountName' }),
163+
'Services': () => coreApi.namespace(namespace).getServiceList(),
164+
'Pods': () => coreApi.namespace(namespace).getPodList(),
165+
'Events': () => coreApi.namespace(namespace).getEventList(),
166+
'Storageclass': () => storageApi.getStorageClassList(),
167+
};
168+
169+
for (const [dir, fetcher] of Object.entries(dataFetchers)) {
170+
const resources = await fetcher();
171+
if (dir === 'Pods') {
172+
await saveItems(resources.items, dir);
173+
await Promise.all(resources.items.map(async (item) => {
174+
const log = await coreApi.namespace(namespace).getPodLog(item.metadata.name, { container: item.spec.containers[0].name });
175+
return Deno.writeTextFile(`${dirPath}/${dir}/${item.metadata.name}.log`, log);
176+
}));
177+
} else {
178+
await saveItems(resources.items, dir);
179+
}
180+
}
181+
182+
Deno.writeTextFile(`${dirPath}/onPremReleases.yaml`, toYaml(helmReleases, { skipInvalid: true }));
183+
Deno.writeTextFile(`${dirPath}/onPremAccounts.yaml`, toYaml(accounts, { skipInvalid: true }));
184+
Deno.writeTextFile(`${dirPath}/onPremRuntimes.yaml`, toYaml(runtimes, { skipInvalid: true }));
122185
}
123186

124187
async function main() {
@@ -131,6 +194,9 @@ async function main() {
131194
case 'gitops':
132195
await gatherGitOps();
133196
break;
197+
case 'onprem':
198+
await gatherOnPrem();
199+
break;
134200
default:
135201
console.log('Invalid runtime type selected');
136202
return;

0 commit comments

Comments
 (0)