Skip to content

Commit 529e670

Browse files
authored
Support Package Refresh 2024-10 (#15)
- Use describe command for collecting data - Remove "get" data except for PV and PVCs - Added list outputs for PV and PVCs (resolves #14 ) - Add an option to run a Test Pipeline before collecting data ( resolves #13 ) - Refactored code to use TypeScript - Refactored code to be in different files for separation of duty - Updated dependency versions and Deno version
1 parent cde27fa commit 529e670

File tree

16 files changed

+824
-420
lines changed

16 files changed

+824
-420
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// For format details, see https://aka.ms/devcontainer.json.
22
{
33
"name": "Codefresh Support Package",
4-
"image": "denoland/deno:1.46.3",
4+
"image": "denoland/deno:2.0.3",
55
"onCreateCommand": "apt-get update && apt-get install git zip -y",
66
"customizations": {
77
"vscode": {
88
"settings": {
99
"deno.enable": true,
10-
"deno.lint": true,
11-
"deno.unstable": false
10+
"deno.lint": true
1211
},
1312
"extensions": [
1413
"denoland.vscode-deno",

CONTRIBUTIONS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ Pull requests are the best way to propose changes to the codebase. We actively w
2020

2121
## Any contributions you make will be under the MIT Software License
2222

23-
In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers
24-
the project. Feel free to contact the maintainers if that's a concern.
23+
In short, when you submit code changes, your submissions are understood to be under the same
24+
[MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the maintainers if
25+
that's a concern.
2526

2627
## Development
2728

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,10 @@ chmod +x cf-support
7676
1. Go the the [Latest](https://github.com/codefresh-support/codefresh-support-package/releases/latest) release.
7777
1. Download the cf-support_windows_x86_64.zip file
7878
1. Run the `.exe` file via CMD or PowerShell
79+
80+
## Exit Codes
81+
82+
- 10 - Failed to get codefresh credentials. Please set the enviroment variables (CF_API_KEY and CF_BASE_URL) or make sure you have a valid codefresh config file.
83+
- 20 - Failed to Create Demo Pipeline / Project or Failed to run Demo Pipeline.
84+
- 30 - Failed to Delete Demo Pipeline / Project
85+
- 40 - Invalid Runtime Type. ex: Selecting On-Prem for a SaaS Account.

ci/codefresh.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.0"
1+
version: '1.0'
22

33
stages:
44
- Clone
@@ -20,7 +20,7 @@ steps:
2020
title: Compiling
2121
stage: Build
2222
arguments:
23-
image: denoland/deno:alpine-1.46.3
23+
image: denoland/deno:alpine-2.0.3
2424
commands:
2525
- cf_export VERSION=$(cat VERSION)
2626
- deno task compile

deno.json

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
{
22
"fmt": {
33
"indentWidth": 2,
4-
"lineWidth": 160,
4+
"lineWidth": 120,
55
"singleQuote": true,
66
"semiColons": true,
7-
"exclude": [".devcontainer/**"]
7+
"exclude": [
8+
".devcontainer/**",
9+
"README.md",
10+
"ci/**"
11+
]
812
},
913
"tasks": {
1014
"pre-compile": "rm -rf ./bin && mkdir ./bin",
11-
"compile:linux": "deno compile --config=./deno.json --allow-env --allow-read --allow-write --allow-net --unsafely-ignore-certificate-errors --allow-run --output=./bin/cf-support_linux_x86_64 --target=x86_64-unknown-linux-gnu ./src/main.js",
12-
"compile:windows": "deno compile --config=./deno.json --allow-env --allow-read --allow-write --allow-net --unsafely-ignore-certificate-errors --allow-run --output=./bin/cf-support_windows_x86_64 --target=x86_64-pc-windows-msvc ./src/main.js",
13-
"compile:apple": "deno compile --config=./deno.json --allow-env --allow-read --allow-write --allow-net --unsafely-ignore-certificate-errors --allow-run --output=./bin/cf-support_darwin_x86_64 --target=x86_64-apple-darwin ./src/main.js",
14-
"compile:apple_arm64": "deno compile --config=./deno.json --allow-env --allow-read --allow-write --allow-net --unsafely-ignore-certificate-errors --allow-run --output=./bin/cf-support_darwin_arm64 --target=aarch64-apple-darwin ./src/main.js",
15+
"compile:linux": "deno compile --config=./deno.json --allow-env --allow-read --allow-write --allow-net --unsafely-ignore-certificate-errors --allow-run --output=./bin/cf-support_linux_x86_64 --target=x86_64-unknown-linux-gnu ./src/main.ts",
16+
"compile:windows": "deno compile --config=./deno.json --allow-env --allow-read --allow-write --allow-net --unsafely-ignore-certificate-errors --allow-run --output=./bin/cf-support_windows_x86_64 --target=x86_64-pc-windows-msvc ./src/main.ts",
17+
"compile:apple": "deno compile --config=./deno.json --allow-env --allow-read --allow-write --allow-net --unsafely-ignore-certificate-errors --allow-run --output=./bin/cf-support_darwin_x86_64 --target=x86_64-apple-darwin ./src/main.ts",
18+
"compile:apple_arm64": "deno compile --config=./deno.json --allow-env --allow-read --allow-write --allow-net --unsafely-ignore-certificate-errors --allow-run --output=./bin/cf-support_darwin_arm64 --target=aarch64-apple-darwin ./src/main.ts",
1519
"compile": "deno task pre-compile && deno task compile:linux && deno task compile:windows && deno task compile:apple && deno task compile:apple_arm64"
1620
},
1721
"imports": {
18-
"@cloudydeno/kubernetes-apis": "jsr:@cloudydeno/kubernetes-apis@^0.5.1",
22+
"@cliffy/table": "jsr:@cliffy/[email protected]",
23+
"@cloudydeno/kubernetes-apis": "jsr:@cloudydeno/kubernetes-apis@^0.5.2",
1924
"@cloudydeno/kubernetes-client": "jsr:@cloudydeno/kubernetes-client@^0.7.3",
2025
"@fakoua/zip-ts": "jsr:@fakoua/zip-ts@^1.3.1",
21-
"@std/yaml": "jsr:@std/yaml@^1.0.5"
26+
"@std/encoding": "jsr:@std/encoding@^1.0.5",
27+
"@std/yaml": "jsr:@std/yaml@^1.0.5",
28+
"pako": "npm:pako@^2.1.0"
2229
}
23-
}
30+
}

src/codefresh.js

Lines changed: 0 additions & 105 deletions
This file was deleted.

src/codefresh/codefresh.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { parse } from '../deps.ts';
2+
3+
enum ContextKeys {
4+
Token = 'token',
5+
Url = 'url',
6+
}
7+
8+
interface Context {
9+
[ContextKeys.Token]: string;
10+
[ContextKeys.Url]: string;
11+
}
12+
13+
interface CodefreshConfig {
14+
contexts: {
15+
[key: string]: Context;
16+
};
17+
'current-context': string;
18+
}
19+
20+
async function readConfigFile() {
21+
const configPath = Deno.build.os === 'windows'
22+
? `${Deno.env.get('USERPROFILE')}/.cfconfig`
23+
: `${Deno.env.get('HOME')}/.cfconfig`;
24+
const configFileContent = await Deno.readTextFile(configPath);
25+
return parse(configFileContent) as CodefreshConfig;
26+
}
27+
28+
async function getCodefreshCredentials(envVar: string, configKey: ContextKeys) {
29+
const envValue = Deno.env.get(envVar);
30+
if (envValue) {
31+
return envValue;
32+
}
33+
34+
try {
35+
const cfConfig = await readConfigFile();
36+
return cfConfig.contexts[cfConfig['current-context']][configKey];
37+
} catch (error) {
38+
console.error('Failed to get Codefresh credentials:', error);
39+
console.error(
40+
'Please set the environment variables (CF_API_KEY and CF_BASE_URL) or make sure you have a valid Codefresh config file.',
41+
);
42+
Deno.exit(10);
43+
}
44+
}
45+
46+
export async function autoDetectCodefreshClient() {
47+
const headers = {
48+
Authorization: await getCodefreshCredentials('CF_API_KEY', ContextKeys.Token),
49+
};
50+
const baseUrl = `${await getCodefreshCredentials('CF_BASE_URL', ContextKeys.Url)}/api`;
51+
return { headers, baseUrl };
52+
}

src/codefresh/gitops.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { fetchAndSaveData, prepareAndCleanup, RuntimeType, selectNamespace } from '../deps.ts';
2+
3+
export async function gitopsRuntime() {
4+
try {
5+
const namespace = await selectNamespace();
6+
console.log(`\nGathering data in "${namespace}" namespace for the GitOps Runtime.`);
7+
await fetchAndSaveData(RuntimeType.gitops, namespace);
8+
console.log('\nData Gathered Successfully.');
9+
await prepareAndCleanup();
10+
} catch (error) {
11+
console.error(`Error gathering GitOps runtime data:`, error);
12+
}
13+
}

src/codefresh/onprem.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { fetchAndSaveData, prepareAndCleanup, RuntimeType, selectNamespace, writeCodefreshFiles } from '../deps.ts';
2+
3+
async function getAllAccounts(config: { headers: { Authorization: string }; baseUrl: string }) {
4+
const response = await fetch(`${config.baseUrl}/admin/accounts`, {
5+
method: 'GET',
6+
headers: config.headers,
7+
});
8+
const accounts = await response.json();
9+
await writeCodefreshFiles(accounts, 'onPrem-accounts');
10+
}
11+
12+
async function getAllRuntimes(config: { headers: { Authorization: string }; baseUrl: string }) {
13+
const response = await fetch(`${config.baseUrl}/admin/runtime-environments`, {
14+
method: 'GET',
15+
headers: config.headers,
16+
});
17+
const onPremRuntimes = await response.json();
18+
await writeCodefreshFiles(onPremRuntimes, 'onPrem-runtimes');
19+
}
20+
21+
async function getTotalUsers(config: { headers: { Authorization: string }; baseUrl: string }) {
22+
const response = await fetch(`${config.baseUrl}/admin/user?limit=1&page=1`, {
23+
method: 'GET',
24+
headers: config.headers,
25+
});
26+
const users = await response.json();
27+
await writeCodefreshFiles({ total: users.total }, 'onPrem-totalUsers');
28+
}
29+
30+
async function getSystemFeatureFlags(config: { headers: { Authorization: string }; baseUrl: string }) {
31+
const response = await fetch(`${config.baseUrl}/admin/features`, {
32+
method: 'GET',
33+
headers: config.headers,
34+
});
35+
const onPremSystemFF = await response.json();
36+
await writeCodefreshFiles(onPremSystemFF, 'onPrem-systemFeatureFlags');
37+
}
38+
39+
export async function onPrem(config: { headers: { Authorization: string }; baseUrl: string }) {
40+
if (config.baseUrl === 'https://g.codefresh.io/api') {
41+
console.error(
42+
`\nCannot gather On-Prem data for Codefresh SaaS. Please select either ${RuntimeType.pipelines} or ${RuntimeType.gitops}.`,
43+
);
44+
console.error(
45+
'If you need to gather data for Codefresh On-Prem, please update your ./cfconfig conext (or Envs) to point to an On-Prem instance.',
46+
);
47+
Deno.exit(40);
48+
}
49+
try {
50+
const namespace = await selectNamespace();
51+
console.log(`\nGathering data in "${namespace}" namespace for Codefresh On-Prem.`);
52+
await fetchAndSaveData(RuntimeType.onprem, namespace);
53+
await Promise.all([
54+
getAllAccounts(config),
55+
getAllRuntimes(config),
56+
getTotalUsers(config),
57+
getSystemFeatureFlags(config),
58+
]);
59+
console.log('\nData Gathered Successfully.');
60+
await prepareAndCleanup();
61+
} catch (error) {
62+
console.error(`Error gathering On-Prem data:`, error);
63+
}
64+
}

0 commit comments

Comments
 (0)