add InventoryInfo() function to the provider interface

This commit is contained in:
Jingfang Liu 2020-11-05 09:46:36 -08:00
parent 7915dc5de7
commit cb0443cccc
12 changed files with 28 additions and 52 deletions

View File

@ -17,7 +17,6 @@ import (
"sigs.k8s.io/cli-utils/cmd/printers"
"sigs.k8s.io/cli-utils/pkg/apply"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/kustomize/kyaml/setters2"
)
@ -111,11 +110,8 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
inventoryClient, err := r.provider.InventoryClient()
if err != nil {
return err
}
inv, objs, err := inventory.SplitUnstructureds(inventoryClient.InvInfoFactoryFunc(), objs)
inv, objs, err := r.provider.InventoryInfo(objs)
if err != nil {
return err
}

View File

@ -13,7 +13,6 @@ import (
"k8s.io/kubectl/pkg/util/i18n"
"sigs.k8s.io/cli-utils/cmd/printers"
"sigs.k8s.io/cli-utils/pkg/apply"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/provider"
)
@ -64,12 +63,7 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
inventoryClient, err := r.provider.InventoryClient()
if err != nil {
return err
}
inv, _, err := inventory.SplitUnstructureds(inventoryClient.InvInfoFactoryFunc(), objs)
inv, _, err := r.provider.InventoryInfo(objs)
if err != nil {
return err
}

View File

@ -16,7 +16,6 @@ import (
"sigs.k8s.io/cli-utils/pkg/apply"
"sigs.k8s.io/cli-utils/pkg/apply/event"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/kustomize/kyaml/setters2"
)
@ -100,12 +99,8 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
inventoryClient, err := r.provider.InventoryClient()
if err != nil {
return err
}
inv, objs, err := inventory.SplitUnstructureds(inventoryClient.InvInfoFactoryFunc(), objs)
inv, objs, err := r.provider.InventoryInfo(objs)
if err != nil {
return err
}

View File

@ -15,7 +15,6 @@ import (
"sigs.k8s.io/cli-utils/cmd/status/printers"
"sigs.k8s.io/cli-utils/pkg/apply/poller"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/aggregator"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector"
@ -88,8 +87,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
return err
}
// Find the inventory template among the manifests.
inv, _, err := inventory.SplitUnstructureds(invClient.InvInfoFactoryFunc(), objs)
inv, _, err := r.provider.InventoryInfo(objs)
if err != nil {
return err
}

View File

@ -551,7 +551,7 @@ func createObjs(resources []resourceInfo) (inventory.InventoryInfo, []*unstructu
}
objs = append(objs, u)
}
return inventory.SplitUnstructureds(inventory.WrapInventoryInfoObj, objs)
return inventory.SplitUnstructureds(objs)
}
// The handler interface allows different testcases to provide

View File

@ -81,7 +81,3 @@ func (fic *FakeInventoryClient) SetError(err error) {
func (fic *FakeInventoryClient) ClearError() {
fic.Err = nil
}
func (fic *FakeInventoryClient) InvInfoFactoryFunc() UnstructuredToInvInfoFunc {
return WrapInventoryInfoObj
}

View File

@ -42,8 +42,6 @@ type InventoryClient interface {
SetDryRunStrategy(drs common.DryRunStrategy)
// ApplyInventoryNamespace applies the Namespace that the inventory object should be in.
ApplyInventoryNamespace(invNamespace *unstructured.Unstructured) error
// InvInfoFactoryFunc returns the factory function to create an InventoryInfo from an unstructured.
InvInfoFactoryFunc() UnstructuredToInvInfoFunc
}
// ClusterInventoryClient is a concrete implementation of the
@ -56,7 +54,6 @@ type ClusterInventoryClient struct {
dryRunStrategy common.DryRunStrategy
InventoryFactoryFunc InventoryFactoryFunc
invToUnstructuredFunc InventoryToUnstructuredFunc
unsToInfoFunc UnstructuredToInvInfoFunc
infoHelper info.InfoHelper
}
@ -66,8 +63,7 @@ var _ InventoryClient = &ClusterInventoryClient{}
// InventoryClient interface or an error.
func NewInventoryClient(factory cmdutil.Factory,
invFunc InventoryFactoryFunc,
invToUnstructuredFunc InventoryToUnstructuredFunc,
unsToInfoFunc UnstructuredToInvInfoFunc) (*ClusterInventoryClient, error) {
invToUnstructuredFunc InventoryToUnstructuredFunc) (*ClusterInventoryClient, error) {
var err error
mapper, err := factory.ToRESTMapper()
if err != nil {
@ -86,16 +82,11 @@ func NewInventoryClient(factory cmdutil.Factory,
dryRunStrategy: common.DryRunNone,
InventoryFactoryFunc: invFunc,
invToUnstructuredFunc: invToUnstructuredFunc,
unsToInfoFunc: unsToInfoFunc,
infoHelper: info.NewInfoHelper(factory),
}
return &clusterInventoryClient, nil
}
func (cic *ClusterInventoryClient) InvInfoFactoryFunc() UnstructuredToInvInfoFunc {
return cic.unsToInfoFunc
}
// Merge stores the union of the passed objects with the objects currently
// stored in the cluster inventory object. Retrieves and caches the cluster
// inventory object. Returns the set differrence of the cluster inventory

View File

@ -58,7 +58,7 @@ func TestGetClusterInventoryInfo(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
invClient, _ := NewInventoryClient(tf,
WrapInventoryObj, InvInfoToConfigMap, WrapInventoryInfoObj)
WrapInventoryObj, InvInfoToConfigMap)
fakeBuilder := FakeBuilder{}
fakeBuilder.SetInventoryObjs(tc.localObjs)
invClient.builderFunc = fakeBuilder.GetBuilder()
@ -163,7 +163,7 @@ func TestMerge(t *testing.T) {
t.Run(name, func(t *testing.T) {
// Create the local inventory object storing "tc.localObjs"
invClient, _ := NewInventoryClient(tf,
WrapInventoryObj, InvInfoToConfigMap, WrapInventoryInfoObj)
WrapInventoryObj, InvInfoToConfigMap)
invClient.SetDryRunStrategy(drs)
// Create a fake builder to return "tc.clusterObjs" from
// the cluster inventory object.
@ -250,7 +250,7 @@ func TestCreateInventory(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
invClient, _ := NewInventoryClient(tf,
WrapInventoryObj, InvInfoToConfigMap, WrapInventoryInfoObj)
WrapInventoryObj, InvInfoToConfigMap)
inv := invClient.invToUnstructuredFunc(tc.inv)
if inv != nil {
inv = storeObjsInInventory(tc.inv, tc.localObjs)
@ -327,7 +327,7 @@ func TestReplace(t *testing.T) {
drs := common.Strategies[i]
t.Run(name, func(t *testing.T) {
invClient, _ := NewInventoryClient(tf,
WrapInventoryObj, InvInfoToConfigMap, WrapInventoryInfoObj)
WrapInventoryObj, InvInfoToConfigMap)
invClient.SetDryRunStrategy(drs)
// Create fake builder returning the cluster inventory object
// storing the "tc.clusterObjs" objects.
@ -384,7 +384,7 @@ func TestGetClusterObjs(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
invClient, _ := NewInventoryClient(tf,
WrapInventoryObj, InvInfoToConfigMap, WrapInventoryInfoObj)
WrapInventoryObj, InvInfoToConfigMap)
// Create fake builder returning "tc.clusterObjs" from cluster inventory.
fakeBuilder := FakeBuilder{}
fakeBuilder.SetInventoryObjs(tc.clusterObjs)
@ -464,7 +464,7 @@ func TestDeleteInventoryObj(t *testing.T) {
drs := common.Strategies[i]
t.Run(name, func(t *testing.T) {
invClient, _ := NewInventoryClient(tf,
WrapInventoryObj, InvInfoToConfigMap, WrapInventoryInfoObj)
WrapInventoryObj, InvInfoToConfigMap)
invClient.SetDryRunStrategy(drs)
inv := invClient.invToUnstructuredFunc(tc.inv)
if inv != nil {
@ -563,7 +563,7 @@ func TestMergeInventoryObjs(t *testing.T) {
drs := common.Strategies[i]
t.Run(name, func(t *testing.T) {
invClient, _ := NewInventoryClient(tf,
WrapInventoryObj, InvInfoToConfigMap, WrapInventoryInfoObj)
WrapInventoryObj, InvInfoToConfigMap)
invClient.SetDryRunStrategy(drs)
inventories := []*unstructured.Unstructured{}
for _, i := range tc.invs {

View File

@ -46,10 +46,6 @@ type InventoryFactoryFunc func(*unstructured.Unstructured) Inventory
// given InventoryInfo.
type InventoryToUnstructuredFunc func(InventoryInfo) *unstructured.Unstructured
// UnstructuredToInvInfoFunc creates the object which implements the InventoryInfo
// interface from the passed unstructured object.
type UnstructuredToInvInfoFunc func(*unstructured.Unstructured) InventoryInfo
// FindInventoryObj returns the "Inventory" object (ConfigMap with
// inventory label) if it exists, or nil if it does not exist.
func FindInventoryObj(objs []*unstructured.Unstructured) *unstructured.Unstructured {
@ -114,7 +110,7 @@ func ValidateNoInventory(objs []*unstructured.Unstructured) error {
// splitUnstructureds takes a slice of unstructured.Unstructured objects and
// splits it into one slice that contains the inventory object templates and
// another one that contains the remaining resources.
func SplitUnstructureds(factoryFunc UnstructuredToInvInfoFunc, objs []*unstructured.Unstructured) (InventoryInfo, []*unstructured.Unstructured, error) {
func SplitUnstructureds(objs []*unstructured.Unstructured) (InventoryInfo, []*unstructured.Unstructured, error) {
invs := make([]*unstructured.Unstructured, 0)
resources := make([]*unstructured.Unstructured, 0)
for _, obj := range objs {
@ -131,7 +127,7 @@ func SplitUnstructureds(factoryFunc UnstructuredToInvInfoFunc, objs []*unstructu
InventoryObjectTemplates: invs,
}
}
return factoryFunc(invs[0]), resources, nil
return WrapInventoryInfoObj(invs[0]), resources, nil
}
// addSuffixToName adds the passed suffix (usually a hash) as a suffix

View File

@ -309,7 +309,7 @@ func TestSplitUnstructureds(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
inv, infos, err := SplitUnstructureds(WrapInventoryInfoObj, tc.allObjs)
inv, infos, err := SplitUnstructureds(tc.allObjs)
if !tc.isError && err != nil {
t.Fatalf("unexpected error received: %s", err)
}

View File

@ -8,6 +8,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
@ -56,3 +57,7 @@ func (f *FakeProvider) ManifestReader(reader io.Reader, _ []string) (manifestrea
ReaderOptions: readerOptions,
}, nil
}
func (f *FakeProvider) InventoryInfo(objs []*unstructured.Unstructured) (inventory.InventoryInfo, []*unstructured.Unstructured, error) {
return inventory.SplitUnstructureds(objs)
}

View File

@ -7,6 +7,7 @@ import (
"io"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
@ -20,6 +21,7 @@ var _ Provider = &InventoryProvider{}
type Provider interface {
Factory() util.Factory
InventoryClient() (inventory.InventoryClient, error)
InventoryInfo([]*unstructured.Unstructured) (inventory.InventoryInfo, []*unstructured.Unstructured, error)
ToRESTMapper() (meta.RESTMapper, error)
ManifestReader(reader io.Reader, args []string) (manifestreader.ManifestReader, error)
}
@ -47,10 +49,13 @@ func (f *InventoryProvider) InventoryClient() (inventory.InventoryClient, error)
return inventory.NewInventoryClient(f.factory,
inventory.WrapInventoryObj,
inventory.InvInfoToConfigMap,
inventory.WrapInventoryInfoObj,
)
}
func (f *InventoryProvider) InventoryInfo(objs []*unstructured.Unstructured) (inventory.InventoryInfo, []*unstructured.Unstructured, error) {
return inventory.SplitUnstructureds(objs)
}
// ToRESTMapper returns a RESTMapper created by the stored kubectl factory.
func (f *InventoryProvider) ToRESTMapper() (meta.RESTMapper, error) {
return f.factory.ToRESTMapper()