Skip to content

Commit

Permalink
Refactor package structure
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 cbfd86c commit 9d50a5c
Show file tree
Hide file tree
Showing 27 changed files with 396 additions and 358 deletions.
21 changes: 16 additions & 5 deletions cmd/kustomizer/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"time"

"github.com/spf13/cobra"
"github.com/stefanprodan/kustomizer/pkg/resmgr"
"github.com/stefanprodan/kustomizer/pkg/inventory"
"github.com/stefanprodan/kustomizer/pkg/manager"
)

var applyCmd = &cobra.Command{
Expand Down Expand Up @@ -77,17 +78,27 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
return err
}

newInventory, err := inventoryMgr.Record(objects)
if err != nil {
newInventory := inventory.NewInventory(applyArgs.inventoryName, applyArgs.inventoryNamespace)
if err := newInventory.AddObjects(objects); err != nil {
return fmt.Errorf("creating inventory failed, error: %w", err)
}
logger.Println(fmt.Sprintf("applying %v manifest(s)...", len(objects)))

resMgr, err := resmgr.NewResourceManager(rootArgs.kubeconfig, rootArgs.kubecontext, PROJECT)
kubeClient, err := newKubeClient(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return err
return fmt.Errorf("client init failed: %w", err)
}

statusPoller, err := newKubeStatusPoller(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return fmt.Errorf("status poller init failed: %w", err)
}

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

ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

Expand Down
12 changes: 6 additions & 6 deletions cmd/kustomizer/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
kustypes "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/filesys"

"github.com/stefanprodan/kustomizer/pkg/resmgr"
"github.com/stefanprodan/kustomizer/pkg/objectutil"
)

var buildCmd = &cobra.Command{
Expand Down Expand Up @@ -72,13 +72,13 @@ func runBuildCmd(cmd *cobra.Command, args []string) error {

switch buildArgs.output {
case "yaml":
yml, err := inventoryMgr.ToYAML(objects)
yml, err := objectutil.ObjectsToYAML(objects)
if err != nil {
return err
}
fmt.Println(yml)
case "json":
json, err := inventoryMgr.ToJSON(objects)
json, err := objectutil.ObjectsToJSON(objects)
if err != nil {
return err
}
Expand All @@ -98,7 +98,7 @@ func buildManifests(kustomizePath string, filePaths []string) ([]*unstructured.U
return nil, err
}

objs, err := inventoryMgr.ReadAll(bytes.NewReader(data))
objs, err := objectutil.ReadObjects(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("%s: %w", kustomizePath, err)
}
Expand All @@ -116,7 +116,7 @@ func buildManifests(kustomizePath string, filePaths []string) ([]*unstructured.U
return nil, err
}

objs, err := inventoryMgr.ReadAll(bufio.NewReader(ms))
objs, err := objectutil.ReadObjects(bufio.NewReader(ms))
ms.Close()
if err != nil {
return nil, fmt.Errorf("%s: %w", manifest, err)
Expand All @@ -125,7 +125,7 @@ func buildManifests(kustomizePath string, filePaths []string) ([]*unstructured.U
}
}

sort.Sort(resmgr.ApplyOrder(objects))
sort.Sort(objectutil.ApplyOrder(objects))
return objects, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/resmgr/client.go → cmd/kustomizer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package resmgr
package main

import (
"fmt"
Expand Down
31 changes: 22 additions & 9 deletions cmd/kustomizer/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
"time"

"github.com/spf13/cobra"
"github.com/stefanprodan/kustomizer/pkg/resmgr"

"github.com/stefanprodan/kustomizer/pkg/manager"
"github.com/stefanprodan/kustomizer/pkg/objectutil"
)

var deleteCmd = &cobra.Command{
Expand Down Expand Up @@ -57,28 +59,39 @@ func deleteCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("--inventory-namespace is required")
}

resMgr, err := resmgr.NewResourceManager(rootArgs.kubeconfig, rootArgs.kubecontext, PROJECT)
if err != nil {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

logger.Println("retrieving inventory...")
inv, err := inventoryMgr.Retrieve(ctx, resMgr.KubeClient(), deleteArgs.inventoryName, deleteArgs.inventoryNamespace)

kubeClient, err := newKubeClient(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return fmt.Errorf("client init failed: %w", err)
}

statusPoller, err := newKubeStatusPoller(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return fmt.Errorf("status poller init failed: %w", err)
}

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

inv, err := inventoryMgr.Retrieve(ctx, kubeClient, deleteArgs.inventoryName, deleteArgs.inventoryNamespace)
if err != nil {
return err
}

objects, err := inv.List()
objects, err := inv.ListObjects()
if err != nil {
return err
}

logger.Println(fmt.Sprintf("deleting %v manifest(s)...", len(objects)))
hasErrors := false
sort.Sort(sort.Reverse(resmgr.ApplyOrder(objects)))
sort.Sort(sort.Reverse(objectutil.ApplyOrder(objects)))
for _, object := range objects {
change, err := resMgr.Delete(ctx, object)
if err != nil {
Expand Down
28 changes: 20 additions & 8 deletions cmd/kustomizer/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (

"github.com/spf13/cobra"

"github.com/stefanprodan/kustomizer/pkg/resmgr"
"github.com/stefanprodan/kustomizer/pkg/inventory"
"github.com/stefanprodan/kustomizer/pkg/manager"
"github.com/stefanprodan/kustomizer/pkg/objectutil"
)

var diffCmd = &cobra.Command{
Expand Down Expand Up @@ -64,16 +66,26 @@ func runDiffCmd(cmd *cobra.Command, args []string) error {
return err
}

newInventory, err := inventoryMgr.Record(objects)
if err != nil {
newInventory := inventory.NewInventory(applyArgs.inventoryName, applyArgs.inventoryNamespace)
if err := newInventory.AddObjects(objects); err != nil {
return fmt.Errorf("creating inventory failed, error: %w", err)
}

resMgr, err := resmgr.NewResourceManager(rootArgs.kubeconfig, rootArgs.kubecontext, PROJECT)
kubeClient, err := newKubeClient(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return err
return fmt.Errorf("client init failed: %w", err)
}

statusPoller, err := newKubeStatusPoller(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return fmt.Errorf("status poller init failed: %w", err)
}

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

ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

Expand All @@ -86,11 +98,11 @@ func runDiffCmd(cmd *cobra.Command, args []string) error {
continue
}

if change.Action == string(resmgr.CreatedAction) {
if change.Action == string(manager.CreatedAction) {
fmt.Println(`►`, change.Subject, "created")
}

if change.Action == string(resmgr.ConfiguredAction) {
if change.Action == string(manager.ConfiguredAction) {
fmt.Println(`►`, change.Subject, "drifted")
fmt.Println(change.Diff)
}
Expand All @@ -104,7 +116,7 @@ func runDiffCmd(cmd *cobra.Command, args []string) error {
}

for _, object := range staleObjects {
fmt.Println(`►`, fmt.Sprintf("%s deleted", resourceFormatter.Unstructured(object)))
fmt.Println(`►`, fmt.Sprintf("%s deleted", objectutil.FmtUnstructured(object)))
}
}

Expand Down
11 changes: 5 additions & 6 deletions cmd/kustomizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ limitations under the License.
package main

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

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

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

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

var (
rootArgs = rootFlags{}
logger = stderrLogger{stderr: os.Stderr}
resourceFormatter = &resmgr.ResourceFormatter{}
inventoryMgr *inventory.InventoryManager
rootArgs = rootFlags{}
logger = stderrLogger{stderr: os.Stderr}
inventoryMgr *inventory.InventoryManager
)

func init() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/inventory/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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:
// - decodes raw manifests (YAML & JSON) into Kubernetes objects
// - records the objects metadata and stores the inventory in a Kubernetes ConfigMap
// - 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
Loading

0 comments on commit 9d50a5c

Please sign in to comment.