-
Notifications
You must be signed in to change notification settings - Fork 83
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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)) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -53,6 +69,7 @@ func (d *DirectApplier) Apply(ctx context.Context, | |
applyOpts.DeleteOptions = &cmdDelete.DeleteOptions{ | ||
IOStreams: ioStreams, | ||
} | ||
applyOpts.DeleteOptions.Filenames = []string{tmpFile.Name()} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} |
There was a problem hiding this comment.
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:
I'm happy to send a follow-on PR to add this if that's easier!
There was a problem hiding this comment.
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.