Skip to content

Commit

Permalink
feat(cdk8s): fluent-bit
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfouquet committed Oct 11, 2023
1 parent 0b2a7b7 commit 5d9e60f
Show file tree
Hide file tree
Showing 11 changed files with 3,470 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
dist/
4 changes: 4 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
...require('@linzjs/style/.prettierrc.cjs'),
};

2 changes: 2 additions & 0 deletions cdk8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app: npx tsx config/namespaces/argo.ts
language: typescript
36 changes: 36 additions & 0 deletions config/charts/fluent-bit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Chart, ChartProps, Helm } from 'cdk8s';
import { Construct } from 'constructs';
import { applyDefaultLabels } from '../util/labels.js';

export class FluentBit extends Chart {
constructor(scope: Construct, id: string, props: ChartProps) {
super(scope, id, applyDefaultLabels(props, 'aws-for-fluent-bit', '2.31.11', 'logs', 'workflows'));

const FluentParserName = 'containerd';
// This needs to be properly formatted, and it was stolen directly from https://github.com/microsoft/fluentbit-containerd-cri-o-json-log
// The key part is the message must be parsed as "log" otherwise it wont be parsed as JSON
const FluentContainerParser = `[PARSER]
Name ${FluentParserName}
Format regex
Regex ^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[^ ]*) (?<log>.*)$
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L%z
`;

new Helm(this, 'aws-for-fluent-bit', {
chart: 'aws-for-fluent-bit',
repo: 'https://aws.github.io/eks-charts',
namespace: 'kube-system',
version: '0.1.30',
values: {
input: { parser: FluentParserName, dockerMode: 'Off' },
serviceAccount: { name: "aws-for-fluent-bit-sa", create: false },
cloudWatchLogs: { enabled: true, region: 'ap-southeast-2', autoCreateGroup: true, logRetentionDays: 30 },
firehose: { enabled: false },
kinesis: { enabled: false },
elasticsearch: { enabled: false },
service: { extraParsers: FluentContainerParser },
},
});
}
}
13 changes: 13 additions & 0 deletions config/charts/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Chart, ChartProps } from 'cdk8s';
import { Namespace } from 'cdk8s-plus-27';
import { Construct } from 'constructs';
import { applyDefaultLabels } from '../util/labels';


export class NamespaceChart extends Chart {
constructor(scope: Construct, id: string, props: ChartProps) {
super(scope, id, applyDefaultLabels(props, 'namespace', '1.0.0', 'namespace', 'workflows'));

new Namespace(this, 'namespace', { metadata: { name: props.namespace } });
}
}
15 changes: 15 additions & 0 deletions config/namespaces/argo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { App } from 'cdk8s';
import { NamespaceChart } from '../charts/namespace.js';
import { FluentBit } from '../charts/fluent-bit.js';

const app = new App();

async function main(): Promise<void> {
const ns = new NamespaceChart(app, 'namespace', { namespace: 'argo' });

const fluentBit = new FluentBit(app, 'fluentBit', {});
fluentBit.addDependency(ns);
app.synth();
}

main();
45 changes: 45 additions & 0 deletions config/util/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { execFileSync } from 'node:child_process';

interface GitBuildInfo {
/**
* Last git version tag
*
* @example
* "v6.45.0"
*/
version: string;
/**
* Current git commit hash
*
* @example
* "e460a1bf611b9464f4c2c3feb48e4823277f14a4"
*/
hash: string;
/**
* Github actions run id and attempt if it exists, otherwise ""
*
* @example
* "6228679664-1"
*/
buildId: string;
}

let buildInfo: GitBuildInfo | undefined;

/**
* Attempt to guess build information from the currently checked out version of the source code
*
* @returns Basic Git/Github build information
*/
export function getGitBuildInfo(): GitBuildInfo {
if (buildInfo == null) {
buildInfo = {
version: execFileSync('git', ['describe', '--tags', '--always', '--match', 'v*']).toString().trim(),
hash: execFileSync('git', ['rev-parse', 'HEAD']).toString().trim(),
buildId: process.env['GITHUB_RUN_ID']
? `${process.env['GITHUB_RUN_ID']}-${process.env['GITHUB_RUN_ATTEMPT']}`
: '',
};
}
return buildInfo;
}
54 changes: 54 additions & 0 deletions config/util/labels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getGitBuildInfo } from "./build";


/**
* Generate a collection of standard labels for all components
* @param name The name of the application
* @param version The current version of the application
* @param component The component within the architecture
* @param partOf The name of a higher level application this one is part of
* @returns labels in the form of `app.kubernetes.io/name`
*/
export function defaultLabels(
name: string,
version: string,
component: string,
partOf: string,
): Record<string, string> {
return {
'app.kubernetes.io/name': name,
// Force a `v` prefix so the yaml doesn't consider it a number
'app.kubernetes.io/version': version.startsWith('v') ? version : `v${version}`,
'app.kubernetes.io/component': component,
'app.kubernetes.io/part-of': partOf,
'app.kubernetes.io/managed-by': 'cdk8s',
'app.kubernetes.io/git-hash': getGitBuildInfo().hash,
'app.kubernetes.io/git-version': getGitBuildInfo().version,
'app.kubernetes.io/build-id': getGitBuildInfo().buildId,
};
}

/**
* Generate and apply a collection of standard labels for all components
* @param name The name of the application
* @param version The current version of the application
* @param component The component within the architecture
* @param partOf The name of a higher level application this one is part of
*
* @returns labels in the form of `app.kubernetes.io/name`
*/
export function applyDefaultLabels<T extends { labels?: Record<string, string> }>(
props: T,
name: string,
version: string,
component: string,
partOf: string,
): T {
return {
...props,
labels: {
...props.labels,
...defaultLabels(name, version, component, partOf),
},
};
}
4 changes: 4 additions & 0 deletions eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
...require('@linzjs/style/.eslintrc.cjs'),
};

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"format": "npx prettier . -w --ignore-path .gitignore --ignore-unknown"
},
"devDependencies": {
"@linzjs/style": "^4.2.0",
"cdk8s": "^2.66.9",
"cdk8s-cli": "^2.134.0",
"cdk8s-plus-27": "^2.7.33",
"prettier": "^2.7.1"
},
"dependencies": {
Expand Down
Loading

0 comments on commit 5d9e60f

Please sign in to comment.