-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The package contains logic to work with managed OVS bridges. // DiscoverBridges returns information about managed bridges on the host DiscoverBridges() (sriovnetworkv1.Bridges, error) // ConfigureBridge configure managed bridges for the host ConfigureBridges(bridgesSpec sriovnetworkv1.Bridges, bridgesStatus sriovnetworkv1.Bridges) error // DetachInterfaceFromManagedBridge detach interface from a managed bridge, // this step is required before applying some configurations to PF, e.g. changing of eSwitch mode. // The function detach interface from managed bridges only. DetachInterfaceFromManagedBridge(pciAddr string) error Signed-off-by: Yury Kulazhenkov <[email protected]>
- Loading branch information
1 parent
2f29a44
commit cdec0b4
Showing
111 changed files
with
17,065 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,81 @@ | ||
package bridge | ||
|
||
import ( | ||
"context" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" | ||
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/bridge/ovs" | ||
ovsStorePkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/bridge/ovs/store" | ||
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types" | ||
) | ||
|
||
type bridge struct { | ||
ovs ovs.Interface | ||
} | ||
|
||
// New return default implementation of the BridgeInterface | ||
func New() types.BridgeInterface { | ||
return &bridge{ | ||
ovs: ovs.New(ovsStorePkg.New()), | ||
} | ||
} | ||
|
||
// DiscoverBridges returns information about managed bridges on the host | ||
func (b *bridge) DiscoverBridges() (sriovnetworkv1.Bridges, error) { | ||
log.Log.V(2).Info("DiscoverBridges(): discover managed bridges") | ||
discoveredOVSBridges, err := b.ovs.GetOVSBridges(context.Background()) | ||
if err != nil { | ||
log.Log.Error(err, "DiscoverBridges(): failed to discover managed OVS bridges") | ||
return sriovnetworkv1.Bridges{}, err | ||
} | ||
return sriovnetworkv1.Bridges{OVS: discoveredOVSBridges}, nil | ||
} | ||
|
||
// ConfigureBridge configure managed bridges for the host | ||
func (b *bridge) ConfigureBridges(bridgesSpec sriovnetworkv1.Bridges, bridgesStatus sriovnetworkv1.Bridges) error { | ||
log.Log.V(2).Info("ConfigureBridges(): configure bridges") | ||
if len(bridgesSpec.OVS) == 0 && len(bridgesStatus.OVS) == 0 { | ||
// there are no reported OVS bridges in the status and the spec doesn't contains bridges. | ||
// no need to validated configuration | ||
log.Log.V(2).Info("ConfigureBridges(): configuration is not required") | ||
return nil | ||
} | ||
for _, curBr := range bridgesStatus.OVS { | ||
found := false | ||
for _, desiredBr := range bridgesSpec.OVS { | ||
if curBr.Name == desiredBr.Name { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
if err := b.ovs.RemoveOVSBridge(context.Background(), curBr.Name); err != nil { | ||
log.Log.Error(err, "ConfigureBridges(): failed to remove OVS bridge", "bridge", curBr.Name) | ||
return err | ||
} | ||
} | ||
} | ||
// create bridges, existing bridges will be updated only if the new config doesn't match current config | ||
for _, desiredBr := range bridgesSpec.OVS { | ||
desiredBr := desiredBr | ||
if err := b.ovs.CreateOVSBridge(context.Background(), &desiredBr); err != nil { | ||
log.Log.Error(err, "ConfigureBridges(): failed to create OVS bridge", "bridge", desiredBr.Name) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// DetachInterfaceFromManagedBridge detach interface from a managed bridge, | ||
// this step is required before applying some configurations to PF, e.g. changing of eSwitch mode. | ||
// The function detach interface from managed bridges only. | ||
func (b *bridge) DetachInterfaceFromManagedBridge(pciAddr string) error { | ||
log.Log.V(2).Info("DetachInterfaceFromManagedBridge(): detach interface", "pciAddr", pciAddr) | ||
if err := b.ovs.RemoveInterfaceFromOVSBridge(context.Background(), pciAddr); err != nil { | ||
log.Log.Error(err, "DetachInterfaceFromManagedBridge(): failed to detach interface from OVS bridge", "pciAddr", pciAddr) | ||
return err | ||
} | ||
return nil | ||
} |
This file contains 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,97 @@ | ||
package bridge | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang/mock/gomock" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" | ||
ovsMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/bridge/ovs/mock" | ||
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types" | ||
) | ||
|
||
var _ = Describe("Bridge", func() { | ||
var ( | ||
testCtrl *gomock.Controller | ||
br types.BridgeInterface | ||
ovsMock *ovsMockPkg.MockInterface | ||
testErr = fmt.Errorf("test") | ||
) | ||
BeforeEach(func() { | ||
testCtrl = gomock.NewController(GinkgoT()) | ||
ovsMock = ovsMockPkg.NewMockInterface(testCtrl) | ||
br = &bridge{ovs: ovsMock} | ||
}) | ||
AfterEach(func() { | ||
testCtrl.Finish() | ||
}) | ||
Context("DiscoverBridges", func() { | ||
It("succeed", func() { | ||
ovsMock.EXPECT().GetOVSBridges(gomock.Any()).Return([]sriovnetworkv1.OVSConfigExt{{Name: "test"}, {Name: "test2"}}, nil) | ||
ret, err := br.DiscoverBridges() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(ret.OVS).To(HaveLen(2)) | ||
}) | ||
It("error", func() { | ||
ovsMock.EXPECT().GetOVSBridges(gomock.Any()).Return(nil, testErr) | ||
_, err := br.DiscoverBridges() | ||
Expect(err).To(MatchError(testErr)) | ||
}) | ||
}) | ||
|
||
Context("ConfigureBridges", func() { | ||
It("succeed", func() { | ||
brCreate1 := sriovnetworkv1.OVSConfigExt{Name: "br-to-create-1"} | ||
brCreate2 := sriovnetworkv1.OVSConfigExt{Name: "br-to-create-2"} | ||
brDelete1 := sriovnetworkv1.OVSConfigExt{Name: "br-to-delete-1"} | ||
brDelete2 := sriovnetworkv1.OVSConfigExt{Name: "br-to-delete-2"} | ||
|
||
ovsMock.EXPECT().RemoveOVSBridge(gomock.Any(), brDelete1.Name).Return(nil) | ||
ovsMock.EXPECT().RemoveOVSBridge(gomock.Any(), brDelete2.Name).Return(nil) | ||
ovsMock.EXPECT().CreateOVSBridge(gomock.Any(), &brCreate1).Return(nil) | ||
ovsMock.EXPECT().CreateOVSBridge(gomock.Any(), &brCreate2).Return(nil) | ||
err := br.ConfigureBridges( | ||
sriovnetworkv1.Bridges{OVS: []sriovnetworkv1.OVSConfigExt{brCreate1, brCreate2}}, | ||
sriovnetworkv1.Bridges{OVS: []sriovnetworkv1.OVSConfigExt{brCreate1, brDelete1, brDelete2}}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
It("empty spec and status", func() { | ||
err := br.ConfigureBridges( | ||
sriovnetworkv1.Bridges{OVS: []sriovnetworkv1.OVSConfigExt{}}, | ||
sriovnetworkv1.Bridges{OVS: []sriovnetworkv1.OVSConfigExt{}}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
It("failed on creation", func() { | ||
brCreate1 := sriovnetworkv1.OVSConfigExt{Name: "br-to-create-1"} | ||
ovsMock.EXPECT().CreateOVSBridge(gomock.Any(), &brCreate1).Return(testErr) | ||
err := br.ConfigureBridges( | ||
sriovnetworkv1.Bridges{OVS: []sriovnetworkv1.OVSConfigExt{brCreate1}}, | ||
sriovnetworkv1.Bridges{OVS: []sriovnetworkv1.OVSConfigExt{}}) | ||
Expect(err).To(MatchError(testErr)) | ||
}) | ||
It("failed on removal", func() { | ||
brDelete1 := sriovnetworkv1.OVSConfigExt{Name: "br-to-delete-1"} | ||
ovsMock.EXPECT().RemoveOVSBridge(gomock.Any(), brDelete1.Name).Return(testErr) | ||
err := br.ConfigureBridges( | ||
sriovnetworkv1.Bridges{OVS: []sriovnetworkv1.OVSConfigExt{}}, | ||
sriovnetworkv1.Bridges{OVS: []sriovnetworkv1.OVSConfigExt{brDelete1}}) | ||
Expect(err).To(MatchError(testErr)) | ||
}) | ||
}) | ||
|
||
Context("DetachInterfaceFromManagedBridge", func() { | ||
It("succeed", func() { | ||
ovsMock.EXPECT().RemoveInterfaceFromOVSBridge(gomock.Any(), "0000:d8:00.0").Return(nil) | ||
err := br.DetachInterfaceFromManagedBridge("0000:d8:00.0") | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
It("error", func() { | ||
ovsMock.EXPECT().RemoveInterfaceFromOVSBridge(gomock.Any(), "0000:d8:00.0").Return(testErr) | ||
err := br.DetachInterfaceFromManagedBridge("0000:d8:00.0") | ||
Expect(err).To(MatchError(testErr)) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.