summaryrefslogtreecommitdiff
path: root/libnetwork
diff options
context:
space:
mode:
authorSebastiaan van Stijn <github@gone.nl>2023-04-28 12:38:08 +0200
committerSebastiaan van Stijn <github@gone.nl>2023-04-28 16:44:54 +0200
commit1e9ebfb00c43775bb6c91dfa0cfa822e7edc5856 (patch)
treee3ac21948ba4865eb50102044f58665260dc7b76 /libnetwork
parent9d8fcb3296a7e1c1e3145f6bb45f71a3a7edc196 (diff)
downloaddocker-1e9ebfb00c43775bb6c91dfa0cfa822e7edc5856.tar.gz
libnetwork: inline sendKey() into SetExternalKey()
This function included a defer to close the net.Conn if an error occurred, but the calling function (SetExternalKey()) also had a defer to close it unconditionally. Rewrite it to use json.NewEncoder(), which accepts a writer, and inline the code. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Diffstat (limited to 'libnetwork')
-rw-r--r--libnetwork/sandbox_externalkey_unix.go28
1 files changed, 5 insertions, 23 deletions
diff --git a/libnetwork/sandbox_externalkey_unix.go b/libnetwork/sandbox_externalkey_unix.go
index 86ade93b76..bb64e23889 100644
--- a/libnetwork/sandbox_externalkey_unix.go
+++ b/libnetwork/sandbox_externalkey_unix.go
@@ -70,11 +70,6 @@ func setKey() error {
// SetExternalKey provides a convenient way to set an External key to a sandbox
func SetExternalKey(shortCtlrID string, containerID string, key string, execRoot string) error {
- keyData := setKeyData{
- ContainerID: containerID,
- Key: key,
- }
-
uds := filepath.Join(execRoot, execSubdir, shortCtlrID+".sock")
c, err := net.Dial("unix", uds)
if err != nil {
@@ -82,29 +77,16 @@ func SetExternalKey(shortCtlrID string, containerID string, key string, execRoot
}
defer c.Close()
- if err = sendKey(c, keyData); err != nil {
+ err = json.NewEncoder(c).Encode(setKeyData{
+ ContainerID: containerID,
+ Key: key,
+ })
+ if err != nil {
return fmt.Errorf("sendKey failed with : %v", err)
}
return processReturn(c)
}
-func sendKey(c net.Conn, data setKeyData) error {
- var err error
- defer func() {
- if err != nil {
- c.Close()
- }
- }()
-
- var b []byte
- if b, err = json.Marshal(data); err != nil {
- return err
- }
-
- _, err = c.Write(b)
- return err
-}
-
func processReturn(r io.Reader) error {
buf := make([]byte, 1024)
n, err := r.Read(buf[:])