Skip to content

Commit a40ee74

Browse files
authored
Merge pull request #650 from kubero-dev/refactoring/configuration
improve settings loading from config.
2 parents ceb8cdf + e2c5dee commit a40ee74

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

server/.env.template

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ KUBERO_AUDIT_LIMIT=1000
3535

3636
#KUBERO_SETUP=enabled
3737

38+
#----- NEW VARIABLES V3 ---------
39+
#KUBERO_READONLY=false
40+
#KUBERO_CONSOLE_ENABLED=true
41+
#KUBERO_ADMIN_DISABLED=true
42+
#KUBERO_BANNER_SHOW=false
43+
#KUBERO_BANNER_MESSAGE=Welcome to Kubero!
44+
#KUBERO_BANNER_BGCOLOR='#8560a963'
45+
#KUBERO_BANNER_FONTCOLOR='#00000087'
46+
#KUBERO_TEMPLATES_ENABLED=true
47+
3848
##########################################
3949
# git repository configuration
4050
#

server/src/apps/apps.controller.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ describe('AppsController', () => {
128128
};
129129

130130
beforeEach(async () => {
131+
process.env.KUBERO_CONSOLE_ENABLED = 'true';
132+
131133
const module: TestingModule = await Test.createTestingModule({
132134
controllers: [AppsController],
133135
providers: [

server/src/apps/apps.controller.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,26 @@ export class AppsController {
246246
@Param('app') app: string,
247247
@Body() body: any,
248248
) {
249+
if (process.env.KUBERO_CONSOLE_ENABLED !== 'true') {
250+
const msg = 'Console is not enabled';
251+
Logger.warn(msg);
252+
throw new HttpException(msg, HttpStatus.BAD_REQUEST);
253+
}
254+
if (!body.podName || !body.containerName || !body.command) {
255+
const msg = 'Missing required fields: podName, containerName, command';
256+
Logger.error(msg);
257+
throw new HttpException(msg, HttpStatus.BAD_REQUEST);
258+
}
259+
if (!Array.isArray(body.command)) {
260+
const msg = 'Command must be an array';
261+
Logger.error(msg);
262+
throw new HttpException(msg, HttpStatus.BAD_REQUEST);
263+
}
264+
if (body.command.length === 0) {
265+
const msg = 'Command array cannot be empty';
266+
Logger.error(msg);
267+
throw new HttpException(msg, HttpStatus.BAD_REQUEST);
268+
}
249269
const user: IUser = {
250270
id: 1,
251271
method: 'local',

server/src/config/config.service.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class ConfigService {
110110

111111
this.kubectl.updateKuberoConfig(namespace, kuberoes);
112112
this.kubectl.updateKuberoSecret(namespace, config.secrets);
113-
this.setEnv(config.secrets);
113+
this.setSecretEnv(config.secrets);
114114

115115
const m = {
116116
name: 'updateSettings',
@@ -129,7 +129,7 @@ export class ConfigService {
129129
return kuberoes;
130130
}
131131

132-
private setEnv(secrets: any) {
132+
private setSecretEnv(secrets: any) {
133133
/*
134134
for (const key in secrets) {
135135
process.env[key] = secrets[key]
@@ -153,11 +153,32 @@ export class ConfigService {
153153
process.env.OAUTH2_CLIENT_SECRET = secrets.OAUTH2_CLIENT_SECRET;
154154
}
155155

156+
private setEnvVar(key: string, value: string): void {
157+
if (process.env[key] == undefined || process.env[key] == '') {
158+
// Only set the environment variable if it is not already set or empty
159+
process.env[key] = value;
160+
//this.logger.warn(`DEPRECATED v3.x.0: Environment variable ${key} set to ${value}. Use configmap instead.`);
161+
}
162+
}
163+
164+
private loadDeprecatedVarsToEnv(config: IKuberoConfig): void {
165+
// Update environment variables based on the config
166+
this.setEnvVar('KUBERO_READONLY', config.kubero?.readonly ? 'true' : 'false');
167+
this.setEnvVar('KUBERO_CONSOLE_ENABLED', config.kubero?.console?.enabled ? 'true' : 'false');
168+
this.setEnvVar('KUBERO_ADMIN_DISABLED', config.kubero?.admin?.disabled ? 'true' : 'false');
169+
this.setEnvVar('KUBERO_BANNER_SHOW', config.kubero?.banner?.show ? 'true' : 'false');
170+
this.setEnvVar('KUBERO_BANNER_MESSAGE', config.kubero?.banner?.message || 'Welcome to Kubero!');
171+
this.setEnvVar('KUBERO_BANNER_BGCOLOR', config.kubero?.banner?.bgcolor || '#8560a963');
172+
this.setEnvVar('KUBERO_BANNER_FONTCOLOR', config.kubero?.banner?.fontcolor || '#00000087');
173+
this.setEnvVar('KUBERO_TEMPLATES_ENABLED', config.templates?.enabled ? 'true' : 'false');
174+
}
175+
156176
private reloadRunningConfig(): void {
157177
this.readConfig()
158178
.then((config) => {
159179
this.logger.debug('Kubero config loaded');
160180
this.runningConfig = config;
181+
this.loadDeprecatedVarsToEnv(config);
161182
})
162183
.catch((error) => {
163184
this.logger.error('Error reading kuberoes config');
@@ -168,8 +189,10 @@ export class ConfigService {
168189
private async readConfig(): Promise<IKuberoConfig> {
169190
if (process.env.NODE_ENV === 'production') {
170191
const kuberoCRD = await this.readConfigFromKubernetes();
192+
this.logger.debug('Kubero config loaded from Kubernetes');
171193
return kuberoCRD.kubero.config;
172194
} else {
195+
this.logger.debug('Kubero config loaded from filesystem (dev mode)');
173196
return this.readConfigFromFS();
174197
}
175198
}
@@ -244,7 +267,11 @@ export class ConfigService {
244267
}
245268

246269
public checkAdminDisabled(): boolean {
247-
return this.runningConfig.kubero.admin?.disabled || false;
270+
if (process.env.KUBERO_ADMIN_DISABLED === 'true') {
271+
this.logger.warn('Admin is disabled');
272+
return true;
273+
}
274+
return false;
248275
}
249276

250277
public async validateKubeconfig(
@@ -339,18 +366,32 @@ export class ConfigService {
339366
}
340367

341368
getTemplateEnabled() {
342-
return this.runningConfig.templates?.enabled || false;
369+
if (process.env.KUBERO_TEMPLATES_ENABLED == undefined) {
370+
return false;
371+
}
372+
if (process.env.KUBERO_TEMPLATES_ENABLED == 'true') {
373+
return true;
374+
}
375+
return false;
343376
}
344377

345378
public async getTemplateConfig() {
346379
return this.runningConfig.templates;
347380
}
348381

349-
getConsoleEnabled() {
350-
if (this.runningConfig.kubero?.console?.enabled == undefined) {
382+
getConsoleEnabled(): boolean {
383+
if (process.env.KUBERO_CONSOLE_ENABLED == undefined) {
384+
this.logger.warn(
385+
'KUBERO_CONSOLE_ENABLED is not set, defaulting to false',
386+
);
351387
return false;
352388
}
353-
return this.runningConfig.kubero?.console?.enabled;
389+
if (process.env.KUBERO_CONSOLE_ENABLED == 'true') {
390+
this.logger.debug('KUBERO_CONSOLE_ENABLED is set to true');
391+
return true;
392+
}
393+
this.logger.debug('KUBERO_CONSOLE_ENABLED is set to false');
394+
return false;
354395
}
355396

356397
setMetricsStatus(status: boolean) {

server/src/config/kubero-config/kubero-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ export class KuberoConfig {
3838
this.templates = kc.templates;
3939
this.kubero = kc.kubero;
4040

41-
for (let i = 0; i < this.buildpacks.length; i++) {
41+
for (let i = 0; i < this.buildpacks?.length; i++) {
4242
this.buildpacks[i] = new Buildpack(kc.buildpacks[i]);
4343
}
4444

45-
for (let i = 0; i < this.podSizeList.length; i++) {
45+
for (let i = 0; i < this.podSizeList?.length; i++) {
4646
this.podSizeList[i] = new PodSize(kc.podSizeList[i]);
4747
}
4848
}

0 commit comments

Comments
 (0)