Repository navigation
Add Nokia SR Linux DHCP relay provider #644
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
Open
ObuMan
wants to merge
3
commits into
ironcore-dev:main
Choose a base branch
from
ObuMan:nokia-srlinux-dhcp-relay
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+327
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package srlinux | ||
|
|
||
| type AdminSt string | ||
|
|
||
| const ( | ||
| AdminStEnable AdminSt = "enable" | ||
| AdminStDisable AdminSt = "disable" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package srlinux | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/ironcore-dev/network-operator/internal/provider" | ||
| ) | ||
|
|
||
| const ( | ||
| // dhcpRelayDefaultSubinterfaceIndex is the subinterface a relay attaches to | ||
| // when the interface name carries no ".<index>" suffix. | ||
| dhcpRelayDefaultSubinterfaceIndex = 0 | ||
| ) | ||
|
|
||
| var _ provider.DHCPRelayProvider = (*Provider)(nil) | ||
|
|
||
| // DeleteDHCPRelay implements [provider.DHCPRelayProvider]. | ||
| func (p *Provider) DeleteDHCPRelay(ctx context.Context, req *provider.DeleteDHCPRelayRequest) error { | ||
| // The deprecated InterfaceRefs path provides no resolved interface in the | ||
| // delete request. SRLinux models the relay per subinterface with no single | ||
| // root to remove, so there is nothing to delete here. | ||
| if len(req.DHCPRelay.Spec.InterfaceRefs) > 0 { //nolint:staticcheck | ||
| return nil | ||
| } | ||
| name, index, err := splitSubinterface(req.Interface.Spec.Name) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return p.client.Delete(ctx, &DHCPRelay{Interface: name, SubinterfaceIndex: index}) | ||
| } | ||
|
|
||
| // EnsureDHCPRelay implements [provider.DHCPRelayProvider]. | ||
| func (p *Provider) EnsureDHCPRelay(ctx context.Context, req *provider.DHCPRelayRequest) error { | ||
| var networkInstance string | ||
| if req.VRF != nil { | ||
| networkInstance = req.VRF.Spec.Name | ||
| } | ||
| if len(req.DHCPRelay.Spec.InterfaceRefs) > 0 { //nolint:staticcheck | ||
| return nil | ||
| } | ||
| name, index, err := splitSubinterface(req.Interface.Spec.Name) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| dhcprelay := &DHCPRelay{ | ||
| Interface: name, | ||
| SubinterfaceIndex: index, | ||
| AdminState: AdminStEnable, | ||
| Server: req.DHCPRelay.Spec.Servers, | ||
| NetworkInstance: networkInstance, | ||
| } | ||
| return p.client.Update(ctx, dhcprelay) | ||
| } | ||
|
|
||
| // splitSubinterface splits an interface name into its base name and | ||
| // subinterface index. SRLinux names a subinterface "<interface>.<index>"; | ||
| // without a suffix the relay attaches to the base subinterface. | ||
| func splitSubinterface(name string) (string, int, error) { | ||
| base, suffix, found := strings.CutLast(name, ".") | ||
| if !found { | ||
| return name, dhcpRelayDefaultSubinterfaceIndex, nil | ||
| } | ||
| index, err := strconv.Atoi(suffix) | ||
| if err != nil { | ||
| return "", 0, fmt.Errorf("invalid subinterface index in interface name %q: %w", name, err) | ||
| } | ||
| return base, index, nil | ||
| } | ||
|
|
||
| type DHCPRelay struct { | ||
| Interface string `json:"-"` | ||
| SubinterfaceIndex int `json:"-"` | ||
| AdminState AdminSt `json:"admin-state"` | ||
| Server []string `json:"server"` | ||
| NetworkInstance string `json:"network-instance,omitempty"` | ||
| } | ||
|
|
||
| func (d *DHCPRelay) XPath() string { | ||
| return fmt.Sprintf("srl_nokia-interfaces:interface[name=%s]/subinterface[index=%d]/ipv4/srl_nokia-interfaces-ip-dhcp-relay:dhcp-relay", d.Interface, d.SubinterfaceIndex) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| // SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package srlinux | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| corev1alpha1 "github.com/ironcore-dev/network-operator/api/core/v1alpha1" | ||
| "github.com/ironcore-dev/network-operator/internal/provider" | ||
| "github.com/ironcore-dev/network-operator/internal/transport/gnmiext" | ||
| ) | ||
|
|
||
| func TestEnsureDHCPRelayNetworkInstance(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| vrf *corev1alpha1.VRF | ||
| want string | ||
| }{ | ||
| { | ||
| name: "no vrf leaves network-instance empty", | ||
| vrf: nil, | ||
| want: "", | ||
| }, | ||
| { | ||
| name: "vrf sets network-instance to the device vrf name", | ||
| vrf: &corev1alpha1.VRF{Spec: corev1alpha1.VRFSpec{Name: "shared-services"}}, | ||
| want: "shared-services", | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| var captured *DHCPRelay | ||
| p := &Provider{client: &gnmiext.ClientMock{ | ||
| UpdateFunc: func(_ context.Context, els ...gnmiext.DataElement) error { | ||
| if r, ok := els[0].(*DHCPRelay); ok { | ||
| captured = r | ||
| } | ||
| return nil | ||
| }, | ||
| }} | ||
|
|
||
| err := p.EnsureDHCPRelay(t.Context(), &provider.DHCPRelayRequest{ | ||
| DHCPRelay: &corev1alpha1.DHCPRelay{ | ||
| Spec: corev1alpha1.DHCPRelaySpec{Servers: []string{"192.0.2.1"}}, | ||
| }, | ||
| Interface: &corev1alpha1.Interface{ | ||
| Spec: corev1alpha1.InterfaceSpec{Name: "ethernet-1/1"}, | ||
| }, | ||
| VRF: test.vrf, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("EnsureDHCPRelay() error = %v", err) | ||
| } | ||
| if captured == nil { | ||
| t.Fatal("EnsureDHCPRelay() did not call Update") | ||
| } | ||
| if got := captured.NetworkInstance; got != test.want { | ||
| t.Errorf("NetworkInstance = %q, want %q", got, test.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestDHCPRelayJSONNetworkInstance guards the omitempty contract: an unset | ||
| // network-instance must be absent from the payload so periodic reconciles do | ||
| // not diff against the device default and trigger a spurious Set. | ||
| func TestDHCPRelayJSONNetworkInstance(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| relay *DHCPRelay | ||
| wantKey bool | ||
| }{ | ||
| { | ||
| name: "empty network-instance is omitted", | ||
| relay: &DHCPRelay{AdminState: AdminStEnable, Server: []string{"192.0.2.1"}}, | ||
| wantKey: false, | ||
| }, | ||
| { | ||
| name: "set network-instance is present", | ||
| relay: &DHCPRelay{AdminState: AdminStEnable, Server: []string{"192.0.2.1"}, NetworkInstance: "shared-services"}, | ||
| wantKey: true, | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| got, err := json.Marshal(test.relay) | ||
| if err != nil { | ||
| t.Fatalf("json.Marshal() error = %v", err) | ||
| } | ||
| if hasKey := strings.Contains(string(got), `"network-instance"`); hasKey != test.wantKey { | ||
| t.Errorf("json.Marshal() = %s, network-instance present = %v, want %v", got, hasKey, test.wantKey) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDHCPRelayXPath(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| relay *DHCPRelay | ||
| want string | ||
| }{ | ||
| { | ||
| name: "base subinterface", | ||
| relay: &DHCPRelay{Interface: "ethernet-1/1"}, | ||
| want: "srl_nokia-interfaces:interface[name=ethernet-1/1]/subinterface[index=0]/ipv4/srl_nokia-interfaces-ip-dhcp-relay:dhcp-relay", | ||
| }, | ||
| { | ||
| name: "explicit subinterface index", | ||
| relay: &DHCPRelay{Interface: "ethernet-1/1", SubinterfaceIndex: 100}, | ||
| want: "srl_nokia-interfaces:interface[name=ethernet-1/1]/subinterface[index=100]/ipv4/srl_nokia-interfaces-ip-dhcp-relay:dhcp-relay", | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| if got := test.relay.XPath(); got != test.want { | ||
| t.Errorf("XPath() = %q, want %q", got, test.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSplitSubinterface(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| in string | ||
| wantIface string | ||
| wantIndex int | ||
| wantErr bool | ||
| }{ | ||
| {name: "no suffix defaults to zero", in: "ethernet-1/1", wantIface: "ethernet-1/1", wantIndex: 0}, | ||
| {name: "explicit subinterface", in: "ethernet-1/1.100", wantIface: "ethernet-1/1", wantIndex: 100}, | ||
| {name: "loopback", in: "lo0", wantIface: "lo0", wantIndex: 0}, | ||
| {name: "non-numeric suffix errors", in: "ethernet-1/1.abc", wantErr: true}, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| iface, index, err := splitSubinterface(test.in) | ||
| if test.wantErr { | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("splitSubinterface(%q) error = %v", test.in, err) | ||
| } | ||
| if iface != test.wantIface || index != test.wantIndex { | ||
| t.Errorf("splitSubinterface(%q) = (%q, %d), want (%q, %d)", test.in, iface, index, test.wantIface, test.wantIndex) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestEnsureDHCPRelaySubinterface(t *testing.T) { | ||
| var captured *DHCPRelay | ||
| p := &Provider{client: &gnmiext.ClientMock{ | ||
| UpdateFunc: func(_ context.Context, els ...gnmiext.DataElement) error { | ||
| if r, ok := els[0].(*DHCPRelay); ok { | ||
| captured = r | ||
| } | ||
| return nil | ||
| }, | ||
| }} | ||
|
|
||
| err := p.EnsureDHCPRelay(t.Context(), &provider.DHCPRelayRequest{ | ||
| DHCPRelay: &corev1alpha1.DHCPRelay{ | ||
| Spec: corev1alpha1.DHCPRelaySpec{Servers: []string{"192.0.2.1"}}, | ||
| }, | ||
| Interface: &corev1alpha1.Interface{ | ||
| Spec: corev1alpha1.InterfaceSpec{Name: "ethernet-1/1.100"}, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("EnsureDHCPRelay() error = %v", err) | ||
| } | ||
| if captured == nil { | ||
| t.Fatal("EnsureDHCPRelay() did not call Update") | ||
| } | ||
| if captured.Interface != "ethernet-1/1" || captured.SubinterfaceIndex != 100 { | ||
| t.Errorf("relay = (%q, %d), want (ethernet-1/1, 100)", captured.Interface, captured.SubinterfaceIndex) | ||
| } | ||
| } | ||
|
|
||
| func TestDeleteDHCPRelay(t *testing.T) { | ||
| t.Run("normal path deletes the interface relay", func(t *testing.T) { | ||
| var deleted []*DHCPRelay | ||
| p := &Provider{client: &gnmiext.ClientMock{ | ||
| DeleteFunc: func(_ context.Context, els ...gnmiext.DataElement) error { | ||
| for _, el := range els { | ||
| if r, ok := el.(*DHCPRelay); ok { | ||
| deleted = append(deleted, r) | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| }} | ||
|
|
||
| err := p.DeleteDHCPRelay(t.Context(), &provider.DeleteDHCPRelayRequest{ | ||
| DHCPRelay: &corev1alpha1.DHCPRelay{}, | ||
| Interface: &corev1alpha1.Interface{Spec: corev1alpha1.InterfaceSpec{Name: "ethernet-1/1.100"}}, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("DeleteDHCPRelay() error = %v", err) | ||
| } | ||
| if len(deleted) != 1 || deleted[0].Interface != "ethernet-1/1" || deleted[0].SubinterfaceIndex != 100 { | ||
| t.Fatalf("deleted = %+v, want one relay for ethernet-1/1 subinterface 100", deleted) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("deprecated InterfaceRefs path is a no-op and does not panic", func(t *testing.T) { | ||
| // Mock with no funcs: any device call panics, and req.Interface is nil. | ||
| p := &Provider{client: &gnmiext.ClientMock{}} | ||
|
|
||
| err := p.DeleteDHCPRelay(t.Context(), &provider.DeleteDHCPRelayRequest{ | ||
| DHCPRelay: &corev1alpha1.DHCPRelay{ | ||
| Spec: corev1alpha1.DHCPRelaySpec{ | ||
| InterfaceRefs: []corev1alpha1.LocalObjectReference{{Name: "eth1"}}, //nolint:staticcheck | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("DeleteDHCPRelay() error = %v", err) | ||
| } | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to make sure that as this is a subtree of the YANG tree of the interface itself, that when we implement the interface provider for native Nokia SRLinux later, that we don't use a
gnmiext.Updateto replace the configuration of an interface, as that would then also overwrite whatever we set here in a nested path. Therefore the interface provider implementation must usePatch()calls only to update the interface tree.