Skip to content

Commit

Permalink
chore: refactor inventory pkg to avoid stuttering
Browse files Browse the repository at this point in the history
  • Loading branch information
karlkfi committed Feb 15, 2022
1 parent e2f4d4c commit 4e94795
Show file tree
Hide file tree
Showing 56 changed files with 392 additions and 391 deletions.
8 changes: 4 additions & 4 deletions cmd/apply/cmdapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"sigs.k8s.io/cli-utils/pkg/printers"
)

func GetRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory,
func GetRunner(factory cmdutil.Factory, invFactory inventory.ClientFactory,
loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *Runner {
r := &Runner{
ioStreams: ioStreams,
Expand Down Expand Up @@ -67,7 +67,7 @@ func GetRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFact
return r
}

func Command(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader,
func Command(f cmdutil.Factory, invFactory inventory.ClientFactory, loader manifestreader.ManifestLoader,
ioStreams genericclioptions.IOStreams) *cobra.Command {
return GetRunner(f, invFactory, loader, ioStreams).Command
}
Expand All @@ -76,7 +76,7 @@ type Runner struct {
Command *cobra.Command
ioStreams genericclioptions.IOStreams
factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
invFactory inventory.ClientFactory
loader manifestreader.ManifestLoader

serverSideOptions common.ServerSideOptions
Expand Down Expand Up @@ -134,7 +134,7 @@ func (r *Runner) RunE(cmd *cobra.Command, args []string) error {
}
inv := inventory.WrapInventoryInfoObj(invObj)

invClient, err := r.invFactory.NewInventoryClient(r.factory)
invClient, err := r.invFactory.NewClient(r.factory)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/destroy/cmddestroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// GetRunner creates and returns the Runner which stores the cobra command.
func GetRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory,
func GetRunner(factory cmdutil.Factory, invFactory inventory.ClientFactory,
loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *Runner {
r := &Runner{
ioStreams: ioStreams,
Expand Down Expand Up @@ -56,7 +56,7 @@ func GetRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFact
}

// Command creates the Runner, returning the cobra command associated with it.
func Command(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader,
func Command(f cmdutil.Factory, invFactory inventory.ClientFactory, loader manifestreader.ManifestLoader,
ioStreams genericclioptions.IOStreams) *cobra.Command {
return GetRunner(f, invFactory, loader, ioStreams).Command
}
Expand All @@ -66,7 +66,7 @@ type Runner struct {
Command *cobra.Command
ioStreams genericclioptions.IOStreams
factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
invFactory inventory.ClientFactory
loader manifestreader.ManifestLoader

output string
Expand Down Expand Up @@ -114,7 +114,7 @@ func (r *Runner) RunE(cmd *cobra.Command, args []string) error {
}
inv := inventory.WrapInventoryInfoObj(invObj)

invClient, err := r.invFactory.NewInventoryClient(r.factory)
invClient, err := r.invFactory.NewClient(r.factory)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/flagutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ func ConvertPropagationPolicy(propagationPolicy string) (metav1.DeletionPropagat
}
}

func ConvertInventoryPolicy(policy string) (inventory.InventoryPolicy, error) {
func ConvertInventoryPolicy(policy string) (inventory.Policy, error) {
switch policy {
case InventoryPolicyStrict:
return inventory.InventoryPolicyMustMatch, nil
return inventory.PolicyMustMatch, nil
case InventoryPolicyAdopt:
return inventory.AdoptIfNoInventory, nil
return inventory.PolicyAdoptIfNoInventory, nil
case InventoryPolicyForceAdopt:
return inventory.AdoptAll, nil
return inventory.PolicyAdoptAll, nil
default:
return inventory.InventoryPolicyMustMatch, fmt.Errorf(
return inventory.PolicyMustMatch, fmt.Errorf(
"inventory policy must be one of strict, adopt")
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/flagutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import (
func TestConvertInventoryPolicy(t *testing.T) {
testcases := []struct {
value string
policy inventory.InventoryPolicy
policy inventory.Policy
err error
}{
{
value: "strict",
policy: inventory.InventoryPolicyMustMatch,
policy: inventory.PolicyMustMatch,
},
{
value: "adopt",
policy: inventory.AdoptIfNoInventory,
policy: inventory.PolicyAdoptIfNoInventory,
},
{
value: "force-adopt",
policy: inventory.AdoptAll,
policy: inventory.PolicyAdoptAll,
},
{
value: "random",
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
initCmd := initcmd.NewCmdInit(f, ioStreams)
updateHelp(names, initCmd)
loader := manifestreader.NewManifestLoader(f)
invFactory := inventory.ClusterInventoryClientFactory{}
invFactory := inventory.ClusterClientFactory{}
applyCmd := apply.Command(f, invFactory, loader, ioStreams)
updateHelp(names, applyCmd)
previewCmd := preview.Command(f, invFactory, loader, ioStreams)
Expand Down
8 changes: 4 additions & 4 deletions cmd/preview/cmdpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
)

// GetRunner creates and returns the Runner which stores the cobra command.
func GetRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory,
func GetRunner(factory cmdutil.Factory, invFactory inventory.ClientFactory,
loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *Runner {
r := &Runner{
factory: factory,
Expand Down Expand Up @@ -65,7 +65,7 @@ func GetRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFact
}

// Command creates the Runner, returning the cobra command associated with it.
func Command(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader,
func Command(f cmdutil.Factory, invFactory inventory.ClientFactory, loader manifestreader.ManifestLoader,
ioStreams genericclioptions.IOStreams) *cobra.Command {
return GetRunner(f, invFactory, loader, ioStreams).Command
}
Expand All @@ -74,7 +74,7 @@ func Command(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loa
type Runner struct {
Command *cobra.Command
factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
invFactory inventory.ClientFactory
loader manifestreader.ManifestLoader
ioStreams genericclioptions.IOStreams

Expand Down Expand Up @@ -126,7 +126,7 @@ func (r *Runner) RunE(cmd *cobra.Command, args []string) error {
}
inv := inventory.WrapInventoryInfoObj(invObj)

invClient, err := r.invFactory.NewInventoryClient(r.factory)
invClient, err := r.invFactory.NewClient(r.factory)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/status/cmdstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"sigs.k8s.io/cli-utils/pkg/manifestreader"
)

func GetRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader) *Runner {
func GetRunner(factory cmdutil.Factory, invFactory inventory.ClientFactory, loader manifestreader.ManifestLoader) *Runner {
r := &Runner{
factory: factory,
invFactory: invFactory,
Expand All @@ -47,7 +47,7 @@ func GetRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFact
return r
}

func Command(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader) *cobra.Command {
func Command(f cmdutil.Factory, invFactory inventory.ClientFactory, loader manifestreader.ManifestLoader) *cobra.Command {
return GetRunner(f, invFactory, loader).Command
}

Expand All @@ -56,7 +56,7 @@ func Command(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loa
type Runner struct {
Command *cobra.Command
factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
invFactory inventory.ClientFactory
loader manifestreader.ManifestLoader

period time.Duration
Expand Down Expand Up @@ -91,7 +91,7 @@ func (r *Runner) runE(cmd *cobra.Command, args []string) error {
}
inv := inventory.WrapInventoryInfoObj(invObj)

invClient, err := r.invFactory.NewInventoryClient(r.factory)
invClient, err := r.invFactory.NewClient(r.factory)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/status/cmdstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ deployment.apps/foo is InProgress: inProgress
loader := manifestreader.NewFakeLoader(tf, tc.inventory)
runner := &Runner{
factory: tf,
invFactory: inventory.FakeInventoryClientFactory(tc.inventory),
invFactory: inventory.FakeClientFactory(tc.inventory),
loader: loader,
pollerFactoryFunc: func(c cmdutil.Factory) (poller.Poller, error) {
return &fakePoller{tc.events}, nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/apply/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const defaultPollInterval = 2 * time.Second
type Applier struct {
pruner *prune.Pruner
statusPoller poller.Poller
invClient inventory.InventoryClient
invClient inventory.Client
client dynamic.Interface
openAPIGetter discovery.OpenAPISchemaInterface
mapper meta.RESTMapper
Expand All @@ -53,7 +53,7 @@ type Applier struct {

// prepareObjects returns the set of objects to apply and to prune or
// an error if one occurred.
func (a *Applier) prepareObjects(localInv inventory.InventoryInfo, localObjs object.UnstructuredSet,
func (a *Applier) prepareObjects(localInv inventory.Info, localObjs object.UnstructuredSet,
o ApplierOptions) (object.UnstructuredSet, object.UnstructuredSet, error) {
if localInv == nil {
return nil, nil, fmt.Errorf("the local inventory can't be nil")
Expand Down Expand Up @@ -102,7 +102,7 @@ func (a *Applier) prepareObjects(localInv inventory.InventoryInfo, localObjs obj
// before all the given resources have been applied to the cluster. Any
// cancellation or timeout will only affect how long we Wait for the
// resources to become current.
func (a *Applier) Run(ctx context.Context, invInfo inventory.InventoryInfo, objects object.UnstructuredSet, options ApplierOptions) <-chan event.Event {
func (a *Applier) Run(ctx context.Context, invInfo inventory.Info, objects object.UnstructuredSet, options ApplierOptions) <-chan event.Event {
klog.V(4).Infof("apply run for %d objects", len(objects))
eventChannel := make(chan event.Event)
setDefaults(&options)
Expand Down Expand Up @@ -274,7 +274,7 @@ type ApplierOptions struct {
PruneTimeout time.Duration

// InventoryPolicy defines the inventory policy of apply.
InventoryPolicy inventory.InventoryPolicy
InventoryPolicy inventory.Policy

// ValidationPolicy defines how to handle invalid objects.
ValidationPolicy validation.Policy
Expand Down Expand Up @@ -304,7 +304,7 @@ func handleError(eventChannel chan event.Event, err error) {
// for the passed non cluster-scoped localObjs, plus the namespace
// of the passed inventory object. This is used to skip deleting
// namespaces which have currently applied objects in them.
func localNamespaces(localInv inventory.InventoryInfo, localObjs []object.ObjMetadata) sets.String {
func localNamespaces(localInv inventory.Info, localObjs []object.ObjMetadata) sets.String {
namespaces := sets.NewString()
for _, obj := range localObjs {
if obj.Namespace != "" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apply/applier_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
type ApplierBuilder struct {
// factory is only used to retrieve things that have not been provided explicitly.
factory util.Factory
invClient inventory.InventoryClient
invClient inventory.Client
client dynamic.Interface
discoClient discovery.CachedDiscoveryInterface
mapper meta.RESTMapper
Expand Down Expand Up @@ -124,7 +124,7 @@ func (b *ApplierBuilder) WithFactory(factory util.Factory) *ApplierBuilder {
return b
}

func (b *ApplierBuilder) WithInventoryClient(invClient inventory.InventoryClient) *ApplierBuilder {
func (b *ApplierBuilder) WithInventoryClient(invClient inventory.Client) *ApplierBuilder {
b.invClient = invClient
return b
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/apply/applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestApplier(t *testing.T) {
clusterObjs: object.UnstructuredSet{},
options: ApplierOptions{
NoPrune: true,
InventoryPolicy: inventory.InventoryPolicyMustMatch,
InventoryPolicy: inventory.PolicyMustMatch,
},
expectedEvents: []testutil.ExpEvent{
{
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestApplier(t *testing.T) {
clusterObjs: object.UnstructuredSet{},
options: ApplierOptions{
ReconcileTimeout: time.Minute,
InventoryPolicy: inventory.InventoryPolicyMustMatch,
InventoryPolicy: inventory.PolicyMustMatch,
EmitStatusEvents: true,
},
statusEvents: []pollevent.Event{
Expand Down Expand Up @@ -425,7 +425,7 @@ func TestApplier(t *testing.T) {
},
options: ApplierOptions{
ReconcileTimeout: time.Minute,
InventoryPolicy: inventory.AdoptIfNoInventory,
InventoryPolicy: inventory.PolicyAdoptIfNoInventory,
EmitStatusEvents: true,
},
statusEvents: []pollevent.Event{
Expand Down Expand Up @@ -606,7 +606,7 @@ func TestApplier(t *testing.T) {
testutil.Unstructured(t, resources["secret"], testutil.AddOwningInv(t, "test")),
},
options: ApplierOptions{
InventoryPolicy: inventory.InventoryPolicyMustMatch,
InventoryPolicy: inventory.PolicyMustMatch,
EmitStatusEvents: true,
},
statusEvents: []pollevent.Event{
Expand Down Expand Up @@ -806,7 +806,7 @@ func TestApplier(t *testing.T) {
},
options: ApplierOptions{
ReconcileTimeout: time.Minute,
InventoryPolicy: inventory.InventoryPolicyMustMatch,
InventoryPolicy: inventory.PolicyMustMatch,
EmitStatusEvents: true,
},
statusEvents: []pollevent.Event{
Expand Down Expand Up @@ -945,7 +945,7 @@ func TestApplier(t *testing.T) {
testutil.Unstructured(t, resources["deployment"], testutil.AddOwningInv(t, "unmatched")),
},
options: ApplierOptions{
InventoryPolicy: inventory.InventoryPolicyMustMatch,
InventoryPolicy: inventory.PolicyMustMatch,
EmitStatusEvents: true,
},
expectedEvents: []testutil.ExpEvent{
Expand Down Expand Up @@ -1052,7 +1052,7 @@ func TestApplier(t *testing.T) {
testutil.Unstructured(t, resources["deployment"], testutil.AddOwningInv(t, "test")),
},
options: ApplierOptions{
InventoryPolicy: inventory.InventoryPolicyMustMatch,
InventoryPolicy: inventory.PolicyMustMatch,
EmitStatusEvents: true,
},
statusEvents: []pollevent.Event{
Expand Down Expand Up @@ -1201,7 +1201,7 @@ func TestApplier(t *testing.T) {
clusterObjs: object.UnstructuredSet{},
options: ApplierOptions{
ReconcileTimeout: time.Minute,
InventoryPolicy: inventory.AdoptIfNoInventory,
InventoryPolicy: inventory.PolicyAdoptIfNoInventory,
EmitStatusEvents: true,
ValidationPolicy: validation.SkipInvalid,
},
Expand Down Expand Up @@ -1382,7 +1382,7 @@ func TestApplier(t *testing.T) {
clusterObjs: object.UnstructuredSet{},
options: ApplierOptions{
ReconcileTimeout: time.Minute,
InventoryPolicy: inventory.AdoptIfNoInventory,
InventoryPolicy: inventory.PolicyAdoptIfNoInventory,
EmitStatusEvents: true,
ValidationPolicy: validation.ExitEarly,
},
Expand Down Expand Up @@ -1562,7 +1562,7 @@ func TestApplierCancel(t *testing.T) {
// EmitStatusEvents required to test event output
EmitStatusEvents: true,
NoPrune: true,
InventoryPolicy: inventory.InventoryPolicyMustMatch,
InventoryPolicy: inventory.PolicyMustMatch,
// ReconcileTimeout required to enable WaitTasks
ReconcileTimeout: 1 * time.Minute,
},
Expand Down Expand Up @@ -1720,7 +1720,7 @@ func TestApplierCancel(t *testing.T) {
// EmitStatusEvents required to test event output
EmitStatusEvents: true,
NoPrune: true,
InventoryPolicy: inventory.InventoryPolicyMustMatch,
InventoryPolicy: inventory.PolicyMustMatch,
// ReconcileTimeout required to enable WaitTasks
ReconcileTimeout: 1 * time.Minute,
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/apply/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (i inventoryInfo) toUnstructured() *unstructured.Unstructured {
}
}

func (i inventoryInfo) toWrapped() inventory.InventoryInfo {
func (i inventoryInfo) toWrapped() inventory.Info {
return inventory.WrapInventoryInfoObj(i.toUnstructured())
}

Expand Down Expand Up @@ -120,10 +120,10 @@ func newTestDestroyer(
func newTestInventory(
t *testing.T,
tf *cmdtesting.TestFactory,
) inventory.InventoryClient {
// Use an InventoryClient with a fakeInfoHelper to allow generating Info
) inventory.Client {
// Use an Client with a fakeInfoHelper to allow generating Info
// objects that use the FakeRESTClient as the UnstructuredClient.
invClient, err := inventory.ClusterInventoryClientFactory{}.NewInventoryClient(tf)
invClient, err := inventory.ClusterClientFactory{}.NewClient(tf)
require.NoError(t, err)
return invClient
}
Expand Down
Loading

0 comments on commit 4e94795

Please sign in to comment.