address comments

This commit is contained in:
Jingfang Liu
2018-08-27 15:44:42 -07:00
parent 6a3e3c3a71
commit 3d9d4bd36f
5 changed files with 50 additions and 17 deletions

View File

@@ -33,6 +33,7 @@ import (
"github.com/kubernetes-sigs/kustomize/pkg/fs"
interror "github.com/kubernetes-sigs/kustomize/pkg/internal/error"
"github.com/kubernetes-sigs/kustomize/pkg/loader"
"github.com/kubernetes-sigs/kustomize/pkg/patch"
"github.com/kubernetes-sigs/kustomize/pkg/resmap"
"github.com/kubernetes-sigs/kustomize/pkg/resource"
"github.com/kubernetes-sigs/kustomize/pkg/transformers"
@@ -65,9 +66,6 @@ func NewApplication(ldr loader.Loader, fSys fs.FileSystem) (*Application, error)
if m.PatchesJson6902 != nil {
log.Printf("field patchesJson6902 ignored; no implementation yet.")
}
if m.Patches != nil {
log.Println("field patches will be deprecated, please change it to patchesStategicMerge")
}
return &Application{kustomization: &m, ldr: ldr, fSys: fSys}, nil
}
@@ -156,7 +154,7 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) {
return nil, err
}
a.kustomization.PatchesStrategicMerge = append(a.kustomization.PatchesStrategicMerge, a.kustomization.Patches...)
a.kustomization.PatchesStrategicMerge = patch.Append(a.kustomization.PatchesStrategicMerge, a.kustomization.Patches)
patches, err := resmap.NewResourceSliceFromPatches(a.ldr, a.kustomization.PatchesStrategicMerge)
if err != nil {
errs.Append(errors.Wrap(err, "NewResourceSliceFromPatches"))

View File

@@ -14,10 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package types
package patch
// JSONPatch represents a json patch for an object
type JSONPatch struct {
// PatchJson6902 represents a json patch for an object
// with format documented https://tools.ietf.org/html/rfc6902.
type PatchJson6902 struct {
// Relative file path within the kustomization for a json patch file.
Path string `json:"path" yaml:"path"`
@@ -26,11 +27,11 @@ type JSONPatch struct {
// purview of this kustomization. Target should use the
// raw name of the object (the name specified in its YAML,
// before addition of a namePrefix).
Target PatchTarget `json:"target" yaml:"target"`
Target Target `json:"target" yaml:"target"`
}
// PatchTarget represents the kubernetes object that the patch is applied to
type PatchTarget struct {
// Target represents the kubernetes object that the patch is applied to
type Target struct {
Group string `json:"group,omitempty" yaml:"group,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`

View File

@@ -0,0 +1,30 @@
/*
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 patch
// PatchStrategicMerge represents a relative path to a
// stategic merget patch with the format
// https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md
type PatchStrategicMerge string
// Append appends a slice of patch paths to a PatchStategicMerge slice
func Append(patches []PatchStrategicMerge, paths []string) []PatchStrategicMerge {
for _, p := range paths {
patches = append(patches, PatchStrategicMerge(p))
}
return patches
}

View File

@@ -29,6 +29,7 @@ import (
"github.com/golang/glog"
internal "github.com/kubernetes-sigs/kustomize/pkg/internal/error"
"github.com/kubernetes-sigs/kustomize/pkg/loader"
"github.com/kubernetes-sigs/kustomize/pkg/patch"
"github.com/kubernetes-sigs/kustomize/pkg/resource"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -131,16 +132,16 @@ func (m ResMap) insert(newName string, obj *unstructured.Unstructured) error {
// NewResourceSliceFromPatches returns a slice of resources given a patch path slice from a kustomization file.
func NewResourceSliceFromPatches(
loader loader.Loader, paths []string) ([]*resource.Resource, error) {
loader loader.Loader, paths []patch.PatchStrategicMerge) ([]*resource.Resource, error) {
var result []*resource.Resource
for _, path := range paths {
content, err := loader.Load(path)
content, err := loader.Load(string(path))
if err != nil {
return nil, err
}
res, err := newResourceSliceFromBytes(content)
if err != nil {
return nil, internal.Handler(err, path)
return nil, internal.Handler(err, string(path))
}
result = append(result, res...)
}

View File

@@ -19,6 +19,8 @@ package types
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/kubernetes-sigs/kustomize/pkg/patch"
)
// Kustomization holds the information needed to generate customized k8s api resources.
@@ -59,12 +61,13 @@ type Kustomization struct {
// URLs and globs are not supported.
// The patch files should be Stategic Merge Patch, the default patching behavior for kubectl.
// https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md
Patches []string `json:"patches,omitempty" yaml:"patches,omitempty"`
PatchesStrategicMerge []string `json:"patchesSrategicMerge,omitempty" yaml:"patchesStategicMerge,omitempty"`
Patches []string `json:"patches,omitempty" yaml:"patches,omitempty"`
PatchesStrategicMerge []patch.PatchStrategicMerge `json:"patchesSrategicMerge,omitempty" yaml:"patchesStategicMerge,omitempty"`
// JSONPatches is a list of JSONPatch for applying JSON patch.
// The JSON patch is documented at http://jsonpatch.com/.
PatchesJson6902 []JSONPatch `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"`
// The JSON patch is documented at https://tools.ietf.org/html/rfc6902
// and http://jsonpatch.com/.
PatchesJson6902 []patch.PatchJson6902 `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"`
// List of configmaps to generate from configuration sources.
// Base/overlay concept doesn't apply to this field.