Skip to content

Commit

Permalink
optimized code
Browse files Browse the repository at this point in the history
Signed-off-by: cl2017 <[email protected]>
  • Loading branch information
cl2017 committed Sep 29, 2022
1 parent f6404b5 commit 68a145f
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 6,158 deletions.
52 changes: 3 additions & 49 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,11 @@ var defaultConfigFile = "./config.yaml"

// Config is the common mapper configuration.
type Config struct {
Mqtt Mqtt `yaml:"mqtt,omitempty"`
HttpServer HTTPServer `yaml:"http_server"`
GrpcServer GRPCServer `yaml:"grpc_server"`
Common Common `yaml:"common"`
DevInit DevInit `yaml:"dev_init"`
}

// Mqtt is the Mqtt configuration.
type Mqtt struct {
ServerAddress string `yaml:"server,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
Cert string `yaml:"certification,omitempty"`
PrivateKey string `yaml:"privatekey,omitempty"`
}

// MetaServer is the MetaServer configuration.
type MetaServer struct {
Addr string `yaml:"addr"`
Namespace string `json:"namespace"`
}

type HTTPServer struct {
Host string `yaml:"host"`
}

type GRPCServer struct {
SocketPath string `yaml:"socket_path"`
}
Expand All @@ -76,9 +55,8 @@ type Common struct {
}

type DevInit struct {
Mode string `yaml:"mode"`
Configmap string `yaml:"configmap"`
MetaServer MetaServer `yaml:"metaserver"`
Mode string `yaml:"mode"`
Configmap string `yaml:"configmap"`
}

// Parse the configuration file. If failed, return error.
Expand All @@ -89,7 +67,6 @@ func (c *Config) Parse() error {

pflag.StringVar(&loglevel, "v", "1", "log level")
pflag.StringVar(&configFile, "config-file", defaultConfigFile, "Config file name")
pflag.StringVar(&c.DevInit.MetaServer.Addr, "metaserver-addr", "http://127.0.0.1:10550", "edgecore meta server addr")

cf, err := ioutil.ReadFile(configFile)
if err != nil {
Expand All @@ -102,10 +79,6 @@ func (c *Config) Parse() error {
return err
}

if err = c.parseFlags(); err != nil {
return err
}

switch c.DevInit.Mode {
case common.DevInitModeConfigmap:
if readFile, err := ioutil.ReadFile(c.DevInit.Configmap); err != nil {
Expand All @@ -121,30 +94,11 @@ func (c *Config) Parse() error {
}
case common.DevInitModeRegister:
case "": // if mode is nil, use meta server mode
c.DevInit.Mode = common.DevInitModeMetaServer
c.DevInit.Mode = common.DevInitModeRegister
fallthrough
case common.DevInitModeMetaServer:
if c.DevInit.MetaServer.Namespace == "" {
c.DevInit.MetaServer.Namespace = "default"
}
default:
return errors.New("unsupported dev init mode " + c.DevInit.Mode)
}

return nil
}

// parseFlags parse flags. Certification and Private key must be provided at the same time.
func (c *Config) parseFlags() error {
pflag.StringVar(&c.Mqtt.ServerAddress, "mqtt-address", c.Mqtt.ServerAddress, "MQTT broker address")
pflag.StringVar(&c.Mqtt.Username, "mqtt-username", c.Mqtt.Username, "username")
pflag.StringVar(&c.Mqtt.Password, "mqtt-password", c.Mqtt.Password, "password")
pflag.StringVar(&c.Mqtt.Cert, "mqtt-certification", c.Mqtt.Cert, "certification file path")
pflag.StringVar(&c.Mqtt.PrivateKey, "mqtt-privatekey", c.Mqtt.PrivateKey, "private key file path")
pflag.Parse()
if (c.Mqtt.Cert != "" && c.Mqtt.PrivateKey == "") ||
(c.Mqtt.Cert == "" && c.Mqtt.PrivateKey != "") {
return ErrConfigCert
}
return nil
}
13 changes: 1 addition & 12 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
mqtt:
server: tcp://127.0.0.1:1883
# server: tcp://192.168.2.69:1883
username: ""
password: ""
certification: ""
http_server:
host: 127.0.0.1:10555
grpc_server:
socket_path: /tmp/a.sock
common:
Expand All @@ -16,8 +8,5 @@ common:
address: 127.0.0.1
edgecore_sock: /tmp/edgecore.sock
dev_init:
mode: register #register/metaserver/configmap
mode: register #register/configmap
configmap: /opt/kubeedge/deviceProfile.json
metaserver:
addr: http://10.32.209.110:10550
namespace: dec-public
3 changes: 1 addition & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ func TestParse(t *testing.T) {
t.FailNow()
}

assert.Equal(t, "tcp://127.0.0.1:1883", config.Mqtt.ServerAddress)
assert.Equal(t, "/opt/kubeedge/deviceProfile.json", config.DevInit.Configmap)
}
}
26 changes: 8 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ require (
github.com/goburrow/serial v0.1.0
github.com/gopcua/opcua v0.1.13
github.com/gorilla/mux v1.8.0
github.com/kubeedge/kubeedge v1.10.0
github.com/onsi/ginkgo v1.15.0
github.com/onsi/gomega v1.10.5
github.com/kubeedge/kubeedge v1.12.0-beta.0
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.19.0
github.com/pkg/errors v0.9.1
github.com/sailorvii/goav v0.1.4
github.com/sailorvii/modbus v0.1.2
Expand All @@ -22,9 +22,8 @@ require (
github.com/stretchr/testify v1.7.0
github.com/tbrandon/mbserver v0.0.0-20210320091329-a1f8ae952881
github.com/use-go/onvif v0.0.1
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.24.1
k8s.io/apimachinery v0.24.1
Expand All @@ -36,9 +35,7 @@ require (
require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/cyphar/filepath-securejoin v0.2.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/elgs/gostrgen v0.0.0-20161222160715-9d61ae07eeae // indirect
github.com/emicklei/go-restful v2.9.6+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
Expand All @@ -48,18 +45,15 @@ require (
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kubeedge/beehive v1.7.0 // indirect
github.com/kubeedge/viaduct v0.0.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
Expand All @@ -69,27 +63,23 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.4 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/runc v1.0.3 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo/v2 v2.1.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/apiserver v0.22.6 // indirect
k8s.io/client-go v0.24.1 // indirect
k8s.io/component-base v0.22.6 // indirect
k8s.io/component-helpers v0.22.6 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/kubernetes v1.22.6 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
Expand Down
Loading

0 comments on commit 68a145f

Please sign in to comment.