@@ -340,6 +340,100 @@ func CheckThatWorkersDeployedWithBondVfs(
340340 return bondName , bondSlaves , nil
341341}
342342
343+ // WithInterfaceIPv4Forwarding returns a function that sets IPv4 forwarding on the specified interface.
344+ func WithInterfaceIPv4Forwarding (
345+ interfaceName string , forwarding bool ) func (* nmstate.PolicyBuilder ) (* nmstate.PolicyBuilder , error ) {
346+ klog .V (90 ).Infof ("Setting IPv4 forwarding=%v on interface %s" , forwarding , interfaceName )
347+
348+ return withInterfaceForwardingMutator (func (iface * nmstate.NetworkInterface ) {
349+ iface .Ipv4 .Forwarding = & forwarding
350+ },
351+ interfaceName ,
352+ )
353+ }
354+
355+ // WithInterfaceIPv6Forwarding returns a function that sets IPv6 forwarding on the specified interface.
356+ func WithInterfaceIPv6Forwarding (
357+ interfaceName string , forwarding bool ) func (* nmstate.PolicyBuilder ) (* nmstate.PolicyBuilder , error ) {
358+ klog .V (90 ).Infof ("Setting IPv6 forwarding=%v on interface %s" , forwarding , interfaceName )
359+
360+ return withInterfaceForwardingMutator (func (iface * nmstate.NetworkInterface ) {
361+ iface .Ipv6 .Forwarding = & forwarding
362+ },
363+ interfaceName ,
364+ )
365+ }
366+
367+ // withInterfaceForwardingMutator returns a function that mutates IP forwarding on a specific interface.
368+ func withInterfaceForwardingMutator (
369+ mutateFunc func (* nmstate.NetworkInterface ),
370+ interfaceName string ) func (* nmstate.PolicyBuilder ) (* nmstate.PolicyBuilder , error ) {
371+ return func (builder * nmstate.PolicyBuilder ) (* nmstate.PolicyBuilder , error ) {
372+ klog .V (90 ).Infof ("Mutating the interface %s forwarding configuration" , interfaceName )
373+
374+ if interfaceName == "" {
375+ klog .V (90 ).Infof ("The interfaceName cannot be an empty string" )
376+
377+ return builder , fmt .Errorf ("the interfaceName is an empty string" )
378+ }
379+
380+ var currentState nmstate.DesiredState
381+
382+ err := yaml .Unmarshal (builder .Definition .Spec .DesiredState .Raw , & currentState )
383+ if err != nil {
384+ klog .V (90 ).Infof ("Failed to unmarshal DesiredState" )
385+
386+ return builder , fmt .Errorf ("failed to unmarshal DesiredState: %w" , err )
387+ }
388+
389+ var foundInterface bool
390+
391+ for i , networkInterface := range currentState .Interfaces {
392+ if networkInterface .Name == interfaceName {
393+ mutateFunc (& currentState .Interfaces [i ])
394+
395+ foundInterface = true
396+
397+ break
398+ }
399+ }
400+
401+ if ! foundInterface {
402+ klog .V (90 ).Infof ("Failed to find the given interface" )
403+
404+ return builder , fmt .Errorf ("failed to find interface %s" , interfaceName )
405+ }
406+
407+ desiredStateYaml , err := yaml .Marshal (currentState )
408+ if err != nil {
409+ klog .V (90 ).Infof ("Failed to marshal DesiredState" )
410+
411+ return builder , fmt .Errorf ("failed to marshal a new Desired state: %w" , err )
412+ }
413+
414+ builder .Definition .Spec .DesiredState = nmstateShared .NewState (string (desiredStateYaml ))
415+
416+ return builder , nil
417+ }
418+ }
419+
420+ // UpdatePolicyAndWaitUntilItsDegraded updates a NodeNetworkConfigurationPolicy and waits until
421+ // it reaches the Degraded condition.
422+ func UpdatePolicyAndWaitUntilItsDegraded (
423+ timeout time.Duration , nmstatePolicy * nmstate.PolicyBuilder ) (* nmstate.PolicyBuilder , error ) {
424+ klog .V (90 ).Infof ("Updating an NMState policy and waiting for it to become Degraded." )
425+
426+ nmstatePolicy , err := nmstatePolicy .Update (true )
427+ if err != nil {
428+ return nmstatePolicy , err
429+ }
430+
431+ err = nmstatePolicy .WaitUntilCondition (
432+ nmstateShared .NodeNetworkConfigurationPolicyConditionDegraded , timeout )
433+
434+ return nmstatePolicy , err
435+ }
436+
343437// withBondOptionMutator returns a function that mutates a specific option for a bond interface.
344438func withBondOptionMutator (
345439 mutateFunc func (* nmstate.OptionsLinkAggregation ),
0 commit comments