diff --git a/agent-common/go.mod b/agent-common/go.mod index 0b0aa56..3ed7f86 100644 --- a/agent-common/go.mod +++ b/agent-common/go.mod @@ -5,7 +5,6 @@ go 1.22.1 require ( go.uber.org/zap v1.27.0 golang.org/x/net v0.24.0 - gopkg.in/yaml.v3 v3.0.1 ) require go.uber.org/multierr v1.10.0 // indirect diff --git a/agent-common/go.sum b/agent-common/go.sum index 245caea..fe54862 100644 --- a/agent-common/go.sum +++ b/agent-common/go.sum @@ -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= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= 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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/agent-common/pusher/CmiiUpdateMessage.go b/agent-common/pusher/CmiiUpdateMessage.go new file mode 100644 index 0000000..d770407 --- /dev/null +++ b/agent-common/pusher/CmiiUpdateMessage.go @@ -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 +} diff --git a/agent-common/pusher/CmiiUpdateMessage_test.go b/agent-common/pusher/CmiiUpdateMessage_test.go new file mode 100644 index 0000000..91af7e7 --- /dev/null +++ b/agent-common/pusher/CmiiUpdateMessage_test.go @@ -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() +} diff --git a/agent-common/pusher/cmii/CmiiUpdateMessage.go b/agent-common/pusher/cmii/CmiiUpdateMessage.go deleted file mode 100644 index b18b5cd..0000000 --- a/agent-common/pusher/cmii/CmiiUpdateMessage.go +++ /dev/null @@ -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 -} diff --git a/agent-operator/CmiiK8sOperator_test.go b/agent-operator/CmiiK8sOperator_test.go index 5ad6b56..e0716dc 100644 --- a/agent-operator/CmiiK8sOperator_test.go +++ b/agent-operator/CmiiK8sOperator_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" "wdd.io/agent-common/assert" + "wdd.io/agent-common/pusher" "wdd.io/agent-common/utils" ) @@ -284,6 +285,15 @@ func TestUpdateCmiiDeploymentImageTag(t *testing.T) { assert.Equal(t, check, true, "deployment run failed!") // push message + message := pusher.CmiiUpdateMessage{ + Namespace: cmiiEnv, + AppName: appName, + FromTag: "", + ToTag: newTag, + Replicas: "", + DeployStatus: check, + } + message.SendMessage() }