Skip to content

Dev: Code architecture

Philippe Martin edited this page Nov 9, 2022 · 14 revisions

Context

A go context is created at the very beginning of the execution of the go program, and is transmitted to the whole program.

import (
	"context"

	"github.com/redhat-developer/odo/pkg/config"
	envcontext "github.com/redhat-developer/odo/pkg/config/context"
	segment "github.com/redhat-developer/odo/pkg/segment/context"
)

func main() {
	// Create a context ready for receiving telemetry data
	// and save into it configuration based on environment variables
	ctx := segment.NewContext(context.Background())
	envConfig, err := config.GetConfiguration()
	if err != nil {
		util.LogErrorAndExit(err, "")
	}
	ctx = envcontext.WithEnvConfig(ctx, *envConfig)

Telemetry data

Thanks to ctx = segment.NewContext(ctx), the context is ready to accept data to be sent to the telemetry platform.

CLI and business layers can use functions provided by the pkg/segment/context package, to set data to be sent: SetComponentType, SetClusterType, etc.

For example, the CLI layer for the odo dev command uses this code to set the component type to be sent to the telemetry:

scontext.SetComponentType(ctx, component.GetComponentTypeFromDevfileMetadata(devFileObj.Data.GetMetadata()))

Configuration based on environment vavriables

odo accepts some environment variables to configure its behaviour.

Thanks to ctx = envcontext.WithEnvConfig(ctx, *envConfig), values of these environment variables are accessible from the context using the function GetEnvConfig from the pkg/config/context package. The value returned by this function is a structure containing pointers to values. A pointer points to nil when the associated environment variable is not defined and points to the value of the variable, when defined.

For example, to get the value of the PODMAN_CMD environment variable, or "podman" if the environment variable is not defined:

podmanCmd := pointer.StringDeref(envcontext.GetEnvConfig(ctx).PodmanCmd, "podman")

These values being injected in the context before calling the NewCmdOdo(ctx, ...) function, they are available when defining the odo commands and flags.

These values are also available when a specific command is executed, from the ctx parameter passed to the Complete, Validate and Run methods of the command.

Context of odo execution

Values defining the context on which odo is executed are stored in the context, and are accessible with specific functions from the pkg/odo/context package:

  • Application name (always "app" for now), with GetApplication(ctx),
  • Directory on which odo is executed, with GetWorkingDirectory(ctx),
  • Path of the devfile used, with GetDevfilePath(ctx),
  • The content of the Devfile used, with GetDevfileObj(ctx),
  • The name of the Devfile component, with GetComponentName(ctx).
  • The current namespace of the Kubernetes/Openshift cluster, with GetNamespace(ctx)

These values are available when a specific command is executed, from the ctx parameter passed to the Complete, Validate and Run methods of the command.

These values are not available when defining the odo commands and flags.

Dependency Injection

[TODO]