Skip to content

Commit

Permalink
fix: lint error
Browse files Browse the repository at this point in the history
Signed-off-by: soyowind0 <[email protected]>
  • Loading branch information
soyowind0 committed Oct 14, 2024
1 parent 5109221 commit 233e7ee
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 35 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ require (
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
google.golang.org/grpc v1.47.0
gopkg.in/yaml.v2 v2.4.0
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde
gorm.io/driver/sqlite v1.4.4
gorm.io/gorm v1.24.0
k8s.io/api v0.24.1
k8s.io/apimachinery v0.24.1
k8s.io/klog v1.0.0
Expand Down Expand Up @@ -81,7 +82,6 @@ require (
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
gorm.io/driver/sqlite v1.5.6
k8s.io/client-go v0.24.1 // indirect
k8s.io/component-base v0.22.6 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down Expand Up @@ -809,6 +810,7 @@ github.com/mattn/go-shellwords v1.0.11/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lL
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -1751,10 +1753,10 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg=
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/driver/sqlite v1.4.4 h1:gIufGoR0dQzjkyqDyYSCvsYR6fba1Gw5YKDqKeChxFc=
gorm.io/driver/sqlite v1.4.4/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
gorm.io/gorm v1.24.0 h1:j/CoiSm6xpRpmzbFJsQHYj+I8bGYWLXVHeYEyyKlF74=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
Expand Down
6 changes: 4 additions & 2 deletions mappers/windows-virtual-exec/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"os/signal"

"k8s.io/klog/v2"
klog "k8s.io/klog/v2"

"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/config"
"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/core/model"
Expand All @@ -26,7 +26,9 @@ func main() {
}

store.InitDB("internal.db")
store.DB.AutoMigrate(&model.Mission{})
if err := store.DB.AutoMigrate(&model.Mission{}); err != nil {
klog.Errorf("Failed to init db: %v", err)
}

mqtt.Client = &mqtt.MqttClient{
IP: c.Mqtt.ServerAddress,
Expand Down
4 changes: 1 addition & 3 deletions mappers/windows-virtual-exec/hack/make-rules/mapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,14 @@ function build() {
local arch=${os_arch[1]}
GOOS=${os} GOARCH=${arch} CGO_ENABLED=0 go build \
-ldflags "${flags} ${ext_flags}" \
-o "${CURR_DIR}/bin/${mapper}_${os}_${arch}" \
-o "${CURR_DIR}/bin/${mapper}_${os}_${arch}.exe" \
"${CURR_DIR}/cmd/main.go"

cp ${CURR_DIR}/bin/${mapper}_${os}_${arch} ${CURR_DIR}/bin/${mapper}
echo "...done"
}

function package() {
echo "docker package not support for windows virtual exec driver"
exit 1
}

function clean() {
Expand Down
4 changes: 2 additions & 2 deletions mappers/windows-virtual-exec/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"

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

// Config is the Exec mapper configuration.
Expand Down
2 changes: 1 addition & 1 deletion mappers/windows-virtual-exec/internal/dto/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type DeviceList struct {
}

type DeviceInfo struct {
Id string `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
Expand Down
12 changes: 6 additions & 6 deletions mappers/windows-virtual-exec/internal/missions/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path"

mq "github.com/eclipse/paho.mqtt.golang"
"k8s.io/klog/v2"
klog "k8s.io/klog/v2"

"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/core/mqtt"
"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/dto"
Expand Down Expand Up @@ -56,15 +56,15 @@ func onMembershipUpdateMessage(_ mq.Client, message mq.Message) {
klog.Info("Receive device list update: ", "nodeId: ", nodeId, " update: ", len(req.AddedDevices), " delete: ", len(req.RemovedDevices))

for _, device := range req.RemovedDevices {
RemoveMission(device.Id)
RemoveMission(device.ID)
}

for _, device := range req.RemovedDevices {
if _, ok := cache.Load(device.Id); ok {
klog.Info("Device already exists: ", device.Id)
if _, ok := cache.Load(device.ID); ok {
klog.Info("Device already exists: ", device.ID)
continue
}
klog.Info("Waiting twin update to create device: ", device.Id)
klog.Info("Waiting twin update to create device: ", device.ID)
}
}

Expand All @@ -84,7 +84,7 @@ func onMembershipListMessage(_ mq.Client, message mq.Message) {

klog.Info("Receive device list: ", "nodeId: ", nodeId, " count: ", len(req.Devices))
for _, device := range req.Devices {
_err := mqtt.Client.Publish(fmt.Sprintf(mqtt.TopicPubTwinInfoRequest, device.Id), mqtt.CreateEmptyMessage())
_err := mqtt.Client.Publish(fmt.Sprintf(mqtt.TopicPubTwinInfoRequest, device.ID), mqtt.CreateEmptyMessage())
if _err != nil {
klog.Error("Publish error: ", _err)
return
Expand Down
6 changes: 5 additions & 1 deletion mappers/windows-virtual-exec/internal/missions/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package missions
import (
"fmt"

"k8s.io/klog"

"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/core/mqtt"
)

func InitMissions(nodeName string) {
mqtt.Client.Publish(fmt.Sprintf(mqtt.TopicPubNodeDeviceListRequest, nodeName), mqtt.CreateEmptyMessage())
if err := mqtt.Client.Publish(fmt.Sprintf(mqtt.TopicPubNodeDeviceListRequest, nodeName), mqtt.CreateEmptyMessage()); err != nil {
klog.Errorf("Failed to init missions on %s", nodeName)
}
}
36 changes: 23 additions & 13 deletions mappers/windows-virtual-exec/internal/missions/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sync"

"k8s.io/klog/v2"
klog "k8s.io/klog/v2"

"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/core/model"
"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/core/mqtt"
"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/core/store"
"github.com/kubeedge/mappers-go/mappers/windows-virtual-exec/internal/utils/encode"
)

const (
StatusOK = "ok"
StatusError = "error"
StatusWaiting = "waiting"
StatusWorking = "working"
)

type Mission struct {
exec *Command
Config MissionConfig `json:"config"`
Expand Down Expand Up @@ -54,7 +62,7 @@ func NewMission(config MissionConfig) (client *Mission, err error) {
}

var mission model.Mission
if store.DB.Model(&mission).Where("unique_name = ?", config.UniqueName).Find(&mission).RowsAffected > 0 && mission.Status != "working" {
if store.DB.Model(&mission).Where("unique_name = ?", config.UniqueName).Find(&mission).RowsAffected > 0 && mission.Status != StatusWorking {
client.Config.UniqueName = mission.UniqueName
client.Config.Command = mission.Command
client.Config.FileContent = mission.FileContent
Expand All @@ -68,9 +76,9 @@ func NewMission(config MissionConfig) (client *Mission, err error) {

client.exec.Cmd = exec.Command("powershell", "-c", client.Config.Command)
client.exec.Cmd.Dir = client.Config.WorkingDirectory
client.Status = "waiting"
client.Status = StatusWaiting

if mission.Status == "working" {
if mission.Status == StatusWorking {
client.UpdateDB()
} else {
client.InsertDB()
Expand All @@ -92,7 +100,6 @@ func RemoveMission(id string) {
}

cache.Delete(id)

}

func (c *Mission) InsertDB() {
Expand Down Expand Up @@ -125,7 +132,7 @@ func (c *Mission) UpdateDB() {
}

func (c *Mission) Run() {
if c.Status == "ok" || c.Status == "error" || c.Status == "working" {
if c.Status == StatusOK || c.Status == StatusError || c.Status == StatusWorking {
klog.Info("Mission status is not waiting, skip with current status ", c.Status)
return
}
Expand All @@ -136,19 +143,22 @@ func (c *Mission) Run() {
klog.Info("Mission finished: ", c.Config.UniqueName, " result: ", c.Status)
}()

c.Status = "working"
c.Status = StatusWorking
c.ReportMissionStatus()
klog.Info("Mission start: ", c.Config.UniqueName, " status: ", c.Status, " output: ", c.Output)

// clean working directory in windows
dir := c.Config.WorkingDirectory
os.RemoveAll(dir)
os.MkdirAll(dir, os.ModePerm)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
klog.Errorf("Failed to make workdir %s: %v", dir, err)
return
}

file, err := os.Create(dir + "/" + c.Config.FileName)
file, err := os.Create(filepath.Join(dir, c.Config.FileName))
if err != nil {
klog.Error("Create file error: ", err)
c.Status = "error"
c.Status = StatusError
c.Output = err.Error()
return
}
Expand All @@ -157,20 +167,20 @@ func (c *Mission) Run() {

if err != nil {
klog.Error("Write file error: ", err)
c.Status = "error"
c.Status = StatusError
c.Output = err.Error()
return
}

err = c.exec.Exec()
if err != nil {
klog.Error("Exec error: ", err)
c.Status = "error"
c.Status = StatusError
c.Output = fmt.Sprintf("【msg】%s\n【err】%s\n", err.Error(), string(c.exec.StdErr))
return
}

c.Status = "ok"
c.Status = StatusOK
c.Output = string(c.exec.StdOut)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package encode
import (
"encoding/base64"

"k8s.io/klog/v2"
klog "k8s.io/klog/v2"
)

func DecodeBase64(encodedString string) string {
Expand Down

0 comments on commit 233e7ee

Please sign in to comment.