Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit d95f937

Browse files
committed
pkg/checkpoint: remove k8s.io/kubernetes import
1 parent 37ae79c commit d95f937

File tree

4 files changed

+147
-7
lines changed

4 files changed

+147
-7
lines changed

pkg/checkpoint/internal/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Copied from pkg/kubelet/util to prevent importing kubernetes
2+
3+
```
4+
wget https://raw.githubusercontent.com/kubernetes/kubernetes/v1.10.2/pkg/kubelet/util/util_unix.go
5+
wget https://raw.githubusercontent.com/kubernetes/kubernetes/v1.10.2/pkg/kubelet/util/util.go
6+
```

pkg/checkpoint/internal/util.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package internal
18+
19+
import (
20+
"fmt"
21+
"net/url"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
)
25+
26+
// FromApiserverCache modifies <opts> so that the GET request will
27+
// be served from apiserver cache instead of from etcd.
28+
func FromApiserverCache(opts *metav1.GetOptions) {
29+
opts.ResourceVersion = "0"
30+
}
31+
32+
func parseEndpoint(endpoint string) (string, string, error) {
33+
u, err := url.Parse(endpoint)
34+
if err != nil {
35+
return "", "", err
36+
}
37+
38+
if u.Scheme == "tcp" {
39+
return "tcp", u.Host, nil
40+
} else if u.Scheme == "unix" {
41+
return "unix", u.Path, nil
42+
} else if u.Scheme == "" {
43+
return "", "", fmt.Errorf("Using %q as endpoint is deprecated, please consider using full url format", endpoint)
44+
} else {
45+
return u.Scheme, "", fmt.Errorf("protocol %q not supported", u.Scheme)
46+
}
47+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// +build freebsd linux darwin
2+
3+
/*
4+
Copyright 2017 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package internal
20+
21+
import (
22+
"fmt"
23+
"net"
24+
"os"
25+
"time"
26+
27+
"github.com/golang/glog"
28+
"golang.org/x/sys/unix"
29+
)
30+
31+
const (
32+
// unixProtocol is the network protocol of unix socket.
33+
unixProtocol = "unix"
34+
)
35+
36+
func CreateListener(endpoint string) (net.Listener, error) {
37+
protocol, addr, err := parseEndpointWithFallbackProtocol(endpoint, unixProtocol)
38+
if err != nil {
39+
return nil, err
40+
}
41+
if protocol != unixProtocol {
42+
return nil, fmt.Errorf("only support unix socket endpoint")
43+
}
44+
45+
// Unlink to cleanup the previous socket file.
46+
err = unix.Unlink(addr)
47+
if err != nil && !os.IsNotExist(err) {
48+
return nil, fmt.Errorf("failed to unlink socket file %q: %v", addr, err)
49+
}
50+
51+
return net.Listen(protocol, addr)
52+
}
53+
54+
func GetAddressAndDialer(endpoint string) (string, func(addr string, timeout time.Duration) (net.Conn, error), error) {
55+
protocol, addr, err := parseEndpointWithFallbackProtocol(endpoint, unixProtocol)
56+
if err != nil {
57+
return "", nil, err
58+
}
59+
if protocol != unixProtocol {
60+
return "", nil, fmt.Errorf("only support unix socket endpoint")
61+
}
62+
63+
return addr, dial, nil
64+
}
65+
66+
func dial(addr string, timeout time.Duration) (net.Conn, error) {
67+
return net.DialTimeout(unixProtocol, addr, timeout)
68+
}
69+
70+
func parseEndpointWithFallbackProtocol(endpoint string, fallbackProtocol string) (protocol string, addr string, err error) {
71+
if protocol, addr, err = parseEndpoint(endpoint); err != nil && protocol == "" {
72+
fallbackEndpoint := fallbackProtocol + "://" + endpoint
73+
protocol, addr, err = parseEndpoint(fallbackEndpoint)
74+
if err == nil {
75+
glog.Warningf("Using %q as endpoint is deprecated, please consider using full url format %q.", endpoint, fallbackEndpoint)
76+
}
77+
}
78+
return
79+
}

pkg/checkpoint/runtime_service.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ import (
88
"google.golang.org/grpc"
99
"k8s.io/api/core/v1"
1010
"k8s.io/apimachinery/pkg/types"
11-
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
12-
"k8s.io/kubernetes/pkg/kubelet/util"
1311

1412
"github.com/kubernetes-incubator/bootkube/pkg/checkpoint/cri/v1alpha1"
1513
"github.com/kubernetes-incubator/bootkube/pkg/checkpoint/cri/v1alpha2"
14+
"github.com/kubernetes-incubator/bootkube/pkg/checkpoint/internal"
15+
)
16+
17+
// Copied from "k8s.io/kubernetes/pkg/kubelet/types"
18+
const (
19+
kubernetesPodNameLabel = "io.kubernetes.pod.name"
20+
kubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace"
21+
kubernetesPodUIDLabel = "io.kubernetes.pod.uid"
22+
kubernetesContainerNameLabel = "io.kubernetes.container.name"
23+
kubernetesContainerTypeLabel = "io.kubernetes.container.type"
1624
)
1725

1826
type remoteRuntimeService struct {
@@ -23,7 +31,7 @@ type remoteRuntimeService struct {
2331

2432
func newRemoteRuntimeService(endpoint string, connectionTimeout time.Duration) (*remoteRuntimeService, error) {
2533
glog.Infof("Connecting to runtime service %s", endpoint)
26-
addr, dialer, err := util.GetAddressAndDialer(endpoint)
34+
addr, dialer, err := internal.GetAddressAndDialer(endpoint)
2735
if err != nil {
2836
return nil, err
2937
}
@@ -74,12 +82,12 @@ func (r *remoteRuntimeService) localRunningPods() map[string]*v1.Pod {
7482
// Add all pods that containers are apart of
7583
for _, c := range containers {
7684

77-
podName := c.Labels[kubelettypes.KubernetesPodNamespaceLabel] + "/" + c.Labels[kubelettypes.KubernetesPodNameLabel]
85+
podName := c.Labels[kubernetesPodNamespaceLabel] + "/" + c.Labels[kubernetesPodNameLabel]
7886
if _, ok := pods[podName]; !ok {
7987
p := &v1.Pod{}
80-
p.UID = types.UID(c.Labels[kubelettypes.KubernetesPodUIDLabel])
81-
p.Name = c.Labels[kubelettypes.KubernetesPodNameLabel]
82-
p.Namespace = c.Labels[kubelettypes.KubernetesPodNamespaceLabel]
88+
p.UID = types.UID(c.Labels[kubernetesPodUIDLabel])
89+
p.Name = c.Labels[kubernetesPodNameLabel]
90+
p.Namespace = c.Labels[kubernetesPodNamespaceLabel]
8391

8492
pods[podName] = p
8593
}

0 commit comments

Comments
 (0)