-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(desktop): add Docker Desktop detection and client skeleton (#11593)
- Loading branch information
Showing
11 changed files
with
318 additions
and
36 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,93 @@ | ||
/* | ||
Copyright 2024 Docker Compose CLI authors | ||
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 desktop | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/docker/compose/v2/internal/memnet" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
) | ||
|
||
// Client for integration with Docker Desktop features. | ||
type Client struct { | ||
client *http.Client | ||
} | ||
|
||
// NewClient creates a Desktop integration client for the provided in-memory | ||
// socket address (AF_UNIX or named pipe). | ||
func NewClient(apiEndpoint string) *Client { | ||
var transport http.RoundTripper = &http.Transport{ | ||
DisableCompression: true, | ||
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
return memnet.DialEndpoint(ctx, apiEndpoint) | ||
}, | ||
} | ||
transport = otelhttp.NewTransport(transport) | ||
|
||
c := &Client{ | ||
client: &http.Client{Transport: transport}, | ||
} | ||
return c | ||
} | ||
|
||
// Close releases any open connections. | ||
func (c *Client) Close() error { | ||
c.client.CloseIdleConnections() | ||
return nil | ||
} | ||
|
||
type PingResponse struct { | ||
ServerTime int64 `json:"serverTime"` | ||
} | ||
|
||
// Ping is a minimal API used to ensure that the server is available. | ||
func (c *Client) Ping(ctx context.Context) (*PingResponse, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, backendURL("/ping"), http.NoBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
var ret PingResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil { | ||
return nil, err | ||
} | ||
return &ret, nil | ||
} | ||
|
||
// backendURL generates a URL for the given API path. | ||
// | ||
// NOTE: Custom transport handles communication. The host is to create a valid | ||
// URL for the Go http.Client that is also descriptive in error/logs. | ||
func backendURL(path string) string { | ||
return "http://docker-desktop/" + strings.TrimPrefix(path, "/") | ||
} |
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,25 @@ | ||
/* | ||
Copyright 2024 Docker Compose CLI authors | ||
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 desktop | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type IntegrationService interface { | ||
MaybeEnableDesktopIntegration(ctx context.Context) error | ||
} |
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,50 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
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 memnet | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
func DialEndpoint(ctx context.Context, endpoint string) (net.Conn, error) { | ||
if addr, ok := strings.CutPrefix(endpoint, "unix://"); ok { | ||
return Dial(ctx, "unix", addr) | ||
} | ||
if addr, ok := strings.CutPrefix(endpoint, "npipe://"); ok { | ||
return Dial(ctx, "npipe", addr) | ||
} | ||
return nil, fmt.Errorf("unsupported protocol for address: %s", endpoint) | ||
} | ||
|
||
func Dial(ctx context.Context, network, addr string) (net.Conn, error) { | ||
var d net.Dialer | ||
switch network { | ||
case "unix": | ||
if err := validateSocketPath(addr); err != nil { | ||
return nil, err | ||
} | ||
return d.DialContext(ctx, "unix", addr) | ||
case "npipe": | ||
// N.B. this will return an error on non-Windows | ||
return dialNamedPipe(ctx, addr) | ||
default: | ||
return nil, fmt.Errorf("unsupported network: %s", network) | ||
} | ||
} |
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
Oops, something went wrong.