Skip to content

Commit

Permalink
image.Build*: Use values from ctx instead of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Nov 8, 2022
1 parent ae46de3 commit 6436cde
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func newDeployHandler(ctx context.Context, fs filesystem.Filesystem, devfileObj

// ApplyImage builds and pushes the OCI image to be used on Kubernetes
func (o *deployHandler) ApplyImage(img v1alpha2.Component) error {
return image.BuildPushSpecificImage(o.ctx, o.fs, o.path, img, true)
return image.BuildPushSpecificImage(o.ctx, o.fs, img, true)
}

// ApplyKubernetes applies inline Kubernetes YAML from the devfile.yaml file
Expand Down
2 changes: 1 addition & 1 deletion pkg/devfile/adapters/kubernetes/component/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type runHandler struct {
var _ libdevfile.Handler = (*runHandler)(nil)

func (a *runHandler) ApplyImage(img devfilev1.Component) error {
return image.BuildPushSpecificImage(a.ctx, a.fs, a.path, img, true)
return image.BuildPushSpecificImage(a.ctx, a.fs, img, true)
}

func (a *runHandler) ApplyKubernetes(kubernetes devfilev1.Component) error {
Expand Down
19 changes: 14 additions & 5 deletions pkg/devfile/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"context"
"errors"
"os/exec"
"path/filepath"

devfile "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"

envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/log"
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"

"k8s.io/utils/pointer"
Expand All @@ -33,7 +34,12 @@ var lookPathCmd = exec.LookPath

// BuildPushImages build all images defined in the devfile with the detected backend
// If push is true, also push the images to their registries
func BuildPushImages(ctx context.Context, fs filesystem.Filesystem, devfileObj parser.DevfileObj, path string, push bool) error {
func BuildPushImages(ctx context.Context, fs filesystem.Filesystem, push bool) error {
var (
devfileObj = odocontext.GetDevfileObj(ctx)
devfilePath = odocontext.GetDevfilePath(ctx)
path = filepath.Dir(devfilePath)
)

backend, err := selectBackend(ctx)
if err != nil {
Expand Down Expand Up @@ -61,13 +67,16 @@ func BuildPushImages(ctx context.Context, fs filesystem.Filesystem, devfileObj p

// BuildPushSpecificImage build an image defined in the devfile present in devfilePath
// If push is true, also push the image to its registry
func BuildPushSpecificImage(ctx context.Context, fs filesystem.Filesystem, devfilePath string, component devfile.Component, push bool) error {
func BuildPushSpecificImage(ctx context.Context, fs filesystem.Filesystem, component devfile.Component, push bool) error {
var (
devfilePath = odocontext.GetDevfilePath(ctx)
path = filepath.Dir(devfilePath)
)
backend, err := selectBackend(ctx)
if err != nil {
return err
}

return buildPushImage(backend, fs, component.Image, devfilePath, push)
return buildPushImage(backend, fs, component.Image, path, push)
}

// buildPushImage build an image using the provided backend
Expand Down
8 changes: 1 addition & 7 deletions pkg/odo/cli/build_images/build_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package build_images
import (
"context"
"fmt"
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"
Expand Down Expand Up @@ -63,12 +62,7 @@ func (o *BuildImagesOptions) Validate(ctx context.Context) (err error) {

// Run contains the logic for the odo command
func (o *BuildImagesOptions) Run(ctx context.Context) (err error) {
var (
devfileObj = odocontext.GetDevfileObj(ctx)
devfilePath = odocontext.GetDevfilePath(ctx)
path = filepath.Dir(devfilePath)
)
return image.BuildPushImages(ctx, o.clientset.FS, *devfileObj, path, o.pushFlag)
return image.BuildPushImages(ctx, o.clientset.FS, o.pushFlag)
}

// NewCmdBuildImages implements the odo command
Expand Down
9 changes: 5 additions & 4 deletions pkg/odo/commonflags/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package commonflags
import (
"errors"
"flag"
"os"
"strconv"

"github.com/redhat-developer/odo/pkg/config"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -35,7 +36,7 @@ func AddOutputFlag() {

// CheckMachineReadableOutputCommand performs machine-readable output functions required to
// have it work correctly
func CheckMachineReadableOutputCommand(cmd *cobra.Command) error {
func CheckMachineReadableOutputCommand(envconfig *config.Configuration, cmd *cobra.Command) error {

// Get the needed values
outputFlag := pflag.Lookup(OutputFlagName)
Expand Down Expand Up @@ -71,8 +72,8 @@ func CheckMachineReadableOutputCommand(cmd *cobra.Command) error {
// Override the logging level by the value (if set) by the ODO_LOG_LEVEL env
// The "-v" flag set on command line will take precedence over ODO_LOG_LEVEL env
v := flag.CommandLine.Lookup("v").Value.String()
if level, ok := os.LookupEnv("ODO_LOG_LEVEL"); ok && v == "0" {
_ = flag.CommandLine.Set("v", level)
if envconfig.OdoLogLevel != nil && v == "0" {
_ = flag.CommandLine.Set("v", strconv.Itoa(*envconfig.OdoLogLevel))
}
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/odo/commonflags/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestUseOutputFlagOK(t *testing.T) {
if err != nil {
t.Errorf("Set error should be nil but is %v", err)
}
err = CheckMachineReadableOutputCommand(cmd)
err = CheckMachineReadableOutputCommand(nil, cmd)
if err != nil {
t.Errorf("Check error should be nil but is %v", err)
}
Expand All @@ -26,7 +26,7 @@ func TestUseOutputFlagNotUsed(t *testing.T) {
if err != nil {
t.Errorf("Set error should be nil but is %v", err)
}
err = CheckMachineReadableOutputCommand(cmd)
err = CheckMachineReadableOutputCommand(nil, cmd)
if err.Error() != "Machine readable output is not yet implemented for this command" {
t.Errorf("Check error is %v", err)
}
Expand All @@ -38,7 +38,7 @@ func TestUseOutputFlagWrongValue(t *testing.T) {
if err != nil {
t.Errorf("Set error should be nil but is %v", err)
}
err = CheckMachineReadableOutputCommand(cmd)
err = CheckMachineReadableOutputCommand(nil, cmd)
if err.Error() != "Please input a valid output format for -o, available format: json" {
t.Errorf("Check error is %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/genericclioptions/runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
startTelemetry(cmd, err, startTime)
})

util.LogErrorAndExit(commonflags.CheckMachineReadableOutputCommand(cmd), "")
util.LogErrorAndExit(commonflags.CheckMachineReadableOutputCommand(envConfig, cmd), "")
util.LogErrorAndExit(commonflags.CheckRunOnCommand(cmd), "")
util.LogErrorAndExit(commonflags.CheckVariablesCommand(cmd), "")

Expand Down

0 comments on commit 6436cde

Please sign in to comment.