This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
ksonnet.go
84 lines (65 loc) · 1.63 KB
/
ksonnet.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
package ksonnet
import (
"bytes"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/kubespec"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/printer"
"github.com/pkg/errors"
)
// Lib is a ksonnet lib.
type Lib struct {
K8s []byte
Extensions []byte
Version string
}
// GenerateLib generates ksonnet lib.
func GenerateLib(source string) (*Lib, error) {
apiSpec, checksum, err := kubespec.Import(source)
if err != nil {
return nil, errors.Wrap(err, "import Kubernetes spec")
}
c, err := NewCatalog(apiSpec, CatalogOptChecksum(checksum))
if err != nil {
return nil, errors.Wrap(err, "create ksonnet catalog")
}
k8s, err := createK8s(c)
if err != nil {
return nil, errors.Wrap(err, "create k8s.libsonnet")
}
k, err := createK(c)
if err != nil {
return nil, errors.Wrap(err, "create k.libsonnet")
}
lib := &Lib{
K8s: k8s,
Extensions: k,
Version: c.apiVersion.String(),
}
return lib, nil
}
func createK8s(c *Catalog) ([]byte, error) {
doc, err := NewDocument(c)
if err != nil {
return nil, errors.Wrapf(err, "create document")
}
node, err := doc.Node()
if err != nil {
return nil, errors.Wrapf(err, "build document node")
}
var buf bytes.Buffer
if err := printer.Fprint(&buf, node.Node()); err != nil {
return nil, errors.Wrap(err, "print AST")
}
return buf.Bytes(), nil
}
func createK(c *Catalog) ([]byte, error) {
e := NewExtension(c)
node, err := e.Node()
if err != nil {
return nil, errors.Wrapf(err, "build extension node")
}
var buf bytes.Buffer
if err := printer.Fprint(&buf, node.Node()); err != nil {
return nil, errors.Wrap(err, "print AST")
}
return buf.Bytes(), nil
}