Skip to content

Commit b171373

Browse files
committed
support configuration
Signed-off-by: Jiangnan Jia <[email protected]>
1 parent c8e75a4 commit b171373

File tree

9 files changed

+220
-7
lines changed

9 files changed

+220
-7
lines changed

control_plane.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"os"
2020
"sync"
2121

22+
"github.com/opensergo/opensergo-control-plane/pkg/config"
23+
2224
"github.com/opensergo/opensergo-control-plane/pkg/controller"
2325
"github.com/opensergo/opensergo-control-plane/pkg/model"
2426
trpb "github.com/opensergo/opensergo-control-plane/pkg/proto/transport/v1"
@@ -35,15 +37,20 @@ type ControlPlane struct {
3537
mux sync.RWMutex
3638
}
3739

38-
func NewControlPlane() (*ControlPlane, error) {
40+
func NewControlPlane(opts ...config.Option) (*ControlPlane, error) {
41+
c, err := config.LoadConfig(opts...)
42+
if err != nil {
43+
return nil, err
44+
}
45+
3946
cp := &ControlPlane{}
4047

4148
operator, err := controller.NewKubernetesOperator(cp.sendMessage)
4249
if err != nil {
4350
return nil, err
4451
}
4552

46-
cp.server = transport.NewServer(uint32(10246), []model.SubscribeRequestHandler{cp.handleSubscribeRequest})
53+
cp.server = transport.NewServer(c.Port, []model.SubscribeRequestHandler{cp.handleSubscribeRequest})
4754
cp.operator = operator
4855

4956
hostname, herr := os.Hostname()

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.14
44

55
require (
66
github.com/alibaba/sentinel-golang v1.0.3
7-
github.com/cncf/xds/go v0.0.0-20220314180256-7f1daf1720fc
87
github.com/envoyproxy/go-control-plane v0.10.3-0.20221109183938-2935a23e638f
98
github.com/envoyproxy/protoc-gen-validate v0.6.7
109
github.com/go-logr/logr v0.4.0
@@ -18,6 +17,7 @@ require (
1817
google.golang.org/grpc v1.51.0
1918
google.golang.org/protobuf v1.28.1
2019
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
20+
gopkg.in/yaml.v2 v2.4.0
2121
k8s.io/apimachinery v0.21.4
2222
k8s.io/client-go v0.21.4
2323
sigs.k8s.io/controller-runtime v0.9.7

go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,6 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
934934
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
935935
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
936936
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
937-
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
938937
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
939938
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
940939
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

pkg/config/config_handler.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2022, OpenSergo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package config
16+
17+
import (
18+
"flag"
19+
"io/ioutil"
20+
"log"
21+
"os"
22+
"strconv"
23+
24+
"github.com/opensergo/opensergo-control-plane/pkg/util"
25+
"gopkg.in/yaml.v2"
26+
)
27+
28+
// LoadConfig for start OpenSergo Server.
29+
// Priority of loading config: LoadOption > SystemEnv > ConfigFile > DefaultConfig.
30+
// 1. NewDefaultConfig() to get the default config.
31+
// 2. Get ConfPath and override config from config file by overrideFromYml() .
32+
// 3. Override config from SystemEnv by overrideFromSystemEnv().
33+
// 4. Override config from Option by overrideFromOpts().
34+
func LoadConfig(opts ...Option) (*OpenSergoConfig, error) {
35+
// default config
36+
mergedConfig := NewDefaultConfig()
37+
38+
// update initial config from File from Opts
39+
tmpConfig := NewDefaultConfig()
40+
tmpConfig.overrideFromOpts(opts...)
41+
mergedConfig.ConfPath = tmpConfig.ConfPath
42+
if err := mergedConfig.overrideFromYml(mergedConfig.ConfPath); err != nil {
43+
log.Println("read config from ConfPath[{}] error", tmpConfig.ConfPath)
44+
}
45+
46+
// update initial config from System Env
47+
if err := mergedConfig.overrideFromSystemEnv(); err != nil {
48+
return nil, err
49+
}
50+
51+
// update initial config from func args
52+
mergedConfig.overrideFromOpts(opts...)
53+
54+
return mergedConfig, nil
55+
}
56+
57+
func (c *OpenSergoConfig) overrideFromOpts(opts ...Option) {
58+
if len(opts) > 0 {
59+
for _, opt := range opts {
60+
opt(c)
61+
}
62+
}
63+
}
64+
65+
func (c *OpenSergoConfig) InitOptsFromCommand() (opts []Option) {
66+
flag.StringVar(&c.ConfPath, "c", DefaultConfPath, "file path of config")
67+
flag.UintVar(&c.Port, "p", DefaultPort, "endpoint port of OpenSergo Control Plane.[ SystemEnvName: "+EnvKeyEndpointPort+" ][ YamlPath:endpointPort ]")
68+
flag.Parse()
69+
if c.ConfPath != DefaultConfPath {
70+
opts = append(opts, WithConfPath(c.ConfPath))
71+
}
72+
if c.Port != DefaultPort {
73+
opts = append(opts, WithEndpointPort(c.Port))
74+
}
75+
return opts
76+
}
77+
78+
func (c *OpenSergoConfig) overrideFromYml(confPath string) error {
79+
_, err := os.Stat(confPath)
80+
if err != nil && !os.IsExist(err) {
81+
return err
82+
}
83+
content, err := ioutil.ReadFile(confPath)
84+
if err != nil {
85+
return err
86+
}
87+
source := NewDefaultConfig()
88+
source.ConfPath = confPath
89+
err = yaml.Unmarshal(content, source)
90+
if err != nil {
91+
return err
92+
}
93+
94+
c.overrideFromOpenSergoConfig(source)
95+
return nil
96+
}
97+
98+
func (c *OpenSergoConfig) overrideFromOpenSergoConfig(source *OpenSergoConfig) error {
99+
c.ConfPath = source.ConfPath
100+
c.Port = source.Port
101+
return nil
102+
}
103+
104+
const (
105+
EnvKeyEndpointPort = "OPENSERGO_ENDPOINT_PORT"
106+
)
107+
108+
func (c *OpenSergoConfig) overrideFromSystemEnv() error {
109+
if portEnv := os.Getenv(EnvKeyEndpointPort); !util.IsBlank(portEnv) {
110+
port, err := strconv.ParseUint(portEnv, 10, 32)
111+
if err != nil {
112+
return err
113+
} else {
114+
c.Port = uint(port)
115+
}
116+
}
117+
return nil
118+
}

pkg/config/config_model.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022, OpenSergo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package config
16+
17+
const (
18+
DefaultConfPath string = "./config"
19+
DefaultPort uint = 10246
20+
)
21+
22+
type OpenSergoConfig struct {
23+
ConfPath string
24+
Port uint `yaml:"endpointPort" json:"endpointPort"`
25+
}
26+
27+
func NewDefaultConfig() *OpenSergoConfig {
28+
return &OpenSergoConfig{
29+
ConfPath: DefaultConfPath,
30+
Port: DefaultPort,
31+
}
32+
}

pkg/config/control_plane_option.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2022, OpenSergo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package config
16+
17+
type Option func(*OpenSergoConfig)
18+
19+
func WithConfPath(path string) Option {
20+
21+
return func(c *OpenSergoConfig) {
22+
c.ConfPath = path
23+
}
24+
}
25+
26+
func WithEndpointPort(port uint) Option {
27+
28+
return func(c *OpenSergoConfig) {
29+
c.Port = port
30+
}
31+
}

pkg/main/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ import (
1818
"log"
1919

2020
"github.com/opensergo/opensergo-control-plane"
21+
"github.com/opensergo/opensergo-control-plane/pkg/config"
2122
)
2223

2324
func main() {
24-
cp, err := opensergo.NewControlPlane()
25+
c := config.NewDefaultConfig()
26+
opts := c.InitOptsFromCommand()
27+
28+
cp, err := opensergo.NewControlPlane(opts...)
2529
if err != nil {
2630
log.Fatal(err)
2731
}

pkg/transport/grpc/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ type Server struct {
3838

3939
connectionManager *ConnectionManager
4040

41-
port uint32
41+
port uint
4242
started *atomic.Bool
4343
}
4444

45-
func NewServer(port uint32, subscribeHandlers []model.SubscribeRequestHandler) *Server {
45+
func NewServer(port uint, subscribeHandlers []model.SubscribeRequestHandler) *Server {
4646
connectionManager := NewConnectionManager()
4747
return &Server{
4848
transportServer: newTransportServer(connectionManager, subscribeHandlers),

pkg/util/string.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2022, OpenSergo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package util
16+
17+
import "strings"
18+
19+
// IsBlank checks whether the given string is blank.
20+
func IsBlank(s string) bool {
21+
return strings.TrimSpace(s) == ""
22+
}

0 commit comments

Comments
 (0)