Skip to content

Commit

Permalink
Pass the image registry preference to the Devfile parser to build the…
Browse files Browse the repository at this point in the history
… effective view
  • Loading branch information
rm3l committed Apr 25, 2023
1 parent cfc4757 commit 65bfd50
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pkg/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func TestGatherName(t *testing.T) {
return nil, dir, err
}

d, err := devfile.ParseAndValidateFromFile(dPath, false)
d, err := devfile.ParseAndValidateFromFile(dPath, "", false)
if err != nil {
return nil, dir, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/dev/kubedev/kubedev.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (o *DevClient) Start(
// RegenerateAdapterAndPush get the new devfile and pushes the files to remote pod
func (o *DevClient) regenerateAdapterAndPush(ctx context.Context, pushParams common.PushParameters, componentStatus *watch.ComponentStatus) error {

devObj, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), pushParams.StartOptions.Variables, true)
devObj, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), pushParams.StartOptions.Variables, o.prefClient.GetImageRegistry(), true)
if err != nil {
return fmt.Errorf("unable to generate component from watch parameters: %w", err)
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/dev/podmandev/podmandev.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/podman"
"github.com/redhat-developer/odo/pkg/portForward"
"github.com/redhat-developer/odo/pkg/preference"
"github.com/redhat-developer/odo/pkg/state"
"github.com/redhat-developer/odo/pkg/sync"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
Expand All @@ -41,6 +42,7 @@ type DevClient struct {
fs filesystem.Filesystem

podmanClient podman.Client
prefClient preference.Client
portForwardClient portForward.Client
syncClient sync.Client
execClient exec.Client
Expand All @@ -56,6 +58,7 @@ var _ dev.Client = (*DevClient)(nil)
func NewDevClient(
fs filesystem.Filesystem,
podmanClient podman.Client,
prefClient preference.Client,
portForwardClient portForward.Client,
syncClient sync.Client,
execClient exec.Client,
Expand All @@ -65,6 +68,7 @@ func NewDevClient(
return &DevClient{
fs: fs,
podmanClient: podmanClient,
prefClient: prefClient,
portForwardClient: portForwardClient,
syncClient: syncClient,
execClient: execClient,
Expand Down Expand Up @@ -171,14 +175,14 @@ func (o *DevClient) checkVolumesFree(pod *corev1.Pod) error {
}

func (o *DevClient) watchHandler(ctx context.Context, pushParams common.PushParameters, componentStatus *watch.ComponentStatus) error {
printWarningsOnDevfileChanges(ctx, pushParams.StartOptions)
o.printWarningsOnDevfileChanges(ctx, pushParams.StartOptions)
return o.reconcile(ctx, pushParams.StartOptions, componentStatus)
}

func printWarningsOnDevfileChanges(ctx context.Context, options dev.StartOptions) {
func (o *DevClient) printWarningsOnDevfileChanges(ctx context.Context, options dev.StartOptions) {
var warning string
currentDevfile := odocontext.GetEffectiveDevfileObj(ctx)
newDevfile, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), options.Variables, true)
newDevfile, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), options.Variables, o.prefClient.GetImageRegistry(), true)
if err != nil {
warning = fmt.Sprintf("error while reading the Devfile. Please restart 'odo dev' if you made any changes to the Devfile. Error message is: %v", err)
} else {
Expand Down
43 changes: 27 additions & 16 deletions pkg/devfile/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package devfile

import (
"fmt"
"os"
"strconv"
"strings"

"github.com/devfile/api/v2/pkg/validation/variables"
Expand All @@ -14,13 +16,12 @@ import (
)

func parseRawDevfile(args parser.ParserArgs) (parser.DevfileObj, error) {
rawArgs := args
rawArgs.FlattenedDevfile = pointer.Bool(false)
rawArgs.ConvertKubernetesContentInUri = pointer.Bool(false)
rawArgs.ImageNamesAsSelector = nil
rawArgs.SetBooleanDefaults = pointer.Bool(false)
args.FlattenedDevfile = pointer.Bool(false)
args.ConvertKubernetesContentInUri = pointer.Bool(false)
args.ImageNamesAsSelector = nil
args.SetBooleanDefaults = pointer.Bool(false)

devfileObj, varWarnings, err := devfile.ParseDevfileAndValidate(rawArgs)
devfileObj, varWarnings, err := devfile.ParseDevfileAndValidate(args)
if err != nil {
return parser.DevfileObj{}, err
}
Expand All @@ -33,13 +34,18 @@ func parseRawDevfile(args parser.ParserArgs) (parser.DevfileObj, error) {

func parseEffectiveDevfile(args parser.ParserArgs) (parser.DevfileObj, error) {
// Effective Devfile with everything resolved (e.g., parent flattened, K8s URIs inlined, ...)
effectiveArgs := args
effectiveArgs.FlattenedDevfile = pointer.Bool(true)
effectiveArgs.ConvertKubernetesContentInUri = pointer.Bool(true)
effectiveArgs.SetBooleanDefaults = pointer.Bool(false)
args.SetBooleanDefaults = pointer.Bool(false)
args.FlattenedDevfile = pointer.Bool(true)
args.ConvertKubernetesContentInUri = pointer.Bool(true)
if args.ImageNamesAsSelector != nil && args.ImageNamesAsSelector.Registry != "" {
// Tag should be a unique build identifier
args.ImageNamesAsSelector.Tag = strconv.Itoa(os.Getpid())
} else {
args.ImageNamesAsSelector = nil
}

var varWarnings variables.VariableWarning
devfileObj, varWarnings, err := devfile.ParseDevfileAndValidate(effectiveArgs)
devfileObj, varWarnings, err := devfile.ParseDevfileAndValidate(args)
if err != nil {
return parser.DevfileObj{}, err
}
Expand All @@ -58,9 +64,12 @@ func parseEffectiveDevfile(args parser.ParserArgs) (parser.DevfileObj, error) {

// ParseAndValidateFromFile reads, parses and validates devfile from a file
// if there are warning it logs them on stdout
func ParseAndValidateFromFile(devfilePath string, wantEffective bool) (parser.DevfileObj, error) {
func ParseAndValidateFromFile(devfilePath string, imageRegistry string, wantEffective bool) (parser.DevfileObj, error) {
parserArgs := parser.ParserArgs{
Path: devfilePath,
ImageNamesAsSelector: &parser.ImageSelectorArgs{
Registry: imageRegistry,
},
}
if wantEffective {
return parseEffectiveDevfile(parserArgs)
Expand All @@ -73,11 +82,13 @@ func ParseAndValidateFromFile(devfilePath string, wantEffective bool) (parser.De
// If wantEffective is true, it returns a complete view of the Devfile, where everything is resolved.
// For example, parent will be flattened in the child, and Kubernetes manifests referenced by URI will be inlined in the related components.
// If there are warnings, it logs them on stdout.
func ParseAndValidateFromFileWithVariables(devfilePath string, variables map[string]string, wantEffective bool) (parser.DevfileObj, error) {
func ParseAndValidateFromFileWithVariables(devfilePath string, variables map[string]string, imageRegistry string, wantEffective bool) (parser.DevfileObj, error) {
parserArgs := parser.ParserArgs{
Path: devfilePath,
ExternalVariables: variables,
SetBooleanDefaults: pointer.Bool(false),
Path: devfilePath,
ExternalVariables: variables,
ImageNamesAsSelector: &parser.ImageSelectorArgs{
Registry: imageRegistry,
},
}
if wantEffective {
return parseEffectiveDevfile(parserArgs)
Expand Down
2 changes: 1 addition & 1 deletion pkg/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (o *InitClient) SelectAndPersonalizeDevfile(ctx context.Context, flags map[
return parser.DevfileObj{}, "", nil, fmt.Errorf("unable to download devfile: %w", err)
}

devfileObj, err := devfile.ParseAndValidateFromFile(devfilePath, true)
devfileObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", true)
if err != nil {
return parser.DevfileObj{}, "", nil, fmt.Errorf("unable to parse devfile: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/add/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (o *AddBindingOptions) Run(ctx context.Context) error {
withDevfile := odoutil.CheckPathExists(location.DevfileLocation(odocontext.GetWorkingDirectory(ctx)))
var devfileObj *parser.DevfileObj
if withDevfile {
rawDevfileObj, err := devfile.ParseAndValidateFromFile(odocontext.GetDevfilePath(ctx), false)
rawDevfileObj, err := devfile.ParseAndValidateFromFile(odocontext.GetDevfilePath(ctx), "", false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (o *InitOptions) run(ctx context.Context) (devfileObj parser.DevfileObj, pa

// in case the starter project contains a devfile, read it again
if _, err = o.clientset.FS.Stat(devfilePath); err == nil {
devfileObj, err = devfile.ParseAndValidateFromFile(devfilePath, true)
devfileObj, err = devfile.ParseAndValidateFromFile(devfilePath, "", true)
if err != nil {
return parser.DevfileObj{}, "", "", nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/remove/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (o *RemoveBindingOptions) Validate(ctx context.Context) (err error) {

func (o *RemoveBindingOptions) Run(ctx context.Context) error {
// Update the raw Devfile only, so we do not break any relationship between parent-child for example
rawDevfileObj, err := devfile.ParseAndValidateFromFile(odocontext.GetDevfilePath(ctx), false)
rawDevfileObj, err := devfile.ParseAndValidateFromFile(odocontext.GetDevfilePath(ctx), "", false)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/odo/genericclioptions/clientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func Fetch(command *cobra.Command, platform string) (*Clientset, error) {
dep.DevClient = podmandev.NewDevClient(
dep.FS,
dep.PodmanClient,
dep.PreferenceClient,
dep.PortForwardClient,
dep.SyncClient,
dep.ExecClient,
Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/genericclioptions/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
odoutil "github.com/redhat-developer/odo/pkg/util"
)

func getDevfileInfo(workingDir string, variables map[string]string) (
func getDevfileInfo(workingDir string, variables map[string]string, imageRegistry string) (
devfilePath string,
devfileObj *parser.DevfileObj,
componentName string,
Expand All @@ -28,7 +28,7 @@ func getDevfileInfo(workingDir string, variables map[string]string) (
}
// Parse devfile and validate
var devObj parser.DevfileObj
devObj, err = devfile.ParseAndValidateFromFileWithVariables(devfilePath, variables, true)
devObj, err = devfile.ParseAndValidateFromFileWithVariables(devfilePath, variables, imageRegistry, true)
if err != nil {
return "", nil, "", fmt.Errorf("failed to parse the devfile %s: %w", devfilePath, 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 @@ -248,7 +248,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) error {

var devfilePath, componentName string
var devfileObj *parser.DevfileObj
devfilePath, devfileObj, componentName, err = getDevfileInfo(cwd, variables)
devfilePath, devfileObj, componentName, err = getDevfileInfo(cwd, variables, userConfig.GetImageRegistry())
if err != nil {
startTelemetry(cmd, err, startTime)
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func (o RegistryClient) retrieveDevfileDataFromRegistry(ctx context.Context, reg
devfileYamlFile := location.DevfileFilenamesProvider(tmpFile)

// Parse and validate the file and return the devfile data
devfileObj, err := devfile.ParseAndValidateFromFile(path.Join(tmpFile, devfileYamlFile), true)
devfileObj, err := devfile.ParseAndValidateFromFile(path.Join(tmpFile, devfileYamlFile), "", true)
if err != nil {
return api.DevfileData{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/testingutil/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func GetTestDevfileObjFromFile(fileName string) parser.DevfileObj {
// path to the devfile
devfilePath := filepath.Join(filepath.Dir(filename), "..", "..", "tests", "examples", filepath.Join("source", "devfiles", "nodejs", fileName))

devfileObj, err := devfile.ParseAndValidateFromFile(devfilePath, false)
devfileObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", false)
if err != nil {
return parser.DevfileObj{}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/helper/odo_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func DeleteProject(projectName string) {

// GetMetadataFromDevfile retrieves the metadata from devfile
func GetMetadataFromDevfile(devfilePath string) devfilepkg.DevfileMetadata {
devObj, err := devfile.ParseAndValidateFromFile(devfilePath, true)
devObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", true)
Expect(err).ToNot(HaveOccurred())
return devObj.Data.GetMetadata()
}

func GetDevfileComponents(devfilePath, componentName string) []v1alpha2.Component {
devObj, err := devfile.ParseAndValidateFromFile(devfilePath, true)
devObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", true)
Expect(err).ToNot(HaveOccurred())
components, err := devObj.Data.GetComponents(common.DevfileOptions{
FilterByName: componentName,
Expand Down

0 comments on commit 65bfd50

Please sign in to comment.