From 85b71a31e35379bfe76327c2e03757b9803bd6af Mon Sep 17 00:00:00 2001 From: jregan Date: Wed, 16 Oct 2019 15:46:34 -0700 Subject: [PATCH] Cleanup types package before going public. --- internal/crawl/README.md | 2 +- internal/crawl/ROADMAP.md | 2 +- .../{kunstruct.go => unstructadapter.go} | 74 ++++++++--------- ...struct_test.go => unstructadapter_test.go} | 17 +--- pkg/types/errors.go | 29 ------- pkg/types/fix.go | 3 - pkg/types/garbagepolicy.go | 12 +++ pkg/types/genargs.go | 19 +---- pkg/types/genargs_test.go | 17 +--- pkg/types/generationbehavior.go | 17 +--- pkg/types/generatorargs.go | 2 +- pkg/types/inventory.go | 16 ++++ pkg/types/kustomization.go | 82 ------------------- pkg/types/patch.go | 19 +++++ pkg/types/patchjson6902.go | 21 +++++ pkg/types/pluginconfig.go | 16 ++++ pkg/types/typemeta.go | 11 +++ pkg/types/var.go | 61 ++++++-------- pkg/types/var_test.go | 17 +--- 19 files changed, 167 insertions(+), 270 deletions(-) rename k8sdeps/kunstruct/{kunstruct.go => unstructadapter.go} (85%) rename k8sdeps/kunstruct/{kunstruct_test.go => unstructadapter_test.go} (97%) delete mode 100644 pkg/types/errors.go create mode 100644 pkg/types/garbagepolicy.go create mode 100644 pkg/types/inventory.go create mode 100644 pkg/types/patch.go create mode 100644 pkg/types/patchjson6902.go create mode 100644 pkg/types/pluginconfig.go create mode 100644 pkg/types/typemeta.go diff --git a/internal/crawl/README.md b/internal/crawl/README.md index d51221ef7..157995eb6 100644 --- a/internal/crawl/README.md +++ b/internal/crawl/README.md @@ -352,7 +352,7 @@ patchesJson6902: ``` the following flattened structure would look like: -```json +``` { "identifiers": [ "resources", diff --git a/internal/crawl/ROADMAP.md b/internal/crawl/ROADMAP.md index eeec05451..ac46de52c 100644 --- a/internal/crawl/ROADMAP.md +++ b/internal/crawl/ROADMAP.md @@ -70,7 +70,7 @@ Each day, when the crawler finds and indexes these structured documents, it should insert aggregate data to a separate index. This data could look like the following: -```json +``` { "kind": "kustomization", "added_identifiers": [ diff --git a/k8sdeps/kunstruct/kunstruct.go b/k8sdeps/kunstruct/unstructadapter.go similarity index 85% rename from k8sdeps/kunstruct/kunstruct.go rename to k8sdeps/kunstruct/unstructadapter.go index 66cfe5441..85014e2c8 100644 --- a/k8sdeps/kunstruct/kunstruct.go +++ b/k8sdeps/kunstruct/unstructadapter.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 // Package kunstruct provides unstructured from api machinery and factory for creating unstructured package kunstruct @@ -20,17 +7,15 @@ package kunstruct import ( "encoding/json" "fmt" - "github.com/evanphx/json-patch" + + jsonpatch "github.com/evanphx/json-patch" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes/scheme" - - "k8s.io/apimachinery/pkg/labels" - "sigs.k8s.io/kustomize/v3/pkg/types" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/kustomize/v3/pkg/gvk" "sigs.k8s.io/kustomize/v3/pkg/ifc" ) @@ -148,7 +133,7 @@ func (fs *UnstructAdapter) selectSubtree(path string) (map[string]interface{}, [ func (fs *UnstructAdapter) GetFieldValue(path string) (interface{}, error) { content, fields, found, err := fs.selectSubtree(path) if !found || err != nil { - return nil, types.NoFieldError{Field: path} + return nil, noFieldError{Field: path} } s, found, err := unstructured.NestedFieldNoCopy( @@ -156,14 +141,14 @@ func (fs *UnstructAdapter) GetFieldValue(path string) (interface{}, error) { if found || err != nil { return s, err } - return nil, types.NoFieldError{Field: path} + return nil, noFieldError{Field: path} } // GetString returns value at the given fieldpath. func (fs *UnstructAdapter) GetString(path string) (string, error) { content, fields, found, err := fs.selectSubtree(path) if !found || err != nil { - return "", types.NoFieldError{Field: path} + return "", noFieldError{Field: path} } s, found, err := unstructured.NestedString( @@ -171,14 +156,14 @@ func (fs *UnstructAdapter) GetString(path string) (string, error) { if found || err != nil { return s, err } - return "", types.NoFieldError{Field: path} + return "", noFieldError{Field: path} } // GetStringSlice returns value at the given fieldpath. func (fs *UnstructAdapter) GetStringSlice(path string) ([]string, error) { content, fields, found, err := fs.selectSubtree(path) if !found || err != nil { - return []string{}, types.NoFieldError{Field: path} + return []string{}, noFieldError{Field: path} } s, found, err := unstructured.NestedStringSlice( @@ -186,14 +171,14 @@ func (fs *UnstructAdapter) GetStringSlice(path string) ([]string, error) { if found || err != nil { return s, err } - return []string{}, types.NoFieldError{Field: path} + return []string{}, noFieldError{Field: path} } // GetBool returns value at the given fieldpath. func (fs *UnstructAdapter) GetBool(path string) (bool, error) { content, fields, found, err := fs.selectSubtree(path) if !found || err != nil { - return false, types.NoFieldError{Field: path} + return false, noFieldError{Field: path} } s, found, err := unstructured.NestedBool( @@ -201,7 +186,7 @@ func (fs *UnstructAdapter) GetBool(path string) (bool, error) { if found || err != nil { return s, err } - return false, types.NoFieldError{Field: path} + return false, noFieldError{Field: path} } // GetFloat64 returns value at the given fieldpath. @@ -216,14 +201,14 @@ func (fs *UnstructAdapter) GetFloat64(path string) (float64, error) { if found || err != nil { return s, err } - return 0, types.NoFieldError{Field: path} + return 0, noFieldError{Field: path} } // GetInt64 returns value at the given fieldpath. func (fs *UnstructAdapter) GetInt64(path string) (int64, error) { content, fields, found, err := fs.selectSubtree(path) if !found || err != nil { - return 0, types.NoFieldError{Field: path} + return 0, noFieldError{Field: path} } s, found, err := unstructured.NestedInt64( @@ -231,14 +216,14 @@ func (fs *UnstructAdapter) GetInt64(path string) (int64, error) { if found || err != nil { return s, err } - return 0, types.NoFieldError{Field: path} + return 0, noFieldError{Field: path} } // GetSlice returns value at the given fieldpath. func (fs *UnstructAdapter) GetSlice(path string) ([]interface{}, error) { content, fields, found, err := fs.selectSubtree(path) if !found || err != nil { - return nil, types.NoFieldError{Field: path} + return nil, noFieldError{Field: path} } s, found, err := unstructured.NestedSlice( @@ -246,14 +231,14 @@ func (fs *UnstructAdapter) GetSlice(path string) ([]interface{}, error) { if found || err != nil { return s, err } - return nil, types.NoFieldError{Field: path} + return nil, noFieldError{Field: path} } // GetStringMap returns value at the given fieldpath. func (fs *UnstructAdapter) GetStringMap(path string) (map[string]string, error) { content, fields, found, err := fs.selectSubtree(path) if !found || err != nil { - return nil, types.NoFieldError{Field: path} + return nil, noFieldError{Field: path} } s, found, err := unstructured.NestedStringMap( @@ -261,14 +246,14 @@ func (fs *UnstructAdapter) GetStringMap(path string) (map[string]string, error) if found || err != nil { return s, err } - return nil, types.NoFieldError{Field: path} + return nil, noFieldError{Field: path} } // GetMap returns value at the given fieldpath. func (fs *UnstructAdapter) GetMap(path string) (map[string]interface{}, error) { content, fields, found, err := fs.selectSubtree(path) if !found || err != nil { - return nil, types.NoFieldError{Field: path} + return nil, noFieldError{Field: path} } s, found, err := unstructured.NestedMap( @@ -276,7 +261,7 @@ func (fs *UnstructAdapter) GetMap(path string) (map[string]interface{}, error) { if found || err != nil { return s, err } - return nil, types.NoFieldError{Field: path} + return nil, noFieldError{Field: path} } func (fs *UnstructAdapter) MatchesLabelSelector(selector string) (bool, error) { @@ -354,3 +339,12 @@ func toSchemaGvk(x gvk.Gvk) schema.GroupVersionKind { Kind: x.Kind, } } + +// noFieldError is returned when a field is expected, but missing. +type noFieldError struct { + Field string +} + +func (e noFieldError) Error() string { + return fmt.Sprintf("no field named '%s'", e.Field) +} diff --git a/k8sdeps/kunstruct/kunstruct_test.go b/k8sdeps/kunstruct/unstructadapter_test.go similarity index 97% rename from k8sdeps/kunstruct/kunstruct_test.go rename to k8sdeps/kunstruct/unstructadapter_test.go index ea471f88d..4a582d883 100644 --- a/k8sdeps/kunstruct/kunstruct_test.go +++ b/k8sdeps/kunstruct/unstructadapter_test.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package kunstruct diff --git a/pkg/types/errors.go b/pkg/types/errors.go deleted file mode 100644 index 7515f2323..000000000 --- a/pkg/types/errors.go +++ /dev/null @@ -1,29 +0,0 @@ -/* -Copyright 2019 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 types - -import ( - "fmt" -) - -type NoFieldError struct { - Field string -} - -func (e NoFieldError) Error() string { - return fmt.Sprintf("no field named '%s'", e.Field) -} diff --git a/pkg/types/fix.go b/pkg/types/fix.go index 2b3e493ca..936ccc765 100644 --- a/pkg/types/fix.go +++ b/pkg/types/fix.go @@ -22,18 +22,15 @@ func FixKustomizationPreUnmarshalling(data []byte) []byte { pattern := regexp.MustCompile(oldname) data = pattern.ReplaceAll(data, []byte(newname)) } - if useLegacyPatch(data) { pattern := regexp.MustCompile("patches:") data = pattern.ReplaceAll(data, []byte("patchesStrategicMerge:")) } - return data } func useLegacyPatch(data []byte) bool { found := false - var object map[string]interface{} err := yaml.Unmarshal(data, &object) if err != nil { diff --git a/pkg/types/garbagepolicy.go b/pkg/types/garbagepolicy.go new file mode 100644 index 000000000..41e677d4b --- /dev/null +++ b/pkg/types/garbagepolicy.go @@ -0,0 +1,12 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package types + +//go:generate stringer -type=GarbagePolicy +type GarbagePolicy int + +const ( + GarbageIgnore GarbagePolicy = iota + 1 + GarbageCollect +) diff --git a/pkg/types/genargs.go b/pkg/types/genargs.go index a6e265ad9..94927b224 100644 --- a/pkg/types/genargs.go +++ b/pkg/types/genargs.go @@ -1,18 +1,5 @@ -/* -Copyright 2019 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package types @@ -21,7 +8,7 @@ import ( "strings" ) -// GenArgs contains both generator args and options +// GenArgs contains both GeneratorArgs and GeneratorOptions. type GenArgs struct { args *GeneratorArgs opts *GeneratorOptions diff --git a/pkg/types/genargs_test.go b/pkg/types/genargs_test.go index 53af4bfca..a628f8929 100644 --- a/pkg/types/genargs_test.go +++ b/pkg/types/genargs_test.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package types_test diff --git a/pkg/types/generationbehavior.go b/pkg/types/generationbehavior.go index 67ba8a0b5..f8f362780 100644 --- a/pkg/types/generationbehavior.go +++ b/pkg/types/generationbehavior.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package types diff --git a/pkg/types/generatorargs.go b/pkg/types/generatorargs.go index b08b61e03..6b06ac9d5 100644 --- a/pkg/types/generatorargs.go +++ b/pkg/types/generatorargs.go @@ -3,7 +3,7 @@ package types -// GeneratorArgs contains arguments common to generators. +// GeneratorArgs contains arguments common to ConfigMap and Secret generators. type GeneratorArgs struct { // Namespace for the configmap, optional Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` diff --git a/pkg/types/inventory.go b/pkg/types/inventory.go new file mode 100644 index 000000000..544deb5e5 --- /dev/null +++ b/pkg/types/inventory.go @@ -0,0 +1,16 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package types + +// Inventory records all objects touched in a build operation. +type Inventory struct { + Type string `json:"type,omitempty" yaml:"type,omitempty"` + ConfigMap NameArgs `json:"configMap,omitempty" yaml:"configMap,omitempty"` +} + +// NameArgs holds both namespace and name. +type NameArgs struct { + Name string `json:"name,omitempty" yaml:"name,omitempty"` + Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` +} diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 2b97eb73f..feb54c4a4 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -13,13 +13,6 @@ const ( KustomizationKind = "Kustomization" ) -// TypeMeta partially copies apimachinery/pkg/apis/meta/v1.TypeMeta -// No need for a direct dependence; the fields are stable. -type TypeMeta struct { - Kind string `json:"kind,omitempty" yaml:"kind,omitempty"` - APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"` -} - // Kustomization holds the information needed to generate customized k8s api resources. type Kustomization struct { TypeMeta `json:",inline" yaml:",inline"` @@ -134,14 +127,6 @@ type Kustomization struct { Inventory *Inventory `json:"inventory,omitempty" yaml:"inventory,omitempty"` } -//go:generate stringer -type=GarbagePolicy -type GarbagePolicy int - -const ( - GarbageIgnore GarbagePolicy = iota + 1 - GarbageCollect -) - // FixKustomizationPostUnmarshalling fixes things // like empty fields that should not be empty, or // moving content of deprecated fields to newer @@ -169,70 +154,3 @@ func (k *Kustomization) EnforceFields() []string { } return errs } - -// PluginConfig holds plugin configuration. -type PluginConfig struct { - // DirectoryPath is an absolute path to a - // directory containing kustomize plugins. - // This directory may contain subdirectories - // further categorizing plugins. - DirectoryPath string - - // Enabled is true if plugins are enabled. - Enabled bool -} - -type PluginType string - -func (p PluginType) IsUndefined() bool { - return p == PluginType("") -} - -// KVSource represents a KV plugin backend. -type KVSource struct { - PluginType PluginType `json:"pluginType,omitempty" yaml:"pluginType,omitempty"` - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Args []string `json:"args,omitempty" yaml:"args,omitempty"` -} - -type Inventory struct { - Type string `json:"type,omitempty" yaml:"type,omitempty"` - ConfigMap NameArgs `json:"configMap,omitempty" yaml:"configMap,omitempty"` -} - -type NameArgs struct { - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` -} - -// PatchJson6902 represents a json patch for an object -// with format documented https://tools.ietf.org/html/rfc6902. -type PatchJson6902 struct { - // PatchTarget refers to a Kubernetes object that the json patch will be - // applied to. It must refer to a Kubernetes resource under the - // purview of this kustomization. PatchTarget should use the - // raw name of the object (the name specified in its YAML, - // before addition of a namePrefix and a nameSuffix). - Target *PatchTarget `json:"target" yaml:"target"` - - // relative file path for a json patch file inside a kustomization - Path string `json:"path,omitempty" yaml:"path,omitempty"` - - // inline patch string - Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` -} - -// Patch represent either a Strategic Merge Patch or a JSON patch -// and its targets. -// The content of the patch can either be from a file -// or from an inline string. -type Patch struct { - // Path is a relative file path to the patch file. - Path string `json:"path,omitempty" yaml:"path,omitempty"` - - // Patch is the content of a patch. - Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` - - // Target points to the resources that the patch is applied to - Target *Selector `json:"target,omitempty" yaml:"target,omitempty"` -} diff --git a/pkg/types/patch.go b/pkg/types/patch.go new file mode 100644 index 000000000..a0de0df8b --- /dev/null +++ b/pkg/types/patch.go @@ -0,0 +1,19 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package types + +// Patch represent either a Strategic Merge Patch or a JSON patch +// and its targets. +// The content of the patch can either be from a file +// or from an inline string. +type Patch struct { + // Path is a relative file path to the patch file. + Path string `json:"path,omitempty" yaml:"path,omitempty"` + + // Patch is the content of a patch. + Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` + + // Target points to the resources that the patch is applied to + Target *Selector `json:"target,omitempty" yaml:"target,omitempty"` +} diff --git a/pkg/types/patchjson6902.go b/pkg/types/patchjson6902.go new file mode 100644 index 000000000..1a189aa6d --- /dev/null +++ b/pkg/types/patchjson6902.go @@ -0,0 +1,21 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package types + +// PatchJson6902 represents a json patch for an object +// with format documented https://tools.ietf.org/html/rfc6902. +type PatchJson6902 struct { + // PatchTarget refers to a Kubernetes object that the json patch will be + // applied to. It must refer to a Kubernetes resource under the + // purview of this kustomization. PatchTarget should use the + // raw name of the object (the name specified in its YAML, + // before addition of a namePrefix and a nameSuffix). + Target *PatchTarget `json:"target" yaml:"target"` + + // relative file path for a json patch file inside a kustomization + Path string `json:"path,omitempty" yaml:"path,omitempty"` + + // inline patch string + Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` +} diff --git a/pkg/types/pluginconfig.go b/pkg/types/pluginconfig.go new file mode 100644 index 000000000..46327a0d3 --- /dev/null +++ b/pkg/types/pluginconfig.go @@ -0,0 +1,16 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package types + +// PluginConfig holds plugin configuration. +type PluginConfig struct { + // DirectoryPath is an absolute path to a + // directory containing kustomize plugins. + // This directory may contain subdirectories + // further categorizing plugins. + DirectoryPath string + + // Enabled is true if plugins are enabled. + Enabled bool +} diff --git a/pkg/types/typemeta.go b/pkg/types/typemeta.go new file mode 100644 index 000000000..0ddafd3d8 --- /dev/null +++ b/pkg/types/typemeta.go @@ -0,0 +1,11 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package types + +// TypeMeta partially copies apimachinery/pkg/apis/meta/v1.TypeMeta +// No need for a direct dependence; the fields are stable. +type TypeMeta struct { + Kind string `json:"kind,omitempty" yaml:"kind,omitempty"` + APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"` +} diff --git a/pkg/types/var.go b/pkg/types/var.go index 1306c1b02..a856c0013 100644 --- a/pkg/types/var.go +++ b/pkg/types/var.go @@ -1,18 +1,5 @@ -/* -Copyright 2017 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package types @@ -58,6 +45,22 @@ type Target struct { Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` } +// GVK returns the Gvk object in Target +func (t *Target) GVK() gvk.Gvk { + if t.APIVersion == "" { + return t.Gvk + } + versions := strings.Split(t.APIVersion, "/") + if len(versions) == 2 { + t.Group = versions[0] + t.Version = versions[1] + } + if len(versions) == 1 { + t.Version = versions[0] + } + return t.Gvk +} + // FieldSelector contains the fieldPath to an object field. // This struct is added to keep the backward compatibility of using ObjectFieldSelector // for Var.FieldRef @@ -102,7 +105,7 @@ func (vs *VarSet) AsSlice() []Var { s[i] = v i++ } - sort.Sort(ByName(s)) + sort.Sort(byName(s)) return s } @@ -202,25 +205,9 @@ func (vs *VarSet) Get(name string) *Var { return nil } -// GVK returns the Gvk object in Target -func (t *Target) GVK() gvk.Gvk { - if t.APIVersion == "" { - return t.Gvk - } - versions := strings.Split(t.APIVersion, "/") - if len(versions) == 2 { - t.Group = versions[0] - t.Version = versions[1] - } - if len(versions) == 1 { - t.Version = versions[0] - } - return t.Gvk -} +// byName is a sort interface which sorts Vars by name alphabetically +type byName []Var -// ByName is a sort interface which sorts Vars by name alphabetically -type ByName []Var - -func (v ByName) Len() int { return len(v) } -func (v ByName) Swap(i, j int) { v[i], v[j] = v[j], v[i] } -func (v ByName) Less(i, j int) bool { return v[i].Name < v[j].Name } +func (v byName) Len() int { return len(v) } +func (v byName) Swap(i, j int) { v[i], v[j] = v[j], v[i] } +func (v byName) Less(i, j int) bool { return v[i].Name < v[j].Name } diff --git a/pkg/types/var_test.go b/pkg/types/var_test.go index efd3db7a9..f4452901f 100644 --- a/pkg/types/var_test.go +++ b/pkg/types/var_test.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package types