Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Fixed local cluster bug and added datasource version compatibility check before importing workflows #650

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getObjFromJsonOrYamlString,
isValidUiWorkflow,
isValidWorkflow,
isCompatibleWorkflow,
} from '../../../utils';
import { getCore } from '../../../services';
import {
Expand All @@ -43,7 +44,7 @@ import {
WORKFLOW_NAME_RESTRICTIONS,
} from '../../../../common';
import { WORKFLOWS_TAB } from '../workflows';
import { getDataSourceId } from '../../../utils/utils';
import { getDataSourceId, getEffectiveVersion, formatDisplayVersion } from '../../../utils/utils';

interface ImportWorkflowModalProps {
isImportModalOpen: boolean;
Expand All @@ -61,6 +62,17 @@ interface ImportWorkflowModalProps {
export function ImportWorkflowModal(props: ImportWorkflowModalProps) {
const dispatch = useAppDispatch();
const dataSourceId = getDataSourceId();
const [dataSourceVersion, setDataSourceVersion] = useState<
string | undefined
>(undefined);
useEffect(() => {
async function getVersion() {
if (dataSourceId !== undefined) {
setDataSourceVersion(await getEffectiveVersion(dataSourceId));
}
}
getVersion();
}, [dataSourceId]);
const { workflows } = useSelector((state: AppState) => state.workflows);

// workflow name state
Expand All @@ -86,6 +98,9 @@ export function ImportWorkflowModal(props: ImportWorkflowModalProps) {
return description.length > MAX_DESCRIPTION_LENGTH;
}

// State for tracking workflow template compatibility with current data source version
const [isCompatible, setIsCompatible] = useState<boolean>(true);

// transient importing state for button state
const [isImporting, setIsImporting] = useState<boolean>(false);

Expand Down Expand Up @@ -115,6 +130,16 @@ export function ImportWorkflowModal(props: ImportWorkflowModalProps) {
}
}, [fileObj]);

useEffect(() => {
async function checkCompatibility() {
if (isValidWorkflow(fileObj)) {
const isCompatible = await isCompatibleWorkflow(fileObj, dataSourceId);
setIsCompatible(isCompatible);
}
}
checkCompatibility();
}, [fileObj, dataSourceId]);

function onModalClose(): void {
props.setIsImportModalOpen(false);
setFileContents(undefined);
Expand Down Expand Up @@ -142,6 +167,18 @@ export function ImportWorkflowModal(props: ImportWorkflowModalProps) {
<EuiSpacer size="m" />
</>
)}
{isValidWorkflow(fileObj) && !isCompatible && dataSourceVersion && (
<>
<EuiFlexItem>
<EuiCallOut
title={`The uploaded file is not compatible with the current data source version ${formatDisplayVersion(dataSourceVersion)}. Upload a compatible file or switch to another data source.`}
iconType={'alert'}
color="danger"
/>
</EuiFlexItem>
<EuiSpacer size="m" />
</>
)}
{isValidWorkflow(fileObj) && !isValidUiWorkflow(fileObj) && (
<>
<EuiFlexItem>
Expand Down Expand Up @@ -230,6 +267,7 @@ export function ImportWorkflowModal(props: ImportWorkflowModalProps) {
<EuiSmallButton
disabled={
!isValidWorkflow(fileObj) ||
!isCompatible ||
isImporting ||
isInvalidName(workflowName) ||
isInvalidDescription(workflowDescription)
Expand Down
1 change: 0 additions & 1 deletion public/route_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ export function configureRoutes(core: CoreStart): RouteService {
return e as HttpFetchError;
}
},

getIndex: async (index: string, dataSourceId?: string) => {
try {
const url = dataSourceId
Expand Down
37 changes: 36 additions & 1 deletion public/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@ export function isValidWorkflow(workflowObj: any): boolean {
return workflowObj?.name !== undefined;
}

// Determines if a file used for import workflow is compatible with the current data source version.
export async function isCompatibleWorkflow(
workflowObj: any,
dataSourceId?: string | undefined
): Promise<boolean> {
const compatibility = workflowObj?.version?.compatibility;

// Default to true when compatibility cannot be assessed (empty/invalid compatibility array or MDS disabled.)
if (!Array.isArray(compatibility) || compatibility.length === 0 || dataSourceId === undefined) {
return true;
}

const dataSourceVersion = await getEffectiveVersion(dataSourceId);
const [effectiveMajorVersion, effectiveMinorVersion] = dataSourceVersion.split('.').map(Number);

// Checks if any version in compatibility array matches the current dataSourceVersion (major.minor)
return compatibility.some(compatibleVersion => {
const [compatibleMajor, compatibleMinor] = compatibleVersion.split('.').map(Number);
return effectiveMajorVersion === compatibleMajor && effectiveMinorVersion === compatibleMinor;
});
}


export function isValidUiWorkflow(workflowObj: any): boolean {
return (
isValidWorkflow(workflowObj) &&
Expand Down Expand Up @@ -530,7 +553,7 @@ export const getDataSourceId = () => {

export const isDataSourceReady = (dataSourceId?: string) => {
const dataSourceEnabled = getDataSourceEnabled().enabled;
return !dataSourceEnabled || (dataSourceId && dataSourceId !== '');
return !dataSourceEnabled || dataSourceId !== undefined;
};

// converts camelCase to a space-delimited string with the first word capitalized.
Expand Down Expand Up @@ -912,3 +935,15 @@ export const getEffectiveVersion = async (
return MIN_SUPPORTED_VERSION;
}
};


/**
* Formats version string to show only major.minor numbers
* Example: "3.0.0-alpha1" -> "3.0"
*/
export function formatDisplayVersion(version: string): string {
// Take first two parts of version number (major.minor)
const [major, minor] = version.split('.');
return `${major}.${minor}`;
}

Loading