Skip to content

Commit acef4fd

Browse files
authored
Merge pull request #139 from terrycain/107-files
Added Helm .Files
2 parents 43632a0 + 4a3acbe commit acef4fd

File tree

12 files changed

+433
-0
lines changed

12 files changed

+433
-0
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v2
2+
name: best-values-example
3+
description: One of the best values parsing example charts here, exhibits several more complicated examples
4+
version: "0.2.0"
5+
home: "https://github.com/norwoodj/helm-docs/tree/master/example-charts/best-values-example"
6+
sources: ["https://github.com/norwoodj/helm-docs/tree/master/example-charts/best-values-example"]
7+
engine: gotpl
8+
type: application
9+
maintainers:
10+
11+
name: John Norwood

example-charts/files-values/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# best-values-example
2+
3+
One of the best values parsing example charts here, exhibits several more complicated examples
4+
5+
![Version: 0.2.0](https://img.shields.io/badge/Version-0.2.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)
6+
7+
## Additional Information
8+
9+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore
10+
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
11+
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
12+
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
13+
culpa qui officia deserunt mollit anim id est laborum.
14+
15+
## Installing the Chart
16+
17+
To install the chart with the release name `my-release`:
18+
19+
```console
20+
$ helm repo add foo-bar http://charts.foo-bar.com
21+
$ helm install my-release foo-bar/best-values-example
22+
```
23+
24+
Some file contents:
25+
26+
```
27+
some:
28+
data: "test"
29+
```
30+
31+
Glob contents as config map:
32+
33+
```
34+
apiVersion: v1
35+
kind: ConfigMap
36+
metadata:
37+
name: test
38+
data:
39+
resource1.yaml: |-
40+
some:
41+
resource: "blah"
42+
resource2.yaml: |-
43+
some:
44+
resource: "blah2"
45+
```
46+
47+
## Values
48+
49+
| Key | Type | Default | Description |
50+
|-----|------|---------|-------------|
51+
| config.databasesToCreate[0] | string | `"postgresql"` | default database for storage of database metadata |
52+
| config.databasesToCreate[1] | string | `"hashbash"` | database for the [hashbash](https://github.com/norwoodj/hashbash) project |
53+
| config.usersToCreate[0] | object | `{"admin":true,"name":"root"}` | admin user |
54+
| config.usersToCreate[1] | object | `{"name":"hashbash","readwriteDatabases":["hashbash"]}` | user with access to the database with the same name |
55+
| statefulset.extraVolumes | list | `[{"emptyDir":{},"name":"data"}]` | Additional volumes to be mounted into the database container |
56+
| statefulset.image.repository | string | `"jnorwood/postgresq"` | Image to use for deploying, must support an entrypoint which creates users/databases from appropriate config files |
57+
| statefulset.image.tag | string | `"11"` | |
58+
| statefulset.livenessProbe | object | `{"enabled":false}` | Configure the healthcheck for the database |
59+
| statefulset.podLabels | object | `{}` | The labels to be applied to instances of the database |
60+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{{ template "chart.header" . }}
2+
{{ template "chart.description" . }}
3+
4+
{{ template "chart.versionBadge" . }}{{ template "chart.typeBadge" . }}{{ template "chart.appVersionBadge" . }}
5+
6+
## Additional Information
7+
8+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore
9+
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
10+
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
11+
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
12+
culpa qui officia deserunt mollit anim id est laborum.
13+
14+
## Installing the Chart
15+
16+
To install the chart with the release name `my-release`:
17+
18+
```console
19+
$ helm repo add foo-bar http://charts.foo-bar.com
20+
$ helm install my-release foo-bar/{{ template "chart.name" . }}
21+
```
22+
23+
Some file contents:
24+
25+
```
26+
{{ .Files.Get "somefile.yaml" }}
27+
```
28+
29+
Glob contents as config map:
30+
31+
```
32+
apiVersion: v1
33+
kind: ConfigMap
34+
metadata:
35+
name: test
36+
data:
37+
{{ (.Files.Glob "templates/**.yaml").AsConfig | indent 2 }}
38+
```
39+
40+
{{ template "chart.requirementsSection" . }}
41+
42+
{{ template "chart.valuesSection" . }}
43+
44+
{{ template "helm-docs.versionFooter" . }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
some:
2+
data: "test"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
some:
2+
resource: "blah"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
some:
2+
resource: "blah2"
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
statefulset:
2+
image:
3+
# -- Image to use for deploying, must support an entrypoint which creates users/databases from appropriate config files
4+
repository: jnorwood/postgresq
5+
tag: "11"
6+
7+
# -- Additional volumes to be mounted into the database container
8+
extraVolumes:
9+
- name: data
10+
emptyDir: {}
11+
12+
# -- Configure the healthcheck for the database
13+
livenessProbe:
14+
enabled: false
15+
16+
# -- The labels to be applied to instances of the database
17+
podLabels: {}
18+
19+
config:
20+
databasesToCreate:
21+
# -- default database for storage of database metadata
22+
- postgresql
23+
24+
# -- database for the [hashbash](https://github.com/norwoodj/hashbash) project
25+
- hashbash
26+
27+
usersToCreate:
28+
# -- admin user
29+
- {name: root, admin: true}
30+
31+
# -- user with access to the database with the same name
32+
- {name: hashbash, readwriteDatabases: [hashbash]}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.14
44

55
require (
66
github.com/Masterminds/sprig/v3 v3.2.2
7+
github.com/gobwas/glob v0.2.3
78
github.com/sirupsen/logrus v1.2.0
89
github.com/spf13/cobra v0.0.5
910
github.com/spf13/viper v1.4.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
3434
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
3535
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
3636
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
37+
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
38+
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
3739
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
3840
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
3941
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=

pkg/document/files.go

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package document
2+
3+
import (
4+
"encoding/base64"
5+
"io/ioutil"
6+
"os"
7+
"path"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/gobwas/glob"
12+
log "github.com/sirupsen/logrus"
13+
"gopkg.in/yaml.v3"
14+
)
15+
16+
// Near identical to https://github.com/helm/helm/blob/main/pkg/engine/files.go as to preserve the interface.
17+
18+
type fileEntry struct {
19+
Path string
20+
data []byte
21+
}
22+
23+
func (f *fileEntry) GetData() []byte {
24+
if f.data == nil {
25+
data, err := ioutil.ReadFile(f.Path)
26+
if err != nil {
27+
log.Warnf("Error reading file contents for %s: %s", f.Path, err.Error())
28+
return []byte{}
29+
}
30+
f.data = data
31+
}
32+
33+
return f.data
34+
}
35+
36+
type files map[string]*fileEntry
37+
38+
func getFiles(dir string) (files, error) {
39+
result := make(files)
40+
41+
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
42+
if err != nil {
43+
return err
44+
}
45+
46+
if info.IsDir() {
47+
return nil
48+
}
49+
50+
result[path] = &fileEntry{Path: path}
51+
return nil
52+
})
53+
if err != nil {
54+
return map[string]*fileEntry{}, err
55+
}
56+
57+
return result, nil
58+
}
59+
60+
func (f files) GetBytes(name string) []byte {
61+
if v, ok := f[name]; ok {
62+
return v.GetData()
63+
}
64+
return []byte{}
65+
}
66+
67+
func (f files) Get(name string) string {
68+
return string(f.GetBytes(name))
69+
}
70+
71+
func (f files) Glob(pattern string) files {
72+
result := make(files)
73+
g, err := glob.Compile(pattern, '/')
74+
if err != nil {
75+
log.Warnf("Error compiling Glob patten %s: %s", pattern, err.Error())
76+
return result
77+
}
78+
79+
for filePath, entry := range f {
80+
if g.Match(filePath) {
81+
result[filePath] = entry
82+
}
83+
}
84+
85+
return result
86+
}
87+
88+
func (f files) AsConfig() string {
89+
if f == nil {
90+
return ""
91+
}
92+
93+
m := make(map[string]string)
94+
95+
// Explicitly convert to strings, and file names
96+
for k, v := range f {
97+
m[path.Base(k)] = string(v.GetData())
98+
}
99+
100+
return toYAML(m)
101+
}
102+
103+
func (f files) AsSecrets() string {
104+
if f == nil {
105+
return ""
106+
}
107+
108+
m := make(map[string]string)
109+
110+
for k, v := range f {
111+
m[path.Base(k)] = base64.StdEncoding.EncodeToString(v.GetData())
112+
}
113+
114+
return toYAML(m)
115+
}
116+
117+
func (f files) Lines(path string) []string {
118+
if f == nil {
119+
return []string{}
120+
}
121+
entry, exists := f[path]
122+
if !exists {
123+
return []string{}
124+
}
125+
126+
return strings.Split(string(entry.GetData()), "\n")
127+
}
128+
129+
func toYAML(v interface{}) string {
130+
data, err := yaml.Marshal(v)
131+
if err != nil {
132+
// Swallow errors inside a template.
133+
return ""
134+
}
135+
return strings.TrimSuffix(string(data), "\n")
136+
}

0 commit comments

Comments
 (0)