Skip to content

Commit

Permalink
Move Devfile param to WatchParams and biuld adapter only once
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Apr 21, 2023
1 parent 8170cfb commit aff78e7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 80 deletions.
58 changes: 21 additions & 37 deletions pkg/dev/kubedev/kubedev.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type DevClient struct {
execClient exec.Client
deleteClient _delete.Client
configAutomountClient configAutomount.Client

adapter component.Adapter
}

var _ dev.Client = (*DevClient)(nil)
Expand All @@ -62,6 +64,17 @@ func NewDevClient(
deleteClient _delete.Client,
configAutomountClient configAutomount.Client,
) *DevClient {
adapter := component.NewKubernetesAdapter(
kubernetesClient,
prefClient,
portForwardClient,
bindingClient,
syncClient,
execClient,
configAutomountClient,
filesystem,
)

return &DevClient{
kubernetesClient: kubernetesClient,
prefClient: prefClient,
Expand All @@ -73,6 +86,7 @@ func NewDevClient(
execClient: execClient,
deleteClient: deleteClient,
configAutomountClient: configAutomountClient,
adapter: adapter,
}
}

Expand All @@ -88,18 +102,6 @@ func (o *DevClient) Start(
devfileObj = odocontext.GetDevfileObj(ctx)
)

adapter := component.NewKubernetesAdapter(
o.kubernetesClient,
o.prefClient,
o.portForwardClient,
o.bindingClient,
o.syncClient,
o.execClient,
o.configAutomountClient,
o.filesystem,
*devfileObj,
)

pushParameters := adapters.PushParameters{
IgnoredFiles: options.IgnorePaths,
Debug: options.Debug,
Expand All @@ -108,13 +110,14 @@ func (o *DevClient) Start(
RandomPorts: options.RandomPorts,
CustomForwardedPorts: options.CustomForwardedPorts,
ErrOut: errOut,
Devfile: *devfileObj,
}

klog.V(4).Infoln("Creating inner-loop resources for the component")
componentStatus := watch.ComponentStatus{
ImageComponentsAutoApplied: make(map[string]v1alpha2.ImageComponent),
}
err := adapter.Push(ctx, pushParameters, &componentStatus)
err := o.adapter.Push(ctx, pushParameters, &componentStatus)
if err != nil {
return err
}
Expand All @@ -138,38 +141,19 @@ func (o *DevClient) Start(
return o.watchClient.WatchAndPush(out, watchParameters, ctx, componentStatus)
}

// RegenerateAdapterAndPush regenerates the adapter and pushes the files to remote pod
// RegenerateAdapterAndPush get the new devfile and pushes the files to remote pod
func (o *DevClient) regenerateAdapterAndPush(ctx context.Context, pushParams adapters.PushParameters, watchParams watch.WatchParameters, componentStatus *watch.ComponentStatus) error {
var adapter component.ComponentAdapter

adapter, err := o.regenerateComponentAdapterFromWatchParams(watchParams)
devObj, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), watchParams.Variables)
if err != nil {
return fmt.Errorf("unable to generate component from watch parameters: %w", err)
}

err = adapter.Push(ctx, pushParams, componentStatus)
pushParams.Devfile = devObj

err = o.adapter.Push(ctx, pushParams, componentStatus)
if err != nil {
return fmt.Errorf("watch command was unable to push component: %w", err)
}

return nil
}

func (o *DevClient) regenerateComponentAdapterFromWatchParams(parameters watch.WatchParameters) (component.ComponentAdapter, error) {
devObj, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(""), parameters.Variables)
if err != nil {
return nil, err
}

return component.NewKubernetesAdapter(
o.kubernetesClient,
o.prefClient,
o.portForwardClient,
o.bindingClient,
o.syncClient,
o.execClient,
o.configAutomountClient,
o.filesystem,
devObj,
), nil
}
65 changes: 32 additions & 33 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (

devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/generator"
"github.com/devfile/library/v2/pkg/devfile/parser"
parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
dfutil "github.com/devfile/library/v2/pkg/util"

Expand All @@ -62,8 +61,8 @@ type Adapter struct {
configAutomountClient configAutomount.Client
fs filesystem.Filesystem // FS is the object used for building image component if present

devfile parser.DevfileObj // Devfile is the object returned by the Devfile parser
logger machineoutput.MachineEventLoggingClient
//devfile parser.DevfileObj // Devfile is the object returned by the Devfile parser
logger machineoutput.MachineEventLoggingClient
}

var _ ComponentAdapter = (*Adapter)(nil)
Expand All @@ -78,7 +77,7 @@ func NewKubernetesAdapter(
execClient exec.Client,
configAutomountClient configAutomount.Client,
fs filesystem.Filesystem,
devfile parser.DevfileObj,
// devfile parser.DevfileObj,
) Adapter {
return Adapter{
kubeClient: kubernetesClient,
Expand All @@ -89,7 +88,6 @@ func NewKubernetesAdapter(
execClient: execClient,
configAutomountClient: configAutomountClient,
fs: fs,
devfile: devfile,

logger: machineoutput.NewMachineEventLoggingClient(),
}
Expand Down Expand Up @@ -124,7 +122,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
}

klog.V(4).Infof("component state: %q\n", componentStatus.State)
err = a.buildPushAutoImageComponents(ctx, a.fs, a.devfile, componentStatus)
err = a.buildPushAutoImageComponents(ctx, a.fs, parameters.Devfile, componentStatus)
if err != nil {
return err
}
Expand All @@ -139,11 +137,11 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
}

// Set the mode to Dev since we are using "odo dev" here
runtime := component.GetComponentRuntimeFromDevfileMetadata(a.devfile.Data.GetMetadata())
runtime := component.GetComponentRuntimeFromDevfileMetadata(parameters.Devfile.Data.GetMetadata())
labels := odolabels.GetLabels(componentName, appName, runtime, odolabels.ComponentDevMode, false)

var updated bool
deployment, updated, err = a.createOrUpdateComponent(ctx, deploymentExists, libdevfile.DevfileCommands{
deployment, updated, err = a.createOrUpdateComponent(ctx, parameters, deploymentExists, libdevfile.DevfileCommands{
BuildCmd: parameters.DevfileBuildCmd,
RunCmd: parameters.DevfileRunCmd,
DebugCmd: parameters.DevfileDebugCmd,
Expand All @@ -156,7 +154,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
// Delete remote resources that are not present in the Devfile
selector := odolabels.GetSelector(componentName, appName, odolabels.ComponentDevMode, false)

objectsToRemove, serviceBindingSecretsToRemove, err := a.getRemoteResourcesNotPresentInDevfile(ctx, selector)
objectsToRemove, serviceBindingSecretsToRemove, err := a.getRemoteResourcesNotPresentInDevfile(ctx, parameters, selector)
if err != nil {
return fmt.Errorf("unable to determine resources to delete: %w", err)
}
Expand All @@ -176,7 +174,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
}

// Create all the K8s components defined in the devfile
_, err = a.pushDevfileKubernetesComponents(ctx, labels, odolabels.ComponentDevMode, ownerReference)
_, err = a.pushDevfileKubernetesComponents(ctx, parameters, labels, odolabels.ComponentDevMode, ownerReference)
if err != nil {
return err
}
Expand Down Expand Up @@ -210,7 +208,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
}

// Check if endpoints changed in Devfile
portsToForward, err := libdevfile.GetDevfileContainerEndpointMapping(a.devfile, parameters.Debug)
portsToForward, err := libdevfile.GetDevfileContainerEndpointMapping(parameters.Devfile, parameters.Debug)
if err != nil {
return err
}
Expand Down Expand Up @@ -280,15 +278,15 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c

// PostStart events from the devfile will only be executed when the component
// didn't previously exist
if !componentStatus.PostStartEventsDone && libdevfile.HasPostStartEvents(a.devfile) {
err = libdevfile.ExecPostStartEvents(ctx, a.devfile, component.NewExecHandler(a.kubeClient, a.execClient, appName, componentName, pod.Name, "Executing post-start command in container", parameters.Show))
if !componentStatus.PostStartEventsDone && libdevfile.HasPostStartEvents(parameters.Devfile) {
err = libdevfile.ExecPostStartEvents(ctx, parameters.Devfile, component.NewExecHandler(a.kubeClient, a.execClient, appName, componentName, pod.Name, "Executing post-start command in container", parameters.Show))
if err != nil {
return err
}
}
componentStatus.PostStartEventsDone = true

cmd, err := libdevfile.ValidateAndGetCommand(a.devfile, cmdName, cmdKind)
cmd, err := libdevfile.ValidateAndGetCommand(parameters.Devfile, cmdName, cmdKind)
if err != nil {
return err
}
Expand All @@ -305,7 +303,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
kubeClient: a.kubeClient,
appName: appName,
componentName: componentName,
devfile: a.devfile,
devfile: parameters.Devfile,
path: path,
podName: pod.GetName(),
ctx: ctx,
Expand Down Expand Up @@ -336,7 +334,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
doExecuteBuildCommand := func() error {
execHandler := component.NewExecHandler(a.kubeClient, a.execClient, appName, componentName, pod.Name,
"Building your application in container", parameters.Show)
return libdevfile.Build(ctx, a.devfile, parameters.DevfileBuildCmd, execHandler)
return libdevfile.Build(ctx, parameters.Devfile, parameters.DevfileBuildCmd, execHandler)
}
if running {
if cmd.Exec == nil || !util.SafeGetBool(cmd.Exec.HotReloadCapable) {
Expand All @@ -349,7 +347,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
return err
}
}
err = libdevfile.ExecuteCommandByNameAndKind(ctx, a.devfile, cmdName, cmdKind, &cmdHandler, false)
err = libdevfile.ExecuteCommandByNameAndKind(ctx, parameters.Devfile, cmdName, cmdKind, &cmdHandler, false)
if err != nil {
return err
}
Expand All @@ -369,7 +367,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
fmt.Fprintln(log.GetStdout())
}

err = a.portForwardClient.StartPortForwarding(ctx, a.devfile, componentName, parameters.Debug, parameters.RandomPorts, log.GetStdout(), parameters.ErrOut, parameters.CustomForwardedPorts)
err = a.portForwardClient.StartPortForwarding(ctx, parameters.Devfile, componentName, parameters.Debug, parameters.RandomPorts, log.GetStdout(), parameters.ErrOut, parameters.CustomForwardedPorts)
if err != nil {
return adapters.NewErrPortForward(err)
}
Expand All @@ -384,6 +382,7 @@ func (a Adapter) Push(ctx context.Context, parameters adapters.PushParameters, c
// Returns the new deployment and if the generation of the deployment has been updated
func (a *Adapter) createOrUpdateComponent(
ctx context.Context,
parameters adapters.PushParameters,
componentExists bool,
commands libdevfile.DevfileCommands,
deployment *appsv1.Deployment,
Expand All @@ -396,13 +395,13 @@ func (a *Adapter) createOrUpdateComponent(
path = filepath.Dir(devfilePath)
)

runtime := component.GetComponentRuntimeFromDevfileMetadata(a.devfile.Data.GetMetadata())
runtime := component.GetComponentRuntimeFromDevfileMetadata(parameters.Devfile.Data.GetMetadata())

// Set the labels
labels := odolabels.GetLabels(componentName, appName, runtime, odolabels.ComponentDevMode, true)

annotations := make(map[string]string)
odolabels.SetProjectType(annotations, component.GetComponentTypeFromDevfileMetadata(a.devfile.Data.GetMetadata()))
odolabels.SetProjectType(annotations, component.GetComponentTypeFromDevfileMetadata(parameters.Devfile.Data.GetMetadata()))
odolabels.AddCommonAnnotations(annotations)
klog.V(4).Infof("We are deploying these annotations: %s", annotations)

Expand All @@ -415,7 +414,7 @@ func (a *Adapter) createOrUpdateComponent(
if err != nil {
return nil, false, err
}
podTemplateSpec, err := generator.GetPodTemplateSpec(a.devfile, generator.PodTemplateParams{
podTemplateSpec, err := generator.GetPodTemplateSpec(parameters.Devfile, generator.PodTemplateParams{
ObjectMeta: deploymentObjectMeta,
PodSecurityAdmissionPolicy: policy,
})
Expand All @@ -429,13 +428,13 @@ func (a *Adapter) createOrUpdateComponent(

initContainers := podTemplateSpec.Spec.InitContainers

containers, err = utils.UpdateContainersEntrypointsIfNeeded(a.devfile, containers, commands.BuildCmd, commands.RunCmd, commands.DebugCmd)
containers, err = utils.UpdateContainersEntrypointsIfNeeded(parameters.Devfile, containers, commands.BuildCmd, commands.RunCmd, commands.DebugCmd)
if err != nil {
return nil, false, err
}

// Returns the volumes to add to the PodTemplate and adds volumeMounts to the containers and initContainers
volumes, err := a.buildVolumes(ctx, containers, initContainers)
volumes, err := a.buildVolumes(ctx, parameters, containers, initContainers)
if err != nil {
return nil, false, err
}
Expand All @@ -459,7 +458,7 @@ func (a *Adapter) createOrUpdateComponent(
originalGeneration = deployment.GetGeneration()
}

deployment, err = generator.GetDeployment(a.devfile, deployParams)
deployment, err = generator.GetDeployment(parameters.Devfile, deployParams)
if err != nil {
return nil, false, err
}
Expand All @@ -485,7 +484,7 @@ func (a *Adapter) createOrUpdateComponent(
ObjectMeta: serviceObjectMeta,
SelectorLabels: selectorLabels,
}
svc, err := generator.GetService(a.devfile, serviceParams, parsercommon.DevfileOptions{})
svc, err := generator.GetService(parameters.Devfile, serviceParams, parsercommon.DevfileOptions{})

if err != nil {
return nil, false, err
Expand Down Expand Up @@ -549,13 +548,13 @@ func (a *Adapter) createOrUpdateComponent(
// - (side effect on input parameters) adds volumeMounts to containers and initContainers for the PVCs and Ephemeral volumes
// - (side effect on input parameters) adds volumeMounts for automounted volumes
// => Returns the list of Volumes to add to the PodTemplate
func (a *Adapter) buildVolumes(ctx context.Context, containers, initContainers []corev1.Container) ([]corev1.Volume, error) {
func (a *Adapter) buildVolumes(ctx context.Context, parameters adapters.PushParameters, containers, initContainers []corev1.Container) ([]corev1.Volume, error) {
var (
appName = odocontext.GetApplication(ctx)
componentName = odocontext.GetComponentName(ctx)
)

runtime := component.GetComponentRuntimeFromDevfileMetadata(a.devfile.Data.GetMetadata())
runtime := component.GetComponentRuntimeFromDevfileMetadata(parameters.Devfile.Data.GetMetadata())

storageClient := storagepkg.NewClient(componentName, appName, storagepkg.ClientOptions{
Client: a.kubeClient,
Expand All @@ -570,7 +569,7 @@ func (a *Adapter) buildVolumes(ctx context.Context, containers, initContainers [

// Create PVCs for non-ephemeral Volumes defined in the Devfile
// and returns the Ephemeral volumes defined in the Devfile
ephemerals, err := storagepkg.Push(storageClient, a.devfile)
ephemerals, err := storagepkg.Push(storageClient, parameters.Devfile)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -600,13 +599,13 @@ func (a *Adapter) buildVolumes(ctx context.Context, containers, initContainers [
utils.AddOdoMandatoryVolume(containers)

// Get PVC volumes and Volume Mounts
pvcVolumes, err := storage.GetPersistentVolumesAndVolumeMounts(a.devfile, containers, initContainers, volumeNameToVolInfo, parsercommon.DevfileOptions{})
pvcVolumes, err := storage.GetPersistentVolumesAndVolumeMounts(parameters.Devfile, containers, initContainers, volumeNameToVolInfo, parsercommon.DevfileOptions{})
if err != nil {
return nil, err
}
allVolumes = append(allVolumes, pvcVolumes...)

ephemeralVolumes, err := storage.GetEphemeralVolumesAndVolumeMounts(a.devfile, containers, initContainers, ephemerals, parsercommon.DevfileOptions{})
ephemeralVolumes, err := storage.GetEphemeralVolumesAndVolumeMounts(parameters.Devfile, containers, initContainers, ephemerals, parsercommon.DevfileOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -682,7 +681,7 @@ func (a Adapter) generateDeploymentObjectMeta(ctx context.Context, deployment *a
// getRemoteResourcesNotPresentInDevfile compares the list of Devfile K8s component and remote K8s resources
// and returns a list of the remote resources not present in the Devfile and in case the SBO is not installed, a list of service binding secrets that must be deleted;
// it ignores the core components (such as deployments, svc, pods; all resources with `component:<something>` label)
func (a Adapter) getRemoteResourcesNotPresentInDevfile(ctx context.Context, selector string) (objectsToRemove, serviceBindingSecretsToRemove []unstructured.Unstructured, err error) {
func (a Adapter) getRemoteResourcesNotPresentInDevfile(ctx context.Context, parameters adapters.PushParameters, selector string) (objectsToRemove, serviceBindingSecretsToRemove []unstructured.Unstructured, err error) {
var (
devfilePath = odocontext.GetDevfilePath(ctx)
path = filepath.Dir(devfilePath)
Expand Down Expand Up @@ -710,7 +709,7 @@ func (a Adapter) getRemoteResourcesNotPresentInDevfile(ctx context.Context, sele
}

var devfileK8sResources []devfilev1.Component
devfileK8sResources, err = libdevfile.GetK8sAndOcComponentsToPush(a.devfile, true)
devfileK8sResources, err = libdevfile.GetK8sAndOcComponentsToPush(parameters.Devfile, true)
if err != nil {
return nil, nil, fmt.Errorf("unable to obtain resources from the Devfile: %w", err)
}
Expand All @@ -719,7 +718,7 @@ func (a Adapter) getRemoteResourcesNotPresentInDevfile(ctx context.Context, sele
var devfileK8sResourcesUnstructured []unstructured.Unstructured
for _, devfileK := range devfileK8sResources {
var devfileKUnstructuredList []unstructured.Unstructured
devfileKUnstructuredList, err = libdevfile.GetK8sComponentAsUnstructuredList(a.devfile, devfileK.Name, path, devfilefs.DefaultFs{})
devfileKUnstructuredList, err = libdevfile.GetK8sComponentAsUnstructuredList(parameters.Devfile, devfileK.Name, path, devfilefs.DefaultFs{})
if err != nil {
return nil, nil, fmt.Errorf("unable to read the resource: %w", err)
}
Expand Down
Loading

0 comments on commit aff78e7

Please sign in to comment.