Skip to content

Commit 9f479ca

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. dummy
1 parent b731a62 commit 9f479ca

File tree

1 file changed

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

1 file changed

+22
-10
lines changed

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

Lines changed: 22 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,36 @@ func (d *DirectApplier) Apply(ctx context.Context,
2726
validate bool,
2827
extraArgs ...string,
2928
) error {
29+
tmpFile, err := ioutil.TempFile("", "tmp-manifest")
30+
if err != nil {
31+
return err
32+
}
33+
tmpFile.Write([]byte(manifest))
34+
tmpFile.Close()
35+
tmpFile, err = os.Open(tmpFile.Name())
36+
defer os.Remove(tmpFile.Name())
37+
os.Stdin = tmpFile
38+
3039
ioStreams := genericclioptions.IOStreams{
3140
In: os.Stdin,
3241
Out: os.Stdout,
3342
ErrOut: os.Stderr,
3443
}
35-
restClient := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
36-
ioReader := strings.NewReader(manifest)
3744

38-
b := resource.NewBuilder(restClient)
39-
res := b.Unstructured().Stream(ioReader, "manifestString").Do()
40-
infos, err := res.Infos()
45+
restClient := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
46+
f := cmdutil.NewFactory(restClient)
47+
schema, err := f.Validator(validate)
4148
if err != nil {
4249
return err
4350
}
44-
4551
applyOpts := apply.NewApplyOptions(ioStreams)
46-
applyOpts.Namespace = namespace
47-
applyOpts.SetObjects(infos)
52+
applyOpts.Namespace, applyOpts.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
53+
if namespace != "" {
54+
applyOpts.Namespace = namespace
55+
}
56+
applyOpts.Validator = schema
57+
applyOpts.Builder = f.NewBuilder()
58+
applyOpts.Mapper, err = f.ToRESTMapper()
4859
applyOpts.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
4960
applyOpts.PrintFlags.NamePrintFlags.Operation = operation
5061
cmdutil.PrintFlagsWithDryRunStrategy(applyOpts.PrintFlags, applyOpts.DryRunStrategy)
@@ -53,6 +64,7 @@ func (d *DirectApplier) Apply(ctx context.Context,
5364
applyOpts.DeleteOptions = &cmdDelete.DeleteOptions{
5465
IOStreams: ioStreams,
5566
}
67+
applyOpts.DeleteOptions.Filenames = []string{"-"}
5668

5769
return applyOpts.Run()
5870
}

0 commit comments

Comments
 (0)