-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
155 lines (143 loc) · 3.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
var (
//跳过证书验证
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{
Transport: tr,
} // http client
api string // rancher api地址
accessKey string // rancher access key
secretKey string // rancher secret key
)
// DATA 字段元素
type container struct {
Name string `json:"name"` // 名称
Image string `json:"image"` // 镜像
Environment map[string]interface{} `json:"environment"` // 参宿
}
// DATA 字段
type containers []container
// 取容器名称对应的image
func (cs containers) get(name string) string {
for i := 0; i < len(cs); i++ {
c := cs[i]
if c.Name == name {
return c.Image
}
}
return ""
}
// 取容器名称对应的image
func (cs containers) getEnvironment(name string) map[string]interface{} {
for i := 0; i < len(cs); i++ {
c := cs[i]
if c.Name == name {
return c.Environment
}
}
return nil
}
// 构建请求并返回结果
func buildRequest(method string, data string) []byte {
req, err := http.NewRequest(method, api, strings.NewReader(data))
if err != nil {
log.Printf("An error occurred during build request: %s", err.Error())
os.Exit(1)
}
req.SetBasicAuth(accessKey, secretKey)
resp, err := client.Do(req)
defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()
if err != nil {
log.Printf("An error occurred during get rancher workload metadata: %s", err.Error())
os.Exit(1)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("An error occurred during reading rancher workload metadata: %s", err.Error())
os.Exit(1)
}
return body
}
// 格式化json
func formatJson(data []byte) string {
var out bytes.Buffer
json.Indent(&out, data, "", " ")
return string(out.Bytes())
}
func init() {
// 初始化数据
api = os.Getenv("PLUGIN_API")
accessKey = os.Getenv("PLUGIN_ACCESS_KEY")
secretKey = os.Getenv("PLUGIN_SECRET_KEY")
}
func main() {
data := os.Getenv("PLUGIN_DATA")
// 打印参数
log.Printf("\n API %s \n ACCESS_KEY: %s\n SECRET_KEY: %s\n DATA: %s", api, accessKey, secretKey, data)
// 解析参数
cs := &containers{}
err := json.Unmarshal([]byte(data), &cs)
if err != nil {
log.Printf("An error occurred during pasre params: data, %s", err.Error())
os.Exit(1)
}
// 请求数据
body := buildRequest("GET", "")
log.Printf("receive data, %s", formatJson(body))
// 解析containers
var apiData map[string]interface{}
json.Unmarshal([]byte(body), &apiData)
containersData, ok := apiData["containers"]
if !ok {
log.Printf("An error occurred during parse rancher workload metadata: %s", err.Error())
os.Exit(1)
}
// 查找参数对应名称的容器并把修改为参数的镜像
containersArrays := containersData.([]interface{})
for _, csItemData := range containersArrays {
csItem := csItemData.(map[string]interface{})
if nameData, ok := csItem["name"]; ok {
name := nameData.(string)
image := cs.get(name)
if image != "" {
csItem["image"] = image
}
environment := cs.getEnvironment(name)
if environment != nil {
environmentData, ok := csItem["environment"].(map[string]interface{})
if ok {
for key, value := range environment {
environmentData[key] = value
}
} else {
environmentData := make(map[string]interface{})
for key, value := range environment {
environmentData[key] = value
}
}
}
}
}
// 调用请求更新rancher workload
target, err := json.Marshal(apiData)
log.Printf("POST DATA: %s\n", formatJson(target))
body = buildRequest("PUT", string(target))
log.Printf("result: %s\n", formatJson(body))
log.Println("update successfull")
}