Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
sync with bootstrap v0.6.0-rc.0-59-ga7044a67
Browse files Browse the repository at this point in the history
  • Loading branch information
Kam D Kasravi committed Aug 21, 2019
1 parent 1ba61cb commit 9730a0d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
12 changes: 7 additions & 5 deletions pkg/kfapp/kustomize/kustomize.go
Expand Up @@ -240,9 +240,9 @@ func (kustomize *kustomize) initK8sClients() error {

// Apply deploys kustomize generated resources to the kubenetes api server
func (kustomize *kustomize) Apply(resources kftypesv3.ResourceEnum) error {
apply := utils.NewApply().Namespace(kustomize.kfDef)
if apply.Error() != nil {
return apply.Error()
apply, err := utils.NewApply().Namespace(kustomize.kfDef)
if err != nil {
return err
}

kustomizeDir := path.Join(kustomize.kfDef.Spec.AppDir, outputDir)
Expand All @@ -263,7 +263,8 @@ func (kustomize *kustomize) Apply(resources kftypesv3.ResourceEnum) error {
Message: fmt.Sprintf("can not encode component %v as yaml Error %v", app.Name, err),
}
}
err = apply.Init(data).Run().Error()
defer apply.Cleanup()
err = apply.Init(data).Run()
if err != nil {
return err
}
Expand Down Expand Up @@ -292,7 +293,8 @@ func (kustomize *kustomize) Apply(resources kftypesv3.ResourceEnum) error {
if err != nil {
return err
}
err = apply.Init(body).Run().Error()
defer apply.Cleanup()
err = apply.Init(body).Run()
if err != nil {
return err
}
Expand Down
49 changes: 27 additions & 22 deletions pkg/utils/k8utils.go
Expand Up @@ -148,29 +148,32 @@ func DeleteResourceFromFile(config *rest.Config, filename string) error {
}

type Apply interface {
Cleanup() error
DefaultProfileNamespace(string) bool
Error() error
Init([]byte) Apply
Namespace(*kfdefs.KfDef) Apply
Run() Apply
Namespace(*kfdefs.KfDef) (Apply, error)
Run() error
}

type apply struct {
matchVersionKubeConfigFlags *cmdutil.MatchVersionFlags
factory cmdutil.Factory
clientset *kubernetes.Clientset
options *kubectlapply.ApplyOptions
error *kfapis.KfError
tmpfile *os.File
stdin *os.File
err *kfapis.KfError
}

func NewApply() Apply {
apply := &apply{
matchVersionKubeConfigFlags: cmdutil.NewMatchVersionFlags(genericclioptions.NewConfigFlags()),
err: nil,
}
apply.factory = cmdutil.NewFactory(apply.matchVersionKubeConfigFlags)
clientset, err := apply.factory.KubernetesClientSet()
if err != nil {
apply.error = &kfapis.KfError{
apply.err = &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: fmt.Sprintf("could not get clientset Error %v", err),
}
Expand All @@ -188,16 +191,15 @@ func (a *apply) DefaultProfileNamespace(name string) bool {
}

func (a *apply) Init(data []byte) Apply {
tmpfile := a.tempFile(data)
oldStdin := os.Stdin
os.Stdin = tmpfile
defer a.cleanup(tmpfile, oldStdin)
a.tmpfile = a.tempFile(data)
a.stdin = os.Stdin
os.Stdin = a.tmpfile
ioStreams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
a.options = kubectlapply.NewApplyOptions(ioStreams)
a.options.DeleteFlags = a.deleteFlags("that contains the configuration to apply")
initializeErr := a.init()
if initializeErr != nil {
a.error = &kfapis.KfError{
a.err = &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: fmt.Sprintf("could not initialize Error %v", initializeErr),
}
Expand All @@ -206,26 +208,29 @@ func (a *apply) Init(data []byte) Apply {
}

func (a *apply) Error() error {
return a.error
return a.err
}

func (a *apply) Run() Apply {
func (a *apply) Run() error {
resourcesErr := a.options.Run()
if resourcesErr != nil {
a.error = &kfapis.KfError{
return &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: fmt.Sprintf("Apply.Run Error %v", resourcesErr),
}
}
return a
return nil
}

func (a *apply) cleanup(tmpfile *os.File, stdin *os.File) error {
os.Stdin = stdin
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
func (a *apply) Cleanup() error {
os.Stdin = a.stdin
if a.tmpfile != nil {
if err := a.tmpfile.Close(); err != nil {
return err
}
return os.Remove(a.tmpfile.Name())
}
return os.Remove(tmpfile.Name())
return nil
}

func (a *apply) init() error {
Expand Down Expand Up @@ -272,7 +277,7 @@ func (a *apply) init() error {
return nil
}

func (a *apply) Namespace(kfDef *kfdefs.KfDef) Apply {
func (a *apply) Namespace(kfDef *kfdefs.KfDef) (Apply, error) {
namespace := kfDef.ObjectMeta.Namespace
log.Infof(string(kftypes.NAMESPACE)+": %v", namespace)
_, nsMissingErr := a.clientset.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{})
Expand All @@ -281,14 +286,14 @@ func (a *apply) Namespace(kfDef *kfdefs.KfDef) Apply {
nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}
_, nsErr := a.clientset.CoreV1().Namespaces().Create(nsSpec)
if nsErr != nil {
a.error = &kfapis.KfError{
return a, &kfapis.KfError{
Code: int(kfapis.INVALID_ARGUMENT),
Message: fmt.Sprintf("couldn't create %v %v Error: %v",
string(kftypes.NAMESPACE), namespace, nsErr),
}
}
}
return a
return a, nil
}

func (a *apply) tempFile(data []byte) *os.File {
Expand Down

0 comments on commit 9730a0d

Please sign in to comment.