Skip to content

Commit f7388b7

Browse files
authored
Merge pull request kubernetes-sigs#101 from SomtochiAma/cli-utils
Uses new apply function so we don't need kubectl
2 parents 77aa147 + 8367f1f commit f7388b7

File tree

7 files changed

+137
-13
lines changed

7 files changed

+137
-13
lines changed

go.mod

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ require (
77
github.com/evanphx/json-patch v4.5.0+incompatible
88
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
99
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
10-
k8s.io/api v0.18.2
11-
k8s.io/apimachinery v0.18.2
12-
k8s.io/client-go v0.18.2
10+
k8s.io/api v0.18.4
11+
k8s.io/apimachinery v0.18.4
12+
k8s.io/cli-runtime v0.18.4
13+
k8s.io/client-go v0.18.4
14+
k8s.io/kubectl v0.18.4
1315
sigs.k8s.io/controller-runtime v0.6.0
1416
sigs.k8s.io/kustomize/api v0.3.2
1517
sigs.k8s.io/yaml v1.2.0

go.sum

+53
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package applier
2+
3+
import (
4+
"context"
5+
"k8s.io/cli-runtime/pkg/genericclioptions"
6+
"k8s.io/cli-runtime/pkg/printers"
7+
"k8s.io/cli-runtime/pkg/resource"
8+
"k8s.io/kubectl/pkg/cmd/apply"
9+
cmdDelete "k8s.io/kubectl/pkg/cmd/delete"
10+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
11+
"os"
12+
"strings"
13+
)
14+
15+
type DirectApplier struct {
16+
a apply.ApplyOptions
17+
}
18+
19+
func NewDirectApplier() *DirectApplier {
20+
return &DirectApplier{}
21+
}
22+
23+
func (d *DirectApplier) Apply(ctx context.Context,
24+
namespace string,
25+
manifest string,
26+
validate bool,
27+
extraArgs ...string,
28+
) error {
29+
ioStreams := genericclioptions.IOStreams{
30+
In: os.Stdin,
31+
Out: os.Stdout,
32+
ErrOut: os.Stderr,
33+
}
34+
restClient := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
35+
ioReader := strings.NewReader(manifest)
36+
37+
b := resource.NewBuilder(restClient)
38+
res := b.Unstructured().Stream(ioReader, "manifestString").Do()
39+
infos, err := res.Infos()
40+
if err != nil {
41+
return err
42+
}
43+
44+
applyOpts := apply.NewApplyOptions(ioStreams)
45+
applyOpts.Namespace = namespace
46+
applyOpts.SetObjects(infos)
47+
applyOpts.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
48+
applyOpts.PrintFlags.NamePrintFlags.Operation = operation
49+
cmdutil.PrintFlagsWithDryRunStrategy(applyOpts.PrintFlags, applyOpts.DryRunStrategy)
50+
return applyOpts.PrintFlags.ToPrinter()
51+
}
52+
applyOpts.DeleteOptions = &cmdDelete.DeleteOptions{
53+
IOStreams: ioStreams,
54+
}
55+
56+
return applyOpts.Run()
57+
}
58+

pkg/patterns/declarative/pkg/kubectlcmd/client.go renamed to pkg/patterns/declarative/pkg/applier/exec.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package kubectlcmd
17+
package applier
1818

1919
import (
2020
"bytes"
@@ -28,12 +28,12 @@ import (
2828
)
2929

3030
// New creates a Client that runs kubectl avaliable on the path with default authentication
31-
func New() *Client {
32-
return &Client{cmdSite: &console{}}
31+
func NewExec() *ExecKubectl {
32+
return &ExecKubectl{cmdSite: &console{}}
3333
}
3434

35-
// Client provides an interface to kubectl
36-
type Client struct {
35+
// ExecKubectl provides an interface to kubectl
36+
type ExecKubectl struct {
3737
cmdSite commandSite
3838
}
3939

@@ -49,7 +49,8 @@ func (console) Run(c *exec.Cmd) error {
4949
}
5050

5151
// Apply runs the kubectl apply with the provided manifest argument
52-
func (c *Client) Apply(ctx context.Context, namespace string, manifest string, validate bool, extraArgs ...string) error {
52+
func (c *ExecKubectl) Apply(ctx context.Context, namespace string, manifest string, validate bool,
53+
extraArgs ...string) error {
5354
log := log.Log
5455

5556
log.Info("applying manifest")
@@ -87,3 +88,4 @@ func (c *Client) Apply(ctx context.Context, namespace string, manifest string, v
8788

8889
return nil
8990
}
91+

pkg/patterns/declarative/pkg/kubectlcmd/client_test.go renamed to pkg/patterns/declarative/pkg/applier/exec_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package kubectlcmd
17+
package applier
1818

1919
import (
2020
"context"
@@ -84,7 +84,7 @@ func TestKubectlApply(t *testing.T) {
8484
for _, test := range tests {
8585
t.Run(test.name, func(t *testing.T) {
8686
cs := collector{Error: test.err}
87-
kubectl := &Client{cmdSite: &cs}
87+
kubectl := &ExecKubectl{cmdSite: &cs}
8888
err := kubectl.Apply(context.Background(), test.namespace, test.manifest, test.validate, test.args...)
8989

9090
if test.err != nil && err == nil {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package applier
2+
3+
import (
4+
"context"
5+
)
6+
7+
type Applier interface {
8+
Applyx(ctx context.Context, namespace string, manifest string, validate bool, extraArgs ...string) error
9+
}

pkg/patterns/declarative/reconciler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/log"
3333
"sigs.k8s.io/controller-runtime/pkg/manager"
3434
"sigs.k8s.io/controller-runtime/pkg/reconcile"
35-
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/kubectlcmd"
35+
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/applier"
3636
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
3737
"sigs.k8s.io/kustomize/api/filesys"
3838
"sigs.k8s.io/kustomize/api/krusty"
@@ -61,7 +61,7 @@ type DeclarativeObject interface {
6161
}
6262

6363
// For mocking
64-
var kubectl = kubectlcmd.New()
64+
var kubectl = applier.NewDirectApplier()
6565

6666
func (r *Reconciler) Init(mgr manager.Manager, prototype DeclarativeObject, opts ...reconcilerOption) error {
6767
r.prototype = prototype

0 commit comments

Comments
 (0)