Merge pull request #362 from ash2k/ash2k/simplify-accessor

Simplify: remove unneeded meta.Accessor() usage
This commit is contained in:
Kubernetes Prow Robot 2021-06-02 22:21:38 -07:00 committed by GitHub
commit 670dee18a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 19 additions and 76 deletions

View File

@ -837,14 +837,9 @@ func newFakeRESTClient(t *testing.T, handlers []handler) *fake.RESTClient {
func toIdentifier(t *testing.T, manifest string) object.ObjMetadata {
obj := Unstructured(t, manifest)
accessor, err := meta.Accessor(obj)
if err != nil {
t.Fatal(err)
}
return object.ObjMetadata{
GroupKind: obj.GetObjectKind().GroupVersionKind().GroupKind(),
Name: accessor.GetName(),
Name: obj.GetName(),
Namespace: "default",
}
}

View File

@ -10,7 +10,6 @@ import (
"strings"
"testing"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/meta/testrestmapper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@ -276,7 +275,7 @@ func TestPrune(t *testing.T) {
err := func() error {
defer close(eventChannel)
// Run the prune and validate.
return po.Prune(currentInventory, tc.currentObjs, populateObjectIds(tc.currentObjs, t), taskContext, Options{
return po.Prune(currentInventory, tc.currentObjs, populateObjectIds(tc.currentObjs), taskContext, Options{
DryRunStrategy: drs,
})
}()
@ -326,14 +325,10 @@ func unionObjects(sliceA []*unstructured.Unstructured, sliceB []*unstructured.Un
// populateObjectIds returns a pointer to a set of strings containing
// the UID's of the passed objects (infos).
func populateObjectIds(objs []*unstructured.Unstructured, t *testing.T) sets.String {
func populateObjectIds(objs []*unstructured.Unstructured) sets.String {
uids := sets.NewString()
for _, currObj := range objs {
metadata, err := meta.Accessor(currObj)
if err != nil {
t.Fatalf("Unexpected error retrieving object metadata: %#v", err)
}
uid := string(metadata.GetUID())
uid := string(currObj.GetUID())
uids.Insert(uid)
}
return uids
@ -495,7 +490,7 @@ func TestPruneWithError(t *testing.T) {
err := func() error {
defer close(eventChannel)
// Run the prune and validate.
return po.Prune(currentInventory, tc.currentObjs, populateObjectIds(tc.currentObjs, t), taskContext, Options{
return po.Prune(currentInventory, tc.currentObjs, populateObjectIds(tc.currentObjs), taskContext, Options{
DryRunStrategy: drs,
})
}()

View File

@ -4,7 +4,6 @@
package task
import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
"sigs.k8s.io/cli-utils/pkg/apply/event"
@ -71,9 +70,8 @@ func inventoryNamespaceInSet(inv inventory.InventoryInfo, objs []*unstructured.U
invNamespace := inv.Namespace()
for _, obj := range objs {
acc, _ := meta.Accessor(obj)
gvk := obj.GetObjectKind().GroupVersionKind()
if gvk == object.CoreV1Namespace && acc.GetName() == invNamespace {
if gvk == object.CoreV1Namespace && obj.GetName() == invNamespace {
inventory.AddInventoryIDAnnotation(obj, inv)
return obj
}

View File

@ -15,7 +15,6 @@ import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
"sigs.k8s.io/cli-utils/pkg/common"
@ -73,16 +72,7 @@ func IsInventoryObject(obj *unstructured.Unstructured) bool {
// for the passed inventory object. Returns error if the passed object is nil or
// is not a inventory object.
func retrieveInventoryLabel(obj *unstructured.Unstructured) (string, error) {
if obj == nil {
return "", fmt.Errorf("inventory info is nil")
}
accessor, err := meta.Accessor(obj)
if err != nil {
return "", err
}
labels := accessor.GetLabels()
inventoryLabel, exists := labels[common.InventoryLabel]
inventoryLabel, exists := obj.GetLabels()[common.InventoryLabel]
if !exists {
return "", fmt.Errorf("inventory label does not exist for inventory object: %s", common.InventoryLabel)
}
@ -134,16 +124,12 @@ func SplitUnstructureds(objs []*unstructured.Unstructured) (*unstructured.Unstru
// an error if name stored in the object differs from the name in
// the Info struct.
func addSuffixToName(obj *unstructured.Unstructured, suffix string) error {
if obj == nil {
return fmt.Errorf("nil unstructured.Unstructured")
}
suffix = strings.TrimSpace(suffix)
if len(suffix) == 0 {
return fmt.Errorf("passed empty suffix")
}
accessor, _ := meta.Accessor(obj)
name := accessor.GetName()
name := obj.GetName()
if name != obj.GetName() {
return fmt.Errorf("inventory object (%s) and resource.Info (%s) have different names", name, obj.GetName())
}
@ -153,7 +139,7 @@ func addSuffixToName(obj *unstructured.Unstructured, suffix string) error {
return fmt.Errorf("name already has suffix: %s", name)
}
name += suffix
accessor.SetName(name)
obj.SetName(name)
return nil
}
@ -163,15 +149,7 @@ func addSuffixToName(obj *unstructured.Unstructured, suffix string) error {
// This fixes a problem where inventory object names collide if
// they are created in the same namespace.
func fixLegacyInventoryName(obj *unstructured.Unstructured) error {
if obj == nil {
return fmt.Errorf("invalid inventory object is nil")
}
accessor, err := meta.Accessor(obj)
if err != nil {
return err
}
name := accessor.GetName()
if obj.GetName() == legacyInvName || name == legacyInvName {
if obj.GetName() == legacyInvName {
klog.V(4).Infof("renaming legacy inventory name")
randomSuffix := common.RandomStr()
return addSuffixToName(obj, randomSuffix)

View File

@ -14,7 +14,6 @@ import (
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/types"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/engine"
@ -102,7 +101,6 @@ type statusForGenResourcesFunc func(ctx context.Context, mapper meta.RESTMapper,
// resources.
func statusForGeneratedResources(ctx context.Context, mapper meta.RESTMapper, reader engine.ClusterReader, statusReader resourceTypeStatusReader,
object *unstructured.Unstructured, gk schema.GroupKind, selectorPath ...string) (event.ResourceStatuses, error) {
namespace := getNamespaceForNamespacedResource(object)
selector, err := toSelector(object, selectorPath...)
if err != nil {
return event.ResourceStatuses{}, err
@ -114,7 +112,7 @@ func statusForGeneratedResources(ctx context.Context, mapper meta.RESTMapper, re
return event.ResourceStatuses{}, err
}
objectList.SetGroupVersionKind(gvk)
err = reader.ListNamespaceScoped(ctx, &objectList, namespace, selector)
err = reader.ListNamespaceScoped(ctx, &objectList, object.GetNamespace(), selector)
if err != nil {
return event.ResourceStatuses{}, err
}
@ -182,13 +180,3 @@ func toIdentifier(u *unstructured.Unstructured) object.ObjMetadata {
Namespace: u.GetNamespace(),
}
}
// getNamespaceForNamespacedResource returns the namespace for the given object,
// but includes logic for returning the default namespace if it is not set.
func getNamespaceForNamespacedResource(object runtime.Object) string {
acc, err := meta.Accessor(object)
if err != nil {
panic(err)
}
return acc.GetNamespace()
}

View File

@ -50,8 +50,6 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,
var unknownGKs []schema.GroupKind
for _, obj := range objs {
accessor, _ := meta.Accessor(obj)
// Exclude any inventory objects here since we don't want to change
// their namespace.
if inventory.IsInventoryObject(obj) {
@ -60,7 +58,7 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,
// if the resource already has the namespace set, we don't
// need to do anything
if ns := accessor.GetNamespace(); ns != "" {
if ns := obj.GetNamespace(); ns != "" {
if enforceNamespace && ns != defaultNamespace {
return fmt.Errorf("the namespace from the provided object %q "+
"does not match the namespace %q. You must pass '--namespace=%s' to perform this operation",
@ -83,7 +81,7 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,
// This means the resource does not have the namespace set,
// but it is a namespaced resource. So we set the namespace
// to the provided default value.
accessor.SetNamespace(defaultNamespace)
obj.SetNamespace(defaultNamespace)
}
continue
}
@ -110,7 +108,7 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,
case "Cluster":
continue
case "Namespaced":
accessor.SetNamespace(defaultNamespace)
obj.SetNamespace(defaultNamespace)
}
}
if len(unknownGKs) > 0 {
@ -126,11 +124,10 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,
func FilterLocalConfig(objs []*unstructured.Unstructured) []*unstructured.Unstructured {
var filteredObjs []*unstructured.Unstructured
for _, obj := range objs {
acc, _ := meta.Accessor(obj)
// Ignoring the value of the LocalConfigAnnotation here. This is to be
// consistent with the behavior in the kyaml library:
// https://github.com/kubernetes-sigs/kustomize/blob/30b58e90a39485bc5724b2278651c5d26b815cb2/kyaml/kio/filters/local.go#L29
if _, found := acc.GetAnnotations()[filters.LocalConfigAnnotation]; !found {
if _, found := obj.GetAnnotations()[filters.LocalConfigAnnotation]; !found {
filteredObjs = append(filteredObjs, obj)
}
}

View File

@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/assert"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
@ -169,10 +168,7 @@ func TestSetNamespaces(t *testing.T) {
assert.NoError(t, err)
for i, obj := range tc.objs {
expectedNs := tc.expectedNamespaces[i]
assert.Equal(t, expectedNs, obj.GetNamespace())
accessor, _ := meta.Accessor(obj)
assert.Equal(t, expectedNs, accessor.GetNamespace())
assert.Equal(t, tc.expectedNamespaces[i], obj.GetNamespace())
}
})
}

View File

@ -9,7 +9,6 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/meta"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
)
@ -80,8 +79,7 @@ func TestPathManifestReader_Read(t *testing.T) {
for i, obj := range objs {
assert.Equal(t, tc.namespaces[i], obj.GetNamespace())
accessor, _ := meta.Accessor(obj)
_, ok := accessor.GetAnnotations()[kioutil.PathAnnotation]
_, ok := obj.GetAnnotations()[kioutil.PathAnnotation]
assert.True(t, ok)
}
})

View File

@ -4,7 +4,6 @@
package object
import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/resource"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
@ -15,15 +14,14 @@ func InfoToUnstructured(info *resource.Info) *unstructured.Unstructured {
}
func UnstructuredToInfo(obj *unstructured.Unstructured) (*resource.Info, error) {
accessor, _ := meta.Accessor(obj)
annos := accessor.GetAnnotations()
annos := obj.GetAnnotations()
source := "unstructured"
path, ok := annos[kioutil.PathAnnotation]
if ok {
source = path
delete(annos, kioutil.PathAnnotation)
accessor.SetAnnotations(annos)
obj.SetAnnotations(annos)
}
return &resource.Info{