Skip to content

Commit 2250446

Browse files
authored
feat: extend the configuration file with the other providers (#157)
* feat: extend the configuration file with the other providers Signed-off-by: Gergely Brautigam <[email protected]> * fixed not handling group information from the config file Signed-off-by: Gergely Brautigam <[email protected]> --------- Signed-off-by: Gergely Brautigam <[email protected]>
1 parent 6ad4fc0 commit 2250446

File tree

7 files changed

+112
-20
lines changed

7 files changed

+112
-20
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ If no grouping information is provided, the rendered CRD's group version is used
161161

162162
![rendered without groups](imgs/parsed4_groups_2.png)
163163

164+
All ways of fetching CRDs are supported through the configuration file. When dealing with URLs I recommend templating
165+
this file and fetching sensitive data from elsewhere. For Git, I recommend using the local ssh-agent or a link to
166+
an SSH file.
167+
164168
## Schema Generation
165169

166170
`cty` also provides a way to generate a JSON Schema out of a CRD. Simply use:

cmd/config_file_schema.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
type URLs struct {
4+
URL string `json:"url"`
5+
Username string `json:"username,omitempty"`
6+
Password string `json:"password,omitempty"`
7+
Token string `json:"token,omitempty"`
8+
}
9+
10+
type GITUrls struct {
11+
URL string `json:"url"`
12+
Username string `json:"username,omitempty"`
13+
Password string `json:"password,omitempty"`
14+
Token string `json:"token,omitempty"`
15+
Tag string `json:"tag,omitempty"`
16+
PrivateKey string `json:"privateKey,omitempty"`
17+
UseSSHAgent bool `json:"useSSHAgent,omitempty"`
18+
}
19+
20+
// APIGroups defines groups by which grouping will happen in the resulting HTML output.
21+
type APIGroups struct {
22+
Name string `json:"name"`
23+
Description string `json:"description,omitempty"`
24+
Files []string `json:"files,omitempty"`
25+
Folders []string `json:"folders,omitempty"`
26+
URLs []URLs `json:"urls,omitempty"`
27+
GitURLs []GITUrls `json:"gitUrls,omitempty"`
28+
}
29+
30+
// RenderConfig defines a configuration for the resulting rendered HTML content.
31+
type RenderConfig struct {
32+
APIGroups []APIGroups `json:"apiGroups"`
33+
}

cmd/config_handler.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (h *ConfigHandler) CRDs() ([]*pkg.SchemaType, error) {
2222
return nil, fmt.Errorf("failed to read file: %w", err)
2323
}
2424

25-
configFile := &pkg.RenderConfig{}
25+
configFile := &RenderConfig{}
2626
if err = yaml.Unmarshal(content, configFile); err != nil {
2727
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
2828
}
@@ -51,6 +51,41 @@ func (h *ConfigHandler) CRDs() ([]*pkg.SchemaType, error) {
5151

5252
result = append(result, folderResults...)
5353
}
54+
55+
for _, url := range group.URLs {
56+
handler := URLHandler{
57+
url: url.URL,
58+
username: url.Username,
59+
password: url.Password,
60+
token: url.Token,
61+
group: group.Name,
62+
}
63+
crds, err := handler.CRDs()
64+
if err != nil {
65+
return nil, fmt.Errorf("failed to process CRDs for url %s: %w", handler.url, err)
66+
}
67+
68+
result = append(result, crds...)
69+
}
70+
71+
for _, url := range group.GitURLs {
72+
handler := GitHandler{
73+
URL: url.URL,
74+
Username: url.Username,
75+
Password: url.Password,
76+
Token: url.Token,
77+
Tag: url.Tag,
78+
privSSHKey: url.PrivateKey,
79+
useSSHAgent: url.UseSSHAgent,
80+
group: group.Name,
81+
}
82+
crds, err := handler.CRDs()
83+
if err != nil {
84+
return nil, fmt.Errorf("failed to process CRDs for git url %s: %w", handler.URL, err)
85+
}
86+
87+
result = append(result, crds...)
88+
}
5489
}
5590

5691
return result, nil

cmd/git_handler.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type GitHandler struct {
2929
caBundle string
3030
privSSHKey string
3131
useSSHAgent bool
32+
group string // this is used by the configfile.
3233
}
3334

3435
func (g *GitHandler) CRDs() ([]*pkg.SchemaType, error) {
@@ -52,7 +53,7 @@ func (g *GitHandler) CRDs() ([]*pkg.SchemaType, error) {
5253
return nil, fmt.Errorf("failed to construct reference: %w", err)
5354
}
5455

55-
crds, err := gatherSchemaTypesForRef(r, ref)
56+
crds, err := g.gatherSchemaTypesForRef(r, ref)
5657
if err != nil {
5758
return nil, err
5859
}
@@ -62,7 +63,7 @@ func (g *GitHandler) CRDs() ([]*pkg.SchemaType, error) {
6263
return crds, nil
6364
}
6465

65-
func gatherSchemaTypesForRef(r *git.Repository, ref *plumbing.Reference) ([]*pkg.SchemaType, error) {
66+
func (g *GitHandler) gatherSchemaTypesForRef(r *git.Repository, ref *plumbing.Reference) ([]*pkg.SchemaType, error) {
6667
// Need to resolve the ref first to the right hash otherwise it's not found.
6768
hash, err := r.ResolveRevision(plumbing.Revision(ref.Hash().String()))
6869
if err != nil {
@@ -83,7 +84,7 @@ func gatherSchemaTypesForRef(r *git.Repository, ref *plumbing.Reference) ([]*pkg
8384
// Tried to make this concurrent, but there was very little gain. It just takes this long to
8485
// clone a large repository. It's not the processing OR the rendering that takes long.
8586
if err := commitTree.Files().ForEach(func(f *object.File) error {
86-
crd, err := processEntry(f)
87+
crd, err := g.processEntry(f)
8788
if err != nil {
8889
return err
8990
}
@@ -100,7 +101,7 @@ func gatherSchemaTypesForRef(r *git.Repository, ref *plumbing.Reference) ([]*pkg
100101
return crds, nil
101102
}
102103

103-
func processEntry(f *object.File) (*pkg.SchemaType, error) {
104+
func (g *GitHandler) processEntry(f *object.File) (*pkg.SchemaType, error) {
104105
for _, path := range strings.Split(f.Name, string(filepath.Separator)) {
105106
if path == "test" {
106107
return nil, nil
@@ -127,10 +128,14 @@ func processEntry(f *object.File) (*pkg.SchemaType, error) {
127128
}
128129

129130
schemaType, err := pkg.ExtractSchemaType(crd)
130-
if err != nil {
131+
if err != nil || schemaType == nil {
131132
return nil, nil //nolint:nilerr // intentional
132133
}
133134

135+
if g.group != "" {
136+
schemaType.Rendering = pkg.Rendering{Group: g.group}
137+
}
138+
134139
return schemaType, nil
135140
}
136141

cmd/url_handler.go

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type URLHandler struct {
2020
username string
2121
password string
2222
token string
23+
group string
2324
}
2425

2526
func (h *URLHandler) CRDs() ([]*pkg.SchemaType, error) {
@@ -50,5 +51,9 @@ func (h *URLHandler) CRDs() ([]*pkg.SchemaType, error) {
5051
return nil, nil
5152
}
5253

54+
if h.group != "" {
55+
schemaType.Rendering = pkg.Rendering{Group: h.group}
56+
}
57+
5358
return []*pkg.SchemaType{schemaType}, nil
5459
}

docs/release_notes/v1.1.2.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Release v1.1.2
2+
3+
- feat: extend the configuration file with the other providers #157
4+
5+
The configuration file has been extended with all the providers.
6+
7+
```yaml
8+
apiGroups:
9+
- name: "com.aws.services"
10+
description: "Resources related to AWS services"
11+
files: # files and folders can be defined together or on their own
12+
- sample-crd/infrastructure.cluster.x-k8s.io_awsclusters.yaml
13+
- sample-crd/delivery.krok.app_krokcommands
14+
- name: "com.azure.services"
15+
description: "Resources related to Azure services"
16+
folders:
17+
- azure-crds
18+
- name: "whatever"
19+
urls:
20+
- url: https://raw.githubusercontent.com/Skarlso/crd-bootstrap/refs/heads/main/crd-bootstrap/crds/delivery.crd-bootstrap_bootstraps.yaml
21+
gitUrls:
22+
- url: [email protected]:Skarlso/crd-bootstrap
23+
- url: [email protected]:crossplane/crossplane
24+
```

pkg/config_file_schema.go

-14
This file was deleted.

0 commit comments

Comments
 (0)