Skip to content

Commit

Permalink
Refactor inventory manager
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Sep 3, 2021
1 parent 2ce8667 commit a46e652
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 190 deletions.
9 changes: 3 additions & 6 deletions cmd/kustomizer/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("status poller init failed: %w", err)
}

resMgr := manager.NewResourceManager(kubeClient, statusPoller, manager.Owner{
Field: PROJECT,
Group: PROJECT + ".dev",
})
resMgr := manager.NewResourceManager(kubeClient, statusPoller, inventoryOwner)

resMgr.SetOwnerLabels(objects, applyArgs.inventoryName, applyArgs.inventoryNamespace)

Expand Down Expand Up @@ -133,12 +130,12 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("mode not supported")
}

staleObjects, err := inventoryMgr.GetStaleObjects(ctx, resMgr.KubeClient(), newInventory, applyArgs.inventoryName, applyArgs.inventoryNamespace)
staleObjects, err := resMgr.GetInventoryStaleObjects(ctx, newInventory)
if err != nil {
return fmt.Errorf("inventory query failed, error: %w", err)
}

err = inventoryMgr.Store(ctx, resMgr.KubeClient(), newInventory, applyArgs.inventoryName, applyArgs.inventoryNamespace)
err = resMgr.ApplyInventory(ctx, newInventory)
if err != nil {
return fmt.Errorf("inventory apply failed, error: %w", err)
}
Expand Down
13 changes: 5 additions & 8 deletions cmd/kustomizer/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"fmt"
"github.com/stefanprodan/kustomizer/pkg/inventory"
"os"
"sort"
"time"
Expand Down Expand Up @@ -74,13 +75,10 @@ func deleteCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("status poller init failed: %w", err)
}

resMgr := manager.NewResourceManager(kubeClient, statusPoller, manager.Owner{
Field: PROJECT,
Group: PROJECT + ".dev",
})
resMgr := manager.NewResourceManager(kubeClient, statusPoller, inventoryOwner)

inv, err := inventoryMgr.Retrieve(ctx, kubeClient, deleteArgs.inventoryName, deleteArgs.inventoryNamespace)
if err != nil {
inv := inventory.NewInventory(applyArgs.inventoryName, applyArgs.inventoryNamespace)
if err := resMgr.GetInventory(ctx, inv); err != nil {
return err
}

Expand All @@ -106,8 +104,7 @@ func deleteCmdRun(cmd *cobra.Command, args []string) error {
os.Exit(1)
}

err = inventoryMgr.Remove(ctx, resMgr.KubeClient(), deleteArgs.inventoryName, deleteArgs.inventoryNamespace)
if err != nil {
if err := resMgr.DeleteInventory(ctx, inv); err != nil {
return err
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/kustomizer/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ func runDiffCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("status poller init failed: %w", err)
}

resMgr := manager.NewResourceManager(kubeClient, statusPoller, manager.Owner{
Field: PROJECT,
Group: PROJECT + ".dev",
})
resMgr := manager.NewResourceManager(kubeClient, statusPoller, inventoryOwner)

ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()
Expand Down Expand Up @@ -114,7 +111,7 @@ func runDiffCmd(cmd *cobra.Command, args []string) error {
}

if diffArgs.inventoryName != "" {
staleObjects, err := inventoryMgr.GetStaleObjects(ctx, resMgr.KubeClient(), newInventory, diffArgs.inventoryName, diffArgs.inventoryNamespace)
staleObjects, err := resMgr.GetInventoryStaleObjects(ctx, newInventory)
if err != nil {
return fmt.Errorf("inventory query failed, error: %w", err)
}
Expand Down
22 changes: 9 additions & 13 deletions cmd/kustomizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ limitations under the License.
package main

import (
"github.com/stefanprodan/kustomizer/pkg/manager"
"os"
"path/filepath"
"path"
"time"

"github.com/spf13/cobra"
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/stefanprodan/kustomizer/pkg/inventory"
)

var VERSION = "1.0.0-dev.0"
Expand All @@ -46,9 +45,12 @@ type rootFlags struct {
}

var (
rootArgs = rootFlags{}
logger = stderrLogger{stderr: os.Stderr}
inventoryMgr *inventory.InventoryManager
rootArgs = rootFlags{}
logger = stderrLogger{stderr: os.Stderr}
inventoryOwner = manager.Owner{
Field: "kustomizer",
Group: "inventory.kustomizer.dev",
}
)

func init() {
Expand All @@ -65,12 +67,6 @@ func init() {
func main() {
configureKubeconfig()

if im, err := inventory.NewInventoryManager(PROJECT, PROJECT+".dev"); err != nil {
panic(err)
} else {
inventoryMgr = im
}

if err := rootCmd.Execute(); err != nil {
logger.Println(`✗`, err)
os.Exit(1)
Expand All @@ -84,7 +80,7 @@ func configureKubeconfig() {
rootArgs.kubeconfig = os.Getenv("KUBECONFIG")
default:
if home := homeDir(); len(home) > 0 {
rootArgs.kubeconfig = filepath.Join(home, ".kube", "config")
rootArgs.kubeconfig = path.Join(home, ".kube", "config")
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/inventory/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ limitations under the License.
*/

// Package inventory contains utilities for keeping a record of Kubernetes objects applied on a cluster.
//
// The InventoryManager performs the following actions:
// - records the Kubernetes objects metadata in a compacted format
// - stores the inventory in a Kubernetes ConfigMap
// - determines which objects are subject to garbage collection
package inventory
146 changes: 0 additions & 146 deletions pkg/inventory/manager.go

This file was deleted.

2 changes: 2 additions & 0 deletions pkg/manager/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ limitations under the License.
// - waits for the objects to be fully reconciled by looking up their readiness status
// - deletes objects that are subject to garbage collection
// - waits for the deleted objects to be terminated
// - maintains an inventory of objects applied on the cluster
// - performs garbage collection of stale objects based on the inventory entries
package manager
14 changes: 7 additions & 7 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ResourceManager struct {
owner Owner
}

// NewResourceManager creates a ResourceManager for the given Kubernetes client config and context.
// NewResourceManager creates a ResourceManager for the given Kubernetes client.
func NewResourceManager(client client.Client, poller *polling.StatusPoller, owner Owner) *ResourceManager {
return &ResourceManager{
client: client,
Expand All @@ -41,15 +41,11 @@ func NewResourceManager(client client.Client, poller *polling.StatusPoller, owne
}
}

// KubeClient returns the underlying controller-runtime client.
func (m *ResourceManager) KubeClient() client.Client {
// Client returns the underlying controller-runtime client.
func (m *ResourceManager) Client() client.Client {
return m.client
}

func (m *ResourceManager) changeSetEntry(object *unstructured.Unstructured, action Action) *ChangeSetEntry {
return &ChangeSetEntry{Subject: objectutil.FmtUnstructured(object), Action: string(action)}
}

// SetOwnerLabels adds the ownership labels to the given objects.
// The ownership labels are in the format:
// <owner.group>/name: <name>
Expand All @@ -62,3 +58,7 @@ func (m *ResourceManager) SetOwnerLabels(objects []*unstructured.Unstructured, n
})
}
}

func (m *ResourceManager) changeSetEntry(object *unstructured.Unstructured, action Action) *ChangeSetEntry {
return &ChangeSetEntry{Subject: objectutil.FmtUnstructured(object), Action: string(action)}
}
Loading

0 comments on commit a46e652

Please sign in to comment.