refactor(docker/containers): migrate env vars to react [EE-5211] (#10345)

pull/10284/head
Chaim Lev-Ari 2023-09-21 04:11:18 +03:00 committed by GitHub
parent 54112b56f2
commit 16ccf5871e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 103 additions and 24 deletions

View File

@ -12,6 +12,11 @@ import {
import { r2a } from '@/react-tools/react2angular'; import { r2a } from '@/react-tools/react2angular';
import { withCurrentUser } from '@/react-tools/withCurrentUser'; import { withCurrentUser } from '@/react-tools/withCurrentUser';
import { ContainerNetworksDatatable } from '@/react/docker/containers/ItemView/ContainerNetworksDatatable'; import { ContainerNetworksDatatable } from '@/react/docker/containers/ItemView/ContainerNetworksDatatable';
import {
EnvVarsTab,
Values as EnvVarsTabValues,
envVarsTabUtils,
} from '@/react/docker/containers/CreateView/EnvVarsTab';
const ngModule = angular const ngModule = angular
.module('portainer.docker.react.components.containers', []) .module('portainer.docker.react.components.containers', [])
@ -33,3 +38,11 @@ withFormValidation<ComponentProps<typeof CommandsTab>, CommandsTabValues>(
['apiVersion'], ['apiVersion'],
commandsTabValidation commandsTabValidation
); );
withFormValidation<ComponentProps<typeof EnvVarsTab>, EnvVarsTabValues>(
ngModule,
withUIRouter(withReactQuery(EnvVarsTab)),
'dockerCreateContainerEnvVarsTab',
[],
envVarsTabUtils.validation
);

View File

@ -1,6 +1,5 @@
import _ from 'lodash-es'; import _ from 'lodash-es';
import * as envVarsUtils from '@/react/components/form-components/EnvironmentVariablesFieldset/utils';
import { PorImageRegistryModel } from 'Docker/models/porImageRegistry'; import { PorImageRegistryModel } from 'Docker/models/porImageRegistry';
import { confirmDestructive } from '@@/modals/confirm'; import { confirmDestructive } from '@@/modals/confirm';
@ -13,6 +12,7 @@ import { AccessControlFormData } from '@/portainer/components/accessControlForm/
import { ContainerDetailsViewModel } from '@/docker/models/container'; import { ContainerDetailsViewModel } from '@/docker/models/container';
import './createcontainer.css'; import './createcontainer.css';
import { envVarsTabUtils } from '@/react/docker/containers/CreateView/EnvVarsTab';
angular.module('portainer.docker').controller('CreateContainerController', [ angular.module('portainer.docker').controller('CreateContainerController', [
'$q', '$q',
@ -89,12 +89,12 @@ angular.module('portainer.docker').controller('CreateContainerController', [
MemoryLimit: 0, MemoryLimit: 0,
MemoryReservation: 0, MemoryReservation: 0,
ShmSize: 64, ShmSize: 64,
Env: [],
NodeName: null, NodeName: null,
capabilities: [], capabilities: [],
Sysctls: [], Sysctls: [],
RegistryModel: new PorImageRegistryModel(), RegistryModel: new PorImageRegistryModel(),
commands: commandsTabUtils.getDefaultViewModel(), commands: commandsTabUtils.getDefaultViewModel(),
envVars: envVarsTabUtils.getDefaultViewModel(),
}; };
$scope.extraNetworks = {}; $scope.extraNetworks = {};
@ -114,6 +114,7 @@ angular.module('portainer.docker').controller('CreateContainerController', [
$scope.handlePrivilegedChange = handlePrivilegedChange; $scope.handlePrivilegedChange = handlePrivilegedChange;
$scope.handleInitChange = handleInitChange; $scope.handleInitChange = handleInitChange;
$scope.handleCommandsChange = handleCommandsChange; $scope.handleCommandsChange = handleCommandsChange;
$scope.handleEnvVarsChange = handleEnvVarsChange;
function handleCommandsChange(commands) { function handleCommandsChange(commands) {
return $scope.$evalAsync(() => { return $scope.$evalAsync(() => {
@ -121,6 +122,12 @@ angular.module('portainer.docker').controller('CreateContainerController', [
}); });
} }
function handleEnvVarsChange(value) {
return $scope.$evalAsync(() => {
$scope.formValues.envVars = value;
});
}
function onAlwaysPullChange(checked) { function onAlwaysPullChange(checked) {
return $scope.$evalAsync(() => { return $scope.$evalAsync(() => {
$scope.formValues.alwaysPull = checked; $scope.formValues.alwaysPull = checked;
@ -151,11 +158,6 @@ angular.module('portainer.docker').controller('CreateContainerController', [
}); });
} }
$scope.handleEnvVarChange = handleEnvVarChange;
function handleEnvVarChange(value) {
$scope.formValues.Env = value;
}
$scope.refreshSlider = function () { $scope.refreshSlider = function () {
$timeout(function () { $timeout(function () {
$scope.$broadcast('rzSliderForceRender'); $scope.$broadcast('rzSliderForceRender');
@ -281,10 +283,6 @@ angular.module('portainer.docker').controller('CreateContainerController', [
config.HostConfig.PortBindings = bindings; config.HostConfig.PortBindings = bindings;
} }
function prepareEnvironmentVariables(config) {
config.Env = envVarsUtils.convertToArrayOfStrings($scope.formValues.Env);
}
function prepareVolumes(config) { function prepareVolumes(config) {
var binds = []; var binds = [];
var volumes = {}; var volumes = {};
@ -462,11 +460,11 @@ angular.module('portainer.docker').controller('CreateContainerController', [
function prepareConfiguration() { function prepareConfiguration() {
var config = angular.copy($scope.config); var config = angular.copy($scope.config);
config = commandsTabUtils.toRequest(config, $scope.formValues.commands); config = commandsTabUtils.toRequest(config, $scope.formValues.commands);
config = envVarsTabUtils.toRequest(config, $scope.formValues.envVars);
prepareNetworkConfig(config); prepareNetworkConfig(config);
prepareImageConfig(config); prepareImageConfig(config);
preparePortBindings(config); preparePortBindings(config);
prepareEnvironmentVariables(config);
prepareVolumes(config); prepareVolumes(config);
prepareLabels(config); prepareLabels(config);
prepareDevices(config); prepareDevices(config);
@ -561,10 +559,6 @@ angular.module('portainer.docker').controller('CreateContainerController', [
} }
} }
function loadFromContainerEnvironmentVariables() {
$scope.formValues.Env = envVarsUtils.parseArrayOfStrings($scope.config.Env);
}
function loadFromContainerLabels() { function loadFromContainerLabels() {
for (var l in $scope.config.Labels) { for (var l in $scope.config.Labels) {
if ({}.hasOwnProperty.call($scope.config.Labels, l)) { if ({}.hasOwnProperty.call($scope.config.Labels, l)) {
@ -687,11 +681,12 @@ angular.module('portainer.docker').controller('CreateContainerController', [
$scope.config = ContainerHelper.configFromContainer(fromContainer.Model); $scope.config = ContainerHelper.configFromContainer(fromContainer.Model);
$scope.formValues.commands = commandsTabUtils.toViewModel(d); $scope.formValues.commands = commandsTabUtils.toViewModel(d);
$scope.formValues.envVars = envVarsTabUtils.toViewModel(d);
loadFromContainerPortBindings(d); loadFromContainerPortBindings(d);
loadFromContainerVolumes(d); loadFromContainerVolumes(d);
loadFromContainerNetworkConfig(d); loadFromContainerNetworkConfig(d);
loadFromContainerEnvironmentVariables(d);
loadFromContainerLabels(d); loadFromContainerLabels(d);
loadFromContainerDevices(d); loadFromContainerDevices(d);
loadFromContainerDeviceRequests(d); loadFromContainerDeviceRequests(d);

View File

@ -434,13 +434,11 @@
<!-- !tab-labels --> <!-- !tab-labels -->
<!-- tab-env --> <!-- tab-env -->
<div class="tab-pane" id="env"> <div class="tab-pane" id="env">
<div class="form-horizontal"> <docker-create-container-env-vars-tab
<environment-variables-panel ng-if="state.containerIsLoaded"
values="formValues.Env" values="formValues.envVars"
explanation="'These values will be applied to the container when deployed'" on-change="(handleEnvVarsChange)"
on-change="(handleEnvVarChange)" ></docker-create-container-env-vars-tab>
></environment-variables-panel>
</div>
</div> </div>
<!-- !tab-env --> <!-- !tab-env -->
<!-- tab-restart-policy --> <!-- tab-restart-policy -->

View File

@ -0,0 +1,32 @@
import { useState } from 'react';
import { EnvironmentVariablesPanel } from '@@/form-components/EnvironmentVariablesFieldset';
import { ArrayError } from '@@/form-components/InputList/InputList';
import { Values } from './types';
export function EnvVarsTab({
values: initialValues,
onChange,
errors,
}: {
values: Values;
onChange(value: Values): void;
errors?: ArrayError<Values>;
}) {
const [values, setControlledValues] = useState(initialValues);
return (
<EnvironmentVariablesPanel
values={values}
explanation="These values will be applied to the container when deployed"
onChange={handleChange}
errors={errors}
/>
);
function handleChange(values: Values) {
setControlledValues(values);
onChange(values);
}
}

View File

@ -0,0 +1,14 @@
import { envVarValidation } from '@@/form-components/EnvironmentVariablesFieldset';
import { toRequest } from './toRequest';
import { toViewModel, getDefaultViewModel } from './toViewModel';
export { EnvVarsTab } from './EnvVarsTab';
export type { Values } from './types';
export const envVarsTabUtils = {
toRequest,
toViewModel,
validation: envVarValidation,
getDefaultViewModel,
};

View File

@ -0,0 +1,15 @@
import { convertToArrayOfStrings } from '@@/form-components/EnvironmentVariablesFieldset/utils';
import { CreateContainerRequest } from '../types';
import { Values } from './types';
export function toRequest(
oldConfig: CreateContainerRequest,
values: Values
): CreateContainerRequest {
return {
...oldConfig,
Env: convertToArrayOfStrings(values),
};
}

View File

@ -0,0 +1,11 @@
import { parseArrayOfStrings } from '@@/form-components/EnvironmentVariablesFieldset/utils';
import { ContainerJSON } from '../../queries/container';
export function getDefaultViewModel() {
return [];
}
export function toViewModel(container: ContainerJSON) {
return parseArrayOfStrings(container.Config?.Env);
}

View File

@ -0,0 +1 @@
export type { Value as Values } from '@@/form-components/EnvironmentVariablesFieldset/types';