Skip to content

Commit

Permalink
✨ feat: support config provider support aws appconfig (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing committed Jul 18, 2024
1 parent c88d6a1 commit 02bcef7
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 40 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ go 1.22.0
toolchain go1.22.2

require (
github.com/aws/aws-sdk-go-v2 v1.26.1
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.11
github.com/aws/aws-sdk-go-v2/credentials v1.17.11
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.3
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.31.1
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
github.com/casbin/casbin/v2 v2.87.1
Expand Down Expand Up @@ -187,8 +188,8 @@ require (
require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
Expand Down
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
github.com/aws/aws-sdk-go v1.38.35/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go v1.50.32 h1:POt81DvegnpQKM4DMDLlHz1CO6OBnEoQ1gRhYFd7QRY=
github.com/aws/aws-sdk-go v1.50.32/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA=
github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY=
github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg=
github.com/aws/aws-sdk-go-v2/config v1.27.11 h1:f47rANd2LQEYHda2ddSCKYId18/8BhSRM4BULGmfgNA=
Expand All @@ -114,14 +114,16 @@ github.com/aws/aws-sdk-go-v2/credentials v1.17.11 h1:YuIB1dJNf1Re822rriUOTxopaHH
github.com/aws/aws-sdk-go-v2/credentials v1.17.11/go.mod h1:AQtFPsDH9bI2O+71anW6EKL+NcD7LG3dpKGMV4SShgo=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 h1:FVJ0r5XTHSmIHJV6KuDmdYhEpvlHpiSd38RQWhut5J4=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1/go.mod h1:zusuAeqezXzAB24LGuzuekqMAEgWkVYukBec3kr3jUg=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 h1:aw39xVGeRWlWx9EzGVnhOR4yOjQDHPQ6o6NmBlscyQg=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5/go.mod h1:FSaRudD0dXiMPK2UjknVwwTYyZMRsHv3TtkabsZih5I=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 h1:PG1F3OD1szkuQPzDw3CIQsRIrtTlUC3lP84taWzHlq0=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5/go.mod h1:jU1li6RFryMz+so64PpKtudI+QzbKoIEivqdf6LNpOc=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 h1:81KE7vaZzrl7yHBYHVEzYB8sypz11NMOZ40YlWvPxsU=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5/go.mod h1:LIt2rg7Mcgn09Ygbdh/RdIm0rQ+3BNkbP1gyVMFtRK0=
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.3 h1:a8T5x683phwsf2Us9G63hqepjlTyKAO5KteNuMlNO2I=
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.3/go.mod h1:h22STrNFoH0uMiKwxw3VXy1Tu7kgXLHcdjhOewAvD1Y=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.31.1 h1:dZXY07Dm59TxAjJcUfNMJHLDI/gLMxTRZefn2jFAVsw=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.31.1/go.mod h1:lVLqEtX+ezgtfalyJs7Peb0uv9dEpAQP5yuq2O26R44=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 h1:Ji0DY1xUsUr3I8cHps0G+XM3WWU16lP6yG8qu1GAZAs=
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/mss-boot-io/mss-boot/pkg"
"github.com/mss-boot-io/mss-boot/pkg/config/source"
"github.com/mss-boot-io/mss-boot/pkg/config/source/appconfig"
"github.com/mss-boot-io/mss-boot/pkg/config/source/configmap"
sourceConsul "github.com/mss-boot-io/mss-boot/pkg/config/source/consul"
sourceFS "github.com/mss-boot-io/mss-boot/pkg/config/source/fs"
Expand Down Expand Up @@ -64,6 +65,8 @@ func Init(cfg source.Entity, options ...source.Option) (err error) {
f, err = configmap.New(options...)
case source.Consul:
f, err = sourceConsul.New(options...)
case source.APPConfig:
f, err = appconfig.New(options...)
default:
f, err = sourceLocal.New(options...)
}
Expand Down
95 changes: 95 additions & 0 deletions pkg/config/source/appconfig/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package appconfig

import (
"context"
"errors"
"fmt"
"io/fs"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/appconfigdata"

"github.com/mss-boot-io/mss-boot/pkg/config/source"
)

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2024/7/18 19:23:30
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2024/7/18 19:23:30
*/

type Source struct {
opt *source.Options
}

func (s *Source) Open(string) (fs.File, error) {
return nil, errors.New("method Get not implemented")
}

func (s *Source) ReadFile(name string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.opt.Timeout)
defer cancel()
for i := range source.Extends {
sessionOutput, err := s.opt.APPConfigDataClient.
StartConfigurationSession(ctx, &appconfigdata.StartConfigurationSessionInput{
ApplicationIdentifier: aws.String(s.opt.ProjectName),
EnvironmentIdentifier: aws.String(s.opt.Namespace),
ConfigurationProfileIdentifier: aws.String(fmt.Sprintf("%s.%s", name, source.Extends[i])),
})
if err != nil {
if strings.Contains(err.Error(), "ConfigurationProfile not found") {
continue
}
return nil, err
}
output, err := s.opt.APPConfigDataClient.
GetLatestConfiguration(ctx, &appconfigdata.GetLatestConfigurationInput{
ConfigurationToken: sessionOutput.InitialConfigurationToken,
})
if err != nil {
return nil, err
}
s.opt.Extend = source.Extends[i]
return output.Configuration, nil
}
return nil, nil
}

func (s *Source) Watch(_ source.Entity, _ func([]byte, any) error) error {
return nil
}

// GetExtend get extend
func (s *Source) GetExtend() source.Scheme {
return s.opt.Extend
}

func New(options ...source.Option) (*Source, error) {
s := &Source{
opt: source.DefaultOptions(),
}
for _, opt := range options {
opt(s.opt)
}
if s.opt.Timeout == 0 {
s.opt.Timeout = 5 * time.Second
}
if s.opt.ProjectName == "" {
return nil, errors.New("project name is required")
}
if s.opt.Namespace == "" {
return nil, errors.New("namespace is required")
}
ctx, cancel := context.WithTimeout(context.TODO(), s.opt.Timeout)
defer cancel()
cfg, err := awsConfig.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
s.opt.APPConfigDataClient = appconfigdata.NewFromConfig(cfg)
return s, nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 29 additions & 25 deletions pkg/config/source/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/service/appconfigdata"
"github.com/aws/aws-sdk-go-v2/service/s3"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -34,6 +35,8 @@ const (
ConfigMap Provider = "configmap"
// Consul consul
Consul Provider = "consul"
// APPConfig aws appconfig
APPConfig Provider = "appconfig"
)

// Extends extends
Expand All @@ -51,30 +54,31 @@ type Option func(*Options)

// Options options
type Options struct {
Provider Provider
Driver Driver
Name string
Extend Scheme
Dir string
Region string
Bucket string
ProjectName string
Timeout time.Duration
Client *s3.Client
FS fs.ReadFileFS
MongoDBURL string
MongoDBName string
MongoDBCollection string
Datasource string
GORMDriver string
GORMDsn string
Watch bool
Namespace string
Configmap string
PrefixHook PrefixHook
Clientset *kubernetes.Clientset
Kubeconfig string
KubeconfigPath string
Provider Provider
Driver Driver
Name string
Extend Scheme
Dir string
Region string
Bucket string
ProjectName string
Timeout time.Duration
S3Client *s3.Client
APPConfigDataClient *appconfigdata.Client
FS fs.ReadFileFS
MongoDBURL string
MongoDBName string
MongoDBCollection string
Datasource string
GORMDriver string
GORMDsn string
Watch bool
Namespace string
Configmap string
PrefixHook PrefixHook
Clientset *kubernetes.Clientset
Kubeconfig string
KubeconfigPath string
}

func (o *Options) GetExtend() Scheme {
Expand Down Expand Up @@ -194,7 +198,7 @@ func WithTimeout(timeout time.Duration) Option {
// WithClient set s3 client
func WithClient(client *s3.Client) Option {
return func(args *Options) {
args.Client = client
args.S3Client = client
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/config/source/s3/s3.go → pkg/config/source/s3/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func (s *Source) Open(string) (fs.File, error) {
}

// ReadFile read file
func (s *Source) ReadFile(name string) (rb []byte, err error) {
func (s *Source) ReadFile(name string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.opt.Timeout)
defer cancel()
for i := range source.Extends {
object, err := s.opt.Client.GetObject(ctx, &s3.GetObjectInput{
object, err := s.opt.S3Client.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(s.opt.Bucket),
Key: aws.String(
fmt.Sprintf("%s/%s",
Expand All @@ -48,14 +48,14 @@ func (s *Source) ReadFile(name string) (rb []byte, err error) {
if err != nil {
return nil, err
}
rb, err = io.ReadAll(object.Body)
rb, err := io.ReadAll(object.Body)
if err == nil {
_ = object.Body.Close()
s.opt.Extend = source.Extends[i]
return rb, nil
}
}
return nil, err
return nil, nil
}

func (s *Source) Watch(_ source.Entity, _ func([]byte, any) error) error {
Expand All @@ -81,7 +81,7 @@ func New(options ...source.Option) (*Source, error) {
if s.opt.ProjectName != "" {
s.opt.Dir = s.opt.Dir[strings.Index(s.opt.Dir, s.opt.ProjectName+"/"):]
}
if s.opt.Client != nil {
if s.opt.S3Client != nil {
return s, nil
}

Expand All @@ -91,6 +91,6 @@ func New(options ...source.Option) (*Source, error) {
if err != nil {
return nil, err
}
s.opt.Client = s3.NewFromConfig(cfg)
s.opt.S3Client = s3.NewFromConfig(cfg)
return s, nil
}

0 comments on commit 02bcef7

Please sign in to comment.