Skip to content
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

introduce ConfigPostProcessor extension #943

Merged
merged 8 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
45 changes: 45 additions & 0 deletions common/extension/config_post_processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package extension

import (
"github.com/apache/dubbo-go/config/interfaces"
)

var (
processors = make(map[string]interfaces.ConfigPostProcessor)
)

// SetConfigPostProcessor registers a ConfigPostProcessor with the given name.
func SetConfigPostProcessor(name string, processor interfaces.ConfigPostProcessor) {
processors[name] = processor
}

// GetConfigPostProcessor finds a ConfigPostProcessor by name.
func GetConfigPostProcessor(name string) interfaces.ConfigPostProcessor {
return processors[name]
}

// GetConfigPostProcessors returns all registered instances of ConfigPostProcessor.
func GetConfigPostProcessors() []interfaces.ConfigPostProcessor {
ret := make([]interfaces.ConfigPostProcessor, 0, len(processors))
for _, v := range processors {
ret = append(ret, v)
}
return ret
}
28 changes: 28 additions & 0 deletions config/interfaces/config_post_processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package interfaces

// ConfigPostProcessor is an extension to give users a chance to customize configs against ReferenceConfig and
// ServiceConfig during deployment time.
type ConfigPostProcessor interface {
// PostProcessReferenceConfig customizes ReferenceConfig's params
PostProcessReferenceConfig(params map[string]string)

// PostProcessServiceConfig customizes ServiceConfig's params
PostProcessServiceConfig(params map[string]string)
}
9 changes: 9 additions & 0 deletions config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func (c *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error

// Refer ...
func (c *ReferenceConfig) Refer(_ interface{}) {
c.postProcessConfig()

cfgURL := common.NewURLWithOptions(
common.WithPath(c.InterfaceName),
common.WithProtocol(c.Protocol),
Expand Down Expand Up @@ -248,3 +250,10 @@ func (c *ReferenceConfig) GenericLoad(id string) {
c.Refer(genericService)
c.Implement(genericService)
}

// postProcessConfig asks registered ConfigPostProcessor to post-process the current ReferenceConfig.
func (c *ReferenceConfig) postProcessConfig() {
for _, p := range extension.GetConfigPostProcessors() {
p.PostProcessReferenceConfig(c.Params)
}
}
8 changes: 8 additions & 0 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func getRandomPort(protocolConfigs []*ProtocolConfig) *list.List {

// Export exports the service
func (c *ServiceConfig) Export() error {
c.postProcessConfig()
// TODO: config center start here

// TODO: delay export
Expand Down Expand Up @@ -332,3 +333,10 @@ func (c *ServiceConfig) GetExportedUrls() []*common.URL {
}
return nil
}

// postProcessConfig asks registered ConfigPostProcessor to post-process the current ServiceConfig.
func (c *ServiceConfig) postProcessConfig() {
for _, p := range extension.GetConfigPostProcessors() {
p.PostProcessServiceConfig(c.Params)
}
}