Skip to content

fix: golangci complexity #258

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

Merged
merged 2 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ linters-settings:
- whyNoLint
- wrapperFunc
gocyclo:
min-complexity: 20
min-complexity: 15
goimports:
local-prefixes: github.com/aeraki-mesh/aeraki
gomnd:
Expand Down
91 changes: 54 additions & 37 deletions pkg/envoyfilter/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"time"

"istio.io/istio/pkg/config"

"github.com/aeraki-mesh/aeraki/pkg/config/constants"

"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -194,6 +196,7 @@ func (c *Controller) generateEnvoyFilters() (map[string]*model.EnvoyFilterWrappe
// We can't retry in this scenario
return envoyFilters, nil
}

if len(service.Hosts) > 1 {
controllerLog.Warnf("multiple hosts found for service: %s, only the first one will be processed", serviceEntries[i].Name)
}
Expand All @@ -203,49 +206,19 @@ func (c *Controller) generateEnvoyFilters() (map[string]*model.EnvoyFilterWrappe
if generator, ok := c.generators[instance]; ok {
controllerLog.Infof("found generator for port: %s", port.Name)

relatedVs, err := c.findRelatedVirtualService(service)
if err != nil {
return envoyFilters, fmt.Errorf("failed in finding the related virtual service : %s: %v", serviceEntries[i].Name, err)
}
relatedMr, err := c.findRelatedMetaRouter(service)
ctx, err := c.envoyFilterContext(service, &serviceEntries[i])
if err != nil {
return envoyFilters, fmt.Errorf("failed in finding the related meta router : %s: %v", serviceEntries[i].Name, err)
return envoyFilters, err
}
ctx := &model.EnvoyFilterContext{
ServiceEntry: &model.ServiceEntryWrapper{
Meta: serviceEntries[i].Meta,
Spec: service,
},
VirtualService: relatedVs,
MetaRouter: relatedMr,
}

envoyFilterWrappers, err := generator.Generate(ctx)
if err != nil {
if err == nil {
for _, wrapper := range envoyFilterWrappers {
envoyFilters = c.createEnvoyFiltersOnExportNSs(ctx, wrapper, envoyFilters)
}
} else {
controllerLog.Errorf("failed to generate envoy filter: service: %s, port: %s, error: %v",
serviceEntries[i].Name,
port.Name, err)
} else {
for _, wrapper := range envoyFilterWrappers {
var exportNSs []string
if ctx.MetaRouter != nil {
exportNSs = ctx.MetaRouter.Spec.ExportTo
}
if len(exportNSs) == 0 {
wrapper.Namespace = c.defaultEnvoyFilterNS(ctx.ServiceEntry.Namespace)
envoyFilters[envoyFilterMapKey(wrapper.Name, wrapper.Namespace)] = wrapper
} else {
for _, exportNS := range exportNSs {
if exportNS == "." {
exportNS = ctx.MetaRouter.Namespace
} else if exportNS == "*" {
exportNS = constants.DefaultRootNamespace
}
wrapper.Namespace = exportNS
envoyFilters[envoyFilterMapKey(wrapper.Name, exportNS)] = wrapper
}
}
}
}
break
}
Expand All @@ -254,6 +227,50 @@ func (c *Controller) generateEnvoyFilters() (map[string]*model.EnvoyFilterWrappe
return envoyFilters, nil
}

func (c *Controller) createEnvoyFiltersOnExportNSs(ctx *model.EnvoyFilterContext, wrapper *model.EnvoyFilterWrapper,
envoyFilters map[string]*model.EnvoyFilterWrapper) map[string]*model.EnvoyFilterWrapper {
var exportNSs []string
if ctx.MetaRouter != nil {
exportNSs = ctx.MetaRouter.Spec.ExportTo
}
if len(exportNSs) == 0 {
wrapper.Namespace = c.defaultEnvoyFilterNS(ctx.ServiceEntry.Namespace)
envoyFilters[envoyFilterMapKey(wrapper.Name, wrapper.Namespace)] = wrapper
} else {
for _, exportNS := range exportNSs {
if exportNS == "." {
exportNS = ctx.MetaRouter.Namespace
} else if exportNS == "*" {
exportNS = constants.DefaultRootNamespace
}
wrapper.Namespace = exportNS
envoyFilters[envoyFilterMapKey(wrapper.Name, wrapper.Namespace)] = wrapper
}
}
return envoyFilters
}

// envoyFilterContext wraps all the resources needed to create the EnvoyFilter
func (c *Controller) envoyFilterContext(service *networking.ServiceEntry,
serviceEntry *config.Config) (*model.EnvoyFilterContext, error) {
relatedVs, err := c.findRelatedVirtualService(service)
if err != nil {
return nil, fmt.Errorf("failed in finding the related virtual service : %s: %v", service.Hosts[0], err)
}
relatedMr, err := c.findRelatedMetaRouter(service)
if err != nil {
return nil, fmt.Errorf("failed in finding the related meta router : %s: %v", service.Hosts[0], err)
}
return &model.EnvoyFilterContext{
ServiceEntry: &model.ServiceEntryWrapper{
Meta: serviceEntry.Meta,
Spec: service,
},
VirtualService: relatedVs,
MetaRouter: relatedMr,
}, nil
}

func (c *Controller) defaultEnvoyFilterNS(serviceNS string) string {
if c.namespaceScoped {
return serviceNS
Expand Down
36 changes: 21 additions & 15 deletions pkg/xds/cache_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,32 @@ func (c *CacheMgr) updateRouteCache() error {
return fmt.Errorf("failed to list service entries from the config store: %v", err)
}

routes := c.generateMetaRoutes(serviceEntries)
snapshot, err := generateSnapshot(routes)
if err != nil {
xdsLog.Errorf("failed to generate route cache: %v", err)
// We don't retry in this scenario
return nil
}

for _, node := range c.routeCache.GetStatusKeys() {
xdsLog.Debugf("set route cahe for: %s", node)
if err := c.routeCache.SetSnapshot(context.TODO(), node, snapshot); err != nil {
xdsLog.Errorf("failed to set route cache: %v", err)
// We don't retry in this scenario
}
}
return nil
}

func (c *CacheMgr) generateMetaRoutes(serviceEntries []istioconfig.Config) []*metaroute.RouteConfiguration {
var routes []*metaroute.RouteConfiguration

for i := range serviceEntries {
config := serviceEntries[i]
service, ok := config.Spec.(*networking.ServiceEntry)
if !ok { // should never happen
xdsLog.Errorf("failed in getting a service entry: %s: %v", config.Labels, err)
xdsLog.Errorf("failed in getting a service entry: %s", config.Name)
continue
}

Expand Down Expand Up @@ -173,20 +192,7 @@ func (c *CacheMgr) updateRouteCache() error {
}
}
}

snapshot, err := generateSnapshot(routes)
if err != nil {
xdsLog.Errorf("failed to generate route cache: %v", err)
} else {
for _, node := range c.routeCache.GetStatusKeys() {
xdsLog.Debugf("set route cahe for: %s", node)
if err := c.routeCache.SetSnapshot(context.TODO(), node, snapshot); err != nil {
xdsLog.Errorf("failed to set route cache: %v", err)
// We can't retry in this scenario
}
}
}
return nil
return routes
}

func isMetaProtocolService(service *networking.ServiceEntry) bool {
Expand Down