Skip to content

Commit

Permalink
move all generated resource in one parent folder
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Dec 3, 2021
1 parent 3c9db0f commit 0f86215
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 61 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ This generator allows you to convert a hosted helm chart into locally stored res
```
├── kustomization-generator.yaml
├── kustomization.yaml
└── templates
├── cainjector-deployment.yaml
├── ...
└── webhook-validating-webhook.yaml
└── generated
└── crds
└── ...
└── templates
└── ...
```

## Usage kustomize
Expand All @@ -48,9 +49,13 @@ This generator allows you to convert a remote kustomization into a locally store
```
├── kustomization-generator.yaml
├── kustomization.yaml
└── resources.yaml
└── generated
└── resources.yaml
```

## Usage download

TODO

## Installation

Expand Down
12 changes: 6 additions & 6 deletions internal/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import (
"github.com/spf13/viper"
)

type Kustomization struct {
Namespace string `yaml:"namespace"`
Resources []string `yaml:"resources"`
}

type Generator interface {
Generate(dir string) error
Generate(dir string) (*Kustomization, error)
}

func LoadGenerator(viperInst viper.Viper, path string) (*Generator, error) {
Expand Down Expand Up @@ -52,8 +57,3 @@ func LoadGenerator(viperInst viper.Viper, path string) (*Generator, error) {
}
return &result, nil
}

type kustomization struct {
Namespace string `yaml:"namespace"`
Resources []string `yaml:"resources"`
}
18 changes: 7 additions & 11 deletions internal/generator_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,31 @@ type DownloadGenerator struct {
Url string `yaml:"url"`
}

func (g DownloadGenerator) Generate(dir string) error {
func (g DownloadGenerator) Generate(dir string) (*Kustomization, error) {
req, err := http.NewRequest("GET", g.Url, nil)
client := &http.Client{}
if err != nil {
return fmt.Errorf("failed to download %s: %v", g.Url, err)
return nil, fmt.Errorf("failed to download %s: %v", g.Url, err)
}

resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to download %s: %v", g.Url, err)
return nil, fmt.Errorf("failed to download %s: %v", g.Url, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to download %s: %v", g.Url, err)
return nil, fmt.Errorf("failed to download %s: %v", g.Url, err)
}

kustomization := kustomization{
kustomization := Kustomization{
Namespace: g.Namespace,
Resources: []string{"resources.yaml"},
}
err = writeYamlFile(path.Join(dir, "kustomization.yaml"), kustomization)
if err != nil {
return fmt.Errorf("writing kustomization failed: %v", err)
}
err = ioutil.WriteFile(path.Join(dir, "resources.yaml"), body, 0o644)
if err != nil {
return fmt.Errorf("writing generated resource failed: %v", err)
return nil, err
}

return nil
return &kustomization, nil
}
43 changes: 15 additions & 28 deletions internal/generator_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,34 @@ type HelmGenerator struct {
Values map[string]interface{} `yaml:"values"`
}

func (g HelmGenerator) Generate(dir string) error {
func (g HelmGenerator) Generate(dir string) (*Kustomization, error) {
url, err := retrieveHelmChartUrl(g.Registry, g.Chart, g.Version)
if err != nil {
return err
return nil, err
}

valuesPath, err := ioutil.TempFile("", ".tmp-*-values.yaml")
valuesPath, err := ioutil.TempFile("", ".kustomization-generator-*-values.yaml")
if err != nil {
return fmt.Errorf("writing temporary values file failed: %v", err)
return nil, fmt.Errorf("writing temporary values file failed: %v", err)
}
defer os.Remove(valuesPath.Name())
valuesBytes, err := yaml.Marshal(g.Values)
if err != nil {
return fmt.Errorf("writing temporary values file failed: %v", err)
return nil, fmt.Errorf("writing temporary values file failed: %v", err)
}
err = os.WriteFile(valuesPath.Name(), valuesBytes, 0o600)
if err != nil {
return fmt.Errorf("writing temporary values file failed: %v", err)
return nil, fmt.Errorf("writing temporary values file failed: %v", err)
}

tempDir, err := ioutil.TempDir("", ".tmp-")
tempDir, err := ioutil.TempDir("", ".kustomization-generator-")
if err != nil {
return fmt.Errorf("preparing temporary folder failed: %v", err)
return nil, fmt.Errorf("preparing temporary folder failed: %v", err)
}
defer os.RemoveAll(tempDir)
helmPath, err := exec.LookPath("helm")
if err != nil {
return fmt.Errorf("executing helm failed: executable not found")
return nil, fmt.Errorf("executing helm failed: executable not found")
}
helmArgs := []string{
"template",
Expand All @@ -64,46 +64,33 @@ func (g HelmGenerator) Generate(dir string) error {
helmArgs = append(helmArgs, g.Args...)
helmOutput, err := exec.Command(helmPath, helmArgs...).CombinedOutput()
if err != nil {
return fmt.Errorf("executing helm failed: %v\n%s", err, string(helmOutput))
return nil, fmt.Errorf("executing helm failed: %v\n%s", err, string(helmOutput))
}

kustomization := kustomization{
kustomization := Kustomization{
Namespace: g.Namespace,
}
tempDir2 := path.Join(tempDir, g.Chart)
includes := []regexp.Regexp{*regexp.MustCompile(`\.ya?ml$`)}
excludes := []regexp.Regexp{}
files, err := fileList(tempDir2, includes, excludes)
if err != nil {
return fmt.Errorf("listing helm generated resources failed: %v", err)
return nil, fmt.Errorf("listing helm generated resources failed: %v", err)
}
for _, file := range *files {
rel, err := filepath.Rel(tempDir2, file)
if err != nil {
return fmt.Errorf("listing helm generated resources failed: %v", err)
return nil, fmt.Errorf("listing helm generated resources failed: %v", err)
}
kustomization.Resources = append(kustomization.Resources, rel)
}

err = os.RemoveAll(path.Join(dir, "crds"))
if err != nil {
return fmt.Errorf("cleaning up target failed: %v", err)
}
err = os.RemoveAll(path.Join(dir, "templates"))
if err != nil {
return fmt.Errorf("cleaning up target failed: %v", err)
}

err = writeYamlFile(path.Join(tempDir2, "kustomization.yaml"), kustomization)
if err != nil {
return fmt.Errorf("writing kustomization failed: %v", err)
}
err = copyDir(tempDir2, dir)
if err != nil {
return fmt.Errorf("copying files to target failed: %v", err)
return nil, fmt.Errorf("copying files to target failed: %v", err)
}

return nil
return &kustomization, nil
}

type helmRegistryIndex struct {
Expand Down
16 changes: 6 additions & 10 deletions internal/generator_kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type KustomizeGenerator struct {
Args []string `yaml:"args"`
}

func (g KustomizeGenerator) Generate(dir string) error {
func (g KustomizeGenerator) Generate(dir string) (*Kustomization, error) {
kustomizePath, err := exec.LookPath("kustomize")
if err != nil {
return fmt.Errorf("executing kustomize failed: executable not found")
return nil, fmt.Errorf("executing kustomize failed: executable not found")
}
kustomizeArgs := []string{
"build",
Expand All @@ -25,21 +25,17 @@ func (g KustomizeGenerator) Generate(dir string) error {
kustomizeArgs = append(kustomizeArgs, g.Args...)
kustomizeOutput, err := exec.Command(kustomizePath, kustomizeArgs...).CombinedOutput()
if err != nil {
return fmt.Errorf("executing kustomize failed: %v\n%s", err, string(kustomizeOutput))
return nil, fmt.Errorf("executing kustomize failed: %v\n%s", err, string(kustomizeOutput))
}

kustomization := kustomization{
kustomization := Kustomization{
Namespace: g.Namespace,
Resources: []string{"resources.yaml"},
}
err = writeYamlFile(path.Join(dir, "kustomization.yaml"), kustomization)
if err != nil {
return fmt.Errorf("writing kustomization failed: %v", err)
}
err = ioutil.WriteFile(path.Join(dir, "resources.yaml"), kustomizeOutput, 0o644)
if err != nil {
return fmt.Errorf("writing generated resource failed: %v", err)
return nil, err
}

return nil
return &kustomization, nil
}
36 changes: 35 additions & 1 deletion internal/run.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package internal

import (
"fmt"
"io/ioutil"
"os"
"path"
)

func Run(dir string, config Generator) error {
return config.Generate(dir)
tempDir, err := ioutil.TempDir("", ".kustomization-generator-")
if err != nil {
return fmt.Errorf("preparing temporary folder failed: %v", err)
}
defer os.RemoveAll(tempDir)

kustomization, err := config.Generate(tempDir)
if err != nil {
return err
}
for i, res := range kustomization.Resources {
kustomization.Resources[i] = path.Join("generated", res)
}
err = writeYamlFile(path.Join(dir, "kustomization.yaml"), kustomization)
if err != nil {
return fmt.Errorf("writing kustomization failed: %v", err)
}

os.RemoveAll(path.Join(dir, "generated"))
if err != nil {
return fmt.Errorf("clearing generated files failed: %v", err)
}
err = copyDir(tempDir, path.Join(dir, "generated"))
if err != nil {
return fmt.Errorf("copying generated files failed: %v", err)
}

return nil
}

0 comments on commit 0f86215

Please sign in to comment.