-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from cl2017/dmi-chenlin-rebase
add implement dmi interface
- Loading branch information
Showing
40 changed files
with
4,540 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
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 { | ||
GrpcServer GRPCServer `yaml:"grpc_server"` | ||
Common Common `yaml:"common"` | ||
DevInit DevInit `yaml:"dev_init"` | ||
} | ||
|
||
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"` | ||
} | ||
|
||
// 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") | ||
|
||
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 | ||
} | ||
|
||
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.DevInitModeRegister | ||
fallthrough | ||
default: | ||
return errors.New("unsupported dev init mode " + c.DevInit.Mode) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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/configmap | ||
configmap: /opt/kubeedge/deviceProfile.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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, "/opt/kubeedge/deviceProfile.json", config.DevInit.Configmap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.