Skip to content

Commit

Permalink
fix: better skipSecrets handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zostay committed Aug 9, 2024
1 parent 62778a7 commit 5f6b460
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
19 changes: 16 additions & 3 deletions pkg/config/lazyTools.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/zostay/genifest/pkg/client/k8s"

k8scfg "github.com/zostay/genifest/pkg/config/kubecfg"
k8smgr "github.com/zostay/genifest/pkg/manager/k8scfg"
)

type LazyTools struct {
Expand Down Expand Up @@ -53,9 +54,9 @@ func (t *LazyTools) IAM() (*iam.Client, error) {
return t.iam, nil
}

func (t *LazyTools) ResMgr(ctx context.Context) (*k8scfg.Client, error) {
func (t *LazyTools) ResMgr(ctx context.Context, skipSecrets bool) (*k8scfg.Client, error) {
rmgr := k8scfg.New(t.cf.CloudHome)
rmgr.SetFuncMap(t.makeFuncMap(ctx, rmgr))
rmgr.SetFuncMap(t.makeFuncMap(ctx, rmgr, skipSecrets))
return rmgr, nil
}

Expand All @@ -64,6 +65,7 @@ func (t *LazyTools) ResMgr(ctx context.Context) (*k8scfg.Client, error) {
func (t *LazyTools) makeFuncMap(
ctx context.Context,
rmgr *k8scfg.Client,
skipSecrets bool,
) template.FuncMap {
aws := tmpltools.AWS{
Region: t.c.AWS.Region,
Expand All @@ -83,7 +85,7 @@ func (t *LazyTools) makeFuncMap(
return rmgr.TemplateConfigFile(name, []byte(data))
}

return template.FuncMap{
fm := template.FuncMap{
"tomlize": tmpltools.Tomlize,
"secretDict": ghost.SecretDict,
"ddbLookup": aws.DDBLookup,
Expand All @@ -96,4 +98,15 @@ func (t *LazyTools) makeFuncMap(
"zostaySecret": ghost.Secret,
"kubeseal": tmpltools.KubeSeal,
}

if skipSecrets {
secretsDie := func(_ ...interface{}) (string, error) {
return "", k8smgr.ErrSecret
}
fm["kubeseal"] = secretsDie
fm["sshKey"] = secretsDie
fm["zostaySecret"] = secretsDie
}

return fm
}
2 changes: 1 addition & 1 deletion pkg/manager/k8s/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func GenerateK8sResources(
continue
}

err = k8scfg.SaveResourceFile(ctx, tools, appDir, sr)
err = k8scfg.SaveResourceFile(ctx, tools, appDir, sr, skipSecrets)
if err != nil {
errs = append(errs, fmt.Errorf("k8scfg.SaveResourceFile(): %w", err))
errsThisTime++
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/k8scfg/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type Tools interface {
Kube() (*k8s.Client, error)

ResMgr(context.Context) (*k8scfg.Client, error)
ResMgr(context.Context, bool) (*k8scfg.Client, error)

IAM() (*iam.Client, error)
}
11 changes: 1 addition & 10 deletions pkg/manager/k8scfg/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,11 @@ func ProcessResourceFile(
config string,
skipSecrets bool,
) ([]k8scfg.Resource, error) {
c, err := tools.ResMgr(ctx)
c, err := tools.ResMgr(ctx, skipSecrets)
if err != nil {
return nil, fmt.Errorf("tools.ResMgr(): %w", err)
}

if skipSecrets {
secretsDie := func(_ ...interface{}) (string, error) {
return "", ErrSecret
}
c.SetFunc("kubeseal", secretsDie)
c.SetFunc("sshKey", secretsDie)
c.SetFunc("zostaySecret", secretsDie)
}

cfs, err := c.ReadResourceFile(config)
if err != nil {
return nil, fmt.Errorf("c.ReadResourceFile(): %w", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/manager/k8scfg/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func SaveResourceFile(
tools Tools,
saveDir string,
sr *k8s.SerializedResource,
skipSecrets bool,
) error {
c, err := tools.ResMgr(ctx)
c, err := tools.ResMgr(ctx, skipSecrets)
if err != nil {
return fmt.Errorf("tools.ResMgr(): %w", err)
}
Expand Down

0 comments on commit 5f6b460

Please sign in to comment.