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 pathhandler.go
More file actions
169 lines (146 loc) · 5.7 KB
/
handler.go
File metadata and controls
169 lines (146 loc) · 5.7 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
package component
import (
"context"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser"
"k8s.io/klog/v2"
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/configAutomount"
"github.com/redhat-developer/odo/pkg/devfile/image"
"github.com/redhat-developer/odo/pkg/exec"
"github.com/redhat-developer/odo/pkg/kclient"
odolabels "github.com/redhat-developer/odo/pkg/labels"
"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/platform"
"github.com/redhat-developer/odo/pkg/remotecmd"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)
type runHandler struct {
ctx context.Context
platformClient platform.Client
execClient exec.Client
configAutomountClient configAutomount.Client
podName string
ComponentExists bool
containersRunning []string
msg string
directRun bool
fs filesystem.Filesystem
imageBackend image.Backend
devfile parser.DevfileObj
path string
}
var _ libdevfile.Handler = (*runHandler)(nil)
type HandlerOptions struct {
PodName string
ComponentExists bool
ContainersRunning []string
Msg string
DirectRun bool
// For apply Kubernetes / Openshift
Devfile parser.DevfileObj
Path string
}
func NewRunHandler(
ctx context.Context,
platformClient platform.Client,
execClient exec.Client,
configAutomountClient configAutomount.Client,
// For building images
fs filesystem.Filesystem,
imageBackend image.Backend,
options HandlerOptions,
) *runHandler {
return &runHandler{
ctx: ctx,
platformClient: platformClient,
execClient: execClient,
configAutomountClient: configAutomountClient,
podName: options.PodName,
ComponentExists: options.ComponentExists,
containersRunning: options.ContainersRunning,
msg: options.Msg,
directRun: options.DirectRun,
fs: fs,
imageBackend: imageBackend,
devfile: options.Devfile,
path: options.Path,
}
}
func (a *runHandler) ApplyImage(img devfilev1.Component) error {
return image.BuildPushSpecificImage(a.ctx, a.imageBackend, a.fs, img, envcontext.GetEnvConfig(a.ctx).PushImages)
}
func (a *runHandler) ApplyKubernetes(kubernetes devfilev1.Component, kind v1alpha2.CommandGroupKind) error {
var (
componentName = odocontext.GetComponentName(a.ctx)
appName = odocontext.GetApplication(a.ctx)
)
mode := odolabels.ComponentDevMode
if kind == v1alpha2.DeployCommandGroupKind {
mode = odolabels.ComponentDeployMode
}
switch platform := a.platformClient.(type) {
case kclient.ClientInterface:
return ApplyKubernetes(mode, appName, componentName, a.devfile, kubernetes, platform, a.path)
default:
klog.V(4).Info("apply kubernetes/Openshift commands are not implemented on podman")
log.Warningf("Apply Kubernetes/Openshift components are not supported on Podman. Skipping: %v.", kubernetes.Name)
return nil
}
}
func (a *runHandler) ApplyOpenShift(openshift devfilev1.Component, kind v1alpha2.CommandGroupKind) error {
return a.ApplyKubernetes(openshift, kind)
}
func (a *runHandler) ExecuteNonTerminatingCommand(ctx context.Context, command devfilev1.Command) error {
var (
componentName = odocontext.GetComponentName(a.ctx)
appName = odocontext.GetApplication(a.ctx)
)
if isContainerRunning(command.Exec.Component, a.containersRunning) {
return ExecuteRunCommand(ctx, a.execClient, a.platformClient, command, a.ComponentExists, a.podName, appName, componentName)
}
switch platform := a.platformClient.(type) {
case kclient.ClientInterface:
return ExecuteInNewContainer(ctx, platform, a.configAutomountClient, a.devfile, componentName, appName, command)
default:
klog.V(4).Info("executing a command in a new container is not implemented on podman")
log.Warningf("executing a command in a new container is not implemented on podman. Skipping: %v.", command.Id)
return nil
}
}
func (a *runHandler) ExecuteTerminatingCommand(ctx context.Context, command devfilev1.Command) error {
var (
componentName = odocontext.GetComponentName(a.ctx)
appName = odocontext.GetApplication(a.ctx)
)
if isContainerRunning(command.Exec.Component, a.containersRunning) {
return ExecuteTerminatingCommand(ctx, a.execClient, a.platformClient, command, a.ComponentExists, a.podName, appName, componentName, a.msg, a.directRun)
}
switch platform := a.platformClient.(type) {
case kclient.ClientInterface:
return ExecuteInNewContainer(ctx, platform, a.configAutomountClient, a.devfile, componentName, appName, command)
default:
klog.V(4).Info("executing a command in a new container is not implemented on podman")
log.Warningf("executing a command in a new container is not implemented on podman. Skipping: %v.", command.Id)
return nil
}
}
// IsRemoteProcessForCommandRunning returns true if the command is running
func (a *runHandler) IsRemoteProcessForCommandRunning(ctx context.Context, command devfilev1.Command, podName string) (bool, error) {
remoteProcess, err := remotecmd.NewKubeExecProcessHandler(a.execClient).GetProcessInfoForCommand(ctx, remotecmd.CommandDefinition{Id: command.Id}, podName, command.Exec.Component)
if err != nil {
return false, err
}
return remoteProcess.Status == remotecmd.Running, nil
}
func isContainerRunning(container string, containers []string) bool {
for _, cnt := range containers {
if container == cnt {
return true
}
}
return false
}