1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00

feat(edge/templates): introduce edge specific settings [EE-6276] (#10609)

This commit is contained in:
Chaim Lev-Ari 2023-11-15 14:43:18 +02:00 committed by GitHub
parent 68950fbb24
commit e43d076269
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 885 additions and 319 deletions

View file

@ -0,0 +1,47 @@
import axios, {
json2formData,
parseAxiosError,
} from '@/portainer/services/axios';
import { RegistryId } from '@/react/portainer/registries/types';
import { Pair } from '@/react/portainer/settings/types';
import { EdgeGroup } from '@/react/edge/edge-groups/types';
import { DeploymentType, EdgeStack, StaggerConfig } from '../../types';
import { buildUrl } from '../buildUrl';
/**
* Payload to create an EdgeStack from a git repository
*/
export type FileUploadPayload = {
Name: string;
file: File;
EdgeGroups: Array<EdgeGroup['Id']>;
DeploymentType: DeploymentType;
Registries?: Array<RegistryId>;
/** * Uses the manifest's namespaces instead of the default one */
UseManifestNamespaces?: boolean;
PrePullImage?: boolean;
RetryDeploy?: boolean;
/** List of environment variables */
EnvVars?: Array<Pair>;
/** Configuration for stagger updates */
StaggerConfig?: StaggerConfig;
Webhook?: string;
};
export async function createStackFromFile(payload: FileUploadPayload) {
try {
const { data } = await axios.post<EdgeStack>(
buildUrl(undefined, 'create/file'),
json2formData(payload),
{
headers: {
'Content-Type': 'multipart/form-data',
},
}
);
return data;
} catch (e) {
throw parseAxiosError(e as Error);
}
}

View file

@ -0,0 +1,47 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { RegistryId } from '@/react/portainer/registries/types';
import { Pair } from '@/react/portainer/settings/types';
import { EdgeGroup } from '@/react/edge/edge-groups/types';
import { DeploymentType, EdgeStack, StaggerConfig } from '../../types';
import { buildUrl } from '../buildUrl';
/**
* Payload for creating an EdgeStack from a string
*/
export interface FileContentPayload {
/** Name of the stack */
name: string;
/** Content of the Stack file */
stackFileContent: string;
/** List of identifiers of EdgeGroups */
edgeGroups: Array<EdgeGroup['Id']>;
/** Deployment type to deploy this stack */
deploymentType: DeploymentType;
/** List of Registries to use for this stack */
registries?: Array<RegistryId>;
/** Uses the manifest's namespaces instead of the default one */
useManifestNamespaces?: boolean;
/** Pre Pull image */
prePullImage?: boolean;
/** Retry deploy */
retryDeploy?: boolean;
/** Optional webhook configuration */
webhook?: string;
/** List of environment variables */
envVars?: Array<Pair>;
/** Configuration for stagger updates */
staggerConfig?: StaggerConfig;
}
export async function createStackFromFileContent(payload: FileContentPayload) {
try {
const { data } = await axios.post<EdgeStack>(
buildUrl(undefined, 'create/string'),
payload
);
return data;
} catch (e) {
throw parseAxiosError(e as Error);
}
}

View file

@ -0,0 +1,74 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { RegistryId } from '@/react/portainer/registries/types';
import { Pair } from '@/react/portainer/settings/types';
import { EdgeGroup } from '@/react/edge/edge-groups/types';
import { AutoUpdateModel } from '@/react/portainer/gitops/types';
import { DeploymentType, EdgeStack, StaggerConfig } from '../../types';
import { buildUrl } from '../buildUrl';
/**
* Payload to create an EdgeStack from a git repository
*/
export type GitRepositoryPayload = {
/** Name of the stack */
name: string;
/** URL of a Git repository hosting the Stack file */
repositoryUrl: string;
/** Reference name of a Git repository hosting the Stack file */
repositoryReferenceName?: string;
/** Use basic authentication to clone the Git repository */
repositoryAuthentication?: boolean;
/** Username used in basic authentication. Required when RepositoryAuthentication is true. */
repositoryUsername?: string;
/** Password used in basic authentication. Required when RepositoryAuthentication is true. */
repositoryPassword?: string;
/** GitCredentialID used to identify the binded git credential */
repositoryGitCredentialId?: number;
/** Path to the Stack file inside the Git repository */
filePathInRepository?: string;
/** List of identifiers of EdgeGroups */
edgeGroups: Array<EdgeGroup['Id']>;
/** Deployment type to deploy this stack. Valid values are: 0 - 'compose', 1 - 'kubernetes', 2 - 'nomad'. Compose is enabled only for docker environments, kubernetes is enabled only for kubernetes environments, nomad is enabled only for nomad environments */
deploymentType: DeploymentType;
/** List of Registries to use for this stack */
registries?: Array<RegistryId>;
/** Uses the manifest's namespaces instead of the default one */
useManifestNamespaces?: boolean;
/** Pre Pull image */
prePullImage?: boolean;
/** Retry deploy */
retryDeploy?: boolean;
/** TLSSkipVerify skips SSL verification when cloning the Git repository */
tlsSkipVerify?: boolean;
/** Optional GitOps update configuration */
autoUpdate?: AutoUpdateModel;
/** Whether the stack supports relative path volume */
supportRelativePath?: boolean;
/** Local filesystem path */
filesystemPath?: string;
/** Whether the edge stack supports per device configs */
supportPerDeviceConfigs?: boolean;
/** Per device configs match type */
perDeviceConfigsMatchType?: string;
/** Per device configs group match type */
perDeviceConfigsGroupMatchType?: string;
/** Per device configs path */
perDeviceConfigsPath?: string;
/** List of environment variables */
envVars?: Array<Pair>;
/** Configuration for stagger updates */
staggerConfig?: StaggerConfig;
};
export async function createStackFromGit(payload: GitRepositoryPayload) {
try {
const { data } = await axios.post<EdgeStack>(
buildUrl(undefined, 'create/repository'),
payload
);
return data;
} catch (e) {
throw parseAxiosError(e as Error);
}
}

View file

@ -0,0 +1,135 @@
import { useMutation } from 'react-query';
import { EdgeGroup } from '@/react/edge/edge-groups/types';
import { RegistryId } from '@/react/portainer/registries/types';
import { Pair } from '@/react/portainer/settings/types';
import {
GitFormModel,
RelativePathModel,
} from '@/react/portainer/gitops/types';
import { DeploymentType, StaggerConfig } from '../../types';
import { createStackFromFile } from './createStackFromFile';
import { createStackFromFileContent } from './createStackFromFileContent';
import { createStackFromGit } from './createStackFromGit';
export function useCreateEdgeStack() {
return useMutation(createEdgeStack);
}
type BasePayload = {
/** Name of the stack */
name: string;
/** Content of the Stack file */
/** List of identifiers of EdgeGroups */
edgeGroups: Array<EdgeGroup['Id']>;
/** Deployment type to deploy this stack */
deploymentType: DeploymentType;
/** List of Registries to use for this stack */
registries?: Array<RegistryId>;
/** Uses the manifest's namespaces instead of the default one */
useManifestNamespaces?: boolean;
/** Pre Pull image */
prePullImage?: boolean;
/** Retry deploy */
retryDeploy?: boolean;
/** List of environment variables */
envVars?: Array<Pair>;
/** Configuration for stagger updates */
staggerConfig?: StaggerConfig;
};
/**
* Payload for creating an EdgeStack from a string
*/
export type CreateEdgeStackPayload =
| {
method: 'file';
payload: BasePayload & {
/** File to upload */
file: File;
/** Optional webhook configuration */
webhook?: string;
};
}
| {
method: 'string';
payload: BasePayload & {
/** Content of the Stack file */
fileContent: string;
/** Optional webhook configuration */
webhook?: string;
};
}
| {
method: 'git';
payload: BasePayload & {
git: GitFormModel;
relativePathSettings?: RelativePathModel;
};
};
function createEdgeStack({ method, payload }: CreateEdgeStackPayload) {
switch (method) {
case 'file':
return createStackFromFile({
DeploymentType: payload.deploymentType,
EdgeGroups: payload.edgeGroups,
Name: payload.name,
file: payload.file,
EnvVars: payload.envVars,
PrePullImage: payload.prePullImage,
Registries: payload.registries,
RetryDeploy: payload.retryDeploy,
StaggerConfig: payload.staggerConfig,
UseManifestNamespaces: payload.useManifestNamespaces,
Webhook: payload.webhook,
});
case 'git':
return createStackFromGit({
deploymentType: payload.deploymentType,
edgeGroups: payload.edgeGroups,
name: payload.name,
envVars: payload.envVars,
prePullImage: payload.prePullImage,
registries: payload.registries,
retryDeploy: payload.retryDeploy,
staggerConfig: payload.staggerConfig,
useManifestNamespaces: payload.useManifestNamespaces,
repositoryUrl: payload.git.RepositoryURL,
repositoryReferenceName: payload.git.RepositoryReferenceName,
filePathInRepository: payload.git.ComposeFilePathInRepository,
repositoryAuthentication: payload.git.RepositoryAuthentication,
repositoryUsername: payload.git.RepositoryUsername,
repositoryPassword: payload.git.RepositoryPassword,
repositoryGitCredentialId: payload.git.RepositoryGitCredentialID,
filesystemPath: payload.relativePathSettings?.FilesystemPath,
supportRelativePath: payload.relativePathSettings?.SupportRelativePath,
perDeviceConfigsGroupMatchType:
payload.relativePathSettings?.PerDeviceConfigsGroupMatchType,
perDeviceConfigsMatchType:
payload.relativePathSettings?.PerDeviceConfigsMatchType,
perDeviceConfigsPath:
payload.relativePathSettings?.PerDeviceConfigsPath,
tlsSkipVerify: payload.git.TLSSkipVerify,
autoUpdate: payload.git.AutoUpdate,
});
case 'string':
return createStackFromFileContent({
deploymentType: payload.deploymentType,
edgeGroups: payload.edgeGroups,
name: payload.name,
envVars: payload.envVars,
prePullImage: payload.prePullImage,
registries: payload.registries,
retryDeploy: payload.retryDeploy,
staggerConfig: payload.staggerConfig,
useManifestNamespaces: payload.useManifestNamespaces,
stackFileContent: payload.fileContent,
webhook: payload.webhook,
});
default:
throw new Error('Invalid method');
}
}