WIP: Some refactoring

This commit is contained in:
Nikolai Rodionov
2023-10-12 13:23:50 +02:00
parent 6a7e541b82
commit c67a9c84e4
7 changed files with 127 additions and 79 deletions

View File

@ -18,6 +18,8 @@ type LockEntry struct {
Namespace string
RepoUrl string
RepoName string
Values []string
Secrets []string
}
type LockRepository struct {
@ -27,51 +29,52 @@ type LockRepository struct {
type LockFile []*LockEntry
func NewFromFile(dir string) (LockFile, error) {
// Init the LockFile object by reading the yaml file
func NewFromFile(lockfileDirPath string) (LockFile, error) {
var lockEntries LockFile
lockfilePath := fmt.Sprintf("%s/%s", dir, LOCKFILE_NAME)
lockfilePath := fmt.Sprintf("%s/%s", lockfileDirPath, LOCKFILE_NAME)
logrus.Infof("reading the lockfile file: %s", lockfilePath)
lockFile, err := os.ReadFile(lockfilePath)
lockFileData, err := os.ReadFile(lockfilePath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(lockFile, &lockEntries); err != nil {
if err := yaml.Unmarshal(lockFileData, &lockEntries); err != nil {
return nil, err
}
return lockEntries, nil
}
func (lockfile LockFile) ReposFromLockfile() (repository.Repositories, error) {
reposEntries := []LockRepository{}
repositories := repository.Repositories{}
for _, lockentry := range lockfile {
newRepoEntry := &LockRepository{
newRepoEntry := &repository.Repository{
URL: lockentry.RepoUrl,
Name: lockentry.RepoName,
}
reposEntries = append(reposEntries, *newRepoEntry)
repositories = append(repositories, newRepoEntry)
}
allKeys := make(map[string]bool)
dedupedEntries := []LockRepository{}
for _, repo := range reposEntries {
// Lockfile contains an entry per a release, so one repo might be met several times
allKeys := make(map[string]bool)
dedupedRepositories := repository.Repositories{}
for _, repo := range repositories {
if _, value := allKeys[repo.Name]; !value {
allKeys[repo.Name] = true
dedupedEntries = append(dedupedEntries, repo)
dedupedRepositories = append(dedupedRepositories, repo)
}
}
repos := repository.Repositories{}
for _, repoEntry := range dedupedEntries {
repo := &repository.Repository{
Name: repoEntry.Name,
URL: repoEntry.URL,
}
if err := repo.KindFromUrl(); err != nil {
for _, repoEntry := range dedupedRepositories {
if err := repoEntry.KindFromUrl(); err != nil {
return nil, err
}
repos = append(repos, repo)
}
return repos, nil
return dedupedRepositories, nil
}
func (lf LockFile) WriteToFile(dir string) error {

View File

@ -2,6 +2,7 @@ package release
import (
"fmt"
"path/filepath"
"reflect"
"strings"
@ -30,6 +31,14 @@ type Release struct {
// Private fields that should be pupulated during the run-time
RepositoryObj *repository.Repository `yaml:"-"`
UnencryptedSecrets map[string][]byte `yaml:"-"`
DestValues []ValuesHolder `yaml:"-"`
DestSecrets []string `yaml:"-"`
}
type ValuesHolder struct {
SrcPath string
DestPath string
Data []byte
}
type Releases []*Release
@ -74,6 +83,11 @@ func (r *Release) VersionHandler(dir string, hh helmhelper.Helmhelper) error {
func (r *Release) ValuesHandler(dir string) {
for i := range r.Values {
r.Values[i] = fmt.Sprintf("%s/%s", dir, strings.ReplaceAll(r.Values[i], "./", ""))
destValues := fmt.Sprintf("%s-%s-%s", r.Namespace, r.Release, filepath.Base(r.Values[i]))
r.DestValues = append(r.DestValues, ValuesHolder{
SrcPath: r.Values[i],
DestPath: destValues,
})
}
}
@ -84,6 +98,8 @@ func (r *Release) SecretsHandler(dir string, sops sopshelper.SopsHelper) error {
if err != nil {
return err
}
destSecrets := fmt.Sprintf("%s-%s-%s", r.Namespace, r.Release, filepath.Base(r.Secrets[i]))
r.DestSecrets = append(r.DestSecrets, destSecrets)
r.UnencryptedSecrets[path] = res
}
return nil
@ -91,13 +107,15 @@ func (r *Release) SecretsHandler(dir string, sops sopshelper.SopsHelper) error {
func FindReleaseByNames(releases []string, releasesObj Releases) Releases {
result := Releases{}
for _, rObj := range releasesObj {
for _, r := range releases {
if rObj.Release == r {
result = append(result, rObj)
for _, repoObj := range releasesObj {
for _, release := range releases {
if repoObj.Release == release {
result = append(result, repoObj)
}
}
}
return result
}
@ -135,6 +153,8 @@ func (r *Release) LockEntry() *lockfile.LockEntry {
Namespace: r.Namespace,
RepoUrl: r.RepositoryObj.URL,
RepoName: r.RepositoryObj.Name,
Values: r.DestValues,
Secrets: r.DestSecrets,
}
}

View File

@ -42,6 +42,10 @@ func (r *Repository) ValidateURL() error {
func (r *Repository) KindFromUrl() error {
// It panics if URL is not valid,
// but invalid url should not pass the ValidateURL function
if err := r.ValidateURL(); err != nil {
return err
}
prefix := r.URL[:strings.IndexByte(r.URL, ':')]
switch prefix {
case "oci":