-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgit_handler.go
More file actions
190 lines (158 loc) · 4.38 KB
/
git_handler.go
File metadata and controls
190 lines (158 loc) · 4.38 KB
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
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5/storage/memory"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/Skarlso/crd-to-sample-yaml/pkg"
"github.com/Skarlso/crd-to-sample-yaml/pkg/sanitize"
)
// GitHandler contains data to parse git configuration and values.
type GitHandler struct {
URL string
Username string
Password string
Token string
Tag string
caBundle string
privSSHKey string
useSSHAgent bool
group string // this is used by the configfile.
}
// CRDs returns a list of crds parsed out from crds contained in a git repository.
func (g *GitHandler) CRDs() ([]*pkg.SchemaType, error) {
opts, err := g.constructGitOptions()
if err != nil {
return nil, err
}
r, err := git.Clone(memory.NewStorage(), nil, opts)
if err != nil {
return nil, fmt.Errorf("error cloning git repository: %w", err)
}
var ref *plumbing.Reference
if g.Tag != "" {
ref, err = r.Tag(g.Tag)
} else {
ref, err = r.Head()
}
if err != nil {
return nil, fmt.Errorf("failed to construct reference: %w", err)
}
crds, err := g.gatherSchemaTypesForRef(r, ref)
if err != nil {
return nil, err
}
_, _ = fmt.Fprintln(os.Stderr, "Discovered number of CRDs: ", len(crds))
return crds, nil
}
func (g *GitHandler) gatherSchemaTypesForRef(r *git.Repository, ref *plumbing.Reference) ([]*pkg.SchemaType, error) {
// Need to resolve the ref first to the right hash otherwise it's not found.
hash, err := r.ResolveRevision(plumbing.Revision(ref.Hash().String()))
if err != nil {
return nil, fmt.Errorf("failed to resolve revision: %w", err)
}
commit, err := r.CommitObject(*hash)
if err != nil {
return nil, fmt.Errorf("error getting commit object: %w", err)
}
commitTree, err := commit.Tree()
if err != nil {
return nil, err
}
var crds []*pkg.SchemaType
// Tried to make this concurrent, but there was very little gain. It just takes this long to
// clone a large repository. It's not the processing OR the rendering that takes long.
if err := commitTree.Files().ForEach(func(f *object.File) error {
crd, err := g.processEntry(f)
if err != nil {
return err
}
if crd != nil {
crds = append(crds, crd)
}
return nil
}); err != nil {
return nil, err
}
return crds, nil
}
func (g *GitHandler) processEntry(f *object.File) (*pkg.SchemaType, error) {
for _, path := range strings.Split(f.Name, string(filepath.Separator)) {
if path == "test" {
return nil, nil
}
}
if ext := filepath.Ext(f.Name); ext != ".yaml" {
return nil, nil
}
content, err := f.Contents()
if err != nil {
return nil, err
}
sanitized, err := sanitize.Sanitize([]byte(content))
if err != nil {
return nil, fmt.Errorf("failed to sanitize content: %w", err)
}
crd := &unstructured.Unstructured{}
if err := yaml.Unmarshal(sanitized, crd); err != nil {
return nil, nil //nolint:nilerr // intentional
}
schemaType, err := pkg.ExtractSchemaType(crd)
if err != nil || schemaType == nil {
return nil, nil //nolint:nilerr // intentional
}
if g.group != "" {
schemaType.Rendering = pkg.Rendering{Group: g.group}
}
return schemaType, nil
}
func (g *GitHandler) constructGitOptions() (*git.CloneOptions, error) {
opts := &git.CloneOptions{
URL: g.URL,
Depth: 1,
}
// trickle down. if ssh key is set, this will be overwritten.
if g.Username != "" && g.Password != "" {
opts.Auth = &http.BasicAuth{
Username: g.Username,
Password: g.Password,
}
}
if g.Token != "" {
opts.Auth = &http.TokenAuth{
Token: g.Token,
}
}
if g.caBundle != "" {
opts.CABundle = []byte(g.caBundle)
}
if g.privSSHKey != "" {
if !strings.Contains(g.URL, "@") {
return nil, fmt.Errorf("git URL does not contain an ssh address: %s", g.URL)
}
keys, err := ssh.NewPublicKeysFromFile("git", g.privSSHKey, g.Password)
if err != nil {
return nil, err
}
opts.Auth = keys
}
if g.useSSHAgent {
if !strings.Contains(g.URL, "@") {
return nil, fmt.Errorf("git URL does not contain an ssh address: %s", g.URL)
}
authMethod, err := ssh.NewSSHAgentAuth("git")
if err != nil {
return nil, err
}
opts.Auth = authMethod
}
return opts, nil
}