1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 13:29:41 +02:00
portainer/app/react/kubernetes/components/YAMLInspector.tsx
Ali d38085a560
Some checks are pending
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run
chore(data-cy): require data-cy attributes [EE-6880] (#11453)
2024-04-11 12:11:38 +12:00

81 lines
2.4 KiB
TypeScript

import { useMemo, useState } from 'react';
import YAML from 'yaml';
import { Minus, Plus } from 'lucide-react';
import { FeatureId } from '@/react/portainer/feature-flags/enums';
import { AutomationTestingProps } from '@/types';
import { WebEditorForm } from '@@/WebEditorForm';
import { Button } from '@@/buttons';
import { BETeaserButton } from '@@/BETeaserButton';
type Props = {
identifier: string;
data: string;
hideMessage?: boolean;
} & AutomationTestingProps;
export function YAMLInspector({
identifier,
data,
hideMessage,
'data-cy': dataCy,
}: Props) {
const [expanded, setExpanded] = useState(false);
const yaml = useMemo(() => cleanYamlUnwantedFields(data), [data]);
return (
<div>
<WebEditorForm
data-cy={dataCy}
value={yaml}
placeholder={
hideMessage
? undefined
: 'Define or paste the content of your manifest here'
}
readonly
hideTitle
id={identifier}
yaml
height={expanded ? '800px' : '500px'}
onChange={() => {}} // all kube yaml inspectors in CE are read only
/>
<div className="flex items-center justify-between py-5">
<Button
icon={expanded ? Minus : Plus}
data-cy={`expand-collapse-yaml-${identifier}`}
color="default"
className="!ml-0"
onClick={() => setExpanded(!expanded)}
>
{expanded ? 'Collapse' : 'Expand'}
</Button>
<BETeaserButton
featureId={FeatureId.K8S_EDIT_YAML}
heading="Apply YAML changes"
message="Applies any changes that you make in the YAML editor by calling the Kubernetes API to patch the relevant resources. Any resource removals or unexpected resource additions that you make in the YAML will be ignored. Note that editing is disabled for resources in namespaces marked as system."
buttonText="Apply changes"
data-cy="yaml-inspector-apply-changes-teaser-button"
/>
</div>
</div>
);
}
export function cleanYamlUnwantedFields(yml: string) {
try {
const ymls = yml.split('---');
const cleanYmls = ymls.map((yml) => {
const y = YAML.parse(yml);
if (y.metadata) {
const { managedFields, resourceVersion, ...metadata } = y.metadata;
y.metadata = metadata;
}
return YAML.stringify(y);
});
return cleanYmls.join('---\n');
} catch (e) {
return yml;
}
}