A big progress
A big progress
This commit is contained in:
@ -2,8 +2,11 @@ package chart
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.badhouseplants.net/allanger/shoebill/internal/utils/helmhelper"
|
||||
"git.badhouseplants.net/allanger/shoebill/pkg/chart/patches"
|
||||
"git.badhouseplants.net/allanger/shoebill/pkg/mirror"
|
||||
"git.badhouseplants.net/allanger/shoebill/pkg/repository"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -20,6 +23,8 @@ type Chart struct {
|
||||
// Version of a chart
|
||||
Version string
|
||||
Mirrors []string
|
||||
Extensions []string
|
||||
Patches []string
|
||||
// Private fields that should be pupulated during the run-time
|
||||
RepositoryObj *repository.Repository `yaml:"-"`
|
||||
MirrorObjs mirror.Mirrors `yaml:"-"`
|
||||
@ -45,13 +50,52 @@ func (r *Chart) MirrorObjsFromName(mirrors mirror.Mirrors) {
|
||||
func (ch *Chart) SyncMirrors(workDir string, mirrors mirror.Mirrors, hh helmhelper.Helmhelper) error {
|
||||
if len(ch.Mirrors) > 0 {
|
||||
ch.MirrorObjsFromName(mirrors)
|
||||
_, err := hh.PullChart(workDir, ch.ToHelmReleaseData())
|
||||
path, err := hh.PullChart(workDir, ch.ToHelmReleaseData())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, mr := range ch.MirrorObjs {
|
||||
for _, ext := range ch.Extensions {
|
||||
|
||||
err := hh.PushChart(workDir, mr.OCI.URL, mr.OCI.Prefix, "", "", ch.ToHelmReleaseData())
|
||||
files, err := os.ReadDir(ext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, filePath := range files {
|
||||
extensionFilePath := fmt.Sprintf("%s/%s", ext, filePath.Name())
|
||||
|
||||
file, err := os.ReadFile(extensionFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Info(path)
|
||||
extenrsionTargetDir := fmt.Sprintf("%s/templates/extensions", path)
|
||||
if err := os.MkdirAll(extenrsionTargetDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
extensionTargetPath := fmt.Sprintf("%s/%s", extenrsionTargetDir, filePath.Name())
|
||||
if err := os.WriteFile(extensionTargetPath, file, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, patch := range ch.Patches {
|
||||
files, err := os.ReadDir(patch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, filePath := range files {
|
||||
fullPatchPath := fmt.Sprintf("%s/%s", patch, filePath.Name())
|
||||
ptch, err := patches.NewPatchFromFile(fullPatchPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ptch.Apply(path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, mr := range ch.MirrorObjs {
|
||||
err := hh.PushChart(path, mr.OCI.URL, mr.OCI.Prefix, "", "", ch.ToHelmReleaseData())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -59,6 +103,8 @@ func (ch *Chart) SyncMirrors(workDir string, mirrors mirror.Mirrors, hh helmhelp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// RepositoryObjFromName gather the whole repository object by its name
|
||||
func (r *Chart) RepositoryObjFromName(repos repository.Repositories) error {
|
||||
for _, repo := range repos {
|
||||
@ -97,6 +143,18 @@ func (r *Chart) VersionHandler(dir string, hh helmhelper.Helmhelper) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Chart) ExtensionsHandler(dir string) {
|
||||
for i := range r.Extensions {
|
||||
r.Extensions[i] = fmt.Sprintf("%s/%s", dir, strings.ReplaceAll(r.Extensions[i], "./", ""))
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Chart) PatchesHandler(dir string) {
|
||||
for i := range r.Patches {
|
||||
r.Patches[i] = fmt.Sprintf("%s/%s", dir, strings.ReplaceAll(r.Patches[i], "./", ""))
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Chart) ToHelmReleaseData() *helmhelper.ChartData {
|
||||
// valuesData =
|
||||
// for _, data := range r.DestValues {
|
||||
|
72
pkg/chart/patches/patches.go
Normal file
72
pkg/chart/patches/patches.go
Normal file
@ -0,0 +1,72 @@
|
||||
package patches
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Patch struct {
|
||||
Name string
|
||||
Targets []string
|
||||
Before string
|
||||
After string
|
||||
}
|
||||
|
||||
type Patches []*Patch
|
||||
|
||||
func NewPatchFromFile(filePath string) (*Patch, error){
|
||||
var patch Patch
|
||||
logrus.Infof("reading a new patch file: %s", filePath)
|
||||
patchFile, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(patchFile, &patch); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &patch, nil
|
||||
}
|
||||
|
||||
func(p *Patch) Apply(chartDir string) error {
|
||||
logrus.Infof("Applying patch: %s", p.Name)
|
||||
if len(p.Before) > 0 {
|
||||
beforeCompiled, err := regexp.Compile(p.Before)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, target := range p.Targets {
|
||||
fullTarget := fmt.Sprintf("%s/%s", chartDir, target)
|
||||
file, err := os.ReadFile(fullTarget)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newfile := beforeCompiled.ReplaceAll(file, []byte(p.After))
|
||||
err = os.WriteFile(fullTarget, newfile, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, target := range p.Targets {
|
||||
fullTarget := fmt.Sprintf("%s/%s", chartDir, target)
|
||||
f, err := os.OpenFile(fullTarget, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := f.Write([]byte(p.After + "\n")); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -35,7 +35,6 @@ type Release struct {
|
||||
// Private fields that should be pupulated during the run-time
|
||||
RepositoryObj *repository.Repository `yaml:"-"`
|
||||
ChartObj *chart.Chart `yaml:"-"`
|
||||
MirrorObj *mirror.Mirror `yaml:"-"`
|
||||
DestValues ValuesHolders `yaml:"-"`
|
||||
DestSecrets ValuesHolders `yaml:"-"`
|
||||
}
|
||||
@ -98,7 +97,7 @@ func (r *Release) MirrorObjFromName(mirrors mirror.Mirrors) error {
|
||||
}
|
||||
}
|
||||
if r.RepositoryObj == nil {
|
||||
return fmt.Errorf("couldn't gather the RepositoryObj for %s", r.Repository)
|
||||
return fmt.Errorf("couldn't gather the RepositoryObj from mirror for %s", r.Repository)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -249,11 +248,17 @@ func (src Releases) Diff(dest Releases) Diff {
|
||||
return diff
|
||||
}
|
||||
|
||||
func (rs *Releases) PopulateRepositories(repos repository.Repositories) error {
|
||||
func (rs *Releases) PopulateRepositories(repos repository.Repositories, mirrors mirror.Mirrors) error {
|
||||
for _, r := range *rs {
|
||||
if len(r.Mirror) > 0 {
|
||||
if err := r.MirrorObjFromName(mirrors); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := r.RepositoryObjFromName(repos); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user