Skip to content

Commit

Permalink
add DMI
Browse files Browse the repository at this point in the history
Signed-off-by: chenlin.liu <[email protected]>
  • Loading branch information
cl2017 committed Sep 28, 2022
1 parent bbb0f62 commit f6404b5
Show file tree
Hide file tree
Showing 40 changed files with 10,563 additions and 168 deletions.
150 changes: 150 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
Copyright 2022 The KubeEdge 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 (
"errors"
"io/ioutil"
"os"
"strings"

"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
"k8s.io/klog/v2"

"github.com/kubeedge/mappers-go/pkg/common"
)

// ErrConfigCert error of certification configuration.
var ErrConfigCert = errors.New("Both certification and private key must be provided")

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"`
}

type Common struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
APIVersion string `yaml:"api_version"`
Protocol string `yaml:"protocol"`
Address string `yaml:"address"`
EdgeCoreSock string `yaml:"edgecore_sock"`
}

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

// Parse the configuration file. If failed, return error.
func (c *Config) Parse() error {
var level klog.Level
var loglevel string
var configFile string

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 {
return err
}
if err = yaml.Unmarshal(cf, c); err != nil {
return err
}
if err = level.Set(loglevel); err != nil {
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 {
if !os.IsNotExist(err) {
return err
}
c.DevInit.Configmap = strings.TrimSpace(os.Getenv("DEVICE_PROFILE"))
} else {
c.DevInit.Configmap = string(readFile)
}
if strings.TrimSpace(c.DevInit.Configmap) == "" {
return errors.New("can not parse configmap")
}
case common.DevInitModeRegister:
case "": // if mode is nil, use meta server mode
c.DevInit.Mode = common.DevInitModeMetaServer
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
}
23 changes: 23 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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:
name: mapper-test
version: v1.0.0
api_version: v1.0.0
protocol: modbus
address: 127.0.0.1
edgecore_sock: /tmp/edgecore.sock
dev_init:
mode: register #register/metaserver/configmap
configmap: /opt/kubeedge/deviceProfile.json
metaserver:
addr: http://10.32.209.110:10550
namespace: dec-public
18 changes: 18 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParse(t *testing.T) {
config := Config{}
if err := config.Parse(); err != nil {
t.Log(err)
t.FailNow()
}

assert.Equal(t, "tcp://127.0.0.1:1883", config.Mqtt.ServerAddress)
assert.Equal(t, "/opt/kubeedge/deviceProfile.json", config.DevInit.Configmap)
}
94 changes: 81 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,39 +1,102 @@
module github.com/kubeedge/mappers-go

go 1.14
go 1.17

require (
github.com/beevik/etree v1.1.0
github.com/currantlabs/ble v0.0.0-20171229162446-c1d21c164cf8
github.com/eclipse/paho.mqtt.golang v1.3.0
github.com/go-resty/resty/v2 v2.7.0
github.com/goburrow/modbus v0.1.0
github.com/goburrow/serial v0.1.0
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/gopcua/opcua v0.1.13
github.com/gorilla/mux v1.8.0 // indirect
github.com/kubeedge/kubeedge v1.5.0
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mgutz/logxi v0.0.0-20161027140823-aebf8a7d67ab // indirect
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/pkg/errors v0.9.1
github.com/sailorvii/goav v0.1.4
github.com/sailorvii/modbus v0.1.2
github.com/spf13/cobra v1.0.0
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
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-20201202161906-c7110b5ffcbb
google.golang.org/protobuf v1.25.0 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
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.19.3
k8s.io/api v0.24.1
k8s.io/apimachinery v0.24.1
k8s.io/klog v1.0.0
k8s.io/klog/v2 v2.4.0
k8s.io/klog/v2 v2.60.1
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
)

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
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
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
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mgutz/logxi v0.0.0-20161027140823-aebf8a7d67ab // indirect
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/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/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
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
)

replace (
github.com/kubeedge/beehive v0.0.0 => github.com/kubeedge/beehive v0.0.0-20201125122335-cd19bca6e436
github.com/kubeedge/beehive v0.0.0 => github.com/kubeedge/beehive v1.7.0
github.com/kubeedge/viaduct v0.0.0 => github.com/kubeedge/viaduct v0.0.0-20201130063818-e33931917980
k8s.io/api v0.0.0 => k8s.io/api v0.19.3
k8s.io/apiextensions-apiserver v0.0.0 => k8s.io/apiextensions-apiserver v0.19.3
Expand All @@ -45,6 +108,8 @@ replace (
k8s.io/cluster-bootstrap v0.0.0 => k8s.io/cluster-bootstrap v0.19.3
k8s.io/code-generator v0.0.0 => k8s.io/code-generator v0.19.3
k8s.io/component-base v0.0.0 => k8s.io/component-base v0.19.3
k8s.io/component-helpers v0.0.0 => k8s.io/component-helpers v0.22.6
k8s.io/controller-manager v0.0.0 => k8s.io/controller-manager v0.22.6
k8s.io/cri-api v0.0.0 => k8s.io/cri-api v0.19.3
k8s.io/csi-api v0.0.0 => k8s.io/csi-api v0.19.3
k8s.io/csi-translation-lib v0.0.0 => k8s.io/csi-translation-lib v0.19.3
Expand All @@ -60,8 +125,11 @@ replace (
k8s.io/kubelet v0.0.0 => k8s.io/kubelet v0.19.3
k8s.io/legacy-cloud-providers v0.0.0 => k8s.io/legacy-cloud-providers v0.19.3
k8s.io/metrics v0.0.0 => k8s.io/metrics v0.19.3
k8s.io/mount-utils v0.0.0 => k8s.io/mount-utils v0.22.6
k8s.io/node-api v0.0.0 => k8s.io/node-api v0.19.3
k8s.io/pod-security-admission v0.0.0 => k8s.io/pod-security-admission v0.22.6
k8s.io/repo-infra v0.0.0 => k8s.io/repo-infra v0.19.3
k8s.io/sample-apiserver v0.0.0 => k8s.io/sample-apiserver v0.19.3
k8s.io/utils v0.0.0 => k8s.io/utils v0.19.3
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.0 => sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.27
)
Loading

0 comments on commit f6404b5

Please sign in to comment.