mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-29 09:40:49 +00:00
Create and document diff package.
This commit is contained in:
38
pkg/diff/directory.go
Normal file
38
pkg/diff/directory.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package diff
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// directory represents a new temp directory and lets one create files in it.
|
||||
type directory struct {
|
||||
n string
|
||||
}
|
||||
|
||||
// newDirectory makes a directory instance holding a new temp directory on disk.
|
||||
// The directory name is the given prefix following by a random string.
|
||||
func newDirectory(prefix string) (*directory, error) {
|
||||
name, err := ioutil.TempDir("", prefix+"-")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &directory{n: name}, nil
|
||||
}
|
||||
|
||||
// newFile creates a new file in the directory.
|
||||
func (d *directory) newFile(name string) (*os.File, error) {
|
||||
return os.OpenFile(filepath.Join(d.n, name), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0700)
|
||||
}
|
||||
|
||||
// delete removes the directory recursively.
|
||||
func (d *directory) delete() error {
|
||||
return os.RemoveAll(d.n)
|
||||
}
|
||||
|
||||
// name is the name of the directory.
|
||||
func (d *directory) name() string {
|
||||
return d.n
|
||||
}
|
||||
2
pkg/diff/doc.go
Normal file
2
pkg/diff/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package diff runs system diff to compare resource collections
|
||||
package diff
|
||||
59
pkg/diff/program.go
Normal file
59
pkg/diff/program.go
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
Copyright 2018 The Kubernetes 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 diff
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"k8s.io/utils/exec"
|
||||
)
|
||||
|
||||
// program wraps the system diff program.
|
||||
// If specified, the value of KUBERNETES_EXTERNAL_DIFF environment variable
|
||||
// will be used instead of simply `diff(1)`.
|
||||
type program struct {
|
||||
stdout io.Writer
|
||||
stderr io.Writer
|
||||
}
|
||||
|
||||
func newProgram(out, errOut io.Writer) *program {
|
||||
return &program{
|
||||
stdout: out,
|
||||
stderr: errOut,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *program) makeCommand(args ...string) exec.Cmd {
|
||||
diff := ""
|
||||
if envDiff := os.Getenv("KUBERNETES_EXTERNAL_DIFF"); envDiff != "" {
|
||||
diff = envDiff
|
||||
} else {
|
||||
diff = "diff"
|
||||
args = append([]string{"-u", "-N"}, args...)
|
||||
}
|
||||
cmd := exec.New().Command(diff, args...)
|
||||
cmd.SetStdout(d.stdout)
|
||||
cmd.SetStderr(d.stderr)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// run runs the detected diff program. `from` and `to` are the directory to diff.
|
||||
func (d *program) run(from, to string) error {
|
||||
d.makeCommand(from, to).Run() // Ignore diff return code
|
||||
return nil
|
||||
}
|
||||
63
pkg/diff/rundiff.go
Normal file
63
pkg/diff/rundiff.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package diff
|
||||
|
||||
import (
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
"io"
|
||||
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/resource"
|
||||
)
|
||||
|
||||
// RunDiff runs system diff program to compare two ResourceCollections.
|
||||
func RunDiff(raw, transformed resource.ResourceCollection,
|
||||
out, errOut io.Writer) error {
|
||||
transformedDir, err := writeYamlToNewDir(transformed, "transformed")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer transformedDir.delete()
|
||||
|
||||
noopDir, err := writeYamlToNewDir(raw, "noop")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer noopDir.delete()
|
||||
|
||||
return newProgram(out, errOut).run(noopDir.name(), transformedDir.name())
|
||||
}
|
||||
|
||||
// writeYamlToNewDir writes each obj in ResourceCollection to a file in a new directory.
|
||||
// The directory's name will begin with the given prefix.
|
||||
// Each file is named with GroupVersionKindName.
|
||||
func writeYamlToNewDir(in resource.ResourceCollection, prefix string) (*directory, error) {
|
||||
dir, err := newDirectory(prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for gvkn, obj := range in {
|
||||
f, err := dir.newFile(gvkn.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = print(obj.Data, f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
// Print the object as YAML.
|
||||
func print(obj interface{}, w io.Writer) error {
|
||||
if obj == nil {
|
||||
return nil
|
||||
}
|
||||
data, err := yaml.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(data)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user