[agent-operator] - message pusher
This commit is contained in:
@@ -5,7 +5,6 @@ go 1.22.1
|
|||||||
require (
|
require (
|
||||||
go.uber.org/zap v1.27.0
|
go.uber.org/zap v1.27.0
|
||||||
golang.org/x/net v0.24.0
|
golang.org/x/net v0.24.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require go.uber.org/multierr v1.10.0 // indirect
|
require go.uber.org/multierr v1.10.0 // indirect
|
||||||
|
|||||||
@@ -12,7 +12,5 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
|||||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||||
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||||
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
80
agent-common/pusher/CmiiUpdateMessage.go
Normal file
80
agent-common/pusher/CmiiUpdateMessage.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package pusher
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"wdd.io/agent-common/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
var log = logger.Log
|
||||||
|
|
||||||
|
// CmiiUpdateMessage message_pusher/cmii/CmiiMessage.go
|
||||||
|
type CmiiUpdateMessage struct {
|
||||||
|
Namespace string
|
||||||
|
AppName string
|
||||||
|
FromTag string
|
||||||
|
ToTag string
|
||||||
|
Replicas string
|
||||||
|
DeployStatus bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Message struct { // TODO combine with server.message
|
||||||
|
ID string
|
||||||
|
Event string
|
||||||
|
Time int64
|
||||||
|
Topic string
|
||||||
|
Message string
|
||||||
|
Title string
|
||||||
|
Priority int
|
||||||
|
Tags []string
|
||||||
|
Click string
|
||||||
|
Icon string
|
||||||
|
Attachment *Attachment
|
||||||
|
|
||||||
|
// Additional fields
|
||||||
|
TopicURL string
|
||||||
|
SubscriptionID string
|
||||||
|
Raw string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CmiiUpdateMessage) SendMessage() (message Message) {
|
||||||
|
|
||||||
|
// 将结构体转换为JSON字符串
|
||||||
|
requestBytes, err := json.Marshal(c)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error encoding request body to JSON:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
url := "http://192.168.35.71:8080/cmii/update" // 替换为实际的API地址
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBytes))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error creating request:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加请求头
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// 发送请求并获取响应
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error sending request:", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
var m Message
|
||||||
|
err = json.Unmarshal(bodyBytes, &m)
|
||||||
|
if err != nil {
|
||||||
|
log.ErrorF("Error unmarshaling response body to JSON:", err)
|
||||||
|
return message
|
||||||
|
}
|
||||||
|
|
||||||
|
return m
|
||||||
|
}
|
||||||
16
agent-common/pusher/CmiiUpdateMessage_test.go
Normal file
16
agent-common/pusher/CmiiUpdateMessage_test.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package pusher
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestCmiiUpdateMessage_SendMessage(t *testing.T) {
|
||||||
|
c := &CmiiUpdateMessage{
|
||||||
|
Namespace: "dev",
|
||||||
|
AppName: "cmii-uav-gateway",
|
||||||
|
FromTag: "5.1.0",
|
||||||
|
ToTag: "5.5.0",
|
||||||
|
Replicas: "2",
|
||||||
|
DeployStatus: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.SendMessage()
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package cmii
|
|
||||||
|
|
||||||
// CmiiUpdateMessage message_pusher/cmii/CmiiMessage.go
|
|
||||||
type CmiiUpdateMessage struct {
|
|
||||||
Namespace string
|
|
||||||
AppName string
|
|
||||||
FromTag string
|
|
||||||
ToTag string
|
|
||||||
Replicas string
|
|
||||||
DeployStatus bool
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
"wdd.io/agent-common/assert"
|
"wdd.io/agent-common/assert"
|
||||||
|
"wdd.io/agent-common/pusher"
|
||||||
"wdd.io/agent-common/utils"
|
"wdd.io/agent-common/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -284,6 +285,15 @@ func TestUpdateCmiiDeploymentImageTag(t *testing.T) {
|
|||||||
assert.Equal(t, check, true, "deployment run failed!")
|
assert.Equal(t, check, true, "deployment run failed!")
|
||||||
|
|
||||||
// push message
|
// push message
|
||||||
|
message := pusher.CmiiUpdateMessage{
|
||||||
|
Namespace: cmiiEnv,
|
||||||
|
AppName: appName,
|
||||||
|
FromTag: "",
|
||||||
|
ToTag: newTag,
|
||||||
|
Replicas: "",
|
||||||
|
DeployStatus: check,
|
||||||
|
}
|
||||||
|
message.SendMessage()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user