Skip to content

Commit

Permalink
fix: only have yaml out put on stdout if dryrun is enabled (#780)
Browse files Browse the repository at this point in the history
## Description

open-component-model/ocm-project#67


## What type of PR is this? (check all applicable)

- [ ] 🍕 Feature
- [ ] 🎇 Restructuring
- [ ] 🐛 Bug Fix
- [ ] 📝 Documentation Update
- [ ] 🎨 Style
- [ ] 🧑‍💻 Code Refactor
- [ ] 🔥 Performance Improvements
- [ ] ✅ Test
- [ ] 🤖 Build
- [ ] 🔁 CI
- [ ] 📦 Chore (Release)
- [ ] ⏩ Revert

## Related Tickets & Documents

<!-- 
Please use this format link issue numbers: Fixes #123

https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->
- Related Issue # (issue)
- Closes # (issue)
- Fixes # (issue)
> Remove if not applicable

## Screenshots

<!-- Visual changes require screenshots -->


## Added tests?

- [ ] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help
- [ ] Separate ticket for tests # (issue/pr)

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration


## Added to documentation?

- [ ] 📜 README.md
- [ ] 🙅 no documentation needed

## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
  • Loading branch information
Skarlso authored May 27, 2024
1 parent b835e96 commit ab463b2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
19 changes: 19 additions & 0 deletions cmds/ocm/commands/controllercmds/common/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package common

import (
"fmt"

"github.com/open-component-model/ocm/pkg/out"
)

func Outf(ctx out.Context, dryRun bool, msg string, args ...any) (int, error) {
if dryRun {
return -1, nil
}

if len(args) == 0 {
return fmt.Fprint(ctx.StdOut(), msg)
}

return fmt.Fprintf(ctx.StdOut(), msg, args...)
}
10 changes: 5 additions & 5 deletions cmds/ocm/commands/controllercmds/common/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Install(ctx context.Context, octx clictx.Context, sm *ssa.ResourceManager,
return fmt.Errorf("✗ failed to apply manifests: %w", err)
}

out.Outf(octx, "► waiting for ocm deployment to be ready\n")
Outf(octx, dryRun, "► waiting for ocm deployment to be ready\n")
if err = sm.Wait(objects, ssa.DefaultWaitOptions()); err != nil {
return fmt.Errorf("✗ failed to wait for objects to be ready: %w", err)
}
Expand All @@ -50,7 +50,7 @@ func Uninstall(ctx context.Context, octx clictx.Context, sm *ssa.ResourceManager
return fmt.Errorf("✗ failed to delete manifests: %w", err)
}

out.Outf(octx, "► waiting for ocm deployment to be deleted\n")
Outf(octx, dryRun, "► waiting for ocm deployment to be deleted\n")
if err = sm.WaitForTermination(objects, ssa.DefaultWaitOptions()); err != nil {
return fmt.Errorf("✗ failed to wait for objects to be deleted: %w", err)
}
Expand All @@ -64,7 +64,7 @@ func fetchObjects(ctx context.Context, octx clictx.Context, releaseURL, baseURL,
if err != nil {
return nil, fmt.Errorf("✗ failed to retrieve latest version for %s: %w", manifest, err)
}
out.Outf(octx, "► got latest version %q\n", latest)
Outf(octx, dryRun, "► got latest version %q\n", latest)
version = latest
} else {
exists, err := existingVersion(ctx, releaseURL, version)
Expand All @@ -90,7 +90,7 @@ func fetchObjects(ctx context.Context, octx clictx.Context, releaseURL, baseURL,
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, fmt.Errorf("✗ failed to find %s file at location: %w", filename, err)
}
out.Outf(octx, "✔ successfully fetched install file\n")
Outf(octx, dryRun, "✔ successfully fetched install file\n")
if dryRun {
content, err := os.ReadFile(path)
if err != nil {
Expand All @@ -101,7 +101,7 @@ func fetchObjects(ctx context.Context, octx clictx.Context, releaseURL, baseURL,

return nil, nil
}
out.Outf(octx, "► applying to cluster...\n")
Outf(octx, dryRun, "► applying to cluster...\n")

objects, err := ReadObjects(path)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions cmds/ocm/commands/controllercmds/install/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/out"
)

var (
Expand Down Expand Up @@ -97,22 +96,22 @@ func (o *Command) Run() (err error) {

ctx := context.Background()
if !o.SkipPreFlightCheck {
out.Outf(o.Context, "► running pre-install check\n")
common.Outf(o.Context, o.DryRun, "► running pre-install check\n")
if err := o.RunPreFlightCheck(ctx); err != nil {
if o.InstallPrerequisites {
out.Outf(o.Context, "► installing prerequisites\n")
common.Outf(o.Context, o.DryRun, "► installing prerequisites\n")
if err := o.installPrerequisites(ctx); err != nil {
return err
}

out.Outf(o.Context, "✔ successfully installed prerequisites\n")
common.Outf(o.Context, o.DryRun, "✔ successfully installed prerequisites\n")
} else {
return fmt.Errorf("✗ failed to run pre-flight check: %w\n", err)
}
}
}

out.Outf(o.Context, "► installing ocm-controller with version %s\n", o.Version)
common.Outf(o.Context, o.DryRun, "► installing ocm-controller with version %s\n", o.Version)
version := o.Version
if err := common.Install(
ctx,
Expand All @@ -128,7 +127,7 @@ func (o *Command) Run() (err error) {
return err
}

out.Outf(o.Context, "✔ ocm-controller successfully installed\n")
common.Outf(o.Context, o.DryRun, "✔ ocm-controller successfully installed\n")
return nil
}

Expand Down
11 changes: 2 additions & 9 deletions cmds/ocm/commands/controllercmds/install/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,14 @@ var _ = Describe("Test Environment", func() {
It("install latest version", func() {
buf := bytes.NewBuffer(nil)
Expect(env.CatchOutput(buf).Execute("controller", "install", "-d", "-s", "-u", testServer.URL, "-a", testServer.URL)).To(Succeed())
Expect(buf.String()).To(StringEqualTrimmedWithContext(`► installing ocm-controller with version latest
► got latest version "v0.0.1-test"
✔ successfully fetched install file
test: content
✔ ocm-controller successfully installed
Expect(buf.String()).To(StringEqualTrimmedWithContext(`test: content
`))
})

It("install specific version", func() {
buf := bytes.NewBuffer(nil)
Expect(env.CatchOutput(buf).Execute("controller", "install", "-d", "-s", "-u", testServer.URL, "-a", testServer.URL, "-v", "v0.1.0-test-2")).To(Succeed())
Expect(buf.String()).To(StringEqualTrimmedWithContext(`► installing ocm-controller with version v0.1.0-test-2
✔ successfully fetched install file
test: content
✔ ocm-controller successfully installed
Expect(buf.String()).To(StringEqualTrimmedWithContext(`test: content
`))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,25 @@ import (
"github.com/mandelsoft/filepath/pkg/filepath"

"github.com/open-component-model/ocm/cmds/ocm/commands/controllercmds/common"
"github.com/open-component-model/ocm/pkg/out"
)

//go:embed issuer/registry_certificate.yaml
var issuer []byte

func (o *Command) installPrerequisites(ctx context.Context) error {
out.Outf(o.Context, "► installing cert-manager with version %s\n", o.CertManagerVersion)
common.Outf(o.Context, o.DryRun, "► installing cert-manager with version %s\n", o.CertManagerVersion)

if err := common.Install(ctx, o.Context, o.SM, o.CertManagerReleaseAPIURL, o.CertManagerBaseURL, "cert-manager", "cert-manager.yaml", o.CertManagerVersion, o.DryRun); err != nil {
return err
}

out.Outf(o.Context, "✔ cert-manager successfully installed\n")
common.Outf(o.Context, o.DryRun, "✔ cert-manager successfully installed\n")

if o.DryRun {
return nil
}

out.Outf(o.Context, "► creating certificate for internal registry\n")
common.Outf(o.Context, o.DryRun, "► creating certificate for internal registry\n")

if err := o.createRegistryCertificate(); err != nil {
return fmt.Errorf("✗ failed to create registry certificate: %w", err)
Expand Down
5 changes: 2 additions & 3 deletions cmds/ocm/commands/controllercmds/uninstall/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ var _ = Describe("Test Environment", func() {
It("uninstall latest version", func() {
buf := bytes.NewBuffer(nil)
Expect(env.CatchOutput(buf).Execute("controller", "uninstall", "-d", "-u", testServer.URL, "-a", testServer.URL)).To(Succeed())
fmt.Println(buf.String())
Expect(buf.String()).To(StringEqualTrimmedWithContext(`► uninstalling ocm-controller with version latest
► got latest version "v0.0.1-test"
✔ successfully fetched install file
test: content
✔ ocm-controller successfully uninstalled
`))
Expand All @@ -61,8 +60,8 @@ test: content
It("uninstall specific version", func() {
buf := bytes.NewBuffer(nil)
Expect(env.CatchOutput(buf).Execute("controller", "uninstall", "-d", "-u", testServer.URL, "-a", testServer.URL, "-v", "v0.1.0-test-2")).To(Succeed())
fmt.Println(buf.String())
Expect(buf.String()).To(StringEqualTrimmedWithContext(`► uninstalling ocm-controller with version v0.1.0-test-2
✔ successfully fetched install file
test: content
✔ ocm-controller successfully uninstalled
`))
Expand Down

0 comments on commit ab463b2

Please sign in to comment.