...
1 package kubedev
2
3 import (
4 "context"
5 "fmt"
6
7 devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
8
9 "github.com/redhat-developer/odo/pkg/binding"
10 _delete "github.com/redhat-developer/odo/pkg/component/delete"
11 "github.com/redhat-developer/odo/pkg/configAutomount"
12 "github.com/redhat-developer/odo/pkg/dev"
13 "github.com/redhat-developer/odo/pkg/dev/common"
14 "github.com/redhat-developer/odo/pkg/devfile"
15 "github.com/redhat-developer/odo/pkg/devfile/location"
16 "github.com/redhat-developer/odo/pkg/exec"
17 "github.com/redhat-developer/odo/pkg/kclient"
18 "github.com/redhat-developer/odo/pkg/portForward"
19 "github.com/redhat-developer/odo/pkg/preference"
20 "github.com/redhat-developer/odo/pkg/sync"
21 "github.com/redhat-developer/odo/pkg/testingutil/filesystem"
22 "github.com/redhat-developer/odo/pkg/watch"
23
24 "k8s.io/klog"
25 )
26
27 type DevClient struct {
28 kubernetesClient kclient.ClientInterface
29 prefClient preference.Client
30 portForwardClient portForward.Client
31 watchClient watch.Client
32 bindingClient binding.Client
33 syncClient sync.Client
34 filesystem filesystem.Filesystem
35 execClient exec.Client
36 deleteClient _delete.Client
37 configAutomountClient configAutomount.Client
38
39
40 deploymentExists bool
41
42 portsChanged bool
43
44 portsToForward map[string][]devfilev1.Endpoint
45 }
46
47 var _ dev.Client = (*DevClient)(nil)
48
49 func NewDevClient(
50 kubernetesClient kclient.ClientInterface,
51 prefClient preference.Client,
52 portForwardClient portForward.Client,
53 watchClient watch.Client,
54 bindingClient binding.Client,
55 syncClient sync.Client,
56 filesystem filesystem.Filesystem,
57 execClient exec.Client,
58 deleteClient _delete.Client,
59 configAutomountClient configAutomount.Client,
60 ) *DevClient {
61 return &DevClient{
62 kubernetesClient: kubernetesClient,
63 prefClient: prefClient,
64 portForwardClient: portForwardClient,
65 watchClient: watchClient,
66 bindingClient: bindingClient,
67 syncClient: syncClient,
68 filesystem: filesystem,
69 execClient: execClient,
70 deleteClient: deleteClient,
71 configAutomountClient: configAutomountClient,
72 }
73 }
74
75 func (o *DevClient) Start(
76 ctx context.Context,
77 options dev.StartOptions,
78 ) error {
79 klog.V(4).Infoln("Creating new adapter")
80
81 var (
82 componentStatus = watch.ComponentStatus{
83 ImageComponentsAutoApplied: make(map[string]devfilev1.ImageComponent),
84 }
85 )
86
87 klog.V(4).Infoln("Creating inner-loop resources for the component")
88
89 watchParameters := watch.WatchParameters{
90 StartOptions: options,
91 DevfileWatchHandler: o.regenerateAdapterAndPush,
92 WatchCluster: true,
93 }
94
95 return o.watchClient.WatchAndPush(ctx, watchParameters, componentStatus)
96 }
97
98
99 func (o *DevClient) regenerateAdapterAndPush(ctx context.Context, pushParams common.PushParameters, componentStatus *watch.ComponentStatus) error {
100
101 devObj, err := devfile.ParseAndValidateFromFileWithVariables(location.DevfileLocation(o.filesystem, ""), pushParams.StartOptions.Variables, o.prefClient.GetImageRegistry(), true)
102 if err != nil {
103 return fmt.Errorf("unable to read devfile: %w", err)
104 }
105
106 pushParams.Devfile = devObj
107
108 err = o.reconcile(ctx, pushParams, componentStatus)
109 if err != nil {
110 return fmt.Errorf("watch command was unable to push component: %w", err)
111 }
112 return nil
113 }
114
View as plain text