forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call plugins with custom transports.
Small refactor to be able to use custom transports to call remote plugins. Signed-off-by: David Calavera <[email protected]>
- Loading branch information
Showing
5 changed files
with
122 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package transport | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// httpTransport holds an http.RoundTripper | ||
// and information about the scheme and address the transport | ||
// sends request to. | ||
type httpTransport struct { | ||
http.RoundTripper | ||
scheme string | ||
addr string | ||
} | ||
|
||
// NewHTTPTransport creates a new httpTransport. | ||
func NewHTTPTransport(r http.RoundTripper, scheme, addr string) Transport { | ||
return httpTransport{ | ||
RoundTripper: r, | ||
scheme: scheme, | ||
addr: addr, | ||
} | ||
} | ||
|
||
// NewRequest creates a new http.Request and sets the URL | ||
// scheme and address with the transport's fields. | ||
func (t httpTransport) NewRequest(path string, data io.Reader) (*http.Request, error) { | ||
req, err := newHTTPRequest(path, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.URL.Scheme = t.scheme | ||
req.URL.Host = t.addr | ||
return req, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package transport | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// VersionMimetype is the Content-Type the engine sends to plugins. | ||
const VersionMimetype = "application/vnd.docker.plugins.v1.2+json" | ||
|
||
// RequestFactory defines an interface that | ||
// transports can implement to create new requests. | ||
type RequestFactory interface { | ||
NewRequest(path string, data io.Reader) (*http.Request, error) | ||
} | ||
|
||
// Transport defines an interface that plugin transports | ||
// must implement. | ||
type Transport interface { | ||
http.RoundTripper | ||
RequestFactory | ||
} | ||
|
||
// newHTTPRequest creates a new request with a path and a body. | ||
func newHTTPRequest(path string, data io.Reader) (*http.Request, error) { | ||
if !strings.HasPrefix(path, "/") { | ||
path = "/" + path | ||
} | ||
req, err := http.NewRequest("POST", path, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("Accept", VersionMimetype) | ||
return req, nil | ||
} |