Skip to content

improve settings loading from config. #650

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

Merged
merged 3 commits into from
Jun 8, 2025
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
10 changes: 10 additions & 0 deletions server/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ KUBERO_AUDIT_LIMIT=1000

#KUBERO_SETUP=enabled

#----- NEW VARIABLES V3 ---------
#KUBERO_READONLY=false
#KUBERO_CONSOLE_ENABLED=true
#KUBERO_ADMIN_DISABLED=true
#KUBERO_BANNER_SHOW=false
#KUBERO_BANNER_MESSAGE=Welcome to Kubero!
#KUBERO_BANNER_BGCOLOR='#8560a963'
#KUBERO_BANNER_FONTCOLOR='#00000087'
#KUBERO_TEMPLATES_ENABLED=true

##########################################
# git repository configuration
#
Expand Down
2 changes: 2 additions & 0 deletions server/src/apps/apps.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ describe('AppsController', () => {
};

beforeEach(async () => {
process.env.KUBERO_CONSOLE_ENABLED = 'true';

const module: TestingModule = await Test.createTestingModule({
controllers: [AppsController],
providers: [
Expand Down
20 changes: 20 additions & 0 deletions server/src/apps/apps.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ export class AppsController {
@Param('app') app: string,
@Body() body: any,
) {
if (process.env.KUBERO_CONSOLE_ENABLED !== 'true') {
const msg = 'Console is not enabled';
Logger.warn(msg);
throw new HttpException(msg, HttpStatus.BAD_REQUEST);
}
if (!body.podName || !body.containerName || !body.command) {
const msg = 'Missing required fields: podName, containerName, command';
Logger.error(msg);
throw new HttpException(msg, HttpStatus.BAD_REQUEST);
}
if (!Array.isArray(body.command)) {
const msg = 'Command must be an array';
Logger.error(msg);
throw new HttpException(msg, HttpStatus.BAD_REQUEST);
}
if (body.command.length === 0) {
const msg = 'Command array cannot be empty';
Logger.error(msg);
throw new HttpException(msg, HttpStatus.BAD_REQUEST);
}
const user: IUser = {
id: 1,
method: 'local',
Expand Down
55 changes: 48 additions & 7 deletions server/src/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class ConfigService {

this.kubectl.updateKuberoConfig(namespace, kuberoes);
this.kubectl.updateKuberoSecret(namespace, config.secrets);
this.setEnv(config.secrets);
this.setSecretEnv(config.secrets);

const m = {
name: 'updateSettings',
Expand All @@ -129,7 +129,7 @@ export class ConfigService {
return kuberoes;
}

private setEnv(secrets: any) {
private setSecretEnv(secrets: any) {
/*
for (const key in secrets) {
process.env[key] = secrets[key]
Expand All @@ -153,11 +153,32 @@ export class ConfigService {
process.env.OAUTH2_CLIENT_SECRET = secrets.OAUTH2_CLIENT_SECRET;
}

private setEnvVar(key: string, value: string): void {
if (process.env[key] == undefined || process.env[key] == '') {
// Only set the environment variable if it is not already set or empty
process.env[key] = value;
//this.logger.warn(`DEPRECATED v3.x.0: Environment variable ${key} set to ${value}. Use configmap instead.`);
}
}

private loadDeprecatedVarsToEnv(config: IKuberoConfig): void {
// Update environment variables based on the config
this.setEnvVar('KUBERO_READONLY', config.kubero?.readonly ? 'true' : 'false');
this.setEnvVar('KUBERO_CONSOLE_ENABLED', config.kubero?.console?.enabled ? 'true' : 'false');
this.setEnvVar('KUBERO_ADMIN_DISABLED', config.kubero?.admin?.disabled ? 'true' : 'false');
this.setEnvVar('KUBERO_BANNER_SHOW', config.kubero?.banner?.show ? 'true' : 'false');
this.setEnvVar('KUBERO_BANNER_MESSAGE', config.kubero?.banner?.message || 'Welcome to Kubero!');
this.setEnvVar('KUBERO_BANNER_BGCOLOR', config.kubero?.banner?.bgcolor || '#8560a963');
this.setEnvVar('KUBERO_BANNER_FONTCOLOR', config.kubero?.banner?.fontcolor || '#00000087');
this.setEnvVar('KUBERO_TEMPLATES_ENABLED', config.templates?.enabled ? 'true' : 'false');
}

private reloadRunningConfig(): void {
this.readConfig()
.then((config) => {
this.logger.debug('Kubero config loaded');
this.runningConfig = config;
this.loadDeprecatedVarsToEnv(config);
})
.catch((error) => {
this.logger.error('Error reading kuberoes config');
Expand All @@ -168,8 +189,10 @@ export class ConfigService {
private async readConfig(): Promise<IKuberoConfig> {
if (process.env.NODE_ENV === 'production') {
const kuberoCRD = await this.readConfigFromKubernetes();
this.logger.debug('Kubero config loaded from Kubernetes');
return kuberoCRD.kubero.config;
} else {
this.logger.debug('Kubero config loaded from filesystem (dev mode)');
return this.readConfigFromFS();
}
}
Expand Down Expand Up @@ -244,7 +267,11 @@ export class ConfigService {
}

public checkAdminDisabled(): boolean {
return this.runningConfig.kubero.admin?.disabled || false;
if (process.env.KUBERO_ADMIN_DISABLED === 'true') {
this.logger.warn('Admin is disabled');
return true;
}
return false;
}

public async validateKubeconfig(
Expand Down Expand Up @@ -339,18 +366,32 @@ export class ConfigService {
}

getTemplateEnabled() {
return this.runningConfig.templates?.enabled || false;
if (process.env.KUBERO_TEMPLATES_ENABLED == undefined) {
return false;
}
if (process.env.KUBERO_TEMPLATES_ENABLED == 'true') {
return true;
}
return false;
}

public async getTemplateConfig() {
return this.runningConfig.templates;
}

getConsoleEnabled() {
if (this.runningConfig.kubero?.console?.enabled == undefined) {
getConsoleEnabled(): boolean {
if (process.env.KUBERO_CONSOLE_ENABLED == undefined) {
this.logger.warn(
'KUBERO_CONSOLE_ENABLED is not set, defaulting to false',
);
return false;
}
return this.runningConfig.kubero?.console?.enabled;
if (process.env.KUBERO_CONSOLE_ENABLED == 'true') {
this.logger.debug('KUBERO_CONSOLE_ENABLED is set to true');
return true;
}
this.logger.debug('KUBERO_CONSOLE_ENABLED is set to false');
return false;
}

setMetricsStatus(status: boolean) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/config/kubero-config/kubero-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export class KuberoConfig {
this.templates = kc.templates;
this.kubero = kc.kubero;

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

for (let i = 0; i < this.podSizeList.length; i++) {
for (let i = 0; i < this.podSizeList?.length; i++) {
this.podSizeList[i] = new PodSize(kc.podSizeList[i]);
}
}
Expand Down
Loading