Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"
"sync"

"github.com/opensergo/opensergo-control-plane/pkg/config"

"github.com/opensergo/opensergo-control-plane/pkg/controller"
"github.com/opensergo/opensergo-control-plane/pkg/model"
trpb "github.com/opensergo/opensergo-control-plane/pkg/proto/transport/v1"
Expand All @@ -35,15 +37,20 @@ type ControlPlane struct {
mux sync.RWMutex
}

func NewControlPlane() (*ControlPlane, error) {
func NewControlPlane(opts ...config.Option) (*ControlPlane, error) {
c, err := config.LoadConfig(opts...)
if err != nil {
return nil, err
}

cp := &ControlPlane{}

operator, err := controller.NewKubernetesOperator(cp.sendMessage)
if err != nil {
return nil, err
}

cp.server = transport.NewServer(uint32(10246), []model.SubscribeRequestHandler{cp.handleSubscribeRequest})
cp.server = transport.NewServer(c.Port, []model.SubscribeRequestHandler{cp.handleSubscribeRequest})
cp.operator = operator

hostname, herr := os.Hostname()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.14

require (
github.com/alibaba/sentinel-golang v1.0.3
github.com/cncf/xds/go v0.0.0-20220314180256-7f1daf1720fc
github.com/envoyproxy/go-control-plane v0.10.3-0.20221109183938-2935a23e638f
github.com/envoyproxy/protoc-gen-validate v0.6.7
github.com/go-logr/logr v0.4.0
Expand All @@ -18,6 +17,7 @@ require (
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.28.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.21.4
k8s.io/client-go v0.21.4
sigs.k8s.io/controller-runtime v0.9.7
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,6 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
Expand Down
118 changes: 118 additions & 0 deletions pkg/config/config_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2022, OpenSergo Authors
//
// Licensed 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 config

import (
"flag"
"io/ioutil"
"log"
"os"
"strconv"

"github.com/opensergo/opensergo-control-plane/pkg/util"
"gopkg.in/yaml.v2"
)

// LoadConfig for start OpenSergo Server.
// Priority of loading config: LoadOption > SystemEnv > ConfigFile > DefaultConfig.
// 1. NewDefaultConfig() to get the default config.
// 2. Get ConfPath and override config from config file by overrideFromYml() .
// 3. Override config from SystemEnv by overrideFromSystemEnv().
// 4. Override config from Option by overrideFromOpts().
func LoadConfig(opts ...Option) (*OpenSergoConfig, error) {
// default config
mergedConfig := NewDefaultConfig()

// update initial config from File from Opts
tmpConfig := NewDefaultConfig()
tmpConfig.overrideFromOpts(opts...)
mergedConfig.ConfPath = tmpConfig.ConfPath
if err := mergedConfig.overrideFromYml(mergedConfig.ConfPath); err != nil {
log.Println("read config from ConfPath[{}] error", tmpConfig.ConfPath)
}

// update initial config from System Env
if err := mergedConfig.overrideFromSystemEnv(); err != nil {
return nil, err
}

// update initial config from func args
mergedConfig.overrideFromOpts(opts...)

return mergedConfig, nil
}

func (c *OpenSergoConfig) overrideFromOpts(opts ...Option) {
if len(opts) > 0 {
for _, opt := range opts {
opt(c)
}
}
}

func (c *OpenSergoConfig) InitOptsFromCommand() (opts []Option) {
flag.StringVar(&c.ConfPath, "c", DefaultConfPath, "file path of config")
flag.UintVar(&c.Port, "p", DefaultPort, "endpoint port of OpenSergo Control Plane.[ SystemEnvName: "+EnvKeyEndpointPort+" ][ YamlPath:endpointPort ]")
flag.Parse()
if c.ConfPath != DefaultConfPath {
opts = append(opts, WithConfPath(c.ConfPath))
}
if c.Port != DefaultPort {
opts = append(opts, WithEndpointPort(c.Port))
}
return opts
}

func (c *OpenSergoConfig) overrideFromYml(confPath string) error {
_, err := os.Stat(confPath)
if err != nil && !os.IsExist(err) {
return err
}
content, err := ioutil.ReadFile(confPath)
if err != nil {
return err
}
source := NewDefaultConfig()
source.ConfPath = confPath
err = yaml.Unmarshal(content, source)
if err != nil {
return err
}

c.overrideFromOpenSergoConfig(source)
return nil
}

func (c *OpenSergoConfig) overrideFromOpenSergoConfig(source *OpenSergoConfig) error {
c.ConfPath = source.ConfPath
c.Port = source.Port
return nil
}

const (
EnvKeyEndpointPort = "OPENSERGO_ENDPOINT_PORT"
)

func (c *OpenSergoConfig) overrideFromSystemEnv() error {
if portEnv := os.Getenv(EnvKeyEndpointPort); !util.IsBlank(portEnv) {
port, err := strconv.ParseUint(portEnv, 10, 32)
if err != nil {
return err
} else {
c.Port = uint(port)
}
}
return nil
}
32 changes: 32 additions & 0 deletions pkg/config/config_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022, OpenSergo Authors
//
// Licensed 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 config

const (
DefaultConfPath string = "./config"
DefaultPort uint = 10246
)

type OpenSergoConfig struct {
ConfPath string
Port uint `yaml:"endpointPort" json:"endpointPort"`
}

func NewDefaultConfig() *OpenSergoConfig {
return &OpenSergoConfig{
ConfPath: DefaultConfPath,
Port: DefaultPort,
}
}
31 changes: 31 additions & 0 deletions pkg/config/control_plane_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022, OpenSergo Authors
//
// Licensed 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 config

type Option func(*OpenSergoConfig)

func WithConfPath(path string) Option {

return func(c *OpenSergoConfig) {
c.ConfPath = path
}
}

func WithEndpointPort(port uint) Option {

return func(c *OpenSergoConfig) {
c.Port = port
}
}
6 changes: 5 additions & 1 deletion pkg/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import (
"log"

"github.com/opensergo/opensergo-control-plane"
"github.com/opensergo/opensergo-control-plane/pkg/config"
)

func main() {
cp, err := opensergo.NewControlPlane()
c := config.NewDefaultConfig()
opts := c.InitOptsFromCommand()

cp, err := opensergo.NewControlPlane(opts...)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type Server struct {

connectionManager *ConnectionManager

port uint32
port uint
started *atomic.Bool
}

func NewServer(port uint32, subscribeHandlers []model.SubscribeRequestHandler) *Server {
func NewServer(port uint, subscribeHandlers []model.SubscribeRequestHandler) *Server {
connectionManager := NewConnectionManager()
return &Server{
transportServer: newTransportServer(connectionManager, subscribeHandlers),
Expand Down
22 changes: 22 additions & 0 deletions pkg/util/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2022, OpenSergo Authors
//
// Licensed 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 util

import "strings"

// IsBlank checks whether the given string is blank.
func IsBlank(s string) bool {
return strings.TrimSpace(s) == ""
}