diff --git a/pkg/state/filter.go b/pkg/state/filter.go index d1b2d8e17..381a01ffc 100644 --- a/pkg/state/filter.go +++ b/pkg/state/filter.go @@ -73,11 +73,11 @@ func filterOutDynamicAttributes(iface map[string]interface{}) { delete(options, "hello-timer") } -func filterOutInterfaces(ifaces []interface{}, interfacesFilterGlob glob.Glob) []interface{} { +func filterOutInterfaces(ifaces []interface{}, ifacesNames []Interface, interfacesFilterGlob glob.Glob) []interface{} { filteredInterfaces := []interface{}{} - for _, iface := range ifaces { - name := iface.(map[string]interface{})["name"] - if !interfacesFilterGlob.Match(name.(string)) { + for i, iface := range ifaces { + name := ifacesNames[i].Name + if !interfacesFilterGlob.Match(name) { filterOutDynamicAttributes(iface.(map[string]interface{})) filteredInterfaces = append(filteredInterfaces, iface) } @@ -92,7 +92,12 @@ func filterOut(currentState shared.State, interfacesFilterGlob glob.Glob) (share return currentState, err } - state.Interfaces = filterOutInterfaces(state.Interfaces, interfacesFilterGlob) + var ifacesState InterfacesState + if err := yaml.Unmarshal(currentState.Raw, &ifacesState); err != nil { + return currentState, err + } + + state.Interfaces = filterOutInterfaces(state.Interfaces, ifacesState.Interfaces, interfacesFilterGlob) if state.Routes != nil { state.Routes.Running = filterOutRoutes(state.Routes.Running, interfacesFilterGlob) state.Routes.Config = filterOutRoutes(state.Routes.Config, interfacesFilterGlob) diff --git a/pkg/state/filter_test.go b/pkg/state/filter_test.go index c87149ffe..0e2050578 100644 --- a/pkg/state/filter_test.go +++ b/pkg/state/filter_test.go @@ -331,4 +331,51 @@ interfaces: }) }) + Context("when the interfaces have (only) numeric characters", func() { + BeforeEach(func() { + state = nmstate.NewState(` +interfaces: +- name: eth0 +- name: 0 +- name: 1101010 +`) + filteredState = nmstate.NewState(` +interfaces: +- name: eth0 +- name: 1101010 +`) + interfacesFilterGlob = glob.MustCompile("0*") + }) + + It("should filter out interfaces correctly", func() { + returnedState, err := filterOut(state, interfacesFilterGlob) + Expect(err).NotTo(HaveOccurred()) + Expect(returnedState).To(MatchYAML(filteredState)) + }) + }) + + Context("when the interfaces have special numeric characters", func() { + BeforeEach(func() { + state = nmstate.NewState(` +interfaces: +- name: eth0 +- name: 0.0 +- name: 1.0 +- name: 0xfe +`) + filteredState = nmstate.NewState(` +interfaces: +- name: eth0 +- name: 0 +- name: 254 +`) + interfacesFilterGlob = glob.MustCompile("1*") + }) + + It("should filter out interfaces correctly but they are not represented correctly", func() { + returnedState, err := filterOut(state, interfacesFilterGlob) + Expect(err).NotTo(HaveOccurred()) + Expect(returnedState).To(MatchYAML(filteredState)) + }) + }) }) diff --git a/pkg/state/type.go b/pkg/state/type.go index 76123795b..87b531438 100644 --- a/pkg/state/type.go +++ b/pkg/state/type.go @@ -9,3 +9,11 @@ type Routes struct { Config []interface{} `json:"config"` Running []interface{} `json:"running"` } + +type InterfacesState struct { + Interfaces []Interface `json:"interfaces"` +} + +type Interface struct { + Name string `json:"name"` +}