From 792d33ac3828d81fd447e935db0fa7af6da74482 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Tue, 9 Jan 2024 15:21:39 +0200 Subject: [PATCH] state-get-v3: Unify the user,SV&hostApp app state generation helpers Change-type: patch --- .../device-state/routes/state-get-v3.ts | 72 +++++++++---------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/src/features/device-state/routes/state-get-v3.ts b/src/features/device-state/routes/state-get-v3.ts index 1b30e671b..da76d6436 100644 --- a/src/features/device-state/routes/state-get-v3.ts +++ b/src/features/device-state/routes/state-get-v3.ts @@ -306,17 +306,24 @@ const getStateV3 = async (req: Request, uuid: string): Promise => { storedDeviceFields: _.pick(device, getStateEventAdditionalFields), }); + // We use an empty config for the supervisor & hostApp as we don't want any labels applied to them due to user app config + const svAndHostAppConfig = {}; const apps = { - ...getSystemAppState(device, 'should_be_managed_by__release'), - ...getSystemAppState(device, 'should_be_operated_by__release', { - // This label is necessary for older supervisors to properly detect the hostApp - // and ignore it, sinc `is_host: true` wasn't enough. W/o this the device would - // try to install the hostApp container like a normal user app and restart it - // constantly b/c the image doesn't have a CMD specified. - // See: https://github.com/balena-os/balena-supervisor/blob/v15.2.0/src/compose/app.ts#L839 - 'io.balena.image.store': 'root', - }), - ...getUserAppState(device, config), + ...getAppState(device, 'should_be_managed_by__release', svAndHostAppConfig), + ...getAppState( + device, + 'should_be_operated_by__release', + svAndHostAppConfig, + { + // This label is necessary for older supervisors to properly detect the hostApp + // and ignore it, sinc `is_host: true` wasn't enough. W/o this the device would + // try to install the hostApp container like a normal user app and restart it + // constantly b/c the image doesn't have a CMD specified. + // See: https://github.com/balena-os/balena-supervisor/blob/v15.2.0/src/compose/app.ts#L839 + 'io.balena.image.store': 'root', + }, + ), + ...getAppState(device, 'should_be_running__release', config), }; const state: StateV3 = { @@ -360,11 +367,26 @@ const getDevice = getStateDelayingEmpty( const getAppState = ( device: AnyObject, - application: AnyObject, - release: AnyObject | undefined, + targetReleaseField: + | 'should_be_running__release' + | 'should_be_managed_by__release' + | 'should_be_operated_by__release', config: Dictionary, defaultLabels?: Dictionary, -): StateV3[string]['apps'] => { +): StateV3[string]['apps'] | null => { + let application: AnyObject; + let release: AnyObject | undefined; + if (targetReleaseField === 'should_be_running__release') { + application = device.belongs_to__application[0]; + release = getReleaseForDevice(device); + } else { + release = device[targetReleaseField][0]; + if (!release) { + return null; + } + application = release.belongs_to__application[0]; + } + return { [application.uuid]: { id: application.id, @@ -383,27 +405,3 @@ const getAppState = ( }, }; }; - -const getUserAppState = ( - device: AnyObject, - config: Dictionary, -): StateV3[string]['apps'] => { - const userApp = device.belongs_to__application[0]; - const userAppRelease = getReleaseForDevice(device); - return getAppState(device, userApp, userAppRelease, config); -}; -const getSystemAppState = ( - device: AnyObject, - targetReleaseField: - | 'should_be_managed_by__release' - | 'should_be_operated_by__release', - defaultLabels?: Dictionary, -): StateV3[string]['apps'] | null => { - const release = device[targetReleaseField][0]; - if (!release) { - return null; - } - const app = release.belongs_to__application[0]; - // We use an empty config as we don't want any labels applied to the supervisor/hostApp due to user app config - return getAppState(device, app, release, {}, defaultLabels); -};