Skip to content

Commit

Permalink
Simple kubectl plugin for plan functionality
Browse files Browse the repository at this point in the history
We would like to modularize kpt into composable functionality.  Start
with creating a kubectl plugin that can hold "plan" functionality.

Signed-off-by: justinsb <[email protected]>
  • Loading branch information
justinsb committed Nov 22, 2023
1 parent 4c4cf98 commit 1f8ef3a
Show file tree
Hide file tree
Showing 20 changed files with 1,043 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plugins/cmd/kubectl-plan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# kubectl-plan

kubectl-plan is an experimental kubectl plugin that dry-runs
apply operations and shows the changes in an easy to read format.

It is still under development and highly experimental, it should
not be treated as stable.
35 changes: 35 additions & 0 deletions plugins/cmd/kubectl-plan/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2023 The kpt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"fmt"
"os"

"github.com/GoogleContainerTools/kpt/plugins/pkg/cmd/plan"
)

func main() {
if err := Run(context.Background()); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}

func Run(ctx context.Context) error {
root := plan.NewCommand()
return root.ExecuteContext(ctx)
}
60 changes: 60 additions & 0 deletions plugins/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module github.com/GoogleContainerTools/kpt/plugins

go 1.21.3

require (
github.com/google/go-cmp v0.5.9
github.com/spf13/cobra v1.8.0
k8s.io/apimachinery v0.28.4
k8s.io/client-go v0.28.4
k8s.io/klog/v2 v2.100.1
sigs.k8s.io/controller-runtime v0.13.0
sigs.k8s.io/kubebuilder-declarative-pattern v0.13.0
sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver v0.0.0-20231030230424-f6a5c89244f2
sigs.k8s.io/kustomize/kyaml v0.15.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.4 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
190 changes: 190 additions & 0 deletions plugins/go.sum

Large diffs are not rendered by default.

121 changes: 121 additions & 0 deletions plugins/pkg/cmd/plan/cluster_target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2023 The kpt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plan

import (
"context"
"encoding/json"
"fmt"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/restmapper"
)

// ClusterTarget supports actions against a running kubernetes cluster.
type ClusterTarget struct {
client dynamic.Interface
restMapper resourceFinder
}

func NewClusterTarget(restConfig *rest.Config) (*ClusterTarget, error) {
client, err := dynamic.NewForConfig(restConfig)
if err != nil {
return nil, fmt.Errorf("creating kubernetes client: %w", err)
}

restMapper, err := restmapper.NewControllerRESTMapper(restConfig)
if err != nil {
return nil, fmt.Errorf("building REST mapper: %w", err)
}

return &ClusterTarget{
client: client,
restMapper: restMapper,
}, nil
}

type resourceFinder interface {
RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error)
}

// ResourceForGVK gets the GVR / Scope for the specified object.
func (c *ClusterTarget) ResourceForGVK(ctx context.Context, gvk schema.GroupVersionKind) (*clusterResourceTarget, error) {
mapping, err := c.restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, fmt.Errorf("cannot get RESTMapping for %v: %w", gvk, err)
}
return &clusterResourceTarget{info: mapping, client: c.client}, nil
}

// Apply is a wrapper around applying changes to a live cluster.
func (c *clusterResourceTarget) Apply(ctx context.Context, obj *unstructured.Unstructured, options metav1.PatchOptions) (*unstructured.Unstructured, error) {
target, err := c.buildResource(ctx, obj)
if err != nil {
return nil, err
}

j, err := json.Marshal(obj)
if err != nil {
return nil, fmt.Errorf("error marshalling object to JSON: %w", err)
}

// Apply with server-side apply (specified with ApplyPatchType)
patched, err := target.Patch(ctx, obj.GetName(), types.ApplyPatchType, j, options)
if err != nil {
return nil, fmt.Errorf("server-side-apply failed: %w", err)
}

return patched, nil
}

// buildResource creates the dynamic ResourceInterface for the object
func (c *clusterResourceTarget) buildResource(ctx context.Context, obj *unstructured.Unstructured) (dynamic.ResourceInterface, error) {
if c.info.Scope == meta.RESTScopeRoot {
return c.client.Resource(c.info.Resource), nil
} else {
namespace := obj.GetNamespace()
if namespace == "" {
return nil, fmt.Errorf("namespace was not set, but is required for namespace-scoped objects")
}
return c.client.Resource(c.info.Resource).Namespace(namespace), nil
}
}

// Get reads the current version of an object.
func (c *clusterResourceTarget) Get(ctx context.Context, obj *unstructured.Unstructured, options metav1.GetOptions) (*unstructured.Unstructured, error) {
target, err := c.buildResource(ctx, obj)
if err != nil {
return nil, err
}

existing, err := target.Get(ctx, obj.GetName(), options)
if err != nil {
return nil, fmt.Errorf("get failed: %w", err)
}

return existing, nil
}

type clusterResourceTarget struct {
info *meta.RESTMapping

client dynamic.Interface
}
115 changes: 115 additions & 0 deletions plugins/pkg/cmd/plan/golden_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2023 The kpt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plan

import (
"bytes"
"context"
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver"
"sigs.k8s.io/yaml"
)

func TestPlanner(t *testing.T) {
k8s, err := mockkubeapiserver.NewMockKubeAPIServer(":0")
if err != nil {
t.Fatalf("error building mock kube-apiserver: %v", err)
}
defer func() {
if err := k8s.Stop(); err != nil {
t.Fatalf("error closing mock kube-apiserver: %v", err)
}
}()
addr, err := k8s.StartServing()
if err != nil {
t.Errorf("error starting mock kube-apiserver: %v", err)
}
klog.Infof("mock kubeapiserver will listen on %v", addr)

restConfig := &rest.Config{
Host: addr.String(),
}

dir := "testdata"
files, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("failed to read directory %q: %v", dir, err)
}
for _, file := range files {
p := filepath.Join(dir, file.Name())
if !file.IsDir() {
t.Errorf("found non-directory %q", p)
continue
}

t.Run(file.Name(), func(t *testing.T) {
p := filepath.Join(dir, file.Name())

ctx := context.Background()

objects, err := loadObjectsFromFilesystem(filepath.Join(p, "apply.yaml"))
if err != nil {
t.Fatalf("error loading objects: %v", err)
}

target, err := NewClusterTarget(restConfig)
if err != nil {
t.Fatalf("error building target: %v", err)
}

planner := &Planner{}

plan, err := planner.BuildPlan(ctx, objects, target)
if err != nil {
t.Fatalf("error from BuildPlan: %v", err)
}

actual, err := yaml.Marshal(plan)
if err != nil {
t.Fatalf("yaml.Marshal failed: %v", err)
}
CompareGoldenFile(t, filepath.Join(p, "plan.yaml"), actual)
})
}
}

func CompareGoldenFile(t *testing.T, p string, got []byte) {
if os.Getenv("WRITE_GOLDEN_OUTPUT") != "" {
// Short-circuit when the output is correct
b, err := os.ReadFile(p)
if err == nil && bytes.Equal(b, got) {
return
}

if err := os.WriteFile(p, got, 0644); err != nil {
t.Fatalf("failed to write golden output %s: %v", p, err)
}
t.Errorf("wrote output to %s", p)
} else {
want, err := os.ReadFile(p)
if err != nil {
t.Fatalf("failed to read file %q: %v", p, err)
}
if diff := cmp.Diff(string(want), string(got)); diff != "" {
t.Errorf("unexpected diff in %s: %s", p, diff)
}
}
}
Loading

0 comments on commit 1f8ef3a

Please sign in to comment.