forked from containers/virtcontainers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.go
146 lines (125 loc) · 3.91 KB
/
agent.go
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
//
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package virtcontainers
import (
"fmt"
"syscall"
"github.com/mitchellh/mapstructure"
)
// AgentType describes the type of guest agent a Pod should run.
type AgentType string
const (
// NoopAgentType is the No-Op agent.
NoopAgentType AgentType = "noop"
// SSHdAgent is the SSH daemon agent.
SSHdAgent AgentType = "sshd"
// HyperstartAgent is the Hyper hyperstart agent.
HyperstartAgent AgentType = "hyperstart"
)
// Set sets an agent type based on the input string.
func (agentType *AgentType) Set(value string) error {
switch value {
case "noop":
*agentType = NoopAgentType
return nil
case "sshd":
*agentType = SSHdAgent
return nil
case "hyperstart":
*agentType = HyperstartAgent
return nil
default:
return fmt.Errorf("Unknown agent type %s", value)
}
}
// String converts an agent type to a string.
func (agentType *AgentType) String() string {
switch *agentType {
case NoopAgentType:
return string(NoopAgentType)
case SSHdAgent:
return string(SSHdAgent)
case HyperstartAgent:
return string(HyperstartAgent)
default:
return ""
}
}
// newAgent returns an agent from an agent type.
func newAgent(agentType AgentType) agent {
switch agentType {
case NoopAgentType:
return &noopAgent{}
case SSHdAgent:
return &sshd{}
case HyperstartAgent:
return &hyper{}
default:
return &noopAgent{}
}
}
// newAgentConfig returns an agent config from a generic PodConfig interface.
func newAgentConfig(config PodConfig) interface{} {
switch config.AgentType {
case NoopAgentType:
return nil
case SSHdAgent:
var sshdConfig SshdConfig
err := mapstructure.Decode(config.AgentConfig, &sshdConfig)
if err != nil {
return err
}
return sshdConfig
case HyperstartAgent:
var hyperConfig HyperConfig
err := mapstructure.Decode(config.AgentConfig, &hyperConfig)
if err != nil {
return err
}
return hyperConfig
default:
return nil
}
}
// agent is the virtcontainers agent interface.
// Agents are running in the guest VM and handling
// communications between the host and guest.
type agent interface {
// init is used to pass agent specific configuration to the agent implementation.
// agent implementations also will typically start listening for agent events from
// init().
// After init() is called, agent implementations should be initialized and ready
// to handle all other Agent interface methods.
init(pod *Pod, config interface{}) error
// start will start the agent.
start(pod *Pod) error
// stop will stop the agent.
stop(pod Pod) error
// exec will tell the agent to run a command in an already running container.
exec(pod *Pod, c Container, cmd Cmd) (*Process, error)
// startPod will tell the agent to start all containers related to the Pod.
startPod(pod Pod) error
// stopPod will tell the agent to stop all containers related to the Pod.
stopPod(pod Pod) error
// createContainer will tell the agent to create a container related to a Pod.
createContainer(pod *Pod, c *Container) error
// startContainer will tell the agent to start a container related to a Pod.
startContainer(pod Pod, c Container) error
// stopContainer will tell the agent to stop a container related to a Pod.
stopContainer(pod Pod, c Container) error
// killContainer will tell the agent to send a signal to a container related to a Pod.
killContainer(pod Pod, c Container, signal syscall.Signal) error
}