forked from vmware-archive/kibosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
220 lines (186 loc) · 6.71 KB
/
client.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// kibosh
//
// Copyright (c) 2017-Present Pivotal Software, Inc. All Rights Reserved.
//
// This program and the accompanying materials are made available under the terms of the under the Apache License,
// Version 2.0 (the "License”); you may not use this file except in compliance with the License. You may
// obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.
package helm
import (
"fmt"
"code.cloudfoundry.org/lager"
"github.com/cf-platform-eng/kibosh/k8s"
"github.com/ghodss/yaml"
helmstaller "k8s.io/helm/cmd/helm/installer"
"k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/helm/portforwarder"
"k8s.io/helm/pkg/kube"
"k8s.io/helm/pkg/proto/hapi/chart"
rls "k8s.io/helm/pkg/proto/hapi/services"
)
type myHelmClient struct {
privateRegistryServer string
cluster k8s.Cluster
logger lager.Logger
chart *MyChart
}
//- go:generate counterfeiter ./ MyHelmClient
//^ counterfeiter is generating bad stubs interface. If needing to regenerate, fix above line & then re-fix stubs
type MyHelmClient interface {
helm.Interface
Install(*helmstaller.Options) error
Upgrade(*helmstaller.Options) error
InstallChart(namespace string, planName string, options ...helm.InstallOption) (*rls.InstallReleaseResponse, error)
MergeValueBytes(base []byte, override []byte) ([]byte, error)
}
func NewMyHelmClient(chart *MyChart, cluster k8s.Cluster, logger lager.Logger) MyHelmClient {
return &myHelmClient{
chart: chart,
cluster: cluster,
logger: logger,
}
}
func (c myHelmClient) open() (*kube.Tunnel, helm.Interface, error) {
config, client := c.cluster.GetClientConfig(), c.cluster.GetClient()
tunnel, err := portforwarder.New(nameSpace, client, config)
if err != nil {
return nil, nil, err
}
host := fmt.Sprintf("127.0.0.1:%d", tunnel.Local)
c.logger.Debug("Tunnel", lager.Data{"host": host})
return tunnel, helm.NewClient(helm.Host(host)), nil
}
func (c *myHelmClient) Install(opts *helmstaller.Options) error {
return helmstaller.Install(c.cluster.GetClient(), opts)
}
func (c *myHelmClient) Upgrade(opts *helmstaller.Options) error {
return helmstaller.Upgrade(c.cluster.GetClient(), opts)
}
func (c myHelmClient) ListReleases(opts ...helm.ReleaseListOption) (*rls.ListReleasesResponse, error) {
tunnel, client, err := c.open()
if err != nil {
return nil, err
}
defer tunnel.Close()
return client.ListReleases(opts...)
}
func (c myHelmClient) InstallRelease(chStr, namespace string, opts ...helm.InstallOption) (*rls.InstallReleaseResponse, error) {
panic("Not yet implemented")
}
func (c myHelmClient) InstallReleaseFromChart(myChart *chart.Chart, namespace string, opts ...helm.InstallOption) (*rls.InstallReleaseResponse, error) {
tunnel, client, err := c.open()
if err != nil {
return nil, err
}
defer tunnel.Close()
return client.InstallReleaseFromChart(myChart, namespace, opts...)
}
func (c myHelmClient) InstallChart(namespace string, planName string, opts ...helm.InstallOption) (*rls.InstallReleaseResponse, error) {
overrideValues, err := c.MergeValueBytes(c.chart.Values, c.chart.Plans[planName].Values)
if err != nil {
return nil, err
}
newOpts := append(opts, helm.ValueOverrides(overrideValues))
return c.InstallReleaseFromChart(c.chart.Chart, namespace, newOpts...)
}
func (c myHelmClient) DeleteRelease(rlsName string, opts ...helm.DeleteOption) (*rls.UninstallReleaseResponse, error) {
tunnel, client, err := c.open()
if err != nil {
return nil, err
}
defer tunnel.Close()
return client.DeleteRelease(rlsName, opts...)
}
func (c myHelmClient) ReleaseStatus(rlsName string, opts ...helm.StatusOption) (*rls.GetReleaseStatusResponse, error) {
tunnel, client, err := c.open()
if err != nil {
return nil, err
}
defer tunnel.Close()
return client.ReleaseStatus(rlsName, opts...)
}
func (c myHelmClient) UpdateRelease(rlsName, chStr string, opts ...helm.UpdateOption) (*rls.UpdateReleaseResponse, error) {
panic("Not yet implemented")
}
func (c myHelmClient) UpdateReleaseFromChart(rlsName string, chart *chart.Chart, opts ...helm.UpdateOption) (*rls.UpdateReleaseResponse, error) {
panic("Not yet implemented")
}
func (c myHelmClient) RollbackRelease(rlsName string, opts ...helm.RollbackOption) (*rls.RollbackReleaseResponse, error) {
panic("Not yet implemented")
}
func (c myHelmClient) ReleaseContent(rlsName string, opts ...helm.ContentOption) (*rls.GetReleaseContentResponse, error) {
panic("Not yet implemented")
}
func (c myHelmClient) ReleaseHistory(rlsName string, opts ...helm.HistoryOption) (*rls.GetHistoryResponse, error) {
panic("Not yet implemented")
}
func (c myHelmClient) GetVersion(opts ...helm.VersionOption) (*rls.GetVersionResponse, error) {
tunnel, client, err := c.open()
if err != nil {
return nil, err
}
defer tunnel.Close()
return client.GetVersion(opts...)
}
func (c myHelmClient) RunReleaseTest(rlsName string, opts ...helm.ReleaseTestOption) (<-chan *rls.TestReleaseResponse, <-chan error) {
panic("Not yet implemented")
}
func (c myHelmClient) PingTiller() error {
panic("Not yet implemented")
}
func (c myHelmClient) MergeValueBytes(base []byte, override []byte) ([]byte, error) {
baseVals := map[string]interface{}{}
err := yaml.Unmarshal(base, &baseVals)
if err != nil {
panic(err)
}
overrideVals := map[string]interface{}{}
err = yaml.Unmarshal(override, &overrideVals)
if err != nil {
panic(err)
}
mergeValueMaps(baseVals, overrideVals)
merged, err := yaml.Marshal(baseVals)
if err != nil {
panic(err)
}
return merged, nil
}
// we stole this from Helm cmd/helm/installer/install.mergeValues
func mergeValueMaps(dest map[string]interface{}, src map[string]interface{}) map[string]interface{} {
for k, v := range src {
// If the key doesn't exist already, then just set the key to that value
if _, exists := dest[k]; !exists {
dest[k] = v
continue
}
nextMap, ok := v.(map[string]interface{})
// If it isn't another map, overwrite the value
if !ok {
dest[k] = v
continue
}
// If the key doesn't exist already, then just set the key to that value
if _, exists := dest[k]; !exists {
dest[k] = nextMap
continue
}
// Edge case: If the key exists in the destination, but isn't a map
destMap, isMap := dest[k].(map[string]interface{})
// If the source map has a map for this key, prefer it
if !isMap {
dest[k] = v
continue
}
// If we got to this point, it is a map in both, so merge them
dest[k] = mergeValueMaps(destMap, nextMap)
}
return dest
}