Skip to content

Commit

Permalink
Fix setting of custom weights
Browse files Browse the repository at this point in the history
Signed-off-by: David Martin <[email protected]>
  • Loading branch information
david-martin committed Sep 6, 2024
1 parent 6f3e8ca commit f101759
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
20 changes: 13 additions & 7 deletions src/components/KuadrantDNSPolicyCreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => {
const [routingStrategy, setRoutingStrategy] = React.useState('simple');
const [loadBalancing, setLoadBalancing] = React.useState<LoadBalancing>({
geo: { defaultGeo: '' },
weighted: { defaultWeight: 0 }
weighted: { defaultWeight: null }
});

const initialHealthCheck: HealthCheck = {
endpoint: '/',
failureThreshold: 1,
port: 80,
endpoint: '',
failureThreshold: null,
port: null,
protocol: 'HTTP',
};

Expand All @@ -79,6 +79,12 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => {
const history = useHistory();

const handleSubmit = async () => {
const isHealthCheckValid =
healthCheck.endpoint &&
healthCheck.failureThreshold > 0 &&
healthCheck.port > 0 &&
healthCheck.protocol;

const dnsPolicy = {
apiVersion: 'kuadrant.io/v1alpha1',
kind: 'DNSPolicy',
Expand All @@ -100,7 +106,7 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => {
name: selectedGateway.name,
namespace: selectedGateway.namespace
},
healthCheck: healthCheck,
...(isHealthCheckValid && { healthCheck: healthCheck }), // Include healthCheck only if valid
},
};

Expand Down Expand Up @@ -162,14 +168,14 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => {
))}
</FormSelect>
</FormGroup>
<GatewaySelect selectedGateway={selectedGateway} onChange={setSelectedGateway}/>
<GatewaySelect selectedGateway={selectedGateway} onChange={setSelectedGateway} />
<ExpandableSection toggleText="Routing Strategy">
<FormGroup role="radiogroup" isInline fieldId='routing-strategy' label='Routing Strategy to use' isRequired aria-labelledby="routing-strategy-label">
<Radio name='routing-strategy' label='Simple' id='routing-strategy-simple' isChecked={routingStrategy === 'simple'} onChange={() => setRoutingStrategy('simple')} />
<Radio name='routing-strategy' label='Load Balanced' id='routing-strategy-loadbalanced' isChecked={routingStrategy === 'loadbalanced'} onChange={() => setRoutingStrategy('loadbalanced')} />
</FormGroup>
{routingStrategy === 'loadbalanced' && (
<LoadBalancingField loadBalancing={loadBalancing} onChange={setLoadBalancing}/>
<LoadBalancingField loadBalancing={loadBalancing} onChange={setLoadBalancing} />
)}
</ExpandableSection>
{/* TODO: default to not setting these fields at all as the entire section is optional */}
Expand Down
31 changes: 21 additions & 10 deletions src/components/dnspolicy/LoadBalancingField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,40 @@ const LoadBalancingField: React.FC<LoadBalancingProps> = ({ loadBalancing, onCha
const operatorOptions = ['In', 'NotIn', 'Exists', 'DoesNotExist'];

const [isSelectOpen, setIsSelectOpen] = React.useState<{ [key: string]: boolean }>({});
const [customWeights, setCustomWeights] = React.useState<WeightedCustom[]>([]);
const [customWeights, setCustomWeights] = React.useState<WeightedCustom[]>(loadBalancing.weighted.custom || []);

// Update customWeights and ensure it updates the loadBalancing object
const updateCustomWeights = (updatedWeights: WeightedCustom[]) => {
setCustomWeights(updatedWeights);
onChange({
...loadBalancing,
weighted: { ...loadBalancing.weighted, custom: updatedWeights },
});
};

const addCustomSelector = () => {
setCustomWeights([
const updatedWeights = [
...customWeights,
{
selector: { matchExpressions: [], matchLabels: {} },
weight: 0,
},
]);
];
updateCustomWeights(updatedWeights);
};


const removeCustomSelector = (index: number) => {
const updatedWeights = customWeights.filter((_, i) => i !== index);
setCustomWeights(updatedWeights);
updateCustomWeights(updatedWeights);
};

const updateMatchExpression = (index: number, expIndex: number, field: keyof MatchExpression, value: any) => {
const updatedCustoms = [...customWeights];
const matchExpressions = updatedCustoms[index].selector.matchExpressions || [];
matchExpressions[expIndex] = { ...matchExpressions[expIndex], [field]: value };
updatedCustoms[index].selector.matchExpressions = matchExpressions;
setCustomWeights(updatedCustoms);
updateCustomWeights(updatedCustoms);
};

const addMatchExpression = (index: number) => {
Expand All @@ -53,13 +64,13 @@ const LoadBalancingField: React.FC<LoadBalancingProps> = ({ loadBalancing, onCha
[key]: false,
}));

setCustomWeights(updatedCustoms);
updateCustomWeights(updatedCustoms);
};

const updateWeight = (index: number, value: number) => {
const updatedCustoms = [...customWeights];
updatedCustoms[index].weight = value;
setCustomWeights(updatedCustoms);
updateCustomWeights(updatedCustoms);
};

const addValue = (index: number, expIndex: number) => {
Expand All @@ -68,18 +79,18 @@ const LoadBalancingField: React.FC<LoadBalancingProps> = ({ loadBalancing, onCha
...(updatedCustoms[index].selector.matchExpressions![expIndex].values || []),
''
];
setCustomWeights(updatedCustoms);
updateCustomWeights(updatedCustoms);
};
const removeValue = (index: number, expIndex: number, valueIndex: number) => {
const updatedCustoms = [...customWeights];
updatedCustoms[index].selector.matchExpressions![expIndex].values!.splice(valueIndex, 1);
setCustomWeights(updatedCustoms);
updateCustomWeights(updatedCustoms);
};

const updateMatchExpressionValue = (index: number, expIndex: number, valueIndex: number, newValue: string) => {
const updatedCustoms = [...customWeights];
updatedCustoms[index].selector.matchExpressions![expIndex].values![valueIndex] = newValue;
setCustomWeights(updatedCustoms);
updateCustomWeights(updatedCustoms);
};
return (
<>
Expand Down

0 comments on commit f101759

Please sign in to comment.