diff --git a/src/components/KuadrantDNSPolicyCreatePage.tsx b/src/components/KuadrantDNSPolicyCreatePage.tsx index cd447f1..046d8ff 100644 --- a/src/components/KuadrantDNSPolicyCreatePage.tsx +++ b/src/components/KuadrantDNSPolicyCreatePage.tsx @@ -64,13 +64,13 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => { const [routingStrategy, setRoutingStrategy] = React.useState('simple'); const [loadBalancing, setLoadBalancing] = React.useState({ geo: { defaultGeo: '' }, - weighted: { defaultWeight: 0 } + weighted: { defaultWeight: null } }); const initialHealthCheck: HealthCheck = { - endpoint: '/', - failureThreshold: 1, - port: 80, + endpoint: '', + failureThreshold: null, + port: null, protocol: 'HTTP', }; @@ -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', @@ -100,7 +106,7 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => { name: selectedGateway.name, namespace: selectedGateway.namespace }, - healthCheck: healthCheck, + ...(isHealthCheckValid && { healthCheck: healthCheck }), // Include healthCheck only if valid }, }; @@ -162,14 +168,14 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => { ))} - + setRoutingStrategy('simple')} /> setRoutingStrategy('loadbalanced')} /> {routingStrategy === 'loadbalanced' && ( - + )} {/* TODO: default to not setting these fields at all as the entire section is optional */} diff --git a/src/components/dnspolicy/LoadBalancingField.tsx b/src/components/dnspolicy/LoadBalancingField.tsx index 3d81ef1..433f2f3 100644 --- a/src/components/dnspolicy/LoadBalancingField.tsx +++ b/src/components/dnspolicy/LoadBalancingField.tsx @@ -12,21 +12,32 @@ const LoadBalancingField: React.FC = ({ loadBalancing, onCha const operatorOptions = ['In', 'NotIn', 'Exists', 'DoesNotExist']; const [isSelectOpen, setIsSelectOpen] = React.useState<{ [key: string]: boolean }>({}); - const [customWeights, setCustomWeights] = React.useState([]); + const [customWeights, setCustomWeights] = React.useState(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) => { @@ -34,7 +45,7 @@ const LoadBalancingField: React.FC = ({ loadBalancing, onCha 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) => { @@ -53,13 +64,13 @@ const LoadBalancingField: React.FC = ({ 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) => { @@ -68,18 +79,18 @@ const LoadBalancingField: React.FC = ({ 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 ( <>