Skip to content

Commit a1fe1ee

Browse files
committed
Modify apply logic in direct.go
This commit changes a logic to apply in direct.go, because current codes ignores some features which are validate, etc.
1 parent b731a62 commit a1fe1ee

File tree

1 file changed

+27
-10
lines changed
  • pkg/patterns/declarative/pkg/applier

1 file changed

+27
-10
lines changed

pkg/patterns/declarative/pkg/applier/direct.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package applier
22

33
import (
44
"context"
5+
"io/ioutil"
56
"os"
6-
"strings"
77

88
"k8s.io/cli-runtime/pkg/genericclioptions"
99
"k8s.io/cli-runtime/pkg/printers"
10-
"k8s.io/cli-runtime/pkg/resource"
1110
"k8s.io/kubectl/pkg/cmd/apply"
1211
cmdDelete "k8s.io/kubectl/pkg/cmd/delete"
1312
cmdutil "k8s.io/kubectl/pkg/cmd/util"
@@ -27,24 +26,41 @@ func (d *DirectApplier) Apply(ctx context.Context,
2726
validate bool,
2827
extraArgs ...string,
2928
) error {
29+
30+
tmpFile, err := ioutil.TempFile("", "tmp-manifest-*.yaml")
31+
if err != nil {
32+
return err
33+
}
34+
tmpFile.Write([]byte(manifest))
35+
tmpFile.Close()
36+
defer os.Remove(tmpFile.Name())
3037
ioStreams := genericclioptions.IOStreams{
31-
In: os.Stdin,
38+
In: tmpFile,
3239
Out: os.Stdout,
3340
ErrOut: os.Stderr,
3441
}
42+
3543
restClient := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
36-
ioReader := strings.NewReader(manifest)
44+
f := cmdutil.NewFactory(restClient)
45+
schema, err := f.Validator(validate)
46+
if err != nil {
47+
return err
48+
}
49+
applyOpts := apply.NewApplyOptions(ioStreams)
3750

38-
b := resource.NewBuilder(restClient)
39-
res := b.Unstructured().Stream(ioReader, "manifestString").Do()
40-
infos, err := res.Infos()
51+
applyOpts.DynamicClient, err = f.DynamicClient()
4152
if err != nil {
4253
return err
4354
}
55+
applyOpts.DeleteOptions = applyOpts.DeleteFlags.ToOptions(applyOpts.DynamicClient, applyOpts.IOStreams)
4456

45-
applyOpts := apply.NewApplyOptions(ioStreams)
46-
applyOpts.Namespace = namespace
47-
applyOpts.SetObjects(infos)
57+
applyOpts.Namespace, applyOpts.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
58+
if namespace != "" {
59+
applyOpts.Namespace = namespace
60+
}
61+
applyOpts.Validator = schema
62+
applyOpts.Builder = f.NewBuilder()
63+
applyOpts.Mapper, err = f.ToRESTMapper()
4864
applyOpts.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
4965
applyOpts.PrintFlags.NamePrintFlags.Operation = operation
5066
cmdutil.PrintFlagsWithDryRunStrategy(applyOpts.PrintFlags, applyOpts.DryRunStrategy)
@@ -53,6 +69,7 @@ func (d *DirectApplier) Apply(ctx context.Context,
5369
applyOpts.DeleteOptions = &cmdDelete.DeleteOptions{
5470
IOStreams: ioStreams,
5571
}
72+
applyOpts.DeleteOptions.Filenames = []string{tmpFile.Name()}
5673

5774
return applyOpts.Run()
5875
}

0 commit comments

Comments
 (0)