Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
FemiNoviaLina committed Nov 6, 2024
1 parent e47c71d commit b2e804a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 61 deletions.
8 changes: 4 additions & 4 deletions cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ProxyCommand() *cobra.Command {
Short: "Proxy management",
Long: "Server management commands.",
Example: heredoc.Doc(`
$ shield proxy envoy-xds start -c ./config.yaml
$ shield proxy envoy start -c ./config.yaml
`),
}

Expand All @@ -27,11 +27,11 @@ func ProxyCommand() *cobra.Command {

func proxyEnvoyXDSCommand() *cobra.Command {
c := &cli.Command{
Use: "envoy-xds",
Use: "envoy",
Short: "Envoy Agent xDS management",
Long: "Envoy Agent xDS management commands.",
Example: heredoc.Doc(`
$ shield proxy envoy-xds start
$ shield proxy envoy start
`),
}

Expand All @@ -47,7 +47,7 @@ func envoyXDSStartCommand() *cobra.Command {
Use: "start",
Short: "Start Envoy Agent xDS server",
Long: "Start Envoy Agent xDS server commands.",
Example: "shield proxy envoy-xds start",
Example: "shield proxy envoy start",
RunE: func(cmd *cli.Command, args []string) error {
appConfig, err := config.Load(configFile)
if err != nil {
Expand Down
57 changes: 0 additions & 57 deletions cmd/serve_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/goto/shield/internal/adapter"
"github.com/goto/shield/internal/api/v1beta1"
"github.com/goto/shield/internal/proxy"
"github.com/goto/shield/internal/proxy/envoy/xds"
"github.com/goto/shield/internal/proxy/envoy/xds/ads"
"github.com/goto/shield/internal/proxy/hook"
authz_hook "github.com/goto/shield/internal/proxy/hook/authz"
"github.com/goto/shield/internal/proxy/middleware/attributes"
Expand Down Expand Up @@ -100,61 +98,6 @@ func serveProxies(
return cleanUpBlobs, cleanUpProxies, nil
}

func serveXDS(ctx context.Context, logger *log.Zap, cfg proxy.ServicesConfig, pgRuleRepository *postgres.RuleRepository) ([]func() error, error) {
cleanUpBlobs, repositories, err := buildXDSDependencies(ctx, logger, cfg, pgRuleRepository)
if err != nil {
return nil, err
}

errChan := make(chan error)
go func() {
err := xds.Serve(ctx, logger, cfg, repositories)
if err != nil {
errChan <- err
logger.Error("error while running envoy xds server", "error", err)
}
}()

return cleanUpBlobs, nil
}

func buildXDSDependencies(ctx context.Context, logger *log.Zap, cfg proxy.ServicesConfig, pgRuleRepository *postgres.RuleRepository) ([]func() error, map[string]ads.Repository, error) {
var cleanUpBlobs []func() error
repositories := make(map[string]ads.Repository)

for _, svcConfig := range cfg.Services {
parsedRuleConfigURL, err := url.Parse(svcConfig.RulesPath)
if err != nil {
return nil, nil, err
}

var repository ads.Repository
switch parsedRuleConfigURL.Scheme {
case rule.RULES_CONFIG_STORAGE_PG:
repository = pgRuleRepository
case rule.RULES_CONFIG_STORAGE_GS,
rule.RULES_CONFIG_STORAGE_FILE,
rule.RULES_CONFIG_STORAGE_MEM:
ruleBlobFS, err := blob.NewStore(ctx, svcConfig.RulesPath, svcConfig.RulesPathSecret)
if err != nil {
return nil, nil, err
}

blobRuleRepository := blob.NewRuleRepository(logger, ruleBlobFS)
if err := blobRuleRepository.InitCache(ctx, ruleCacheRefreshDelay); err != nil {
return nil, nil, err
}
cleanUpBlobs = append(cleanUpBlobs, blobRuleRepository.Close)
repository = blobRuleRepository
default:
return nil, nil, errors.New("invalid rule config storage")
}
repositories[svcConfig.Name] = repository
}

return cleanUpBlobs, repositories, nil
}

func buildHookPipeline(
log log.Logger,
resourceService v1beta1.ResourceService,
Expand Down
70 changes: 70 additions & 0 deletions cmd/serve_xds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"context"
"errors"
"net/url"

"github.com/goto/salt/log"
"github.com/goto/shield/core/rule"
"github.com/goto/shield/internal/proxy"
"github.com/goto/shield/internal/proxy/envoy/xds"
"github.com/goto/shield/internal/proxy/envoy/xds/ads"
"github.com/goto/shield/internal/store/blob"
"github.com/goto/shield/internal/store/postgres"
)

func serveXDS(ctx context.Context, logger *log.Zap, cfg proxy.ServicesConfig, pgRuleRepository *postgres.RuleRepository) ([]func() error, error) {
cleanUpBlobs, repositories, err := buildXDSDependencies(ctx, logger, cfg, pgRuleRepository)
if err != nil {
return nil, err
}

errChan := make(chan error)
go func() {
err := xds.Serve(ctx, logger, cfg, repositories)
if err != nil {
errChan <- err
logger.Error("error while running envoy xds server", "error", err)
}
}()

return cleanUpBlobs, nil
}

func buildXDSDependencies(ctx context.Context, logger *log.Zap, cfg proxy.ServicesConfig, pgRuleRepository *postgres.RuleRepository) ([]func() error, map[string]ads.Repository, error) {
var cleanUpBlobs []func() error
repositories := make(map[string]ads.Repository)

for _, svcConfig := range cfg.Services {
parsedRuleConfigURL, err := url.Parse(svcConfig.RulesPath)
if err != nil {
return nil, nil, err
}

var repository ads.Repository
switch parsedRuleConfigURL.Scheme {
case rule.RULES_CONFIG_STORAGE_PG:
repository = pgRuleRepository
case rule.RULES_CONFIG_STORAGE_GS,
rule.RULES_CONFIG_STORAGE_FILE,
rule.RULES_CONFIG_STORAGE_MEM:
ruleBlobFS, err := blob.NewStore(ctx, svcConfig.RulesPath, svcConfig.RulesPathSecret)
if err != nil {
return nil, nil, err
}

blobRuleRepository := blob.NewRuleRepository(logger, ruleBlobFS)
if err := blobRuleRepository.InitCache(ctx, ruleCacheRefreshDelay); err != nil {
return nil, nil, err
}
cleanUpBlobs = append(cleanUpBlobs, blobRuleRepository.Close)
repository = blobRuleRepository
default:
return nil, nil, errors.New("invalid rule config storage")
}
repositories[svcConfig.Name] = repository
}

return cleanUpBlobs, repositories, nil
}

0 comments on commit b2e804a

Please sign in to comment.