-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull.go
114 lines (100 loc) · 3.16 KB
/
pull.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"k8s.io/helm/pkg/chartutil"
"log"
"os"
"path/filepath"
"github.com/containerd/containerd/remotes/docker"
"github.com/deislabs/oras/pkg/content"
"github.com/deislabs/oras/pkg/oras"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
chartmeta "k8s.io/helm/pkg/proto/hapi/chart"
)
const (
helmChartMetaMediaType = "application/vnd.cncf.helm.chart.meta.v1+json"
helmChartContentMediaType = "application/vnd.cncf.helm.chart.content.v1+tar"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
ctx := context.Background()
memoryStore := content.NewMemoryStore()
resolver := docker.NewResolver(docker.ResolverOptions{})
cwd, err := os.Getwd()
check(err)
// Read command line args
remoteRef := os.Args[1]
fmt.Printf("Attempting to pull %s into ./output...\n", remoteRef)
// Pull layers from remote, filtering on only media types we care about
allowedMediaTypes := []string{helmChartMetaMediaType, helmChartContentMediaType}
layers, err := oras.Pull(ctx, resolver, remoteRef, memoryStore, allowedMediaTypes...)
check(err)
// Make sure we have layers we need to construct a Helm Chart
var metaLayer, contentLayer ocispec.Descriptor
var metaLayerFound, contentLayerFound bool
for _, layer := range layers {
if layer.MediaType == helmChartMetaMediaType {
metaLayer = layer
metaLayerFound = true
} else if layer.MediaType == helmChartContentMediaType {
contentLayer = layer
contentLayerFound = true
}
}
if !metaLayerFound || !contentLayerFound {
panic(fmt.Sprintf("%s does not have the necessary layers", remoteRef))
}
// Extract chart name and version from annotations
name, hasName := contentLayer.Annotations["chart.name"]
version, hasVersion := contentLayer.Annotations["chart.version"]
if !hasName || !hasVersion {
panic(fmt.Sprintf("%s does not chart name and version saved on annoations", remoteRef))
}
// Contruct metadata
_, metaJsonRaw, ok := memoryStore.Get(metaLayer)
if !ok {
panic("error accessing chart meta layer")
}
metadata := chartmeta.Metadata{}
err = json.Unmarshal(metaJsonRaw, &metadata)
check(err)
metadata.Name = name
metadata.Version = version
fmt.Printf("name: %s\nversion: %s\nmetadata: %s\n", name, version, metaJsonRaw)
// Construct chart and attach metadata
_, contentRaw, ok := memoryStore.Get(contentLayer)
if !ok {
panic("error accessing chart content layer")
}
chart, err := chartutil.LoadArchive(bytes.NewBuffer(contentRaw))
check(err)
chart.Metadata = &metadata
// Save the chart to local directory
tempDirPrefix := filepath.Join(cwd, ".pull")
os.MkdirAll(tempDirPrefix, 0755)
tempDir, err := ioutil.TempDir(tempDirPrefix, "oras-helm-demo")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)
tarballAbsPath, err := chartutil.Save(chart, tempDir)
check(err)
outputDir := filepath.Join(cwd, "output")
os.RemoveAll(outputDir)
err = chartutil.ExpandFile(outputDir, tarballAbsPath)
check(err)
fmt.Printf("Success! Chart saved to ./output/%s\n", name)
}
// TODO: remove once WARN lines removed from oras/containerd
func init() {
logrus.SetLevel(logrus.ErrorLevel)
}