Skip to content

Uses new apply function so we don't need kubectl #101

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

Merged
merged 3 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ require (
github.com/evanphx/json-patch v4.5.0+incompatible
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
k8s.io/api v0.18.2
k8s.io/apimachinery v0.18.2
k8s.io/client-go v0.18.2
k8s.io/api v0.18.4
k8s.io/apimachinery v0.18.4
k8s.io/cli-runtime v0.18.4
k8s.io/client-go v0.18.4
k8s.io/kubectl v0.18.4
sigs.k8s.io/controller-runtime v0.6.0
sigs.k8s.io/kustomize/api v0.3.2
sigs.k8s.io/yaml v1.2.0
Expand Down
53 changes: 53 additions & 0 deletions go.sum

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions pkg/patterns/declarative/pkg/applier/direct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package applier

import (
"context"
"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"
"os"
"strings"
)

type DirectApplier struct {
a apply.ApplyOptions
}

func NewDirectApplier() *DirectApplier {
return &DirectApplier{}
}

func (d *DirectApplier) Apply(ctx context.Context,
namespace string,
manifest string,
validate bool,
extraArgs ...string,
) error {
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
restClient := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
ioReader := strings.NewReader(manifest)

b := resource.NewBuilder(restClient)
res := b.Unstructured().Stream(ioReader, "manifestString").Do()
infos, err := res.Infos()
if err != nil {
return err
}

applyOpts := apply.NewApplyOptions(ioStreams)
applyOpts.Namespace = namespace
applyOpts.SetObjects(infos)
applyOpts.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
applyOpts.PrintFlags.NamePrintFlags.Operation = operation
cmdutil.PrintFlagsWithDryRunStrategy(applyOpts.PrintFlags, applyOpts.DryRunStrategy)
return applyOpts.PrintFlags.ToPrinter()
}
applyOpts.DeleteOptions = &cmdDelete.DeleteOptions{
IOStreams: ioStreams,
}

return applyOpts.Run()
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package kubectlcmd
package applier

import (
"bytes"
Expand All @@ -28,12 +28,12 @@ import (
)

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

// Client provides an interface to kubectl
type Client struct {
// ExecKubectl provides an interface to kubectl
type ExecKubectl struct {
cmdSite commandSite
}

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

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

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

return nil
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package kubectlcmd
package applier

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

if test.err != nil && err == nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/patterns/declarative/pkg/applier/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package applier

import (
"context"
)

type Applier interface {
Applyx(ctx context.Context, namespace string, manifest string, validate bool, extraArgs ...string) error
}
4 changes: 2 additions & 2 deletions pkg/patterns/declarative/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/kubectlcmd"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/applier"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/krusty"
Expand Down Expand Up @@ -61,7 +61,7 @@ type DeclarativeObject interface {
}

// For mocking
var kubectl = kubectlcmd.New()
var kubectl = applier.NewDirectApplier()

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