Skip to content

Commit

Permalink
Update deps and address api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Oct 29, 2020
1 parent 5aee47c commit a348747
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 148 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
// Currently, we have to import the latest version of kubectl.
// Once there is a 0.18 release, we can import a semver release.
k8s.io/kubectl v0.0.0-20191219154910-1528d4eea6dd
sigs.k8s.io/cli-utils v0.20.7-0.20201016204101-35a4eb0f63f9
sigs.k8s.io/kustomize/cmd/config v0.8.4-0.20201022184337-55b4448862b7
sigs.k8s.io/kustomize/kyaml v0.9.3-0.20201022184337-55b4448862b7
sigs.k8s.io/cli-utils v0.20.7-0.20201027223957-5e6805052d6c
sigs.k8s.io/kustomize/cmd/config v0.8.4-0.20201029003859-6fd04dd25379
sigs.k8s.io/kustomize/kyaml v0.9.3-0.20201029003859-6fd04dd25379
)
16 changes: 6 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -729,22 +729,18 @@ modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
sigs.k8s.io/cli-utils v0.20.6 h1:leIIk2NOzacXqhuNnm0tB328RSzp+1QR9PkAdSP1aRI=
sigs.k8s.io/cli-utils v0.20.6/go.mod h1:Lsj0EXMtqcSYyIPY9IXemMQW/muYDHMEWNfQI/DctrA=
sigs.k8s.io/cli-utils v0.20.7-0.20201016204101-35a4eb0f63f9 h1:jVCjSNcisbAtk4PE7WMXAuKJPVLHkk+8iF+zu9HjHhc=
sigs.k8s.io/cli-utils v0.20.7-0.20201016204101-35a4eb0f63f9/go.mod h1:Lsj0EXMtqcSYyIPY9IXemMQW/muYDHMEWNfQI/DctrA=
sigs.k8s.io/cli-utils v0.20.7-0.20201027223957-5e6805052d6c h1:cQPVMTMyoGeV09szqN1JVfPnMAxAl7fTiKEURXQYLKY=
sigs.k8s.io/cli-utils v0.20.7-0.20201027223957-5e6805052d6c/go.mod h1:Lsj0EXMtqcSYyIPY9IXemMQW/muYDHMEWNfQI/DctrA=
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
sigs.k8s.io/kustomize/cmd/config v0.8.3 h1:enT5nNDHht+stIsxt8LC4LyNfT3oUoxT6Iiu8NB6neY=
sigs.k8s.io/kustomize/cmd/config v0.8.3/go.mod h1:c7sIYoV9UOfM9tkVTOCNE8ycUMloWrUoaZp+kOuf+kc=
sigs.k8s.io/kustomize/cmd/config v0.8.4-0.20201022184337-55b4448862b7 h1:X3FMOWGdMnCrEP6f0IrjtsZlSVbKi7PzC3OcayLuHb4=
sigs.k8s.io/kustomize/cmd/config v0.8.4-0.20201022184337-55b4448862b7/go.mod h1:NgL9AMLDYJ+ju+D1OIB4SjH8Rq/y52hNsdLakEq0m8U=
sigs.k8s.io/kustomize/cmd/config v0.8.4-0.20201029003859-6fd04dd25379 h1:by5DsIzfsG4N1BPL6Xax+ZFr0cfpm6dHxbcFELQLFCc=
sigs.k8s.io/kustomize/cmd/config v0.8.4-0.20201029003859-6fd04dd25379/go.mod h1:NgL9AMLDYJ+ju+D1OIB4SjH8Rq/y52hNsdLakEq0m8U=
sigs.k8s.io/kustomize/kyaml v0.9.2 h1:QNP1Lg4V2wOgBeUim9Kmz1+2GqHtRyfoVEUQH0omrCI=
sigs.k8s.io/kustomize/kyaml v0.9.2/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
sigs.k8s.io/kustomize/kyaml v0.9.3-0.20201022184337-55b4448862b7 h1:Ivr+7ZwKizIY3K1NE9ybFhmyoghJPyCc5TNBccTlO8Q=
sigs.k8s.io/kustomize/kyaml v0.9.3-0.20201022184337-55b4448862b7/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
sigs.k8s.io/kustomize/kyaml v0.9.3-0.20201029003859-6fd04dd25379 h1:w6hCR4QpXnok8Eqz59vtsSKGCK7I1U+t1xvXeEyp9NI=
sigs.k8s.io/kustomize/kyaml v0.9.3-0.20201029003859-6fd04dd25379/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
sigs.k8s.io/structured-merge-diff v0.0.0-20190817042607-6149e4549fca/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8APh18qZnxkomBpxA=
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06/go.mod h1:/ULNhyfzRopfcjskuui0cTITekDduZ7ycKN3oUT9R18=
Expand Down
36 changes: 7 additions & 29 deletions pkg/live/inventoryrg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/klog"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/object"
Expand All @@ -30,16 +29,16 @@ import (
// the Inventory interface. This wrapper loads and stores the
// object metadata (inventory) to and from the wrapped ResourceGroup.
type InventoryResourceGroup struct {
inv *resource.Info
inv *unstructured.Unstructured
objMetas []object.ObjMetadata
}

// WrapInventoryObj takes a passed ResourceGroup (as a resource.Info),
// wraps it with the InventoryResourceGroup and upcasts the wrapper as
// an the Inventory interface.
func WrapInventoryObj(info *resource.Info) inventory.Inventory {
func WrapInventoryObj(obj *unstructured.Unstructured) inventory.Inventory {
klog.V(4).Infof("wrapping inventory info")
return &InventoryResourceGroup{inv: info}
return &InventoryResourceGroup{inv: obj}
}

// Load is an Inventory interface function returning the set of
Expand All @@ -50,12 +49,7 @@ func (icm *InventoryResourceGroup) Load() ([]object.ObjMetadata, error) {
return objs, fmt.Errorf("inventory info is nil")
}
klog.V(4).Infof("loading inventory...")
inventoryObj, ok := icm.inv.Object.(*unstructured.Unstructured)
if !ok {
err := fmt.Errorf("inventory object is not an Unstructured: %#v", inventoryObj)
return objs, err
}
items, exists, err := unstructured.NestedSlice(inventoryObj.Object, "spec", "resources")
items, exists, err := unstructured.NestedSlice(icm.inv.Object, "spec", "resources")
if err != nil {
err := fmt.Errorf("error retrieving object metadata from inventory object")
return objs, err
Expand Down Expand Up @@ -107,20 +101,11 @@ func (icm *InventoryResourceGroup) Store(objMetas []object.ObjMetadata) error {

// GetObject returns the wrapped object (ResourceGroup) as a resource.Info
// or an error if one occurs.
func (icm *InventoryResourceGroup) GetObject() (*resource.Info, error) {
func (icm *InventoryResourceGroup) GetObject() (*unstructured.Unstructured, error) {
if icm.inv == nil {
return nil, fmt.Errorf("inventory info is nil")
}
klog.V(4).Infof("getting inventory resource group")
// Verify the ResourceGroup is in Unstructured format.
obj := icm.inv.Object
if obj == nil {
return nil, fmt.Errorf("inventory info has nil Object")
}
iot, ok := obj.(*unstructured.Unstructured)
if !ok {
return nil, fmt.Errorf("inventory ResourceGroup is not in Unstructured format")
}
// Create a slice of Resources as empty Interface
klog.V(4).Infof("Creating list of %d resources", len(icm.objMetas))
var objs []interface{}
Expand All @@ -134,20 +119,13 @@ func (icm *InventoryResourceGroup) GetObject() (*resource.Info, error) {
})
}
// Create the inventory object by copying the template.
invCopy := iot.DeepCopy()
invCopy := icm.inv.DeepCopy()
// Adds the inventory ObjMetadata to the ResourceGroup "spec.resources" section
klog.V(4).Infof("storing inventory resources")
err := unstructured.SetNestedSlice(invCopy.UnstructuredContent(),
objs, "spec", "resources")
if err != nil {
return nil, err
}
return &resource.Info{
Client: icm.inv.Client,
Mapping: icm.inv.Mapping,
Source: "generated",
Name: invCopy.GetName(),
Namespace: invCopy.GetNamespace(),
Object: invCopy,
}, nil
return invCopy, nil
}
41 changes: 8 additions & 33 deletions pkg/live/inventoryrg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ package live
import (
"testing"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/resource"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/object"
)
Expand All @@ -29,7 +27,7 @@ var testNamespace = "test-inventory-namespace"
var inventoryObjName = "test-inventory-obj"
var testInventoryLabel = "test-inventory-label"

var inventoryObj = unstructured.Unstructured{
var inventoryObj = &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "kpt.dev/v1alpha1",
"kind": "ResourceGroup",
Expand All @@ -46,24 +44,6 @@ var inventoryObj = unstructured.Unstructured{
},
}

var invInfo = &resource.Info{
Namespace: testNamespace,
Name: inventoryObjName,
Mapping: &meta.RESTMapping{
Scope: meta.RESTScopeNamespace,
},
Object: &inventoryObj,
}

var invInfoNilObject = &resource.Info{
Namespace: testNamespace,
Name: inventoryObjName,
Mapping: &meta.RESTMapping{
Scope: meta.RESTScopeNamespace,
},
Object: nil,
}

var testDeployment = object.ObjMetadata{
Namespace: testNamespace,
Name: "test-deployment",
Expand Down Expand Up @@ -93,45 +73,40 @@ var testPod = object.ObjMetadata{

func TestLoadStore(t *testing.T) {
tests := map[string]struct {
invInfo *resource.Info
inv *unstructured.Unstructured
objs []object.ObjMetadata
isError bool
}{
"Nil inventory is error": {
invInfo: nil,
objs: []object.ObjMetadata{},
isError: true,
},
"Inventory with nil object is error": {
invInfo: invInfoNilObject,
inv: nil,
objs: []object.ObjMetadata{},
isError: true,
},
"No inventory objects is valid": {
invInfo: invInfo,
inv: inventoryObj,
objs: []object.ObjMetadata{},
isError: false,
},
"Simple test": {
invInfo: invInfo,
inv: inventoryObj,
objs: []object.ObjMetadata{testPod},
isError: false,
},
"Test two objects": {
invInfo: invInfo,
inv: inventoryObj,
objs: []object.ObjMetadata{testDeployment, testService},
isError: false,
},
"Test three objects": {
invInfo: invInfo,
inv: inventoryObj,
objs: []object.ObjMetadata{testDeployment, testService, testPod},
isError: false,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
wrapped := WrapInventoryObj(tc.invInfo)
wrapped := WrapInventoryObj(tc.inv)
_ = wrapped.Store(tc.objs)
invStored, err := wrapped.GetObject()
if tc.isError {
Expand Down
18 changes: 6 additions & 12 deletions pkg/live/rgpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/GoogleContainerTools/kpt/pkg/kptfile"
"github.com/GoogleContainerTools/kpt/pkg/kptfile/kptfileutil"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/klog"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
Expand All @@ -25,31 +24,31 @@ type ResourceGroupPathManifestReader struct {
// Read reads the manifests and returns them as Info objects.
// Generates and adds a ResourceGroup inventory object from
// Kptfile data.
func (p *ResourceGroupPathManifestReader) Read() ([]*resource.Info, error) {
func (p *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured, error) {
// Using the default path reader to generate the objects.
infos, err := p.pathReader.Read()
if err != nil {
return []*resource.Info{}, err
return []*unstructured.Unstructured{}, err
}
// Read the Kptfile in the top directory to get the inventory
// parameters to create the ResourceGroup inventory object.
kf, err := kptfileutil.ReadFile(p.pathReader.Path)
if err != nil {
return []*resource.Info{}, err
return []*unstructured.Unstructured{}, err
}
inv := kf.Inventory
klog.V(4).Infof("generating ResourceGroup inventory object %s/%s/%s", inv.Namespace, inv.Name, inv.InventoryID)
invInfo, err := generateInventoryObj(inv)
if err != nil {
return []*resource.Info{}, err
return []*unstructured.Unstructured{}, err
}
infos = append(infos, invInfo)
return infos, nil
}

// generateInventoryObj returns the ResourceGroupInventory object using the
// passed information.
func generateInventoryObj(inv kptfile.Inventory) (*resource.Info, error) {
func generateInventoryObj(inv kptfile.Inventory) (*unstructured.Unstructured, error) {
// Validate the parameters
name := strings.TrimSpace(inv.Name)
if name == "" {
Expand Down Expand Up @@ -87,10 +86,5 @@ func generateInventoryObj(inv kptfile.Inventory) (*resource.Info, error) {
labels[common.InventoryLabel] = id
inventoryObj.SetLabels(labels)
inventoryObj.SetAnnotations(inv.Annotations)
var invInfo = &resource.Info{
Namespace: namespace,
Name: name,
Object: inventoryObj,
}
return invInfo, nil
return inventoryObj, nil
}
Loading

0 comments on commit a348747

Please sign in to comment.