Skip to content

Commit

Permalink
Merge pull request #26 from kform-dev/inventory
Browse files Browse the repository at this point in the history
changed inventory such that user can supply inventory id
  • Loading branch information
henderiw authored Aug 9, 2024
2 parents 167c67d + 1d73891 commit 126ad64
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 35 deletions.
3 changes: 3 additions & 0 deletions cmd/kform/commands/applycmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewRunner(ctx context.Context, factory util.Factory, ioStreams genericcliop
r.Command.Flags().BoolVar(&r.DryRun, "dry-run", false, "executes a speculative execution plan, without applying the resources")
r.Command.Flags().StringVarP(&r.Input, "in", "i", "", "a file or directory of KRM resource(s) that act as input rendering the package")
r.Command.Flags().StringVarP(&r.Output, "out", "o", "", "a file or directory where the result is stored, a filename creates a single yaml doc; a dir creates seperated yaml files")
r.Command.Flags().StringVar(&r.InventoryID, "inventory-id", "", "iventory-id to identify the applied resources, use valid semantics")

return r
}
Expand All @@ -47,6 +48,7 @@ type Runner struct {
DryRun bool
Input string
Output string
InventoryID string
}

func (r *Runner) runE(c *cobra.Command, args []string) error {
Expand All @@ -66,6 +68,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error {
Path: path,
DryRun: r.DryRun,
AutoApprove: r.AutoApprove,
InventoryID: r.InventoryID,
})

return kfrunner.Run(ctx)
Expand Down
3 changes: 3 additions & 0 deletions cmd/kform/commands/destroycmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewRunner(ctx context.Context, factory util.Factory, ioStreams genericcliop

r.Command.Flags().BoolVar(&r.AutoApprove, "auto-approve", false, "skip interactive approval of plan before destroying")
r.Command.Flags().BoolVar(&r.DryRun, "dry-run", false, "executes a speculative execution plan, without destroying the resources")
r.Command.Flags().StringVar(&r.InventoryID, "inventory-id", "", "iventory-id to identify the applied resources, use valid semantics")

return r
}
Expand All @@ -45,6 +46,7 @@ type Runner struct {
DryRun bool
Input string
Output string
InventoryID string
}

func (r *Runner) runE(c *cobra.Command, args []string) error {
Expand All @@ -65,6 +67,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error {
Destroy: true,
AutoApprove: r.AutoApprove,
DryRun: r.DryRun,
InventoryID: r.InventoryID,
})

return kfrunner.Run(ctx)
Expand Down
15 changes: 10 additions & 5 deletions cmd/kform/commands/initcmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewRunner(ctx context.Context, ioStreams genericclioptions.IOStreams) *Runn
}

r.Command = cmd
r.Command.Flags().StringVarP(&r.InvConfig.InventoryID, "inventory-id", "i", "", "iventory-id listing the applied resources, use valid semantics")
r.Command.Flags().StringVarP(&r.InvConfig.InventoryID, "inventory-id", "i", "", "iventory-id to identify the applied resources, use valid semantics")
return r
}

Expand All @@ -39,9 +39,14 @@ type Runner struct {

func (r *Runner) runE(c *cobra.Command, args []string) error {
ctx := c.Context()
err := r.InvConfig.Complete(ctx, args[0])
if err != nil {
return err

if r.InvConfig.InventoryID == "" {
err := r.InvConfig.Complete(ctx, args[0])
if err != nil {
return err
}
return r.InvConfig.Run(ctx)
}
return r.InvConfig.Run(ctx)
return nil

}
3 changes: 3 additions & 0 deletions cmd/kform/commands/plancmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewRunner(ctx context.Context, factory util.Factory, ioStreams genericcliop
r.Command.Flags().BoolVar(&r.Destroy, "destroy", false, "destroys resources managed by this plan")
r.Command.Flags().StringVarP(&r.Input, "in", "i", "", "a file or directory of KRM resource(s) that act as input rendering the package")
r.Command.Flags().StringVarP(&r.Output, "out", "o", "", "a file or directory where the result is stored, a filename creates a single yaml doc; a dir creates seperated yaml files")
r.Command.Flags().StringVar(&r.InventoryID, "inventory-id", "", "iventory-id to identify the applied resources, use valid semantics")

return r
}
Expand All @@ -46,6 +47,7 @@ type Runner struct {
Destroy bool
Input string
Output string
InventoryID string
}

func (r *Runner) runE(c *cobra.Command, args []string) error {
Expand All @@ -65,6 +67,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error {
Path: path,
DryRun: true,
Destroy: r.Destroy,
InventoryID: r.InventoryID,
})

return kfrunner.Run(ctx)
Expand Down
33 changes: 18 additions & 15 deletions pkg/exec/kform/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ type Runner interface {
}

type Config struct {
Factory util.Factory
PackageName string
LocalInventory *unstructured.Unstructured
Input string // used for none, file or dir
InputData store.Storer[[]byte]
Output string
OutputData store.Storer[[]byte]
Path string // path of the kform files
ResourceData store.Storer[[]byte] // this providers resource externally w/o having to parse from a filepath
DryRun bool
Destroy bool
AutoApprove bool
Factory util.Factory
PackageName string
Input string // used for none, file or dir
InputData store.Storer[[]byte]
Output string
OutputData store.Storer[[]byte]
Path string // path of the kform files
ResourceData store.Storer[[]byte] // this providers resource externally w/o having to parse from a filepath
DryRun bool
Destroy bool
AutoApprove bool
InventoryID string
}

func NewKformRunner(cfg *Config) Runner {
Expand All @@ -60,14 +60,17 @@ func (r *runner) Run(ctx context.Context) error {
var err error
// get the local inventory file, which serves as a reference to lookup
// the inventory in the cluster backend when it was not supplied
if r.cfg.LocalInventory == nil {
r.cfg.LocalInventory, err = config.GetInventoryInfo(r.cfg.Path)
var localInventory *unstructured.Unstructured
if r.cfg.InventoryID != "" {
localInventory = config.GetFakeInventoryInfo(r.cfg.InventoryID)
} else {
localInventory, err = config.GetInventoryInfo(r.cfg.Path)
if err != nil {
return err
}
}

r.invManager, err = manager.New(ctx, r.cfg.LocalInventory, r.cfg.Factory, invv1alpha1.ActuationStrategyApply)
r.invManager, err = manager.New(ctx, localInventory, r.cfg.Factory, invv1alpha1.ActuationStrategyApply)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/inventory/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Client interface {
// GetClusterInventory returns the inventory, which consists of the providers with their
// resp. configs and the packages with their respective objRefs;
// or an error if one occurred.
GetClusterInventory(ctx context.Context, inv Info) (*invv1alpha1.Inventory, error)
GetClusterInventory(ctx context.Context, invName Info) (*invv1alpha1.Inventory, error)
// GetClusterInventoryInfo returns the cluster inventory object.
GetClusterInventoryInfo(ctx context.Context, inv Info) (*unstructured.Unstructured, error)

Expand Down
15 changes: 12 additions & 3 deletions pkg/inventory/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/kform-dev/kform/pkg/inventory/config/configmap"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/yaml"
//"sigs.k8s.io/cli-utils/pkg/common"
)

const (
Expand Down Expand Up @@ -139,11 +139,11 @@ func (r *Config) GetInventoryFileString() string {
func (r *Config) fillInValues() string {
now := time.Now()
nowStr := now.Format("2006-01-02 15:04:05 MST")
randomSuffix := common.RandomStr()
//randomSuffix := common.RandomStr()
manifestStr := r.Template
manifestStr = strings.ReplaceAll(manifestStr, "<DATETIME>", nowStr)
manifestStr = strings.ReplaceAll(manifestStr, "<NAMESPACE>", r.Namespace)
manifestStr = strings.ReplaceAll(manifestStr, "<RANDOMSUFFIX>", randomSuffix)
//manifestStr = strings.ReplaceAll(manifestStr, "<RANDOMSUFFIX>", randomSuffix)
manifestStr = strings.ReplaceAll(manifestStr, "<INVENTORYID>", r.InventoryID)
manifestStr = strings.ReplaceAll(manifestStr, "<INVENTORYKEY>", invv1alpha1.InventoryLabelKey)
return manifestStr
Expand Down Expand Up @@ -172,3 +172,12 @@ func ParseInventoryFile(b []byte) (*unstructured.Unstructured, error) {
}
return u, nil
}

func GetFakeInventoryInfo(inventoryID string) *unstructured.Unstructured {
u := &unstructured.Unstructured{}
u.SetAPIVersion("v1")
u.SetKind("ConfigMap")
u.SetName(inventoryID)
u.SetNamespace(kformInventoryNamespace)
return u
}
8 changes: 1 addition & 7 deletions pkg/inventory/config/configmap/cm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,5 @@ metadata:
# NOTE: The name of the inventory object does NOT have
# any impact on group-related functionality such as
# deletion or pruning.
name: inventory-<RANDOMSUFFIX>
labels:
# DANGER: Do not change the value of this label.
# Changing this value will cause a loss of continuity
# with previously applied grouped objects. Set deletion
# and pruning functionality will be impaired.
<INVENTORYKEY>: <INVENTORYID>
name: inventory-<INVENTORYID>
`
1 change: 0 additions & 1 deletion pkg/render2/celrenderer/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func getCelEnv(vars map[string]any) (*cel.Env, error) {
}

func concat(strs traits.Lister, separator string) (string, error) {
fmt.Println("wimconcat")
sz := strs.Size().(types.Int)
var sb strings.Builder
for i := types.Int(0); i < sz; i++ {
Expand Down
3 changes: 0 additions & 3 deletions pkg/render2/celrenderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package celrenderer

import (
"context"
"fmt"
"regexp"
"strings"

Expand Down Expand Up @@ -127,8 +126,6 @@ func (r *renderer) RenderString(ctx context.Context, expr string) (any, error) {
}
log.Debug("expression", "expr", expr)
log.Debug("expression", "vars", newVars)
fmt.Println("expression expr", expr)
fmt.Println("expression vars", newVars)
env, err := getCelEnv(newVars)
if err != nil {
log.Error("cel environment failed", "error", err)
Expand Down

0 comments on commit 126ad64

Please sign in to comment.