This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathproxy.go
165 lines (143 loc) · 4.04 KB
/
proxy.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Copyright (c) 2017 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"
"path/filepath"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
)
// ProxyConfig is a structure storing information needed from any
// proxy in order to be properly initialized.
type ProxyConfig struct {
Path string
Debug bool
}
// proxyParams is the structure providing specific parameters needed
// for the execution of the proxy binary.
type proxyParams struct {
agentURL string
}
// ProxyType describes a proxy type.
type ProxyType string
const (
// NoopProxyType is the noopProxy.
NoopProxyType ProxyType = "noopProxy"
// NoProxyType is the noProxy.
NoProxyType ProxyType = "noProxy"
// CCProxyType is the ccProxy.
CCProxyType ProxyType = "ccProxy"
// KataProxyType is the kataProxy.
KataProxyType ProxyType = "kataProxy"
)
const (
// Number of seconds to wait for the proxy to respond to a connection
// request.
waitForProxyTimeoutSecs = 5.0
)
func proxyLogger() *logrus.Entry {
return virtLog.WithField("subsystem", "proxy")
}
// Set sets a proxy type based on the input string.
func (pType *ProxyType) Set(value string) error {
switch value {
case "noopProxy":
*pType = NoopProxyType
return nil
case "noProxy":
*pType = NoProxyType
return nil
case "ccProxy":
*pType = CCProxyType
return nil
case "kataProxy":
*pType = KataProxyType
return nil
default:
return fmt.Errorf("Unknown proxy type %s", value)
}
}
// String converts a proxy type to a string.
func (pType *ProxyType) String() string {
switch *pType {
case NoopProxyType:
return string(NoopProxyType)
case NoProxyType:
return string(NoProxyType)
case CCProxyType:
return string(CCProxyType)
case KataProxyType:
return string(KataProxyType)
default:
return ""
}
}
// newProxy returns a proxy from a proxy type.
func newProxy(pType ProxyType) (proxy, error) {
switch pType {
case NoopProxyType:
return &noopProxy{}, nil
case NoProxyType:
return &noProxy{}, nil
case CCProxyType:
return &ccProxy{}, nil
case KataProxyType:
return &kataProxy{}, nil
default:
return &noopProxy{}, nil
}
}
// newProxyConfig returns a proxy config from a generic PodConfig handler,
// after it properly checked the configuration was valid.
func newProxyConfig(podConfig *PodConfig) (ProxyConfig, error) {
if podConfig == nil {
return ProxyConfig{}, fmt.Errorf("Pod config cannot be nil")
}
var config ProxyConfig
switch podConfig.ProxyType {
case KataProxyType:
fallthrough
case CCProxyType:
if err := mapstructure.Decode(podConfig.ProxyConfig, &config); err != nil {
return ProxyConfig{}, err
}
}
if config.Path == "" {
return ProxyConfig{}, fmt.Errorf("Proxy path cannot be empty")
}
return config, nil
}
func defaultProxyURL(pod Pod, socketType string) (string, error) {
switch socketType {
case SocketTypeUNIX:
socketPath := filepath.Join(runStoragePath, pod.id, "proxy.sock")
return fmt.Sprintf("unix://%s", socketPath), nil
case SocketTypeVSOCK:
// TODO Build the VSOCK default URL
return "", nil
default:
return "", fmt.Errorf("Unknown socket type: %s", socketType)
}
}
// proxy is the virtcontainers proxy interface.
type proxy interface {
// start launches a proxy instance for the specified pod, returning
// the PID of the process and the URL used to connect to it.
start(pod Pod, params proxyParams) (int, string, error)
// stop terminates a proxy instance after all communications with the
// agent inside the VM have been properly stopped.
stop(pod Pod, pid int) error
}