From ada6b31f699062297325653cbafdba55ec71ebe5 Mon Sep 17 00:00:00 2001 From: LP B Date: Thu, 28 Sep 2023 15:53:52 +0200 Subject: [PATCH] fix(docker/container): container logs viewer error when logging is disabled (#10384) * fix(docker/container-logs): invalid string breadcrumb * fix(docker/container): let docker select the logging driver by default on container create * fix(docker/container-logs): information panel in container logs when logging is disabled * fix(docker/container): dont include HostConfig.LogConfig if no driver is selected --- app/docker/react/views/containers.ts | 8 ++++ .../logs/containerLogsController.js | 8 +++- .../views/containers/logs/containerlogs.html | 6 ++- .../CreateView/CommandsTab/LoggerConfig.tsx | 4 +- .../CreateView/CommandsTab/toRequest.ts | 5 ++ .../CreateView/CommandsTab/toViewModel.tsx | 2 +- app/react/docker/containers/LogView/.keep | 0 .../docker/containers/LogView/LogView.tsx | 46 +++++++++++++++++++ app/react/docker/containers/LogView/index.ts | 1 + 9 files changed, 74 insertions(+), 6 deletions(-) delete mode 100644 app/react/docker/containers/LogView/.keep create mode 100644 app/react/docker/containers/LogView/LogView.tsx create mode 100644 app/react/docker/containers/LogView/index.ts diff --git a/app/docker/react/views/containers.ts b/app/docker/react/views/containers.ts index b3aa3ef42..b3669c896 100644 --- a/app/docker/react/views/containers.ts +++ b/app/docker/react/views/containers.ts @@ -6,6 +6,7 @@ import { ListView } from '@/react/docker/containers/ListView'; import { withCurrentUser } from '@/react-tools/withCurrentUser'; import { withReactQuery } from '@/react-tools/withReactQuery'; import { withUIRouter } from '@/react-tools/withUIRouter'; +import { LogView } from '@/react/docker/containers/LogView'; export const containersModule = angular .module('portainer.docker.react.views.containers', []) @@ -13,6 +14,13 @@ export const containersModule = angular 'containersView', r2a(withUIRouter(withReactQuery(withCurrentUser(ListView))), ['endpoint']) ) + // the view only contains the information panel when logging is disabled + // this is a temporary solution to avoid creating a publicly exposed component + // or an AngularJS component until the logs view is migrated to React + .component( + 'containerLogView', + r2a(withUIRouter(withReactQuery(withCurrentUser(LogView))), []) + ) .config(config).name; diff --git a/app/docker/views/containers/logs/containerLogsController.js b/app/docker/views/containers/logs/containerLogsController.js index d6799a451..e63ea2233 100644 --- a/app/docker/views/containers/logs/containerLogsController.js +++ b/app/docker/views/containers/logs/containerLogsController.js @@ -75,7 +75,13 @@ angular.module('portainer.docker').controller('ContainerLogsController', [ .then(function success(data) { var container = data; $scope.container = container; - startLogPolling(!container.Config.Tty); + + const logsEnabled = container.HostConfig && container.HostConfig.LogConfig && container.HostConfig.LogConfig.Type && container.HostConfig.LogConfig.Type !== 'none'; + $scope.logsEnabled = logsEnabled; + + if (logsEnabled) { + startLogPolling(!container.Config.Tty); + } }) .catch(function error(err) { Notifications.error('Failure', err, 'Unable to retrieve container information'); diff --git a/app/docker/views/containers/logs/containerlogs.html b/app/docker/views/containers/logs/containerlogs.html index 5ade29fc5..3e824e2d9 100644 --- a/app/docker/views/containers/logs/containerlogs.html +++ b/app/docker/views/containers/logs/containerlogs.html @@ -5,14 +5,16 @@ { label:(container.Name | trimcontainername), link: 'docker.containers.container', - linkParams:container.Id + linkParams: { id: container.Id } }, 'Logs']" > + + Logging driver that will override the default docker daemon driver. Select Default logging driver if you don't want to override it. - Supported logging drivers can be found + Supported logging drivers can be found{' '} { value: string().required('Value is required'), }) ), - type: string().default('none'), + type: string().default(''), }); } diff --git a/app/react/docker/containers/CreateView/CommandsTab/toRequest.ts b/app/react/docker/containers/CreateView/CommandsTab/toRequest.ts index 3810b5843..589a0c440 100644 --- a/app/react/docker/containers/CreateView/CommandsTab/toRequest.ts +++ b/app/react/docker/containers/CreateView/CommandsTab/toRequest.ts @@ -34,6 +34,11 @@ export function toRequest( delete config.Entrypoint; } + // don't include LogConfig object if "Default logging driver" (type === '') is selected + if (values.logConfig.type === '') { + delete config.HostConfig.LogConfig; + } + return config; function getLogConfig( diff --git a/app/react/docker/containers/CreateView/CommandsTab/toViewModel.tsx b/app/react/docker/containers/CreateView/CommandsTab/toViewModel.tsx index 34474fa66..a67da7692 100644 --- a/app/react/docker/containers/CreateView/CommandsTab/toViewModel.tsx +++ b/app/react/docker/containers/CreateView/CommandsTab/toViewModel.tsx @@ -39,7 +39,7 @@ export function toViewModel(config: ContainerJSON): Values { function getLogConfig(value?: HostConfig['LogConfig']): LogConfig { if (!value || !value.Type) { return { - type: 'none', + type: '', options: [], }; } diff --git a/app/react/docker/containers/LogView/.keep b/app/react/docker/containers/LogView/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/react/docker/containers/LogView/LogView.tsx b/app/react/docker/containers/LogView/LogView.tsx new file mode 100644 index 000000000..cd9bd8c77 --- /dev/null +++ b/app/react/docker/containers/LogView/LogView.tsx @@ -0,0 +1,46 @@ +import { useCurrentStateAndParams } from '@uirouter/react'; + +import { useContainer } from '@/react/docker/containers/queries/container'; + +import { InformationPanel } from '@@/InformationPanel'; +import { TextTip } from '@@/Tip/TextTip'; +import { Link } from '@@/Link'; + +export function LogView() { + const { + params: { endpointId: environmentId, id: containerId }, + } = useCurrentStateAndParams(); + + const containerQuery = useContainer(environmentId, containerId); + if (!containerQuery.data || containerQuery.isLoading) { + return null; + } + + const logsEnabled = + containerQuery.data.HostConfig?.LogConfig?.Type && // if a portion of the object path doesn't exist, logging is likely disabled + containerQuery.data.HostConfig.LogConfig.Type !== 'none'; // if type === none logging is disabled + + return <>{!logsEnabled && }; +} + +function LogsDisabledInfoPanel() { + const { + params: { id: containerId, nodeName }, + } = useCurrentStateAndParams(); + + return ( + + + Logging is disabled for this container. If you want to re-enable + logging, please{' '} + + redeploy your container + {' '} + and select a logging driver in the "Command & logging" panel. + + + ); +} diff --git a/app/react/docker/containers/LogView/index.ts b/app/react/docker/containers/LogView/index.ts new file mode 100644 index 000000000..3f807ded7 --- /dev/null +++ b/app/react/docker/containers/LogView/index.ts @@ -0,0 +1 @@ +export { LogView } from './LogView';