172 lines
4.2 KiB
Go
172 lines
4.2 KiB
Go
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"
|
|
"k8s.io/utils/strings/slices"
|
|
)
|
|
type Chart struct {
|
|
// Internal name of the chart
|
|
Name string
|
|
// Official name of the chart
|
|
Chart string
|
|
// Name of the repository to pull from
|
|
// Defined in repositories
|
|
Repository string
|
|
// 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:"-"`
|
|
}
|
|
|
|
type Charts []*Chart
|
|
|
|
// Possible version placeholders
|
|
const (
|
|
VERSION_LATEST = "latest"
|
|
)
|
|
|
|
func (r *Chart) MirrorObjsFromName(mirrors mirror.Mirrors) {
|
|
var mirObj mirror.Mirrors
|
|
for _, mir := range mirrors{
|
|
if slices.Contains(r.Mirrors, mir.Name){
|
|
mirObj = append(mirObj, mir)
|
|
}
|
|
}
|
|
r.MirrorObjs = mirObj
|
|
}
|
|
|
|
func (ch *Chart) SyncMirrors(workDir string, mirrors mirror.Mirrors, hh helmhelper.Helmhelper) error {
|
|
if len(ch.Mirrors) > 0 {
|
|
ch.MirrorObjsFromName(mirrors)
|
|
path, err := hh.PullChart(workDir, ch.ToHelmReleaseData())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, ext := range ch.Extensions {
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
|
|
// RepositoryObjFromName gather the whole repository object by its name
|
|
func (r *Chart) RepositoryObjFromName(repos repository.Repositories) error {
|
|
for _, repo := range repos {
|
|
if repo.Name == r.Repository {
|
|
r.RepositoryObj = repo
|
|
}
|
|
}
|
|
if r.RepositoryObj == nil {
|
|
return fmt.Errorf("couldn't gather the RepositoryObj for %s", r.Repository)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (chs *Charts) PopulateRepositories(repos repository.Repositories) error {
|
|
for _, ch := range *chs {
|
|
if err := ch.RepositoryObjFromName(repos); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
// Replace the version placeholder with the fixed version
|
|
func (r *Chart) VersionHandler(dir string, hh helmhelper.Helmhelper) error {
|
|
if len(r.Version) == 0 {
|
|
r.Version = VERSION_LATEST
|
|
}
|
|
switch r.Version {
|
|
case VERSION_LATEST:
|
|
version, err := hh.FindLatestVersion(dir, r.ToHelmReleaseData())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r.Version = version
|
|
|
|
}
|
|
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 {
|
|
|
|
// }
|
|
logrus.Info(r)
|
|
return &helmhelper.ChartData{
|
|
Name: r.Chart,
|
|
Version: r.Version,
|
|
RepositoryName: r.RepositoryObj.Name,
|
|
RepositoryURL: r.RepositoryObj.URL,
|
|
RepositoryKind: r.RepositoryObj.Kind,
|
|
}
|
|
}
|