Skip to content

Commit

Permalink
state: Interpret basic state through an explicit structure
Browse files Browse the repository at this point in the history
In order to ease the interpretation of the state yaml data, use a type
structure that represents the basic root items of state:
- interfaces
- routes:
  - config
  - running

In order to keep the generic nature of these items content,
they use as value a generic Go interface{}.

The filter helper functions, with this change, have been also made
immutable. They now return the filtered values.

Signed-off-by: Edward Haas <[email protected]>
  • Loading branch information
EdDev committed Mar 2, 2021
1 parent 66b9357 commit d5025ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
41 changes: 12 additions & 29 deletions pkg/state/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,16 @@ func FilterOut(currentState shared.State) (shared.State, error) {
return filterOut(currentState, interfacesFilterGlobFromEnv)
}

func filterOutRoutes(kind string, state map[string]interface{}, interfacesFilterGlob glob.Glob) {
routesRaw, hasRoutes := state["routes"]
if !hasRoutes {
return
}

routes, ok := routesRaw.(map[string]interface{})
if !ok {
return
}

routesByKind := routes[kind].([]interface{})

if routesByKind == nil {
return
}

func filterOutRoutes(routes []interface{}, interfacesFilterGlob glob.Glob) []interface{} {
filteredRoutes := []interface{}{}
for _, route := range routesByKind {
for _, route := range routes {
name := route.(map[string]interface{})["next-hop-interface"]
if !interfacesFilterGlob.Match(name.(string)) {
filteredRoutes = append(filteredRoutes, route)
}
}

state["routes"].(map[string]interface{})[kind] = filteredRoutes
return filteredRoutes
}

func filterOutDynamicAttributes(iface map[string]interface{}) {
Expand Down Expand Up @@ -89,31 +73,30 @@ func filterOutDynamicAttributes(iface map[string]interface{}) {
delete(options, "hello-timer")
}

func filterOutInterfaces(state map[string]interface{}, interfacesFilterGlob glob.Glob) {
interfaces := state["interfaces"]
func filterOutInterfaces(ifaces []interface{}, interfacesFilterGlob glob.Glob) []interface{} {
filteredInterfaces := []interface{}{}

for _, iface := range interfaces.([]interface{}) {
for _, iface := range ifaces {
name := iface.(map[string]interface{})["name"]
if !interfacesFilterGlob.Match(name.(string)) {
filterOutDynamicAttributes(iface.(map[string]interface{}))
filteredInterfaces = append(filteredInterfaces, iface)
}
}
state["interfaces"] = filteredInterfaces
return filteredInterfaces
}

func filterOut(currentState shared.State, interfacesFilterGlob glob.Glob) (shared.State, error) {
var state map[string]interface{}
var state rootState
err := yaml.Unmarshal(currentState.Raw, &state)
if err != nil {
return currentState, err
}

filterOutInterfaces(state, interfacesFilterGlob)
filterOutRoutes("running", state, interfacesFilterGlob)
filterOutRoutes("config", state, interfacesFilterGlob)

state.Interfaces = filterOutInterfaces(state.Interfaces, interfacesFilterGlob)
if state.Routes != nil {
state.Routes.Running = filterOutRoutes(state.Routes.Running, interfacesFilterGlob)
state.Routes.Config = filterOutRoutes(state.Routes.Config, interfacesFilterGlob)
}
filteredState, err := yaml.Marshal(state)
if err != nil {
return currentState, err
Expand Down
11 changes: 11 additions & 0 deletions pkg/state/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package state

type rootState struct {
Interfaces []interface{} `json:"interfaces"`
Routes *routesState `json:"routes,omitempty"`
}

type routesState struct {
Config []interface{} `json:"config"`
Running []interface{} `json:"running"`
}

0 comments on commit d5025ef

Please sign in to comment.