This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathinnerloop.go
More file actions
278 lines (246 loc) · 9.39 KB
/
innerloop.go
File metadata and controls
278 lines (246 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package kubedev
import (
"context"
"fmt"
"path/filepath"
"time"
devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
corev1 "k8s.io/api/core/v1"
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/dev/common"
"github.com/redhat-developer/odo/pkg/devfile/image"
"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/port"
"github.com/redhat-developer/odo/pkg/sync"
"github.com/redhat-developer/odo/pkg/watch"
"k8s.io/klog/v2"
)
func (o *DevClient) innerloop(ctx context.Context, parameters common.PushParameters, componentStatus *watch.ComponentStatus) error {
var (
componentName = odocontext.GetComponentName(ctx)
devfilePath = odocontext.GetDevfilePath(ctx)
path = filepath.Dir(devfilePath)
)
// Now the Deployment has a Ready replica, we can get the Pod to work inside it
pod, err := o.kubernetesClient.GetPodUsingComponentName(componentName)
if err != nil {
return fmt.Errorf("unable to get pod for component %s: %w", componentName, err)
}
podChanged := componentStatus.GetState() == watch.StateWaitDeployment
execRequired, err := o.syncFiles(ctx, parameters, pod, podChanged)
if err != nil {
componentStatus.SetState(watch.StateReady)
return fmt.Errorf("failed to sync to component with name %s: %w", componentName, err)
}
if !componentStatus.PostStartEventsDone && libdevfile.HasPostStartEvents(parameters.Devfile) {
// PostStart events from the devfile will only be executed when the component
// didn't previously exist
handler := component.NewRunHandler(
ctx,
o.kubernetesClient,
o.execClient,
o.configAutomountClient,
// TODO(feloy) set these values when we want to support Apply Image/Kubernetes/OpenShift commands for PostStart commands
nil, nil,
component.HandlerOptions{
PodName: pod.Name,
ContainersRunning: component.GetContainersNames(pod),
Msg: "Executing post-start command in container",
},
)
err = libdevfile.ExecPostStartEvents(ctx, parameters.Devfile, handler)
if err != nil {
return err
}
}
componentStatus.PostStartEventsDone = true
var hasRunOrDebugCmd bool
innerLoopWithCommands := !parameters.StartOptions.SkipCommands
if innerLoopWithCommands {
var (
cmdKind = devfilev1.RunCommandGroupKind
cmdName = parameters.StartOptions.RunCommand
)
if parameters.StartOptions.Debug {
cmdKind = devfilev1.DebugCommandGroupKind
cmdName = parameters.StartOptions.DebugCommand
}
var cmd devfilev1.Command
cmd, hasRunOrDebugCmd, err = libdevfile.GetCommand(parameters.Devfile, cmdName, cmdKind)
if err != nil {
return err
}
var running bool
var isComposite bool
var runHandler libdevfile.Handler
if hasRunOrDebugCmd {
var commandType devfilev1.CommandType
commandType, err = parsercommon.GetCommandType(cmd)
if err != nil {
return err
}
cmdHandler := component.NewRunHandler(
ctx,
o.kubernetesClient,
o.execClient,
o.configAutomountClient,
o.filesystem,
image.SelectBackend(ctx),
component.HandlerOptions{
PodName: pod.GetName(),
ContainersRunning: component.GetContainersNames(pod),
Devfile: parameters.Devfile,
Path: path,
},
)
if commandType == devfilev1.ExecCommandType {
running, err = cmdHandler.IsRemoteProcessForCommandRunning(ctx, cmd, pod.Name)
if err != nil {
return err
}
} else if commandType == devfilev1.CompositeCommandType {
// this handler will run each command in this composite command individually,
// and will determine whether each command is running or not.
isComposite = true
} else {
return fmt.Errorf("unsupported type %q for Devfile command %s, only exec and composite are handled",
commandType, cmd.Id)
}
cmdHandler.ComponentExists = running || isComposite
runHandler = cmdHandler
}
klog.V(4).Infof("running=%v, execRequired=%v",
running, execRequired)
if isComposite || !running || execRequired {
// Invoke the build command once (before calling libdevfile.ExecuteCommandByNameAndKind), as, if cmd is a composite command,
// the handler we pass will be called for each command in that composite command.
doExecuteBuildCommand := func() error {
execHandler := component.NewRunHandler(
ctx,
o.kubernetesClient,
o.execClient,
o.configAutomountClient,
// TODO(feloy) set these values when we want to support Apply Image/Kubernetes/OpenShift commands for PostStart commands
nil, nil, component.HandlerOptions{
PodName: pod.Name,
ComponentExists: running,
ContainersRunning: component.GetContainersNames(pod),
Msg: "Building your application in container",
},
)
return libdevfile.Build(ctx, parameters.Devfile, parameters.StartOptions.BuildCommand, execHandler)
}
if err = doExecuteBuildCommand(); err != nil {
componentStatus.SetState(watch.StateReady)
return err
}
if hasRunOrDebugCmd {
err = libdevfile.ExecuteCommandByNameAndKind(ctx, parameters.Devfile, cmdName, cmdKind, runHandler, false)
if err != nil {
return err
}
componentStatus.RunExecuted = true
} else {
msg := fmt.Sprintf("Missing default %v command", cmdKind)
if cmdName != "" {
msg = fmt.Sprintf("Missing %v command with name %q", cmdKind, cmdName)
}
log.Warning(msg)
}
}
}
if podChanged || o.portsChanged {
o.portForwardClient.StopPortForwarding(ctx, componentName)
}
if innerLoopWithCommands && hasRunOrDebugCmd && len(o.portsToForward) != 0 {
// Check that the application is actually listening on the ports declared in the Devfile, so we are sure that port-forwarding will work
appReadySpinner := log.Spinner("Waiting for the application to be ready")
err = o.checkAppPorts(ctx, pod.Name, o.portsToForward)
appReadySpinner.End(err == nil)
if err != nil {
log.Warningf("Port forwarding might not work correctly: %v", err)
log.Warning("Running `odo logs --follow` might help in identifying the problem.")
fmt.Fprintln(log.GetStdout())
}
}
err = o.portForwardClient.StartPortForwarding(ctx, parameters.Devfile, componentName, parameters.StartOptions.Debug, parameters.StartOptions.RandomPorts, log.GetStdout(), parameters.StartOptions.ErrOut, parameters.StartOptions.CustomForwardedPorts, parameters.StartOptions.CustomAddress)
if err != nil {
return common.NewErrPortForward(err)
}
componentStatus.EndpointsForwarded = o.portForwardClient.GetForwardedPorts()
componentStatus.SetState(watch.StateReady)
return nil
}
func (o *DevClient) syncFiles(ctx context.Context, parameters common.PushParameters, pod *corev1.Pod, podChanged bool) (bool, error) {
var (
devfileObj = odocontext.GetEffectiveDevfileObj(ctx)
componentName = odocontext.GetComponentName(ctx)
devfilePath = odocontext.GetDevfilePath(ctx)
path = filepath.Dir(devfilePath)
)
s := log.Spinner("Syncing files into the container")
defer s.End(false)
// Find at least one pod with the source volume mounted, error out if none can be found
containerName, syncFolder, err := common.GetFirstContainerWithSourceVolume(pod.Spec.Containers)
if err != nil {
return false, fmt.Errorf("error while retrieving container from pod %s with a mounted project volume: %w", pod.GetName(), err)
}
syncFilesMap := make(map[string]string)
var devfileCmd devfilev1.Command
innerLoopWithCommands := !parameters.StartOptions.SkipCommands
if innerLoopWithCommands {
var (
cmdKind = devfilev1.RunCommandGroupKind
cmdName = parameters.StartOptions.RunCommand
)
if parameters.StartOptions.Debug {
cmdKind = devfilev1.DebugCommandGroupKind
cmdName = parameters.StartOptions.DebugCommand
}
var hasCmd bool
devfileCmd, hasCmd, err = libdevfile.GetCommand(*devfileObj, cmdName, cmdKind)
if err != nil {
return false, err
}
if hasCmd {
syncFilesMap = common.GetSyncFilesFromAttributes(devfileCmd)
} else {
klog.V(2).Infof("no command found with name %q and kind %v, syncing files without command attributes", cmdName, cmdKind)
}
}
// Get a sync adapter. Check if project files have changed and sync accordingly
compInfo := sync.ComponentInfo{
ComponentName: componentName,
ContainerName: containerName,
PodName: pod.GetName(),
SyncFolder: syncFolder,
}
syncParams := sync.SyncParameters{
Path: path,
WatchFiles: parameters.WatchFiles,
WatchDeletedFiles: parameters.WatchDeletedFiles,
IgnoredFiles: parameters.StartOptions.IgnorePaths,
DevfileScanIndexForWatch: parameters.DevfileScanIndexForWatch,
CompInfo: compInfo,
ForcePush: !o.deploymentExists || podChanged,
Files: syncFilesMap,
}
execRequired, err := o.syncClient.SyncFiles(ctx, syncParams)
if err != nil {
return false, err
}
s.End(true)
return execRequired, nil
}
func (o *DevClient) checkAppPorts(ctx context.Context, podName string, portsToFwd map[string][]devfilev1.Endpoint) error {
containerPortsMapping := make(map[string][]int)
for c, ports := range portsToFwd {
for _, p := range ports {
containerPortsMapping[c] = append(containerPortsMapping[c], p.TargetPort)
}
}
return port.CheckAppPortsListening(ctx, o.execClient, podName, containerPortsMapping, 1*time.Minute)
}