shoebill/pkg/chart/chart.go
2023-12-20 17:36:14 +01:00

114 lines
2.7 KiB
Go

package chart
import (
"fmt"
"git.badhouseplants.net/allanger/shoebill/internal/utils/helmhelper"
"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
// 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)
_, err := hh.PullChart(workDir, ch.ToHelmReleaseData())
if err != nil {
return err
}
for _, mr := range ch.MirrorObjs {
err := hh.PushChart(workDir, 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) 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,
}
}