Skip to content

Commit

Permalink
GW status: enforced if affected by any policies succesfully
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Madigan <[email protected]>
  • Loading branch information
jasonmadigan committed Oct 23, 2024
1 parent b391657 commit f7c5c3e
Showing 1 changed file with 99 additions and 91 deletions.
190 changes: 99 additions & 91 deletions src/components/ResourceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,131 +35,139 @@ import {
} from '@patternfly/react-icons';
import DropdownWithKebab from './DropdownWithKebab';

const getStatusLabel = (obj: any) => {
// Check if status has parents and handle accordingly
const parentConditions = obj.status?.parents?.flatMap((parent: any) => parent.conditions) || [];
const getStatusLabel = (obj) => {
const { kind, status } = obj;

if (parentConditions.length > 0) {
const acceptedCondition = parentConditions.find(
(cond: any) => cond.type === 'Accepted' && cond.status === 'True',
const policiesMap = {
Gateway: [
'kuadrant.io/DNSPolicyAffected',
'kuadrant.io/TLSPolicyAffected',
'kuadrant.io/AuthPolicyAffected',
'kuadrant.io/RateLimitPolicyAffected',
],
HTTPRoute: ['kuadrant.io/AuthPolicyAffected', 'kuadrant.io/RateLimitPolicyAffected'],
};

const policiesAffected = policiesMap[kind] || [];

const hasAllPoliciesEnforced = (conditions) => {
return policiesAffected.every((policy) =>
conditions.some((cond) => cond.type === policy && cond.status === 'True'),
);
};

const hasAnyPolicyError = (conditions) => {
return policiesAffected.some((policy) =>
conditions.some((cond) => cond.type === policy && cond.status === 'False'),
);
};

const generateLabel = (labelText, color, icon) => (
<Label isCompact icon={icon} color={color}>
{labelText}
</Label>
);

if (kind === 'Gateway') {
const conditions = status?.conditions || [];

const acceptedCondition = conditions.find(
(cond) => cond.type === 'Accepted' && cond.status === 'True',
);
const programmedCondition = conditions.find(
(cond) => cond.type === 'Programmed' && cond.status === 'True',
);
const programmedCondition = parentConditions.find(
(cond: any) => cond.type === 'Programmed' && cond.status === 'True',

if (acceptedCondition && programmedCondition) {
if (hasAllPoliciesEnforced(conditions) && !hasAnyPolicyError(conditions)) {
return generateLabel('Enforced', 'green', <CheckCircleIcon />);
} else {
return generateLabel('Accepted (Not Enforced)', 'purple', <UploadIcon />);
}
} else if (programmedCondition) {
return generateLabel('Programmed', 'blue', <CheckCircleIcon />);
} else if (conditions.some((cond) => cond.type === 'Conflicted' && cond.status === 'True')) {
return generateLabel('Conflicted', 'red', <ExclamationTriangleIcon />);
} else if (conditions.some((cond) => cond.type === 'ResolvedRefs' && cond.status === 'True')) {
return generateLabel('Resolved Refs', 'blue', <CheckCircleIcon />);
} else {
return generateLabel('Unknown', 'orange', <ExclamationTriangleIcon />);
}
}

if (policiesAffected.length > 0) {
const parentConditions = status?.parents?.flatMap((parent) => parent.conditions) || [];

const acceptedCondition = parentConditions.find(
(cond) => cond.type === 'Accepted' && cond.status === 'True',
);
const conflictedCondition = parentConditions.find(
(cond: any) => cond.type === 'Conflicted' && cond.status === 'True',
(cond) => cond.type === 'Conflicted' && cond.status === 'True',
);
const resolvedRefsCondition = parentConditions.find(
(cond: any) => cond.type === 'ResolvedRefs' && cond.status === 'True',
(cond) => cond.type === 'ResolvedRefs' && cond.status === 'True',
);

let labelText: string;
let color: 'green' | 'blue' | 'red' | 'orange';
let icon: React.ReactNode;

if (acceptedCondition) {
labelText = 'Accepted';
color = 'green';
icon = <CheckCircleIcon />;
} else if (programmedCondition) {
labelText = 'Programmed';
color = 'blue';
icon = <CheckCircleIcon />;
if (hasAllPoliciesEnforced(parentConditions) && !hasAnyPolicyError(parentConditions)) {
return generateLabel('Enforced', 'green', <CheckCircleIcon />);
} else {
return generateLabel('Accepted (Not Enforced)', 'purple', <UploadIcon />);
}
} else if (conflictedCondition) {
labelText = 'Conflicted';
color = 'red';
icon = <ExclamationTriangleIcon />;
return generateLabel('Conflicted', 'red', <ExclamationTriangleIcon />);
} else if (resolvedRefsCondition) {
labelText = 'Resolved Refs';
color = 'blue';
icon = <CheckCircleIcon />;
return generateLabel('Resolved Refs', 'blue', <CheckCircleIcon />);
} else {
labelText = 'Unknown';
color = 'orange';
icon = <ExclamationTriangleIcon />;
return generateLabel('Unknown', 'orange', <ExclamationTriangleIcon />);
}

return (
<Label isCompact icon={icon} color={color}>
{labelText}
</Label>
);
}

// If no parent conditions, fallback to general condition checks
if (!obj.status || !obj.status.conditions || obj.status.conditions.length === 0) {
return (
<Label isCompact icon={<OutlinedHourglassIcon />} color="cyan">
Creating
</Label>
);
const generalConditions = status?.conditions || [];

if (generalConditions.length === 0) {
return generateLabel('Creating', 'cyan', <OutlinedHourglassIcon />);
}

const enforcedCondition = obj.status.conditions.find(
(cond: any) => cond.type === 'Enforced' && cond.status === 'True',
const enforcedCondition = generalConditions.find(
(cond) => cond.type === 'Enforced' && cond.status === 'True',
);
const acceptedCondition = obj.status.conditions.find(
(cond: any) => cond.type === 'Accepted' && cond.status === 'True',
const acceptedCondition = generalConditions.find(
(cond) => cond.type === 'Accepted' && cond.status === 'True',
);
const acceptedConditionFalse = obj.status.conditions.find(
(cond: any) => cond.type === 'Accepted' && cond.status === 'False',
const acceptedConditionFalse = generalConditions.find(
(cond) => cond.type === 'Accepted' && cond.status === 'False',
);
const overriddenCondition = obj.status.conditions.find(
(cond: any) => cond.type === 'Overridden' && cond.status === 'False',
const overriddenCondition = generalConditions.find(
(cond) => cond.type === 'Overridden' && cond.status === 'False',
);
const conflictedCondition = obj.status.conditions.find(
(cond: any) => cond.reason === 'Conflicted' && cond.status === 'False',
const conflictedCondition = generalConditions.find(
(cond) => cond.reason === 'Conflicted' && cond.status === 'False',
);
const targetNotFoundCondition = obj.status.conditions.find(
(cond: any) => cond.reason === 'TargetNotFound' && cond.status === 'False',
const targetNotFoundCondition = generalConditions.find(
(cond) => cond.reason === 'TargetNotFound' && cond.status === 'False',
);
const unknownCondition = obj.status.conditions.find(
(cond: any) => cond.reason === 'Unknown' && cond.status === 'False',
const unknownCondition = generalConditions.find(
(cond) => cond.reason === 'Unknown' && cond.status === 'False',
);

let labelText: string;
let color: 'blue' | 'green' | 'red' | 'orange' | 'grey' | 'purple' | 'cyan';
let icon: React.ReactNode;

if (enforcedCondition) {
labelText = 'Enforced';
color = 'green';
icon = <CheckCircleIcon />;
return generateLabel('Enforced', 'green', <CheckCircleIcon />);
} else if (overriddenCondition) {
labelText = 'Overridden (Not Enforced)';
color = 'grey';
icon = <LayerGroupIcon />;
return generateLabel('Overridden (Not Enforced)', 'grey', <LayerGroupIcon />);
} else if (acceptedCondition) {
labelText = 'Accepted (Not Enforced)';
color = 'purple';
icon = <UploadIcon />;
return generateLabel('Accepted (Not Enforced)', 'purple', <UploadIcon />);
} else if (conflictedCondition) {
labelText = 'Conflicted (Not Accepted)';
color = 'red';
icon = <ExclamationTriangleIcon />;
return generateLabel('Conflicted (Not Accepted)', 'red', <ExclamationTriangleIcon />);
} else if (targetNotFoundCondition) {
labelText = 'TargetNotFound (Not Accepted)';
color = 'red';
icon = <ExclamationTriangleIcon />;
return generateLabel('TargetNotFound (Not Accepted)', 'red', <ExclamationTriangleIcon />);
} else if (unknownCondition) {
labelText = 'Unknown (Not Accepted)';
color = 'orange';
icon = <ExclamationTriangleIcon />;
return generateLabel('Unknown (Not Accepted)', 'orange', <ExclamationTriangleIcon />);
} else if (acceptedConditionFalse) {
labelText = 'Invalid (Not Accepted)';
color = 'red';
icon = <ExclamationTriangleIcon />;
return generateLabel('Invalid (Not Accepted)', 'red', <ExclamationTriangleIcon />);
} else {
labelText = 'Unknown';
color = 'grey';
icon = <ExclamationTriangleIcon />;
return generateLabel('Unknown', 'grey', <ExclamationTriangleIcon />);
}

return (
<Label isCompact icon={icon} color={color}>
{labelText}
</Label>
);
};

type ResourceListProps = {
Expand Down

0 comments on commit f7c5c3e

Please sign in to comment.