This repository has been archived by the owner on Mar 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
347 lines (295 loc) · 11 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"strings"
jsonpatch "github.com/evanphx/json-patch"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/engine"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/releaseutil"
"helm.sh/helm/v3/pkg/storage/driver"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/discovery"
)
var settings = cli.New()
func main() {
valueOpts := &values.Options{}
var rootCmd = &cobra.Command{
Use: "patchdiff <NAME> <CHART> [options]",
Short: "Preview helm upgrade changes as a JSON patch",
Long: "Preview helm upgrade changes as a JSON patch",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
if err := validateReleaseName(name); err != nil {
log.Fatal(err)
}
chartPath := args[1]
vals, err := valueOpts.MergeValues(getter.All(settings))
if err != nil {
log.Fatal(err)
}
ch, err := loader.Load(chartPath)
if err != nil {
log.Fatal(err)
}
patchset, err := createPatchset(name, ch, vals)
if err != nil {
log.Fatal(err)
}
fmt.Println(patchset)
return nil
},
}
f := rootCmd.Flags()
addValueOptionsFlags(f, valueOpts)
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
func createPatchset(name string, ch *chart.Chart, vals map[string]interface{}) (string, error) {
patches := []string{}
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), log.Printf); err != nil {
log.Fatalf("%+v", err)
}
if err := actionConfig.KubeClient.IsReachable(); err != nil {
return "", err
}
originalManifest, targetManifest, err := prepareUpgrade(actionConfig, name, ch, vals)
if err != nil {
return "", err
}
original, err := actionConfig.KubeClient.Build(bytes.NewBufferString(originalManifest), false)
if err != nil {
return "", errors.Wrap(err, "unable to build kubernetes objects from original release manifest")
}
target, err := actionConfig.KubeClient.Build(bytes.NewBufferString(targetManifest), false)
if err != nil {
return "", errors.Wrap(err, "unable to build kubernetes objects from new release manifest")
}
err = target.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
helper := resource.NewHelper(info.Client, info.Mapping)
if _, err := helper.Get(info.Namespace, info.Name, info.Export); apierrors.IsNotFound(err) {
// no patch to generate
return nil
}
originalInfo := original.Get(info)
if originalInfo == nil {
return fmt.Errorf("could not find %q", info.Name)
}
patch, _, err := createPatch(originalInfo.Object, info)
if err != nil {
return err
}
// append patch to patchset
patches = append(patches, string(patch))
return nil
})
return fmt.Sprintf("[%s]", strings.Join(patches, ",")), err
}
func prepareUpgrade(c *action.Configuration, name string, chart *chart.Chart, vals map[string]interface{}) (string, string, error) {
if chart == nil {
return "", "", errors.New("missing chart")
}
// finds the last non-deleted release with the given name
lastRelease, err := c.Releases.Last(name)
if err != nil {
// to keep existing behavior of returning the "%q has no deployed releases" error when an existing release does not exist
if errors.Is(err, driver.ErrReleaseNotFound) {
return "", "", driver.NewErrNoDeployedReleases(name)
}
return "", "", err
}
var currentRelease *release.Release
if lastRelease.Info.Status == release.StatusDeployed {
// no need to retrieve the last deployed release from storage as the last release is deployed
currentRelease = lastRelease
} else {
// finds the deployed release with the given name
currentRelease, err = c.Releases.Deployed(name)
if err != nil {
if errors.Is(err, driver.ErrNoDeployedReleases) &&
(lastRelease.Info.Status == release.StatusFailed || lastRelease.Info.Status == release.StatusSuperseded) {
currentRelease = lastRelease
} else {
return "", "", err
}
}
}
if err := chartutil.ProcessDependencies(chart, vals); err != nil {
return "", "", err
}
// Increment revision count. This is passed to templates, and also stored on
// the release object.
revision := lastRelease.Version + 1
options := chartutil.ReleaseOptions{
Name: name,
Namespace: currentRelease.Namespace,
Revision: revision,
IsUpgrade: true,
}
if err := getCapabilities(c); err != nil {
return "", "", err
}
valuesToRender, err := chartutil.ToRenderValues(chart, vals, options, c.Capabilities)
if err != nil {
return "", "", err
}
manifestDoc, err := renderResources(c, chart, valuesToRender)
if err != nil {
return "", "", err
}
return currentRelease.Manifest, manifestDoc.String(), err
}
// capabilities builds a Capabilities from discovery information.
func getCapabilities(c *action.Configuration) error {
if c.Capabilities != nil {
return nil
}
dc, err := c.RESTClientGetter.ToDiscoveryClient()
if err != nil {
return errors.Wrap(err, "could not get Kubernetes discovery client")
}
// force a discovery cache invalidation to always fetch the latest server version/capabilities.
dc.Invalidate()
kubeVersion, err := dc.ServerVersion()
if err != nil {
return errors.Wrap(err, "could not get server version from Kubernetes")
}
// Issue #6361:
// Client-Go emits an error when an API service is registered but unimplemented.
// We trap that error here and print a warning. But since the discovery client continues
// building the API object, it is correctly populated with all valid APIs.
// See https://github.com/kubernetes/kubernetes/issues/72051#issuecomment-521157642
apiVersions, err := action.GetVersionSet(dc)
if err != nil {
if discovery.IsGroupDiscoveryFailedError(err) {
c.Log("WARNING: The Kubernetes server has an orphaned API service. Server reports: %s", err)
c.Log("WARNING: To fix this, kubectl delete apiservice <service-name>")
} else {
return errors.Wrap(err, "could not get apiVersions from Kubernetes")
}
}
c.Capabilities = &chartutil.Capabilities{
APIVersions: apiVersions,
KubeVersion: chartutil.KubeVersion{
Version: kubeVersion.GitVersion,
Major: kubeVersion.Major,
Minor: kubeVersion.Minor,
},
}
return nil
}
func renderResources(c *action.Configuration, ch *chart.Chart, values chartutil.Values) (*bytes.Buffer, error) {
b := bytes.NewBuffer(nil)
if err := getCapabilities(c); err != nil {
return b, err
}
if ch.Metadata.KubeVersion != "" {
if !chartutil.IsCompatibleRange(ch.Metadata.KubeVersion, c.Capabilities.KubeVersion.String()) {
return b, errors.Errorf("chart requires kubeVersion: %s which is incompatible with Kubernetes %s", ch.Metadata.KubeVersion, c.Capabilities.KubeVersion.String())
}
}
rest, err := c.RESTClientGetter.ToRESTConfig()
if err != nil {
return b, err
}
files, err := engine.RenderWithClient(ch, values, rest)
if err != nil {
return b, err
}
// Sort hooks, manifests, and partials. Only hooks and manifests are returned,
// as partials are not used after renderer.Render. Empty manifests are also
// removed here.
_, manifests, err := releaseutil.SortManifests(files, c.Capabilities.APIVersions, releaseutil.InstallOrder)
if err != nil {
return b, err
}
for _, m := range manifests {
// skip notes
if !strings.Contains(m.Name, "NOTES.txt") {
fmt.Fprintf(b, "---\n# Source: %s\n%s\n", m.Name, m.Content)
}
}
return b, nil
}
func createPatch(current runtime.Object, target *resource.Info) ([]byte, types.PatchType, error) {
oldData, err := json.Marshal(current)
if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing current configuration")
}
newData, err := json.Marshal(target.Object)
if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing target configuration")
}
// Fetch the current object for the three way merge
helper := resource.NewHelper(target.Client, target.Mapping)
currentObj, err := helper.Get(target.Namespace, target.Name, target.Export)
if err != nil && !apierrors.IsNotFound(err) {
return nil, types.StrategicMergePatchType, errors.Wrapf(err, "unable to get data for current object %s/%s", target.Namespace, target.Name)
}
// Even if currentObj is nil (because it was not found), it will marshal just fine
currentData, err := json.Marshal(currentObj)
if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing live configuration")
}
// Get a versioned object
versionedObject := kube.AsVersioned(target)
// Unstructured objects, such as CRDs, may not have an not registered error
// returned from ConvertToVersion. Anything that's unstructured should
// use the jsonpatch.CreateMergePatch. Strategic Merge Patch is not supported
// on objects like CRDs.
_, isUnstructured := versionedObject.(runtime.Unstructured)
// On newer K8s versions, CRDs aren't unstructured but has this dedicated type
_, isCRD := versionedObject.(*apiextv1.CustomResourceDefinition)
if isUnstructured || isCRD {
// fall back to generic JSON merge patch
patch, err := jsonpatch.CreateMergePatch(oldData, newData)
return patch, types.MergePatchType, err
}
patchMeta, err := strategicpatch.NewPatchMetaFromStruct(versionedObject)
if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "unable to create patch metadata from object")
}
patch, err := strategicpatch.CreateThreeWayMergePatch(oldData, newData, currentData, patchMeta, true)
return patch, types.StrategicMergePatchType, err
}
func validateReleaseName(releaseName string) error {
if releaseName == "" {
return fmt.Errorf("no release name set")
}
// Check length first, since that is a less expensive operation.
if len(releaseName) > 53 || !action.ValidName.MatchString(releaseName) {
return fmt.Errorf("invalid release name: %s", releaseName)
}
return nil
}
func addValueOptionsFlags(f *pflag.FlagSet, v *values.Options) {
f.StringSliceVarP(&v.ValueFiles, "values", "f", []string{}, "specify values in a YAML file or a URL (can specify multiple)")
f.StringArrayVar(&v.Values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
f.StringArrayVar(&v.StringValues, "set-string", []string{}, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
f.StringArrayVar(&v.FileValues, "set-file", []string{}, "set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)")
}