Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): set default namespace at the app level #58

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/my-k8s-app/ConfigMap.my-config-map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
kind: ConfigMap
metadata:
namespace: fooo
name: my-config-map
1 change: 1 addition & 0 deletions examples/my-k8s-app/Deployment.my-nginx-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx-deployment
namespace: my-app-namespace
spec:
selector:
matchLabels:
Expand Down
14 changes: 13 additions & 1 deletion examples/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Helm, K8sApp } from '@k8skonf/core';
import { ConfigMapv1 } from '@k8skonf/core/ConfigMapv1';
import { Deploymentv1 } from '@k8skonf/core/Deploymentv1';
import { log } from 'node:console';

const app = new K8sApp('my-k8s-app', 'filePerResource');
const app = new K8sApp('my-k8s-app', { namespace: 'my-app-namespace' });

const chart = new Helm(app, 'ingress-nginx', {
chart: 'ingress-nginx',
Expand Down Expand Up @@ -30,7 +31,18 @@ chart.resources.forEach((resource) => {
console.log(resource.apiVersion);
});

new ConfigMapv1(app, 'my-config-map', {
metadata: {
// name: 'my-config-map2', // inferred from resource name
namespace: 'fooo',
},
});

const d = new Deploymentv1(app, 'my-nginx-deployment', {
metadata: {
// name: 'my-nginx-deployment2', // inferred from resource name
// namespace: 'fooo', // inferred from app namespace
},
spec: {
selector: {
matchLabels: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@types/node": "22.10.2",
"typescript": "5.7.2"
},
"packageManager": "[email protected].0",
"packageManager": "[email protected].1",
"engines": {
"node": ">=20.18.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/generateCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function morph() {
interfaceProps.push(interfaceProp);
}
sourceFile.insertInterface(classDeclaration.getChildIndex(), i);
c.statements.push('app.resources.push(this);');
c.statements.push('app.addResource(this);');
classDeclaration.addConstructor(c);
} else {
if (className === 'ObjectMetav1') {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Helm {
}
if (file.toJS() !== null) {
this.resources.push(file.toJS());
app.resources.push(file.toJS());
app.addResource(file.toJS());
}
}

Expand Down
33 changes: 24 additions & 9 deletions packages/core/src/K8sApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,48 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import pc from 'picocolors';
import * as yaml from 'yaml';
import { ApiObject } from './ApiObject.js';
import { ApiObject, NamespacedApiObject } from './ApiObject.js';

interface K8sAppArgs {
readonly namespace?: string;
/**
* @default 'filePerResource'
*/
readonly outputType?: 'singleFile' | 'filePerResource';
}

/**
* Each Kubernetes Api Object must belong to an instance of this class.
* So we know where to save the resources.
*/
export class K8sApp {
readonly resources: ApiObject[] = [];
private readonly resources: ApiObject[] | NamespacedApiObject[] = [];
readonly namespace?: string;

constructor(
readonly outputName: string,
/**
* @default 'filePerResource'
*/
readonly outputType?: 'singleFile' | 'filePerResource',
) {}
private readonly args?: K8sAppArgs,
) {
this.namespace = args?.namespace;
}

addResource(resource: ApiObject) {
addResource(resource: ApiObject | NamespacedApiObject) {
if (resource instanceof NamespacedApiObject && !resource.metadata.namespace) {
resource.metadata.namespace = this.namespace;
}
this.resources.push(resource);
}

getResources() {
return this.resources;
}

toYaml() {
return `---\n${this.resources.map((resource) => yaml.stringify(resource, { schema: 'yaml-1.1' })).join('---\n')}`;
}

save() {
if (this.outputType === 'singleFile') {
if (this.args?.outputType === 'singleFile') {
console.log(pc.blueBright(`Saving to ${this.outputName}.yaml`));
fs.writeFileSync(`${this.outputName}.yaml`, this.toYaml());
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/models/APIServicev1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class APIServicev1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/AuthenticationTokenRequestv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class AuthenticationTokenRequestv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/Bindingv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class Bindingv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.target = args.target;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/CSIDriverv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class CSIDriverv1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/CSINodev1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class CSINodev1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/CSIStorageCapacityv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ export class CSIStorageCapacityv1 extends NamespacedApiObject {
this.metadata.name ??= name;
this.nodeTopology = args.nodeTopology;
this.storageClassName = args.storageClassName;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/CertificateSigningRequestv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export class CertificateSigningRequestv1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/ClusterRoleBindingv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ export class ClusterRoleBindingv1 extends ApiObject {
this.metadata.name ??= name;
this.roleRef = args.roleRef;
this.subjects = args.subjects;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/ClusterRolev1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ export class ClusterRolev1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.rules = args.rules;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/ClusterTrustBundlev1alpha1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class ClusterTrustBundlev1alpha1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/ConfigMapv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ export class ConfigMapv1 extends NamespacedApiObject {
this.immutable = args.immutable;
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/ControllerRevisionv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export class ControllerRevisionv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.revision = args.revision;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/CoreEventv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ export class CoreEventv1 extends NamespacedApiObject {
this.series = args.series;
this.source = args.source;
this.type = args.type;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/CronJobv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class CronJobv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/CustomResourceDefinitionv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class CustomResourceDefinitionv1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/DaemonSetv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class DaemonSetv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/Deploymentv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class Deploymentv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/DeviceClassv1alpha3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class DeviceClassv1alpha3 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/DeviceClassv1beta1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class DeviceClassv1beta1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/EndpointSlicev1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ export class EndpointSlicev1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.ports = args.ports;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/Endpointsv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class Endpointsv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.subsets = args.subsets;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/EventsEventv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ export class EventsEventv1 extends NamespacedApiObject {
this.reportingInstance = args.reportingInstance;
this.series = args.series;
this.type = args.type;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/Evictionv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class Evictionv1 extends NamespacedApiObject {
this.deleteOptions = args.deleteOptions;
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/FlowSchemav1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class FlowSchemav1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/HorizontalPodAutoscalerv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class HorizontalPodAutoscalerv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/HorizontalPodAutoscalerv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class HorizontalPodAutoscalerv2 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/IPAddressv1beta1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class IPAddressv1beta1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/IngressClassv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export class IngressClassv1 extends ApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/Ingressv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class Ingressv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/Jobv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class Jobv1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/LeaseCandidatev1alpha2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class LeaseCandidatev1alpha2 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/Leasev1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class Leasev1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/models/LimitRangev1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class LimitRangev1 extends NamespacedApiObject {
this.metadata = args.metadata || { name };
this.metadata.name ??= name;
this.spec = args.spec;
app.resources.push(this);
app.addResource(this);
}
}
Loading