shoebill/pkg/chart/chart.go

172 lines
4.2 KiB
Go
Raw Normal View History

2023-12-20 16:36:14 +00:00
package chart
import (
"fmt"
2023-12-20 21:19:08 +00:00
"os"
"strings"
2023-12-20 16:36:14 +00:00
"git.badhouseplants.net/allanger/shoebill/internal/utils/helmhelper"
2023-12-20 21:19:08 +00:00
"git.badhouseplants.net/allanger/shoebill/pkg/chart/patches"
2023-12-20 16:36:14 +00:00
"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
2023-12-20 21:19:08 +00:00
Extensions []string
Patches []string
2023-12-20 16:36:14 +00:00
// 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)
2023-12-20 21:19:08 +00:00
path, err := hh.PullChart(workDir, ch.ToHelmReleaseData())
2023-12-20 16:36:14 +00:00
if err != nil {
return err
}
2023-12-20 21:19:08 +00:00
for _, ext := range ch.Extensions {
2023-12-20 16:36:14 +00:00
2023-12-20 21:19:08 +00:00
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())
2023-12-20 16:36:14 +00:00
if err != nil {
return err
}
}
}
return nil
}
2023-12-20 21:19:08 +00:00
2023-12-20 16:36:14 +00:00
// 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
}
2023-12-20 21:19:08 +00:00
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], "./", ""))
}
}
2023-12-20 16:36:14 +00:00
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,
}
}