Skip to content

Commit

Permalink
Make k8s imports more visible
Browse files Browse the repository at this point in the history
  • Loading branch information
MarSik committed Jun 29, 2023
1 parent 88132b3 commit 7c94708
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 85 deletions.
2 changes: 1 addition & 1 deletion cmd/cpulist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

flag "github.com/spf13/pflag"

cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
"github.com/openshift-kni/debug-tools/pkg/procs"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/numalign/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/openshift-kni/debug-tools/internal/pkg/vfs"

cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
)

func splitCPUList(cpuList string) ([]int, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/irqs/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"strings"

"github.com/openshift-kni/debug-tools/pkg/fswrap"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/irqs/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"bytes"
"encoding/json"
"fmt"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
"io/ioutil"
"log"
"os"
Expand Down
2 changes: 1 addition & 1 deletion pkg/irqs/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"io"
"time"

cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
)

type Reporter interface {
Expand Down
7 changes: 7 additions & 0 deletions pkg/k8s_cpuset/cpuset.go → pkg/k8s_imported/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Copyright 2017 The Kubernetes Authors.
This was copied out of kubernetes source to avoid pulling too
many dependencies with the full kubernetes source code.
This is based on the following kubernetes file:
https://github.com/kubernetes/kubernetes/blob/759e043136b249f9e19355d3b2c9261b3ac82a70/pkg/kubelet/cm/cpuset/cpuset.go
And will be replaced by https://pkg.go.dev/k8s.io/utils/cpuset once
it is released as a stable version.
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
Expand Down
104 changes: 104 additions & 0 deletions pkg/k8s_imported/podresources_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright 2017 The Kubernetes Authors.
This was copied out of kubernetes source to avoid pulling too
many dependencies with the full kubernetes source code.
This is based on the following kubernetes file:
https://github.com/kubernetes/kubernetes/blob/52457842d155743f0e3fc57ade87251cca37d375/pkg/kubelet/apis/podresources/client.go#L56
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 cpuset

import (
"context"
"fmt"
"google.golang.org/grpc"
"k8s.io/klog/v2"
"k8s.io/kubelet/pkg/apis/podresources/v1"
"net"
"net/url"
"time"
)

// GetV1Client returns a client for the PodResourcesLister grpc service
// This was copied from kubelet sources as per the comment there:
//
// Quote:
// Note: Consumers of the pod resources API should not be importing this package.
// They should copy paste the function in their project.
func GetV1Client(socket string, connectionTimeout time.Duration, maxMsgSize int) (v1.PodResourcesListerClient, *grpc.ClientConn, error) {
addr, dialer, err := GetAddressAndDialer(socket)
if err != nil {
return nil, nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout)
defer cancel()

conn, err := grpc.DialContext(ctx, addr, grpc.WithInsecure(), grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)))
if err != nil {
return nil, nil, fmt.Errorf("error dialing socket %s: %v", socket, err)
}
return v1.NewPodResourcesListerClient(conn), conn, nil
}

// GetAddressAndDialer returns the address parsed from the given endpoint and a context dialer.
func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, addr string) (net.Conn, error), error) {
protocol, addr, err := parseEndpointWithFallbackProtocol(endpoint, "unix")
if err != nil {
return "", nil, err
}
if protocol != "unix" {
return "", nil, fmt.Errorf("only support unix socket endpoint")
}

return addr, dial, nil
}

func dial(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
}

func parseEndpointWithFallbackProtocol(endpoint string, fallbackProtocol string) (protocol string, addr string, err error) {
if protocol, addr, err = parseEndpoint(endpoint); err != nil && protocol == "" {
fallbackEndpoint := fallbackProtocol + "://" + endpoint
protocol, addr, err = parseEndpoint(fallbackEndpoint)
if err == nil {
klog.InfoS("Using this endpoint is deprecated, please consider using full URL format", "endpoint", endpoint, "URL", fallbackEndpoint)
}
}
return
}

func parseEndpoint(endpoint string) (string, string, error) {
u, err := url.Parse(endpoint)
if err != nil {
return "", "", err
}

switch u.Scheme {
case "tcp":
return "tcp", u.Host, nil

case "unix":
return "unix", u.Path, nil

case "":
return "", "", fmt.Errorf("using %q as endpoint is deprecated, please consider using full url format", endpoint)

default:
return u.Scheme, "", fmt.Errorf("protocol %q not supported", u.Scheme)
}
}
2 changes: 1 addition & 1 deletion pkg/knit/cmd/cpuaff.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/spf13/cobra"

cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
"github.com/openshift-kni/debug-tools/pkg/procs"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/knit/cmd/irqaff.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/openshift-kni/debug-tools/pkg/irqs"
softirqs "github.com/openshift-kni/debug-tools/pkg/irqs/soft"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
)

type irqAffOptions struct {
Expand Down
77 changes: 2 additions & 75 deletions pkg/knit/cmd/k8s/podres.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ import (
"context"
"encoding/json"
"fmt"
"google.golang.org/grpc"
"k8s.io/klog/v2"
"net"
"net/url"
"os"
"time"

kube "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
"github.com/openshift-kni/debug-tools/pkg/knit/cmd"
"github.com/spf13/cobra"
kubeletpodresourcesv1 "k8s.io/kubelet/pkg/apis/podresources/v1"
Expand Down Expand Up @@ -112,7 +109,7 @@ func showPodResources(cmd *cobra.Command, opts *podResOptions, args []string) er
return err
}

cli, conn, err := GetV1Client(opts.socketPath, defaultPodResourcesTimeout, defaultPodResourcesMaxSize)
cli, conn, err := kube.GetV1Client(opts.socketPath, defaultPodResourcesTimeout, defaultPodResourcesMaxSize)
if err != nil {
return err
}
Expand Down Expand Up @@ -199,73 +196,3 @@ func getTopologyInfo(topologyInfo *kubeletpodresourcesv1.TopologyInfo) *Topology
Nodes: numaNodes,
}
}

// GetV1Client returns a client for the PodResourcesLister grpc service
// This was copied from kubelet sources as per the comment there:
//
// Quote:
// Note: Consumers of the pod resources API should not be importing this package.
// They should copy paste the function in their project.
func GetV1Client(socket string, connectionTimeout time.Duration, maxMsgSize int) (kubeletpodresourcesv1.PodResourcesListerClient, *grpc.ClientConn, error) {
addr, dialer, err := GetAddressAndDialer(socket)
if err != nil {
return nil, nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout)
defer cancel()

conn, err := grpc.DialContext(ctx, addr, grpc.WithInsecure(), grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)))
if err != nil {
return nil, nil, fmt.Errorf("error dialing socket %s: %v", socket, err)
}
return kubeletpodresourcesv1.NewPodResourcesListerClient(conn), conn, nil
}

// GetAddressAndDialer returns the address parsed from the given endpoint and a context dialer.
func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, addr string) (net.Conn, error), error) {
protocol, addr, err := parseEndpointWithFallbackProtocol(endpoint, "unix")
if err != nil {
return "", nil, err
}
if protocol != "unix" {
return "", nil, fmt.Errorf("only support unix socket endpoint")
}

return addr, dial, nil
}

func dial(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
}

func parseEndpointWithFallbackProtocol(endpoint string, fallbackProtocol string) (protocol string, addr string, err error) {
if protocol, addr, err = parseEndpoint(endpoint); err != nil && protocol == "" {
fallbackEndpoint := fallbackProtocol + "://" + endpoint
protocol, addr, err = parseEndpoint(fallbackEndpoint)
if err == nil {
klog.InfoS("Using this endpoint is deprecated, please consider using full URL format", "endpoint", endpoint, "URL", fallbackEndpoint)
}
}
return
}

func parseEndpoint(endpoint string) (string, string, error) {
u, err := url.Parse(endpoint)
if err != nil {
return "", "", err
}

switch u.Scheme {
case "tcp":
return "tcp", u.Host, nil

case "unix":
return "unix", u.Path, nil

case "":
return "", "", fmt.Errorf("using %q as endpoint is deprecated, please consider using full url format", endpoint)

default:
return u.Scheme, "", fmt.Errorf("protocol %q not supported", u.Scheme)
}
}
2 changes: 1 addition & 1 deletion pkg/knit/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/spf13/cobra"

cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
)

type KnitOptions struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/machineinformer/relocatablesysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

"github.com/google/cadvisor/utils/sysfs"

cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/procs/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strconv"
"strings"

cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_cpuset"
cpuset "github.com/openshift-kni/debug-tools/pkg/k8s_imported"

"github.com/openshift-kni/debug-tools/pkg/fswrap"
)
Expand Down

0 comments on commit 7c94708

Please sign in to comment.