Skip to content

Modify apply logic in direct.go #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions pkg/patterns/declarative/pkg/applier/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package applier

import (
"context"
"io/ioutil"
"os"
"strings"

"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/kubectl/pkg/cmd/apply"
cmdDelete "k8s.io/kubectl/pkg/cmd/delete"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
Expand All @@ -27,24 +26,41 @@ func (d *DirectApplier) Apply(ctx context.Context,
validate bool,
extraArgs ...string,
) error {

tmpFile, err := ioutil.TempFile("", "tmp-manifest-*.yaml")
if err != nil {
return err
}
tmpFile.Write([]byte(manifest))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the error handling here is really tricky. I would split this into a function, like this:

func WriteTempFile(dir, pattern string, contents []byte) (string, error) {
  tmpFile, err := ioutil.TempFile(dir, pattern)
  if err != nil {
    return "", err
  }
  success := false
  defer func() {
    if !success {
      tmpFile.Close()
      os.Remove(tmpFile.Name())
    }
  }()
  if _, err :=  tmpFile.Write(contents); err != nil {
    return "", err
  }
  if err := tmpFile.Close(); err != nil {
    return "", err
  }
  success = true // don't remove the temp file
  return tmpFile.Name(), nil
}

I'm happy to send a follow-on PR to add this if that's easier!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you to comment 😄
Current codes are something rough idea, so any review comments are very appreciate.

tmpFile.Close()
defer os.Remove(tmpFile.Name())
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
In: tmpFile,
Out: os.Stdout,
ErrOut: os.Stderr,
}

restClient := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
ioReader := strings.NewReader(manifest)
f := cmdutil.NewFactory(restClient)
schema, err := f.Validator(validate)
if err != nil {
return err
}
applyOpts := apply.NewApplyOptions(ioStreams)

b := resource.NewBuilder(restClient)
res := b.Unstructured().Stream(ioReader, "manifestString").Do()
infos, err := res.Infos()
applyOpts.DynamicClient, err = f.DynamicClient()
if err != nil {
return err
}
applyOpts.DeleteOptions = applyOpts.DeleteFlags.ToOptions(applyOpts.DynamicClient, applyOpts.IOStreams)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we overwrite DeleteOptions on line 69 below?


applyOpts := apply.NewApplyOptions(ioStreams)
applyOpts.Namespace = namespace
applyOpts.SetObjects(infos)
applyOpts.Namespace, applyOpts.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
if namespace != "" {
applyOpts.Namespace = namespace
}
applyOpts.Validator = schema
applyOpts.Builder = f.NewBuilder()
applyOpts.Mapper, err = f.ToRESTMapper()
applyOpts.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
applyOpts.PrintFlags.NamePrintFlags.Operation = operation
cmdutil.PrintFlagsWithDryRunStrategy(applyOpts.PrintFlags, applyOpts.DryRunStrategy)
Expand All @@ -53,6 +69,7 @@ func (d *DirectApplier) Apply(ctx context.Context,
applyOpts.DeleteOptions = &cmdDelete.DeleteOptions{
IOStreams: ioStreams,
}
applyOpts.DeleteOptions.Filenames = []string{tmpFile.Name()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code, it seems that despite being in DeleteOptions.Filenames, this the filename of the objects we want to apply. Is that right? If that's right, do you mind adding a comment?


return applyOpts.Run()
}