diff --git a/cmd/apply/cmdapply.go b/cmd/apply/cmdapply.go index 04cd301..fa8cf9a 100644 --- a/cmd/apply/cmdapply.go +++ b/cmd/apply/cmdapply.go @@ -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 } diff --git a/cmd/destroy/cmddestroy.go b/cmd/destroy/cmddestroy.go index 696f729..20d5a02 100644 --- a/cmd/destroy/cmddestroy.go +++ b/cmd/destroy/cmddestroy.go @@ -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 } diff --git a/cmd/preview/cmdpreview.go b/cmd/preview/cmdpreview.go index da17757..cdf5791 100644 --- a/cmd/preview/cmdpreview.go +++ b/cmd/preview/cmdpreview.go @@ -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 } diff --git a/cmd/status/cmdstatus.go b/cmd/status/cmdstatus.go index 5f9b92c..c04303b 100644 --- a/cmd/status/cmdstatus.go +++ b/cmd/status/cmdstatus.go @@ -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 } diff --git a/pkg/apply/applier_test.go b/pkg/apply/applier_test.go index 3e410b8..65a163e 100644 --- a/pkg/apply/applier_test.go +++ b/pkg/apply/applier_test.go @@ -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 diff --git a/pkg/inventory/fake-inventory-client.go b/pkg/inventory/fake-inventory-client.go index a39dab0..57c95b1 100644 --- a/pkg/inventory/fake-inventory-client.go +++ b/pkg/inventory/fake-inventory-client.go @@ -81,7 +81,3 @@ func (fic *FakeInventoryClient) SetError(err error) { func (fic *FakeInventoryClient) ClearError() { fic.Err = nil } - -func (fic *FakeInventoryClient) InvInfoFactoryFunc() UnstructuredToInvInfoFunc { - return WrapInventoryInfoObj -} diff --git a/pkg/inventory/inventory-client.go b/pkg/inventory/inventory-client.go index 70baf45..85bd2fb 100644 --- a/pkg/inventory/inventory-client.go +++ b/pkg/inventory/inventory-client.go @@ -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 diff --git a/pkg/inventory/inventory-client_test.go b/pkg/inventory/inventory-client_test.go index 3ffbe05..eb29273 100644 --- a/pkg/inventory/inventory-client_test.go +++ b/pkg/inventory/inventory-client_test.go @@ -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 { diff --git a/pkg/inventory/inventory.go b/pkg/inventory/inventory.go index c3f6bb5..8c4909c 100644 --- a/pkg/inventory/inventory.go +++ b/pkg/inventory/inventory.go @@ -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 diff --git a/pkg/inventory/inventory_test.go b/pkg/inventory/inventory_test.go index d1f576f..ed407eb 100644 --- a/pkg/inventory/inventory_test.go +++ b/pkg/inventory/inventory_test.go @@ -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) } diff --git a/pkg/provider/fake-provider.go b/pkg/provider/fake-provider.go index 9a5375f..a1353b8 100644 --- a/pkg/provider/fake-provider.go +++ b/pkg/provider/fake-provider.go @@ -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) +} diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index 4da355a..f6c74ae 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -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()