Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] inventory client rewrite #663

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
}

loader := manifestreader.NewManifestLoader(f)
invFactory := inventory.ConfigMapClientFactory{StatusPolicy: inventory.StatusPolicyNone}
invFactory := inventory.ConfigMapClientFactory{}

names := []string{"init", "apply", "destroy", "diff", "preview", "status"}
subCmds := []*cobra.Command{
Expand Down
41 changes: 24 additions & 17 deletions cmd/status/cmdstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ type Runner struct {
timeout time.Duration
output string

invType string
inventoryNames string
inventoryNameSet map[string]bool
namespaces string
namespaceSet map[string]bool
statuses string
statusSet map[string]bool
invType string
inventoryNames string
inventoryIDSet map[string]bool
namespaces string
namespaceSet map[string]bool
statuses string
statusSet map[string]bool

PollerFactoryFunc func(cmdutil.Factory) (poller.Poller, error)
}
Expand All @@ -124,9 +124,9 @@ func (r *Runner) preRunE(*cobra.Command, []string) error {
}

if r.inventoryNames != "" {
r.inventoryNameSet = make(map[string]bool)
r.inventoryIDSet = make(map[string]bool)
for _, name := range strings.Split(r.inventoryNames, ",") {
r.inventoryNameSet[name] = true
r.inventoryIDSet[name] = true
}
}

Expand Down Expand Up @@ -164,22 +164,24 @@ func (r *Runner) loadInvFromDisk(cmd *cobra.Command, args []string) (*printer.Pr

// Based on the inventory template manifest we look up the inventory
// from the live state using the inventory client.
identifiers, err := invClient.GetClusterObjs(cmd.Context(), inv)
clusterInventory, err := invClient.Get(cmd.Context(), inv, inventory.GetOptions{})
if err != nil {
return nil, err
}

identifiers := clusterInventory.ObjectRefs()

printData := printer.PrintData{
Identifiers: object.ObjMetadataSet{},
InvNameMap: make(map[object.ObjMetadata]string),
InvIDMap: make(map[object.ObjMetadata]fmt.Stringer),
StatusSet: r.statusSet,
}

for _, obj := range identifiers {
// check if the object is under one of the targeted namespaces
if _, ok := r.namespaceSet[obj.Namespace]; ok || len(r.namespaceSet) == 0 {
// add to the map for future reference
printData.InvNameMap[obj] = inv.Name()
printData.InvIDMap[obj] = inv.ID()
// append to identifiers
printData.Identifiers = append(printData.Identifiers, obj)
}
Expand All @@ -197,26 +199,31 @@ func (r *Runner) listInvFromCluster() (*printer.PrintData, error) {
// initialize maps in printData
printData := printer.PrintData{
Identifiers: object.ObjMetadataSet{},
InvNameMap: make(map[object.ObjMetadata]string),
InvIDMap: make(map[object.ObjMetadata]fmt.Stringer),
StatusSet: r.statusSet,
}

identifiersMap, err := invClient.ListClusterInventoryObjs(r.ctx)
inventories, err := invClient.List(r.ctx, inventory.ListOptions{})
if err != nil {
return nil, err
}

for invName, identifiers := range identifiersMap {
identifiersMap := make(map[inventory.ID]object.ObjMetadataSet)
for _, inv := range inventories {
identifiersMap[inv.ID()] = inv.ObjectRefs()
}

for invID, identifiers := range identifiersMap {
// Check if there are targeted inventory names and include the current inventory name
if _, ok := r.inventoryNameSet[invName]; !ok && len(r.inventoryNameSet) != 0 {
if _, ok := r.inventoryIDSet[invID.String()]; !ok && len(r.inventoryIDSet) != 0 {
continue
}
// Filter objects
for _, obj := range identifiers {
// check if the object is under one of the targeted namespaces
if _, ok := r.namespaceSet[obj.Namespace]; ok || len(r.namespaceSet) == 0 {
// add to the map for future reference
printData.InvNameMap[obj] = invName
printData.InvIDMap[obj] = invID
// append to identifiers
printData.Identifiers = append(printData.Identifiers, obj)
}
Expand Down
44 changes: 22 additions & 22 deletions cmd/status/cmdstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ kind: ConfigMap
apiVersion: v1
metadata:
labels:
cli-utils.sigs.k8s.io/inventory-id: test
name: foo
cli-utils.sigs.k8s.io/inventory-id: inventory-id
name: inventory-name
namespace: default
`
depObject = object.ObjMetadata{
Expand Down Expand Up @@ -121,8 +121,8 @@ func TestCommand(t *testing.T) {
},
},
expectedOutput: `
foo/deployment.apps/default/foo is InProgress: inProgress
foo/statefulset.apps/default/bar is Current: current
inventory-id/deployment.apps/default/foo is InProgress: inProgress
inventory-id/statefulset.apps/default/bar is Current: current
`,
},
"wait for all current": {
Expand Down Expand Up @@ -168,10 +168,10 @@ foo/statefulset.apps/default/bar is Current: current
},
},
expectedOutput: `
foo/deployment.apps/default/foo is InProgress: inProgress
foo/statefulset.apps/default/bar is InProgress: inProgress
foo/statefulset.apps/default/bar is Current: current
foo/deployment.apps/default/foo is Current: current
inventory-id/deployment.apps/default/foo is InProgress: inProgress
inventory-id/statefulset.apps/default/bar is InProgress: inProgress
inventory-id/statefulset.apps/default/bar is Current: current
inventory-id/deployment.apps/default/foo is Current: current
`,
},
"wait for all deleted": {
Expand Down Expand Up @@ -201,8 +201,8 @@ foo/deployment.apps/default/foo is Current: current
},
},
expectedOutput: `
foo/statefulset.apps/default/bar is NotFound: notFound
foo/deployment.apps/default/foo is NotFound: notFound
inventory-id/statefulset.apps/default/bar is NotFound: notFound
inventory-id/deployment.apps/default/foo is NotFound: notFound
`,
},
"forever with timeout": {
Expand Down Expand Up @@ -233,8 +233,8 @@ foo/deployment.apps/default/foo is NotFound: notFound
},
},
expectedOutput: `
foo/statefulset.apps/default/bar is InProgress: inProgress
foo/deployment.apps/default/foo is InProgress: inProgress
inventory-id/statefulset.apps/default/bar is InProgress: inProgress
inventory-id/deployment.apps/default/foo is InProgress: inProgress
`,
},
}
Expand Down Expand Up @@ -283,7 +283,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "foo",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "InProgress",
"message": "inProgress",
},
Expand All @@ -294,7 +294,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "bar",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "Current",
"message": "current",
},
Expand Down Expand Up @@ -350,7 +350,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "foo",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "InProgress",
"message": "inProgress",
},
Expand All @@ -361,7 +361,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "bar",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "InProgress",
"message": "inProgress",
},
Expand All @@ -372,7 +372,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "bar",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "Current",
"message": "current",
},
Expand All @@ -383,7 +383,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "foo",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "Current",
"message": "current",
},
Expand Down Expand Up @@ -423,7 +423,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "bar",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "NotFound",
"message": "notFound",
},
Expand All @@ -434,7 +434,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "foo",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "NotFound",
"message": "notFound",
},
Expand Down Expand Up @@ -475,7 +475,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "bar",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "InProgress",
"message": "inProgress",
},
Expand All @@ -486,7 +486,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
"name": "foo",
"timestamp": "",
"type": "status",
"inventory-name": "foo",
"inventory-name": "inventory-id",
"status": "InProgress",
"message": "inProgress",
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/status/printers/event/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func (ep *Printer) printStatusEvent(se pollevent.Event) error {
switch se.Type {
case pollevent.ResourceUpdateEvent:
id := se.Resource.Identifier
var invName string
var invName fmt.Stringer
var ok bool
if invName, ok = ep.Data.InvNameMap[id]; !ok {
if invName, ok = ep.Data.InvIDMap[id]; !ok {
return fmt.Errorf("%s: resource not found", id)
}
// filter out status that are not assigned
Expand Down
4 changes: 2 additions & 2 deletions cmd/status/printers/json/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func (ep *Printer) printStatusEvent(se pollevent.Event) error {
switch se.Type {
case pollevent.ResourceUpdateEvent:
id := se.Resource.Identifier
var invName string
var invName fmt.Stringer
var ok bool
if invName, ok = ep.Data.InvNameMap[id]; !ok {
if invName, ok = ep.Data.InvIDMap[id]; !ok {
return fmt.Errorf("%s: resource not found", id)
}
// filter out status that are not assigned
Expand Down
4 changes: 3 additions & 1 deletion cmd/status/printers/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package printer

import (
"fmt"

"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
"sigs.k8s.io/cli-utils/pkg/object"
Expand All @@ -12,7 +14,7 @@ import (
// PrintData records data required for printing
type PrintData struct {
Identifiers object.ObjMetadataSet
InvNameMap map[object.ObjMetadata]string
InvIDMap map[object.ObjMetadata]fmt.Stringer
StatusSet map[string]bool
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/status/printers/table/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package table

import (
"fmt"
"strings"

"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector"
Expand All @@ -17,13 +18,13 @@ import (
// needed by the BaseTablePrinter.
type CollectorAdapter struct {
collector *collector.ResourceStatusCollector
invNameMap map[object.ObjMetadata]string
invNameMap map[object.ObjMetadata]fmt.Stringer
statusSet map[string]bool
}

type ResourceInfo struct {
resourceStatus *pe.ResourceStatus
invName string
invName fmt.Stringer
}

func (r *ResourceInfo) Identifier() object.ObjMetadata {
Expand Down
4 changes: 2 additions & 2 deletions cmd/status/printers/table/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (t *Printer) Print(ch <-chan event.Event, identifiers object.ObjMetadataSet
// printing the latest state on a regular cadence.
printCompleted := t.runPrintLoop(&CollectorAdapter{
collector: coll,
invNameMap: t.PrintData.InvNameMap,
invNameMap: t.PrintData.InvIDMap,
statusSet: t.PrintData.StatusSet,
}, stop)

Expand Down Expand Up @@ -77,7 +77,7 @@ var invNameColumn = table.ColumnDef{
ColumnHeader: "INVENTORY_NAME",
ColumnWidth: 30,
PrintResourceFunc: func(w io.Writer, width int, r table.Resource) (int, error) {
group := r.(*ResourceInfo).invName
group := r.(*ResourceInfo).invName.String()
if len(group) > width {
group = group[:width]
}
Expand Down
22 changes: 0 additions & 22 deletions pkg/apis/actuation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,9 @@
package actuation

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

// Inventory represents the inventory object in memory.
// Inventory is currently only used for in-memory storage and not serialized to
// disk or to the API server.
type Inventory struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec InventorySpec `json:"spec,omitempty"`
Status InventoryStatus `json:"status,omitempty"`
}

// InventorySpec is the specification of the desired/expected inventory state.
type InventorySpec struct {
Objects []ObjectReference `json:"objects,omitempty"`
}

// InventoryStatus is the status of the current/last-known inventory state.
type InventoryStatus struct {
Objects []ObjectStatus `json:"objects,omitempty"`
}

// ObjectReference is a reference to a KRM resource by name and kind.
//
// Kubernetes only stores one API Version for each Kind at any given time,
Expand Down
Loading