github.com/google/cadvisor@v0.49.1/container/podman/client.go (about)

     1  // Copyright 2021 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package podman
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net"
    21  	"net/http"
    22  	urllib "net/url"
    23  )
    24  
    25  type clientKey struct{}
    26  
    27  func (c clientKey) String() string {
    28  	return "client"
    29  }
    30  
    31  type Connection struct {
    32  	URI    *urllib.URL
    33  	Client *http.Client
    34  }
    35  
    36  func client(ctx *context.Context) (*Connection, error) {
    37  	url, err := urllib.Parse(*endpointFlag)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	switch url.Scheme {
    43  	case "unix":
    44  		connection := Connection{URI: url}
    45  		connection.Client = &http.Client{
    46  			Transport: &http.Transport{
    47  				DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
    48  					return (&net.Dialer{}).DialContext(ctx, "unix", url.Path)
    49  				},
    50  				DisableCompression: true,
    51  			},
    52  		}
    53  		*ctx = context.WithValue(*ctx, clientKey{}, &connection)
    54  		return &connection, nil
    55  	}
    56  
    57  	return nil, fmt.Errorf("couldn't get podman client")
    58  }