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

fix: only have yaml out put on stdout if dryrun is enabled #780

Merged
merged 1 commit into from
May 27, 2024
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
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
Loading