Pull factories out of the bowels.

This commit is contained in:
Jeffrey Regan
2018-07-23 11:46:17 -07:00
parent 5715f4bab4
commit 969f4f28fa
9 changed files with 95 additions and 99 deletions

View File

@@ -2,39 +2,40 @@ package configmapandsecret
import (
"context"
"os/exec"
"path/filepath"
"time"
"github.com/kubernetes-sigs/kustomize/pkg/fs"
"github.com/kubernetes-sigs/kustomize/pkg/types"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"os/exec"
"path/filepath"
"time"
)
// SecretFactory makes Secrets.
type SecretFactory struct {
args types.SecretArgs
fSys fs.FileSystem
wd string
}
// NewSecretFactory returns a new SecretFactory.
func NewSecretFactory(args types.SecretArgs, fSys fs.FileSystem) *SecretFactory {
return &SecretFactory{args: args, fSys: fSys}
func NewSecretFactory(fSys fs.FileSystem, wd string) *SecretFactory {
return &SecretFactory{fSys: fSys, wd: wd}
}
// MakeSecret returns a new secret.
func (f *SecretFactory) MakeSecret(wd string) (*corev1.Secret, error) {
func (f *SecretFactory) MakeSecret(args types.SecretArgs) (*corev1.Secret, error) {
s := &corev1.Secret{}
s.APIVersion = "v1"
s.Kind = "Secret"
s.Name = f.args.Name
s.Type = corev1.SecretType(f.args.Type)
s.Name = args.Name
s.Type = corev1.SecretType(args.Type)
if s.Type == "" {
s.Type = corev1.SecretTypeOpaque
}
s.Data = map[string][]byte{}
for k, v := range f.args.Commands {
out, err := f.createSecretKey(wd, v)
for k, v := range args.Commands {
out, err := f.createSecretKey(v)
if err != nil {
return nil, errors.Wrap(err, "createSecretKey")
}
@@ -44,16 +45,16 @@ func (f *SecretFactory) MakeSecret(wd string) (*corev1.Secret, error) {
}
// Run a command, return its output as the secret.
func (f *SecretFactory) createSecretKey(wd string, command string) ([]byte, error) {
if !f.fSys.IsDir(wd) {
wd = filepath.Dir(wd)
if !f.fSys.IsDir(wd) {
return nil, errors.New("not a directory: " + wd)
func (f *SecretFactory) createSecretKey(command string) ([]byte, error) {
if !f.fSys.IsDir(f.wd) {
f.wd = filepath.Dir(f.wd)
if !f.fSys.IsDir(f.wd) {
return nil, errors.New("not a directory: " + f.wd)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "sh", "-c", command)
cmd.Dir = wd
cmd.Dir = f.wd
return cmd.Output()
}