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

dry-run mode for add commands #211

Merged
merged 1 commit into from
Dec 12, 2022
Merged
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
99 changes: 68 additions & 31 deletions cmds/ocm/commands/ocmcmds/common/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type Resource interface {
type resource struct {
path string
source string
data []byte
spec ResourceSpec
input *ResourceInput
}
Expand All @@ -113,6 +114,10 @@ func (r *resource) Source() string {
return r.source
}

func (r *resource) Data() []byte {
return r.data
}

func (r *resource) Spec() ResourceSpec {
return r.spec
}
Expand All @@ -128,12 +133,13 @@ func (r *resource) Type() string {
return ""
}

func NewResource(spec ResourceSpec, input *ResourceInput, path string, indices ...int) *resource {
func NewResource(spec ResourceSpec, input *ResourceInput, path string, data []byte, indices ...int) *resource {
id := path
for _, i := range indices {
id += fmt.Sprintf("[%d]", i)
}
return &resource{
data: data,
path: path,
source: id,
spec: spec,
Expand Down Expand Up @@ -424,6 +430,8 @@ type ResourceAdderCommand struct {
Resources []ResourceSpecifications
Envs []string

DryRun bool
Outfile string
Archive string
}

Expand All @@ -437,6 +445,8 @@ func NewResourceAdderCommand(ctx clictx.Context, provider ResourceSpecifications
func (o *ResourceAdderCommand) AddFlags(fs *pflag.FlagSet) {
o.BaseCommand.AddFlags(fs)
fs.StringArrayVarP(&o.Envs, "settings", "s", nil, "settings file with variable settings (yaml)")
fs.BoolVarP(&o.DryRun, "dry-run", "", false, "evaluate and print resource specifications")
fs.StringVarP(&o.Outfile, "output", "O", "", "output file for dry-run")
o.Templating.AddFlags(fs)
if o.Adder != nil {
o.Adder.AddFlags(fs)
Expand All @@ -449,6 +459,9 @@ func (o *ResourceAdderCommand) Complete(args []string) error {
return err
}

if o.Outfile != "" && !o.DryRun {
return fmt.Errorf("--output only usable for dry-run mode")
}
o.Archive, args = fileoption.From(o).GetPath(args, o.Context.FileSystem())
o.Templating.Complete(o.Context.FileSystem())

Expand Down Expand Up @@ -497,42 +510,66 @@ func (o *ResourceAdderCommand) ProcessResourceDescriptions(listkey string, h Res

printer.Printf("found %d %s\n", len(resources), listkey)

obj, err := comparch.Open(o.Context.OCMContext(), accessobj.ACC_WRITABLE, o.Archive, 0, accessio.PathFileSystem(fs))
if err != nil {
return err
}
defer obj.Close()

for _, r := range resources {
isctx := ictx.Section("adding %s...", r.Spec().Info())
if h.RequireInputs() {
if r.input.Input != nil {
var acc ocm.AccessSpec
// Local Blob
blob, hint, berr := r.input.Input.GetBlob(isctx, common.VersionedElementKey(obj), r.path)
if berr != nil {
return errors.Wrapf(berr, "cannot get resource blob for %q(%s)", r.spec.GetName(), r.source)
}
acc, err = obj.AddBlob(blob, r.Type(), hint, nil)
if err == nil {
if o.DryRun {
p := printer
if o.Outfile != "" {
f, err := o.FileSystem().OpenFile(o.Outfile, vfs.O_TRUNC|vfs.O_CREATE|vfs.O_WRONLY, 0o644)
if err != nil {
return errors.Wrapf(err, "cannot create output file %q", o.Outfile)
}
p = common.NewPrinter(f)
}

for _, r := range resources {
var i interface{}
err := yaml.Unmarshal(r.Data(), &i)
if err != nil {
return errors.Wrapf(err, "cannot eval data %q", string(r.Data()))
}
data, err := yaml.Marshal(i)
if err != nil {
return err
}
p.Printf("---\n%s\n", string(data))
}
} else {
obj, err := comparch.Open(o.Context.OCMContext(), accessobj.ACC_WRITABLE, o.Archive, 0, accessio.PathFileSystem(fs))
if err != nil {
return err
}
defer obj.Close()

for _, r := range resources {
isctx := ictx.Section("adding %s...", r.Spec().Info())
if h.RequireInputs() {
if r.input.Input != nil {
var acc ocm.AccessSpec
// Local Blob
blob, hint, berr := r.input.Input.GetBlob(isctx, common.VersionedElementKey(obj), r.path)
if berr != nil {
return errors.Wrapf(berr, "cannot get resource blob for %q(%s)", r.spec.GetName(), r.source)
}
acc, err = obj.AddBlob(blob, r.Type(), hint, nil)
if err == nil {
err = CheckHint(obj, acc)
if err == nil {
err = h.Set(obj, r, acc)
}
}
blob.Close()
} else {
acc := compdesc.GenericAccessSpec(r.input.Access)
err = CheckHint(obj, acc)
if err == nil {
err = h.Set(obj, r, acc)
}
}
blob.Close()
} else {
acc := compdesc.GenericAccessSpec(r.input.Access)
err = CheckHint(obj, acc)
if err == nil {
err = h.Set(obj, r, acc)
}
err = h.Set(obj, r, nil)
}
if err != nil {
return errors.Wrapf(err, "cannot add resource %q(%s)", r.spec.GetName(), r.source)
}
} else {
err = h.Set(obj, r, nil)
}
if err != nil {
return errors.Wrapf(err, "cannot add resource %q(%s)", r.spec.GetName(), r.source)
}
}
return nil
Expand Down Expand Up @@ -624,7 +661,7 @@ func determineResources(printer common.Printer, ctx clictx.Context, ictx inputs.
return nil, errors.Wrapf(err, "invalid spec %d[%d]", i, j+1)
}

resources = append(resources, NewResource(r, input, origin, i, j+1))
resources = append(resources, NewResource(r, input, origin, d, i, j+1))
}
}
return resources, nil
Expand Down
4 changes: 4 additions & 0 deletions components/helmdemo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ $(GEN)/ca: $(GEN)/.exists sources.yaml resources.yaml references.yaml $(CHARTSRC
$(OCM) add references $(GEN)/ca VERSION="$(VERSION)" COMMIT="$(COMMIT)" IMAGE="$(IMAGE):$(VERSION)" HELMINSTCOMP=$(HELMINSTCOMP) references.yaml
@touch $(GEN)/ca

.PHONY: eval-resources
eval-resources:
$(OCM) add resources --dry-run VERSION="$(VERSION)" COMMIT="$(COMMIT)" IMAGE="$(IMAGE):$(VERSION)" resources.yaml -O "$(GEN)/resources.yaml"

.PHONY: push
push: $(GEN)/ctf $(GEN)/push.$(NAME)

Expand Down
10 changes: 8 additions & 2 deletions components/helminstaller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ OCM = go run $(REPO_ROOT)/cmds/ocm
CMDSRCS=$(shell find $(REPO_ROOT)/cmds/$(NAME) -type f)
OCMSRCS=$(shell find $(REPO_ROOT)/pkg -type f) $(REPO_ROOT)/go.*

ATTRIBUTES = VERSION="$(VERSION)" COMMIT="$(COMMIT)" IMAGE="$(IMAGE):$(VERSION)" PLATFORMS="$(PLATFORMS)" MULTI=$(MULTI)

ifeq ($(MULTI),true)
FLAGSUF = .multi
endif
Expand All @@ -46,16 +48,20 @@ ca: $(GEN)/ca

$(GEN)/ca: $(GEN)/.exists $(GEN)/image.$(NAME)$(FLAGSUF) resources.yaml executorspec.yaml
$(OCM) create ca -f $(COMPONENT) "$(VERSION)" --provider $(PROVIDER) --file $(GEN)/ca
$(OCM) add resources --templater spiff $(GEN)/ca VERSION="$(VERSION)" COMMIT="$(COMMIT)" IMAGE="$(IMAGE):$(VERSION)" PLATFORMS="$(PLATFORMS)" MULTI=$(MULTI) resources.yaml
$(OCM) add resources --templater spiff $(GEN)/ca $(ATTRIBUTES) resources.yaml
@touch $(GEN)/ca


.PHONY: plain-ca
plain-ca: $(GEN)/.exists
$(OCM) create ca -f $(COMPONENT) "$(VERSION)"--provider $(PROVIDER) --file $(GEN)/ca
$(OCM) add resources --templater spiff $(GEN)/ca VERSION="$(VERSION)" COMMIT="$(COMMIT)" IMAGE="$(IMAGE):$(VERSION)" PLATFORMS="$(PLATFORMS)" MULTI=$(MULTI) resources.yaml
$(OCM) add resources --templater spiff $(GEN)/ca $(ATTRIBUTES) resources.yaml
@touch $(GEN)/ca

.PHONY: eval-resources
eval-resources:
$(OCM) add resources --dry-run --templater spiff $(ATTRIBUTES) resources.yaml

.PHONY: build
build: $(GEN)/image.$(NAME)

Expand Down
2 changes: 2 additions & 0 deletions docs/reference/ocm_add_references.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ ocm add references [<options>] [<target>] {<referencefile> | <var>=<value>}

```
--addenv access environment for templating
--dry-run evaluate and print resource specifications
-F, --file string target file/directory (default "component-archive")
-h, --help help for references
-O, --output string output file for dry-run
-s, --settings stringArray settings file with variable settings (yaml)
--templater string templater to use (subst, spiff, go) (default "subst")
```
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/ocm_add_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ ocm add resources [<options>] [<target>] {<resourcefile> | <var>=<value>}

```
--addenv access environment for templating
--dry-run evaluate and print resource specifications
-F, --file string target file/directory (default "component-archive")
-h, --help help for resources
-O, --output string output file for dry-run
-s, --settings stringArray settings file with variable settings (yaml)
--templater string templater to use (subst, spiff, go) (default "subst")
```
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/ocm_add_sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ ocm add sources [<options>] [<target>] {<resourcefile> | <var>=<value>}

```
--addenv access environment for templating
--dry-run evaluate and print resource specifications
-F, --file string target file/directory (default "component-archive")
-h, --help help for sources
-O, --output string output file for dry-run
-s, --settings stringArray settings file with variable settings (yaml)
--templater string templater to use (subst, spiff, go) (default "subst")
```
Expand Down