-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (128 loc) · 4.43 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"k8s.io/api/core/v1"
jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/kubectl/pkg/scheme"
// yaml "gopkg.in/yaml.v2"
)
func main() {
_, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
reader := bufio.NewReader(os.Stdin)
var output []rune
for {
input, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
output = append(output, input)
}
var data string
for j := 0; j < len(output); j++ {
data = data + string(output[j])
}
action := os.Args[1]
switch action {
case "info":
releaseInfo(data)
case "get-manifest":
getManifest(data)
case "set-manifest":
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "Usage: helm-release-tool set-manifest <path-to-new-manifest>\n")
}
setManifest(data, os.Args[2])
case "set-status-deployed":
setReleaseStatus(data)
case "set-release-name":
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "ERROR: new release name not specify\n")
}
setReleaseName(data, os.Args[2])
case "set-release-namespace":
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "ERROR: new release namespace not specify\n")
}
setReleaseNamespace(data, os.Args[2])
default:
fmt.Fprintf(os.Stderr, "ERROR: invalid action: %s\nYou need to specify action: info or set-status-deployed or set-new-release-name or set-new-release-namespace\n", action)
os.Exit(1)
}
}
func fRead(path string) string {
content, _ := ioutil.ReadFile(path)
return string(content)
}
func releaseInfo(data string) {
decoder := scheme.Codecs.UniversalDeserializer()
obj, _, _ := decoder.Decode([]byte(data), nil, nil)
cm := obj.(*v1.ConfigMap)
release, _ := DecodeRelease(cm.Data["release"])
fmt.Printf("Name: %v\nNamespace: %v\nStatus: %v\n", release.Name, release.Namespace, release.Info.Status.Code)
}
func getManifest(data string) {
decoder := scheme.Codecs.UniversalDeserializer()
obj, _, _ := decoder.Decode([]byte(data), nil, nil)
cm := obj.(*v1.ConfigMap)
release, _ := DecodeRelease(cm.Data["release"])
fmt.Printf("%v\n", release.Manifest)
fmt.Printf("manifest type: %T\n", release.Manifest)
}
func setManifest(data, manifestPath string) {
decoder := scheme.Codecs.UniversalDeserializer()
obj, _, _ := decoder.Decode([]byte(data), nil, nil)
cm := obj.(*v1.ConfigMap)
release, _ := DecodeRelease(cm.Data["release"])
release.Manifest = fRead(manifestPath)
encoded, _ := EncodeRelease(release)
cm.Data["release"] = encoded
jsonEncoder := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, true)
_ = jsonEncoder.Encode(cm, os.Stdout)
}
func setReleaseStatus(data string) {
decoder := scheme.Codecs.UniversalDeserializer()
obj, _, _ := decoder.Decode([]byte(data), nil, nil)
cm := obj.(*v1.ConfigMap)
release, _ := DecodeRelease(cm.Data["release"])
cm.ObjectMeta.Labels["STATUS"] = "DEPLOYED"
// https://github.com/helm/helm/blob/release-2.16/pkg/proto/hapi/release/status.pb.go#L47
release.Info.Status.Code = 1
encoded, _ := EncodeRelease(release)
cm.Data["release"] = encoded
jsonEncoder := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, true)
_ = jsonEncoder.Encode(cm, os.Stdout)
}
func setReleaseName(data, newReleaseName string) {
decoder := scheme.Codecs.UniversalDeserializer()
obj, _, _ := decoder.Decode([]byte(data), nil, nil)
cm := obj.(*v1.ConfigMap)
decoded, _ := DecodeRelease(cm.Data["release"])
decoded.Name = newReleaseName
encoded, _ := EncodeRelease(decoded)
cm.Data["release"] = encoded
currentName := cm.ObjectMeta.Labels["NAME"]
cm.Name = strings.Replace(cm.Name, currentName, newReleaseName, 1)
cm.ObjectMeta.Labels["NAME"] = newReleaseName
cm.SelfLink = strings.Replace(cm.SelfLink, currentName, newReleaseName, 1)
jsonEncoder := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, true)
_ = jsonEncoder.Encode(cm, os.Stdout)
}
func setReleaseNamespace(data, newReleaseNamespace string) {
decoder := scheme.Codecs.UniversalDeserializer()
obj, _, _ := decoder.Decode([]byte(data), nil, nil)
cm := obj.(*v1.ConfigMap)
decoded, _ := DecodeRelease(cm.Data["release"])
decoded.Namespace = newReleaseNamespace
encoded, _ := EncodeRelease(decoded)
cm.Data["release"] = encoded
jsonEncoder := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, true)
_ = jsonEncoder.Encode(cm, os.Stdout)
}