2023-07-20 09:26:25 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2023-09-22 11:02:56 +00:00
|
|
|
"errors"
|
2023-07-20 09:26:25 +00:00
|
|
|
"fmt"
|
2023-09-22 11:02:56 +00:00
|
|
|
"io"
|
2023-07-20 09:26:25 +00:00
|
|
|
"os"
|
2023-10-11 12:14:20 +00:00
|
|
|
"os/exec"
|
2023-09-22 11:02:56 +00:00
|
|
|
"path/filepath"
|
2023-07-20 09:26:25 +00:00
|
|
|
|
2023-08-02 15:00:34 +00:00
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/utils/diff"
|
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/utils/githelper"
|
2023-10-20 11:31:30 +00:00
|
|
|
"git.badhouseplants.net/allanger/shoebill/pkg/lockfile"
|
2023-10-12 09:53:58 +00:00
|
|
|
"git.badhouseplants.net/allanger/shoebill/pkg/release"
|
|
|
|
"git.badhouseplants.net/allanger/shoebill/pkg/repository"
|
2023-07-20 09:26:25 +00:00
|
|
|
release_v2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
|
|
|
|
helmrepo_v1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
|
2023-10-12 16:02:14 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-10-11 12:14:20 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2023-07-20 09:26:25 +00:00
|
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Flux struct {
|
2023-10-11 12:14:20 +00:00
|
|
|
path string
|
|
|
|
sopsBin string
|
|
|
|
gh githelper.Githelper
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 12:14:20 +00:00
|
|
|
func FluxProvider(path, sopsBin string, gh githelper.Githelper) Provider {
|
2023-07-20 09:26:25 +00:00
|
|
|
return &Flux{
|
2023-10-11 12:14:20 +00:00
|
|
|
path: path,
|
|
|
|
sopsBin: sopsBin,
|
|
|
|
gh: gh,
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 11:23:50 +00:00
|
|
|
// TODO: This function is ugly as hell, I need to do something about it
|
2023-10-20 11:31:30 +00:00
|
|
|
func (f *Flux) SyncState(releasesDiffs diff.ReleasesDiffs, repoDiffs diff.RepositoriesDiffs) (lockfile.HashesPerReleases, error) {
|
2023-07-20 09:26:25 +00:00
|
|
|
entity := "repository"
|
2023-10-12 11:23:50 +00:00
|
|
|
srcDirPath := fmt.Sprintf("%s/src", f.path)
|
|
|
|
// It should containe either release or repository as a prefix, because it's how files are called
|
|
|
|
entiryFilePath := fmt.Sprintf("%s/%s-", srcDirPath, entity)
|
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
for _, repository := range repoDiffs {
|
|
|
|
switch repository.Action {
|
|
|
|
case diff.ACTION_ADD:
|
|
|
|
manifest, err := GenerateRepository(repository.Wished)
|
|
|
|
if err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
file, err := os.Create(entiryFilePath + repository.Wished.Name + ".yaml")
|
|
|
|
if err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
if _, err := file.Write(manifest); err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
message := `chore(repository): Add a repo: %s
|
|
|
|
|
|
|
|
A new repo added to the cluster:
|
|
|
|
Name: %s
|
|
|
|
URL: %s
|
|
|
|
`
|
2023-10-20 11:31:30 +00:00
|
|
|
if _, err := f.gh.AddAllAndCommit(f.path, fmt.Sprintf(message, repository.Wished.Name, repository.Wished.Name, repository.Wished.URL)); err != nil {
|
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
2023-10-11 13:37:26 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
case diff.ACTION_PRESERVE:
|
|
|
|
case diff.ACTION_UPDATE:
|
|
|
|
manifest, err := GenerateRepository(repository.Wished)
|
|
|
|
if err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
if err := os.WriteFile(entiryFilePath+repository.Wished.Name+".yaml", manifest, os.ModeExclusive); err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
message := `chore(repository): Update a repo: %s
|
|
|
|
|
|
|
|
A repo has been updated:
|
|
|
|
Name: %s
|
|
|
|
URL: %s
|
|
|
|
`
|
2023-10-20 11:31:30 +00:00
|
|
|
if _, err := f.gh.AddAllAndCommit(f.path, fmt.Sprintf(message, repository.Wished.Name, repository.Wished.Name, repository.Wished.URL)); err != nil {
|
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
case diff.ACTION_DELETE:
|
|
|
|
if err := os.Remove(entiryFilePath + repository.Current.Name + ".yaml"); err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
message := `chore(repository): Removed a repo: %s
|
|
|
|
A repo has been removed from the cluster:
|
|
|
|
Name: %s
|
|
|
|
URL: %s
|
|
|
|
`
|
2023-10-20 11:31:30 +00:00
|
|
|
if _, err := f.gh.AddAllAndCommit(f.path, fmt.Sprintf(message, repository.Current.Name, repository.Current.Name, repository.Current.URL)); err != nil {
|
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
default:
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, fmt.Errorf("unknown action is requests: %s", repository.Action)
|
2023-09-22 11:02:56 +00:00
|
|
|
}
|
2023-07-20 09:26:25 +00:00
|
|
|
|
|
|
|
}
|
2023-10-20 11:31:30 +00:00
|
|
|
hashesPerReleases := lockfile.HashesPerReleases{}
|
2023-10-12 16:02:14 +00:00
|
|
|
entity = "release"
|
|
|
|
entiryFilePath = fmt.Sprintf("%s/%s-", srcDirPath, entity)
|
|
|
|
for _, release := range releasesDiffs {
|
2023-10-20 11:31:30 +00:00
|
|
|
var hash string
|
|
|
|
var err error
|
2023-10-12 16:02:14 +00:00
|
|
|
if err := SyncValues(release.Current, release.Wished, srcDirPath); err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-11 12:14:20 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
if err := SyncSecrets(release.Current, release.Wished, f.path, f.sopsBin); err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
switch release.Action {
|
|
|
|
case diff.ACTION_ADD:
|
|
|
|
manifest, err := GenerateRelease(release.Wished)
|
|
|
|
if err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
file, err := os.Create(entiryFilePath + release.Wished.Release + ".yaml")
|
|
|
|
if err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-07-20 09:26:25 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
if _, err := file.Write(manifest); err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
message := `chore(release): Add a new release: %s
|
|
|
|
|
|
|
|
A new release is added to the cluster:
|
|
|
|
Name: %s
|
|
|
|
Namespace: %s
|
|
|
|
Version: %s
|
|
|
|
Chart: %s/%s
|
|
|
|
`
|
2023-10-20 11:31:30 +00:00
|
|
|
hash, err = f.gh.AddAllAndCommit(f.path, fmt.Sprintf(message, release.Wished.Release, release.Wished.Release, release.Wished.Namespace, release.Wished.Version, release.Wished.Repository, release.Wished.Release))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
2023-07-20 09:26:25 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
case diff.ACTION_UPDATE:
|
|
|
|
manifest, err := GenerateRelease(release.Wished)
|
|
|
|
if err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
if err := os.WriteFile(entiryFilePath+release.Wished.Release+".yaml", manifest, os.ModeExclusive); err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-11 13:37:26 +00:00
|
|
|
}
|
2023-07-20 09:26:25 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
message := `chore(release): Update a release: %s
|
|
|
|
|
|
|
|
A release has been updated:
|
|
|
|
Name: %s
|
|
|
|
Namespace: %s
|
|
|
|
Version: %s
|
|
|
|
Chart: %s/%s
|
|
|
|
`
|
2023-10-20 11:31:30 +00:00
|
|
|
hash, err = f.gh.AddAllAndCommit(f.path, fmt.Sprintf(message, release.Wished.Release, release.Wished.Release, release.Wished.Namespace, release.Wished.Version, release.Wished.Repository, release.Wished.Release))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-10-11 13:37:26 +00:00
|
|
|
}
|
2023-07-20 09:26:25 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
case diff.ACTION_DELETE:
|
|
|
|
if err := os.Remove(entiryFilePath + release.Current.Release + ".yaml"); err != nil {
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
message := `chore(release): Remove a release: %s
|
|
|
|
|
|
|
|
A release has been removed from the cluster:
|
|
|
|
Name: %s
|
|
|
|
Namespace: %s
|
|
|
|
Version: %s
|
|
|
|
Chart: %s/%s
|
|
|
|
`
|
2023-10-20 11:31:30 +00:00
|
|
|
hash, err = f.gh.AddAllAndCommit(f.path, fmt.Sprintf(message, release.Current.Release, release.Current.Release, release.Current.Namespace, release.Current.Version, release.Current.Repository, release.Current.Release))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2023-10-20 11:31:30 +00:00
|
|
|
return nil, fmt.Errorf("unknown action is requests: %s", release.Action)
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
2023-12-20 16:36:14 +00:00
|
|
|
if release.Wished != nil {
|
|
|
|
hashPerRelease := &lockfile.HashPerRelease{
|
|
|
|
Release: release.Wished.Release,
|
|
|
|
Namespace: release.Wished.Namespace,
|
|
|
|
CommitHash: hash,
|
|
|
|
}
|
|
|
|
hashesPerReleases = append(hashesPerReleases, hashPerRelease)
|
|
|
|
}
|
2023-10-20 11:31:30 +00:00
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 11:31:30 +00:00
|
|
|
return hashesPerReleases, nil
|
2023-07-20 09:26:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateRepository(repo *repository.Repository) ([]byte, error) {
|
|
|
|
fluxRepo := &helmrepo_v1beta2.HelmRepository{
|
|
|
|
TypeMeta: v1.TypeMeta{
|
|
|
|
Kind: helmrepo_v1beta2.HelmRepositoryKind,
|
|
|
|
APIVersion: helmrepo_v1beta2.GroupVersion.String(),
|
|
|
|
},
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: repo.Name,
|
2023-10-11 12:14:20 +00:00
|
|
|
Namespace: "flux-system",
|
2023-07-20 09:26:25 +00:00
|
|
|
},
|
|
|
|
Spec: helmrepo_v1beta2.HelmRepositorySpec{
|
|
|
|
URL: repo.URL,
|
|
|
|
Type: repo.Kind,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return yaml.Marshal(&fluxRepo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateRelease and put
|
|
|
|
func GenerateRelease(release *release.Release) ([]byte, error) {
|
|
|
|
fluxRelease := &release_v2beta1.HelmRelease{
|
|
|
|
TypeMeta: v1.TypeMeta{
|
|
|
|
Kind: release_v2beta1.HelmReleaseKind,
|
|
|
|
APIVersion: release_v2beta1.GroupVersion.String(),
|
|
|
|
},
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: release.Release,
|
2023-10-11 12:14:20 +00:00
|
|
|
Namespace: "flux-system",
|
2023-07-20 09:26:25 +00:00
|
|
|
},
|
|
|
|
Spec: release_v2beta1.HelmReleaseSpec{
|
|
|
|
Chart: release_v2beta1.HelmChartTemplate{
|
|
|
|
Spec: release_v2beta1.HelmChartTemplateSpec{
|
|
|
|
Chart: release.Chart,
|
|
|
|
Version: release.Version,
|
|
|
|
SourceRef: release_v2beta1.CrossNamespaceObjectReference{
|
|
|
|
Kind: helmrepo_v1beta2.HelmRepositoryKind,
|
|
|
|
Name: release.RepositoryObj.Name,
|
2023-10-11 12:14:20 +00:00
|
|
|
Namespace: "flux-system",
|
2023-07-20 09:26:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReleaseName: release.Release,
|
|
|
|
Install: &release_v2beta1.Install{
|
|
|
|
CRDs: release_v2beta1.Create,
|
|
|
|
CreateNamespace: true,
|
|
|
|
},
|
2023-10-12 16:02:14 +00:00
|
|
|
TargetNamespace: release.Namespace,
|
2023-10-11 12:14:20 +00:00
|
|
|
ValuesFrom: []release_v2beta1.ValuesReference{},
|
2023-07-20 09:26:25 +00:00
|
|
|
},
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
for _, v := range release.Values {
|
|
|
|
filename := fmt.Sprintf("%s-%s", release.Release, filepath.Base(v))
|
|
|
|
fluxRelease.Spec.ValuesFrom = append(fluxRelease.Spec.ValuesFrom, release_v2beta1.ValuesReference{
|
|
|
|
Kind: "ConfigMap",
|
|
|
|
Name: filename,
|
|
|
|
ValuesKey: filename,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range release.Secrets {
|
|
|
|
filename := fmt.Sprintf("%s-%s", release.Release, filepath.Base(v))
|
|
|
|
fluxRelease.Spec.ValuesFrom = append(fluxRelease.Spec.ValuesFrom, release_v2beta1.ValuesReference{
|
|
|
|
Kind: "Secret",
|
|
|
|
Name: filename,
|
|
|
|
ValuesKey: filename,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
return yaml.Marshal(&fluxRelease)
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
func SyncValues(currentRelease, wishedRelease *release.Release, secDirPath string) error {
|
|
|
|
valuesDirPath := fmt.Sprintf("%s/values", secDirPath)
|
|
|
|
if currentRelease != nil {
|
|
|
|
for _, value := range currentRelease.DestValues {
|
|
|
|
valuesFilePath := fmt.Sprintf("%s/%s", valuesDirPath, value.DestPath)
|
|
|
|
logrus.Infof("trying to remove values file: %s", valuesFilePath)
|
|
|
|
if err := os.RemoveAll(valuesFilePath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
}
|
2023-10-12 16:02:14 +00:00
|
|
|
}
|
|
|
|
if wishedRelease != nil {
|
|
|
|
for _, value := range wishedRelease.DestValues {
|
|
|
|
// Prepare a dir for values
|
|
|
|
valuesPath := fmt.Sprintf("%s/%s", secDirPath, "values")
|
|
|
|
valuesFilePath := fmt.Sprintf("%s/%s", valuesDirPath, value.DestPath)
|
|
|
|
logrus.Infof("trying to create values file: %s", valuesFilePath)
|
|
|
|
if err := os.MkdirAll(valuesPath, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var valuesFile *os.File
|
|
|
|
if _, err := os.Stat(valuesFilePath); err == nil {
|
|
|
|
valuesFile, err = os.Open(valuesFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer valuesFile.Close()
|
|
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
|
|
valuesFile, err = os.Create(valuesFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer valuesFile.Close()
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
k8sConfigMapObj := corev1.ConfigMap{
|
|
|
|
TypeMeta: v1.TypeMeta{
|
|
|
|
Kind: "ConfigMap",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: value.DestPath,
|
|
|
|
Namespace: "flux-system",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"shoebill-release": wishedRelease.Release,
|
|
|
|
"shoebill-chart": wishedRelease.Chart,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Data: map[string]string{
|
|
|
|
value.DestPath: string(value.Data),
|
|
|
|
},
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
valuesFileData, err := yaml.Marshal(k8sConfigMapObj)
|
2023-09-22 11:02:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-12 16:02:14 +00:00
|
|
|
|
|
|
|
if err := os.WriteFile(valuesFilePath, valuesFileData, os.ModeAppend); err != nil {
|
2023-09-22 11:02:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
func SyncSecrets(currentRelease, wishedRelease *release.Release, workdirPath, sopsBin string) error {
|
|
|
|
secretsDirPath := fmt.Sprintf("%s/src/secrets", workdirPath)
|
2024-01-18 10:44:44 +00:00
|
|
|
if err := os.MkdirAll(secretsDirPath, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-12 16:02:14 +00:00
|
|
|
if currentRelease != nil {
|
|
|
|
for _, secrets := range currentRelease.DestSecrets {
|
|
|
|
secretsFilePath := fmt.Sprintf("%s/%s", secretsDirPath, secrets.DestPath)
|
|
|
|
logrus.Infof("trying to remove secrets file: %s", secretsFilePath)
|
|
|
|
if err := os.RemoveAll(secretsFilePath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
}
|
2023-10-12 16:02:14 +00:00
|
|
|
if wishedRelease != nil {
|
|
|
|
for _, secrets := range wishedRelease.DestSecrets {
|
|
|
|
// Prepare a dir for secrets
|
|
|
|
secretsFilePath := fmt.Sprintf("%s/%s", secretsDirPath, secrets.DestPath)
|
|
|
|
logrus.Infof("trying to create secrets file: %s", secretsFilePath)
|
|
|
|
var secretsFile *os.File
|
|
|
|
if _, err := os.Stat(secretsFilePath); err == nil {
|
|
|
|
secretsFile, err = os.Open(secretsFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer secretsFile.Close()
|
|
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
|
|
secretsFile, err = os.Create(secretsFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer secretsFile.Close()
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
k8sSecretObj := corev1.Secret{
|
|
|
|
TypeMeta: v1.TypeMeta{
|
|
|
|
Kind: "Secret",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: secrets.DestPath,
|
|
|
|
Namespace: "flux-system",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"shoebill-release": wishedRelease.Release,
|
|
|
|
"shoebill-chart": wishedRelease.Chart,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
|
|
|
secrets.DestPath: secrets.Data,
|
|
|
|
},
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
secretsFileData, err := yaml.Marshal(k8sSecretObj)
|
2023-10-11 12:14:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-12 16:02:14 +00:00
|
|
|
|
|
|
|
if err := os.WriteFile(secretsFilePath, secretsFileData, os.ModeAppend); err != nil {
|
2023-10-11 12:14:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
// I have to use the sops binary here, because they do not provide a go package that can be used for encryption :(
|
|
|
|
sopsConfPath := fmt.Sprintf("%s/.sops.yaml", workdirPath)
|
|
|
|
cmd := exec.Command(sopsBin, "--encrypt", "--in-place", "--config", sopsConfPath, secretsFilePath)
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
|
2023-10-12 16:02:14 +00:00
|
|
|
errMsg, _ := io.ReadAll(stderr)
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
err := fmt.Errorf("%s - %s", err, errMsg)
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 12:14:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|