forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: envoy control plane #100
Open
FemiNoviaLina
wants to merge
13
commits into
main
Choose a base branch
from
envoy-xds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
05d7fec
feat: envoy control plane
FemiNoviaLina 9dd6a59
fix: uint conversion
FemiNoviaLina 3462887
fix: stream flow
FemiNoviaLina 0a2711c
feat: start xds command
FemiNoviaLina 0563da7
fix: fetch from db
FemiNoviaLina d952af2
test: add test
FemiNoviaLina a629493
test: add test
FemiNoviaLina 85b7732
fix: lint
FemiNoviaLina a7390e1
test: add test
FemiNoviaLina 8b8e742
test: add test
FemiNoviaLina e47c71d
test: add test
FemiNoviaLina b2e804a
chore: refactor
FemiNoviaLina 8969b28
feat: envoy control plane
FemiNoviaLina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/MakeNowJust/heredoc" | ||
"github.com/goto/shield/config" | ||
"github.com/goto/shield/internal/proxy/envoy/xds" | ||
"github.com/goto/shield/internal/store/postgres" | ||
shieldlogger "github.com/goto/shield/pkg/logger" | ||
"github.com/spf13/cobra" | ||
cli "github.com/spf13/cobra" | ||
) | ||
|
||
func ProxyCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "proxy <command>", | ||
Short: "Proxy management", | ||
Long: "Server management commands.", | ||
Example: heredoc.Doc(` | ||
$ shield proxy envoy start -c ./config.yaml | ||
`), | ||
} | ||
|
||
cmd.AddCommand(proxyEnvoyXDSCommand()) | ||
|
||
return cmd | ||
} | ||
|
||
func proxyEnvoyXDSCommand() *cobra.Command { | ||
c := &cli.Command{ | ||
Use: "envoy", | ||
Short: "Envoy Agent xDS management", | ||
Long: "Envoy Agent xDS management commands.", | ||
Example: heredoc.Doc(` | ||
$ shield proxy envoy start | ||
`), | ||
} | ||
|
||
c.AddCommand(envoyXDSStartCommand()) | ||
|
||
return c | ||
} | ||
|
||
func envoyXDSStartCommand() *cobra.Command { | ||
var configFile string | ||
|
||
c := &cli.Command{ | ||
Use: "start", | ||
Short: "Start Envoy Agent xDS server", | ||
Long: "Start Envoy Agent xDS server commands.", | ||
Example: "shield proxy envoy start", | ||
RunE: func(cmd *cli.Command, args []string) error { | ||
appConfig, err := config.Load(configFile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
logger := shieldlogger.InitLogger(shieldlogger.Config{Level: appConfig.Log.Level}) | ||
|
||
dbClient, err := setupDB(appConfig.DB, logger) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
logger.Info("cleaning up db") | ||
dbClient.Close() | ||
}() | ||
|
||
ctx := cmd.Context() | ||
|
||
pgRuleRepository := postgres.NewRuleRepository(dbClient) | ||
if err := pgRuleRepository.InitCache(ctx); err != nil { | ||
return err | ||
} | ||
|
||
cbs, repositories, err := buildXDSDependencies(ctx, logger, appConfig.Proxy, pgRuleRepository) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
logger.Info("cleaning up rules proxy blob") | ||
for _, f := range cbs { | ||
if err := f(); err != nil { | ||
logger.Warn("error occurred during shutdown rules proxy blob storages", "err", err) | ||
} | ||
} | ||
}() | ||
|
||
return xds.Serve(ctx, logger, appConfig.Proxy, repositories) | ||
}, | ||
} | ||
|
||
c.Flags().StringVarP(&configFile, "config", "c", "", "Config file path") | ||
return c | ||
} |
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,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 | ||
} |
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 |
---|---|---|
@@ -1,7 +1,20 @@ | ||
package proxy | ||
|
||
import "time" | ||
|
||
type ServicesConfig struct { | ||
Services []Config `yaml:"services" mapstructure:"services"` | ||
EnvoyAgent EnvoyAgent `yaml:"envoy" mapstructure:"envoy"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's update the config in https://github.com/goto/shield/blob/main/config/config.yaml with the example on how to use this envoy agent? |
||
Services []Config `yaml:"services" mapstructure:"services"` | ||
} | ||
|
||
type EnvoyAgent struct { | ||
XDS XDS `yaml:"xds" mapstructure:"xds"` | ||
} | ||
|
||
type XDS struct { | ||
Host string `yaml:"host" mapstructure:"host"` | ||
Port int `yaml:"port" mapstructure:"port"` | ||
RefreshInterval time.Duration `yaml:"refresh_interval" mapstructure:"refresh_interval" default:"60s"` | ||
} | ||
|
||
type Config struct { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean if we pass host and port of envoy agent, this will use envoy otherwise it is falling back to the old proxy?
If yes, let's not do that way. Let's have an explicit config to choose.