Skip to content

feat: component ref #761

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 61 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
2f4557e
add secret ref key
ZLBer Jul 16, 2022
2ad136e
add secret ref key
ZLBer Jul 16, 2022
59f378c
add secret ref key
ZLBer Jul 16, 2022
82f45d8
Merge branch 'main' into secret_ref_key
seeflood Jul 18, 2022
25a6b1d
need sub_key
ZLBer Jul 18, 2022
8608eaf
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Jul 18, 2022
223758b
Merge branch 'main' into secret_ref_key
seeflood Jul 19, 2022
a7e8d81
rename field
ZLBer Jul 19, 2022
7f435af
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Jul 19, 2022
b4f3955
fix quickstart
ZLBer Jul 19, 2022
3b81ec3
Merge branch 'main' into secret_ref_key
seeflood Jul 19, 2022
72ad844
fix quickstart
ZLBer Jul 20, 2022
673ee45
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Jul 20, 2022
47cfc26
add component ref
ZLBer Aug 11, 2022
7393e3c
Merge branch 'main' into secret_ref_key
ZLBer Aug 11, 2022
5fe66ed
Merge branch 'main' into secret_ref_key
seeflood Aug 13, 2022
c33d8c9
Merge branch 'main' into secret_ref_key
seeflood Aug 19, 2022
8ca9ad6
add component ref
ZLBer Aug 22, 2022
3e31caf
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Aug 22, 2022
27145ef
add component ref
ZLBer Aug 23, 2022
ca59f1f
Merge branch 'main' into secret_ref_key
seeflood Aug 27, 2022
8c3015e
Merge branch 'main' into secret_ref_key
seeflood Aug 30, 2022
d72a348
Merge branch 'main' into secret_ref_key
ZLBer Sep 3, 2022
feececd
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 3, 2022
75fce6a
add component ref
ZLBer Sep 3, 2022
69a7135
fix golint
ZLBer Sep 3, 2022
1606f07
fix golint
ZLBer Sep 3, 2022
207f374
fix golint
ZLBer Sep 3, 2022
93f0933
fix golint
ZLBer Sep 3, 2022
cb878f2
fix component ref
ZLBer Sep 7, 2022
156e5b7
fix component ref
ZLBer Sep 7, 2022
9259c2e
fix component ref
ZLBer Sep 7, 2022
cfcae53
Merge branch 'main' into secret_ref_key
seeflood Sep 8, 2022
856f6de
fix config
ZLBer Sep 8, 2022
8fc2b4f
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 8, 2022
932dba2
fix ut
ZLBer Sep 8, 2022
24d2b96
fix ut
ZLBer Sep 8, 2022
5f9005c
fix ut
ZLBer Sep 8, 2022
6118a08
check ref config nil
ZLBer Sep 8, 2022
c909383
Merge branch 'main' into secret_ref_key
Xunzhuo Sep 14, 2022
f25c577
fix ci
ZLBer Sep 15, 2022
28c0c1a
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 15, 2022
58fd181
fix ci
ZLBer Sep 15, 2022
0cf9b56
rename config
ZLBer Sep 18, 2022
3f51d96
clean code
ZLBer Sep 18, 2022
2d072f2
add more component inject
ZLBer Sep 18, 2022
889fc91
fix ci
ZLBer Sep 18, 2022
66a2b7f
Merge branch 'main' into secret_ref_key
seeflood Sep 18, 2022
33d9aa8
fix ut
ZLBer Sep 20, 2022
4db6cf9
Merge branch 'main' into secret_ref_key
seeflood Sep 23, 2022
0e929d2
Merge branch 'main' into secret_ref_key
seeflood Sep 26, 2022
743aa65
Merge branch 'main' into secret_ref_key
seeflood Sep 27, 2022
15d8560
rename
ZLBer Sep 28, 2022
2ac913d
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 28, 2022
191e2d0
Merge branch 'main' into secret_ref_key
seeflood Sep 29, 2022
3fa775c
clean code
ZLBer Sep 29, 2022
c54e548
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 29, 2022
7fe729d
clean code
ZLBer Sep 29, 2022
06b5a57
clean code
ZLBer Sep 29, 2022
306f770
fix review
ZLBer Oct 1, 2022
2324e92
Merge branch 'main' into secret_ref_key
seeflood Oct 8, 2022
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
9 changes: 7 additions & 2 deletions components/ref/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package ref

//Config is ref json config
type Config struct {
SecretRef []*Item `json:"secret_ref"`
SecretRef []*SecretItem `json:"secret_ref"`
}

type Item struct {
type SecretItem struct {
//secret component name, such as : local.file
StoreName string `json:"store_name"`
// key in the secret component
Expand All @@ -31,3 +31,8 @@ type Item struct {
// key need to inject into metadata
InjectAs string `json:"inject_as"`
}

type ComponentRefConfig struct {
SecretRef []string `json:"secret_ref"`
ConfigRef []string `json:"config_ref"`
}
18 changes: 18 additions & 0 deletions configs/config_ref_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
"hello": "greeting"
}
},
"config_store": {
"config_demo": {
"type": "etcd",
"address": [
"127.0.0.1:2379"
],
"timeout": "10"
}
},
"secret_store": {
"local.file": {
"type": "local.file",
Expand Down Expand Up @@ -59,6 +68,15 @@
"app": {
"app_id": "app1",
"grpc_callback_port": 9999
},
"component_ref": {
"config_store": [
"config_demo"
],
"secret_store": [
"local.file",
"local.env"
]
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package runtime

import (
"encoding/json"
"mosn.io/layotto/components/ref"

"mosn.io/layotto/components/oss"

Expand Down Expand Up @@ -60,6 +61,7 @@ type MosnRuntimeConfig struct {
// <component type,component name,config>
// e.g. <"super_pubsub","etcd",config>
CustomComponent map[string]map[string]custom.Config `json:"custom_component,omitempty"`
ComponentRef ref.ComponentRefConfig `json:"component_ref"`
Extends map[string]json.RawMessage `json:"extends,omitempty"` // extend config
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/runtime/ref/contianer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ type RefContainer struct {
ConfigRef map[string]configstores.Store
}

// NewRefContainer return a new container
func NewRefContainer() *RefContainer {
return &RefContainer{
SecretRef: make(map[string]secretstores.SecretStore),
ConfigRef: make(map[string]configstores.Store),
}
}

func (r *RefContainer) GetSecretStore(key string) secretstores.SecretStore {
func (r *RefContainer) getSecretStore(key string) secretstores.SecretStore {
return r.SecretRef[key]
}

func (r *RefContainer) GetConfigStore(key string) configstores.Store {
func (r *RefContainer) getConfigStore(key string) configstores.Store {
return r.ConfigRef[key]
}
4 changes: 2 additions & 2 deletions pkg/runtime/ref/contianer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRefContainer(t *testing.T) {
container.SecretRef["fake"] = ss
cf := &mock.MockStore{}
container.ConfigRef["mock"] = cf
assert.Equal(t, ss, container.GetSecretStore("fake"))
assert.Equal(t, cf, container.GetConfigStore("mock"))
assert.Equal(t, ss, container.getSecretStore("fake"))
assert.Equal(t, cf, container.getConfigStore("mock"))

}
47 changes: 44 additions & 3 deletions pkg/runtime/ref/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,45 @@
package ref

import (
"fmt"
"github.com/dapr/components-contrib/secretstores"
"mosn.io/layotto/components/configstores"

"mosn.io/layotto/components/ref"
)

type DefaultInjector struct {
Container RefContainer
//limit ref component
ComponentLimit map[string]struct{}
Container RefContainer
}

var defaultInjector *DefaultInjector

// NewDefaultInjector return a single Inject
func NewDefaultInjector(secretStores map[string]secretstores.SecretStore, configStores map[string]configstores.Store, config ref.ComponentRefConfig) *DefaultInjector {
injector := &DefaultInjector{
Container: RefContainer{
SecretRef: secretStores,
ConfigRef: configStores,
},
}
for _, c := range config.ConfigRef {
injector.ComponentLimit[c] = struct{}{}
}
for _, c := range config.SecretRef {
injector.ComponentLimit[c] = struct{}{}
}
defaultInjector = injector
return injector
}
func GetDefaultInjector() *DefaultInjector {
return defaultInjector
}

//InjectSecretRef inject secret to metaData
// TODO: permission control
func (i *DefaultInjector) InjectSecretRef(items []*ref.Item, metaData map[string]string) (map[string]string, error) {
func (i *DefaultInjector) InjectSecretRef(items []*ref.SecretItem, metaData map[string]string) (map[string]string, error) {
if metaData == nil {
metaData = make(map[string]string)
}
Expand All @@ -38,7 +65,7 @@ func (i *DefaultInjector) InjectSecretRef(items []*ref.Item, metaData map[string

meta := make(map[string]string)
for _, item := range items {
store := i.Container.GetSecretStore(item.StoreName)
store := i.Container.getSecretStore(item.StoreName)
secret, err := store.GetSecret(secretstores.GetSecretRequest{
Name: item.Key,
})
Expand All @@ -62,3 +89,17 @@ func (i *DefaultInjector) InjectSecretRef(items []*ref.Item, metaData map[string
}
return metaData, nil
}

func (i *DefaultInjector) InjectConfigRef(key string) (configstores.Store, error) {
if _, ok := i.ComponentLimit[key]; ok {
return i.Container.getConfigStore(key), nil
}
return nil, fmt.Errorf("do not allow get config store %s", key)
}

func (i *DefaultInjector) InjectPubSubRef(key string) (secretstores.SecretStore, error) {
if _, ok := i.ComponentLimit[key]; ok {
return i.Container.getSecretStore(key), nil
}
return nil, fmt.Errorf("do not allow get secret store %s", key)
}
6 changes: 3 additions & 3 deletions pkg/runtime/ref/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ func TestInject(t *testing.T) {
injector := DefaultInjector{Container: *container}
meta := make(map[string]string)

var items []*ref.Item
var items []*ref.SecretItem
secretRef, err := injector.InjectSecretRef(nil, meta)
assert.Nil(t, err)
assert.Equal(t, len(secretRef), 0)
secretRef, err = injector.InjectSecretRef(items, nil)
assert.Nil(t, err)
assert.Equal(t, len(secretRef), 0)
items = append(items, &ref.Item{
items = append(items, &ref.SecretItem{
StoreName: "fake_secret_store",
Key: "good-key",
SubKey: "good-key",
InjectAs: "ref-key",
})
items = append(items, &ref.Item{
items = append(items, &ref.SecretItem{
StoreName: "fake_secret_store",
Key: "good-key",
SubKey: "good-key",
Expand Down
7 changes: 1 addition & 6 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,7 @@ func DefaultInitRuntimeStage(o *runtimeOptions, m *MosnRuntime) error {
if err := m.initConfigStores(o.services.configStores...); err != nil {
return err
}
m.Injector = &ref.DefaultInjector{
Container: ref.RefContainer{
SecretRef: m.secretStores,
ConfigRef: m.configStores,
},
}
m.Injector = ref.NewDefaultInjector(m.secretStores, m.configStores, m.runtimeConfig.ComponentRef)
if err := m.initCustomComponents(o.services.custom); err != nil {
return err
}
Expand Down