Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 089824f

Browse files
committed
Remove dependency on k8s.io/kubernetes from many parts of codebase
1 parent 118a7ae commit 089824f

File tree

17 files changed

+785
-80
lines changed

17 files changed

+785
-80
lines changed

incubator/virtualcluster/cmd/vn-agent/app/options/options.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/pkg/errors"
2525
cliflag "k8s.io/component-base/cli/flag"
26-
kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
2726

2827
"sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/vn-agent/config"
2928
)
@@ -33,7 +32,7 @@ type Options struct {
3332
// ServerOption
3433
ServerOption
3534
// KubeletOption
36-
KubeletOption kubeletclient.KubeletClientConfig
35+
KubeletOption KubeletClientConfig
3736
}
3837

3938
type ServerOption struct {
@@ -52,9 +51,20 @@ type ServerOption struct {
5251
Port uint
5352
}
5453

54+
// Subset of the full options exposed in k8s.io/kubernetes/pkg/kubelet/client.KubeletClientConfig
55+
type KubeletClientConfig struct {
56+
// Port specifies the default port - used if no information about Kubelet port can be found in Node.NodeStatus.DaemonEndpoints.
57+
Port uint
58+
59+
// Server requires TLS client certificate authentication
60+
CertFile string
61+
// Server requires TLS client certificate authentication
62+
KeyFile string
63+
}
64+
5565
func NewVnAgentOptions() (*Options, error) {
5666
return &Options{
57-
KubeletOption: kubeletclient.KubeletClientConfig{},
67+
KubeletOption: KubeletClientConfig{},
5868
}, nil
5969
}
6070

incubator/virtualcluster/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ require (
3030
k8s.io/kubernetes v1.18.6
3131
k8s.io/utils v0.0.0-20200619165400-6e3d28b6ed19
3232
sigs.k8s.io/controller-runtime v0.6.1
33-
sigs.k8s.io/yaml v1.2.0
3433
)
3534

3635
// We use the replace directive to pin k8s.io dependencies that we don't directly

incubator/virtualcluster/pkg/controller/pki/pki.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ import (
2626
"net"
2727

2828
"k8s.io/client-go/util/cert"
29-
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
30-
3129
tenancyv1alpha1 "sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/apis/tenancy/v1alpha1"
3230
"sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/syncer/conversion"
31+
pkiutil "sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/util/pki"
3332
)
3433

3534
const (

incubator/virtualcluster/pkg/controller/secret/secret.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121

2222
v1 "k8s.io/api/core/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
2524

2625
vcpki "sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/controller/pki"
26+
pkiutil "sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/util/pki"
2727
)
2828

2929
const (

incubator/virtualcluster/pkg/controller/virtualcluster/master_provisioner_native.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
apierrors "k8s.io/apimachinery/pkg/api/errors"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
"k8s.io/client-go/util/cert"
29-
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
3029
"sigs.k8s.io/controller-runtime/pkg/client"
3130
"sigs.k8s.io/controller-runtime/pkg/manager"
3231

@@ -36,6 +35,7 @@ import (
3635
"sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/controller/secret"
3736
kubeutil "sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/controller/util/kube"
3837
"sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/syncer/conversion"
38+
pkiutil "sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/util/pki"
3939
)
4040

4141
const (
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright 2014 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 envvars is the package that build the environment variables that kubernetes provides
18+
// to the containers run by it.
19+
// This package has been copied from k8s.io/kubernetes/pkg/kubelet/[email protected]
20+
package envvars // import "k8s.io/kubernetes/pkg/kubelet/envvars"
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
Copyright 2014 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 envvars
18+
19+
import (
20+
"fmt"
21+
"net"
22+
"strconv"
23+
"strings"
24+
25+
"k8s.io/api/core/v1"
26+
)
27+
28+
// FromServices builds environment variables that a container is started with,
29+
// which tell the container where to find the services it may need, which are
30+
// provided as an argument.
31+
func FromServices(services []*v1.Service) []v1.EnvVar {
32+
var result []v1.EnvVar
33+
for i := range services {
34+
service := services[i]
35+
36+
// ignore services where ClusterIP is "None" or empty
37+
// the services passed to this method should be pre-filtered
38+
// only services that have the cluster IP set should be included here
39+
if !isServiceIPSet(service) {
40+
continue
41+
}
42+
43+
// Host
44+
name := makeEnvVariableName(service.Name) + "_SERVICE_HOST"
45+
result = append(result, v1.EnvVar{Name: name, Value: service.Spec.ClusterIP})
46+
// First port - give it the backwards-compatible name
47+
name = makeEnvVariableName(service.Name) + "_SERVICE_PORT"
48+
result = append(result, v1.EnvVar{Name: name, Value: strconv.Itoa(int(service.Spec.Ports[0].Port))})
49+
// All named ports (only the first may be unnamed, checked in validation)
50+
for i := range service.Spec.Ports {
51+
sp := &service.Spec.Ports[i]
52+
if sp.Name != "" {
53+
pn := name + "_" + makeEnvVariableName(sp.Name)
54+
result = append(result, v1.EnvVar{Name: pn, Value: strconv.Itoa(int(sp.Port))})
55+
}
56+
}
57+
// Docker-compatible vars.
58+
result = append(result, makeLinkVariables(service)...)
59+
}
60+
return result
61+
}
62+
63+
// this function aims to check if the service's ClusterIP is set or not
64+
// the objective is not to perform validation here
65+
func isServiceIPSet(service *v1.Service) bool {
66+
return service.Spec.ClusterIP != v1.ClusterIPNone && service.Spec.ClusterIP != ""
67+
}
68+
69+
func makeEnvVariableName(str string) string {
70+
// TODO: If we simplify to "all names are DNS1123Subdomains" this
71+
// will need two tweaks:
72+
// 1) Handle leading digits
73+
// 2) Handle dots
74+
return strings.ToUpper(strings.Replace(str, "-", "_", -1))
75+
}
76+
77+
func makeLinkVariables(service *v1.Service) []v1.EnvVar {
78+
prefix := makeEnvVariableName(service.Name)
79+
all := []v1.EnvVar{}
80+
for i := range service.Spec.Ports {
81+
sp := &service.Spec.Ports[i]
82+
83+
protocol := string(v1.ProtocolTCP)
84+
if sp.Protocol != "" {
85+
protocol = string(sp.Protocol)
86+
}
87+
88+
hostPort := net.JoinHostPort(service.Spec.ClusterIP, strconv.Itoa(int(sp.Port)))
89+
90+
if i == 0 {
91+
// Docker special-cases the first port.
92+
all = append(all, v1.EnvVar{
93+
Name: prefix + "_PORT",
94+
Value: fmt.Sprintf("%s://%s", strings.ToLower(protocol), hostPort),
95+
})
96+
}
97+
portPrefix := fmt.Sprintf("%s_PORT_%d_%s", prefix, sp.Port, strings.ToUpper(protocol))
98+
all = append(all, []v1.EnvVar{
99+
{
100+
Name: portPrefix,
101+
Value: fmt.Sprintf("%s://%s", strings.ToLower(protocol), hostPort),
102+
},
103+
{
104+
Name: portPrefix + "_PROTO",
105+
Value: strings.ToLower(protocol),
106+
},
107+
{
108+
Name: portPrefix + "_PORT",
109+
Value: strconv.Itoa(int(sp.Port)),
110+
},
111+
{
112+
Name: portPrefix + "_ADDR",
113+
Value: service.Spec.ClusterIP,
114+
},
115+
}...)
116+
}
117+
return all
118+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
Copyright 2014 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 envvars_test
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
26+
"sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/syncer/conversion/envvars"
27+
)
28+
29+
func TestFromServices(t *testing.T) {
30+
sl := []*v1.Service{
31+
{
32+
ObjectMeta: metav1.ObjectMeta{Name: "foo-bar"},
33+
Spec: v1.ServiceSpec{
34+
Selector: map[string]string{"bar": "baz"},
35+
ClusterIP: "1.2.3.4",
36+
Ports: []v1.ServicePort{
37+
{Port: 8080, Protocol: "TCP"},
38+
},
39+
},
40+
},
41+
{
42+
ObjectMeta: metav1.ObjectMeta{Name: "abc-123"},
43+
Spec: v1.ServiceSpec{
44+
Selector: map[string]string{"bar": "baz"},
45+
ClusterIP: "5.6.7.8",
46+
Ports: []v1.ServicePort{
47+
{Name: "u-d-p", Port: 8081, Protocol: "UDP"},
48+
{Name: "t-c-p", Port: 8081, Protocol: "TCP"},
49+
},
50+
},
51+
},
52+
{
53+
ObjectMeta: metav1.ObjectMeta{Name: "q-u-u-x"},
54+
Spec: v1.ServiceSpec{
55+
Selector: map[string]string{"bar": "baz"},
56+
ClusterIP: "9.8.7.6",
57+
Ports: []v1.ServicePort{
58+
{Port: 8082, Protocol: "TCP"},
59+
{Name: "8083", Port: 8083, Protocol: "TCP"},
60+
},
61+
},
62+
},
63+
{
64+
ObjectMeta: metav1.ObjectMeta{Name: "svrc-clusterip-none"},
65+
Spec: v1.ServiceSpec{
66+
Selector: map[string]string{"bar": "baz"},
67+
ClusterIP: "None",
68+
Ports: []v1.ServicePort{
69+
{Port: 8082, Protocol: "TCP"},
70+
},
71+
},
72+
},
73+
{
74+
ObjectMeta: metav1.ObjectMeta{Name: "svrc-clusterip-empty"},
75+
Spec: v1.ServiceSpec{
76+
Selector: map[string]string{"bar": "baz"},
77+
ClusterIP: "",
78+
Ports: []v1.ServicePort{
79+
{Port: 8082, Protocol: "TCP"},
80+
},
81+
},
82+
},
83+
{
84+
ObjectMeta: metav1.ObjectMeta{Name: "super-ipv6"},
85+
Spec: v1.ServiceSpec{
86+
Selector: map[string]string{"bar": "baz"},
87+
ClusterIP: "2001:DB8::",
88+
Ports: []v1.ServicePort{
89+
{Name: "u-d-p", Port: 8084, Protocol: "UDP"},
90+
{Name: "t-c-p", Port: 8084, Protocol: "TCP"},
91+
},
92+
},
93+
},
94+
{
95+
ObjectMeta: metav1.ObjectMeta{Name: "sctp-1"},
96+
Spec: v1.ServiceSpec{
97+
Selector: map[string]string{"bar": "sctp-sel"},
98+
ClusterIP: "1.2.3.4",
99+
Ports: []v1.ServicePort{
100+
{Port: 777, Protocol: "SCTP"},
101+
},
102+
},
103+
},
104+
}
105+
vars := envvars.FromServices(sl)
106+
expected := []v1.EnvVar{
107+
{Name: "FOO_BAR_SERVICE_HOST", Value: "1.2.3.4"},
108+
{Name: "FOO_BAR_SERVICE_PORT", Value: "8080"},
109+
{Name: "FOO_BAR_PORT", Value: "tcp://1.2.3.4:8080"},
110+
{Name: "FOO_BAR_PORT_8080_TCP", Value: "tcp://1.2.3.4:8080"},
111+
{Name: "FOO_BAR_PORT_8080_TCP_PROTO", Value: "tcp"},
112+
{Name: "FOO_BAR_PORT_8080_TCP_PORT", Value: "8080"},
113+
{Name: "FOO_BAR_PORT_8080_TCP_ADDR", Value: "1.2.3.4"},
114+
{Name: "ABC_123_SERVICE_HOST", Value: "5.6.7.8"},
115+
{Name: "ABC_123_SERVICE_PORT", Value: "8081"},
116+
{Name: "ABC_123_SERVICE_PORT_U_D_P", Value: "8081"},
117+
{Name: "ABC_123_SERVICE_PORT_T_C_P", Value: "8081"},
118+
{Name: "ABC_123_PORT", Value: "udp://5.6.7.8:8081"},
119+
{Name: "ABC_123_PORT_8081_UDP", Value: "udp://5.6.7.8:8081"},
120+
{Name: "ABC_123_PORT_8081_UDP_PROTO", Value: "udp"},
121+
{Name: "ABC_123_PORT_8081_UDP_PORT", Value: "8081"},
122+
{Name: "ABC_123_PORT_8081_UDP_ADDR", Value: "5.6.7.8"},
123+
{Name: "ABC_123_PORT_8081_TCP", Value: "tcp://5.6.7.8:8081"},
124+
{Name: "ABC_123_PORT_8081_TCP_PROTO", Value: "tcp"},
125+
{Name: "ABC_123_PORT_8081_TCP_PORT", Value: "8081"},
126+
{Name: "ABC_123_PORT_8081_TCP_ADDR", Value: "5.6.7.8"},
127+
{Name: "Q_U_U_X_SERVICE_HOST", Value: "9.8.7.6"},
128+
{Name: "Q_U_U_X_SERVICE_PORT", Value: "8082"},
129+
{Name: "Q_U_U_X_SERVICE_PORT_8083", Value: "8083"},
130+
{Name: "Q_U_U_X_PORT", Value: "tcp://9.8.7.6:8082"},
131+
{Name: "Q_U_U_X_PORT_8082_TCP", Value: "tcp://9.8.7.6:8082"},
132+
{Name: "Q_U_U_X_PORT_8082_TCP_PROTO", Value: "tcp"},
133+
{Name: "Q_U_U_X_PORT_8082_TCP_PORT", Value: "8082"},
134+
{Name: "Q_U_U_X_PORT_8082_TCP_ADDR", Value: "9.8.7.6"},
135+
{Name: "Q_U_U_X_PORT_8083_TCP", Value: "tcp://9.8.7.6:8083"},
136+
{Name: "Q_U_U_X_PORT_8083_TCP_PROTO", Value: "tcp"},
137+
{Name: "Q_U_U_X_PORT_8083_TCP_PORT", Value: "8083"},
138+
{Name: "Q_U_U_X_PORT_8083_TCP_ADDR", Value: "9.8.7.6"},
139+
{Name: "SUPER_IPV6_SERVICE_HOST", Value: "2001:DB8::"},
140+
{Name: "SUPER_IPV6_SERVICE_PORT", Value: "8084"},
141+
{Name: "SUPER_IPV6_SERVICE_PORT_U_D_P", Value: "8084"},
142+
{Name: "SUPER_IPV6_SERVICE_PORT_T_C_P", Value: "8084"},
143+
{Name: "SUPER_IPV6_PORT", Value: "udp://[2001:DB8::]:8084"},
144+
{Name: "SUPER_IPV6_PORT_8084_UDP", Value: "udp://[2001:DB8::]:8084"},
145+
{Name: "SUPER_IPV6_PORT_8084_UDP_PROTO", Value: "udp"},
146+
{Name: "SUPER_IPV6_PORT_8084_UDP_PORT", Value: "8084"},
147+
{Name: "SUPER_IPV6_PORT_8084_UDP_ADDR", Value: "2001:DB8::"},
148+
{Name: "SUPER_IPV6_PORT_8084_TCP", Value: "tcp://[2001:DB8::]:8084"},
149+
{Name: "SUPER_IPV6_PORT_8084_TCP_PROTO", Value: "tcp"},
150+
{Name: "SUPER_IPV6_PORT_8084_TCP_PORT", Value: "8084"},
151+
{Name: "SUPER_IPV6_PORT_8084_TCP_ADDR", Value: "2001:DB8::"},
152+
{Name: "SCTP_1_SERVICE_HOST", Value: "1.2.3.4"},
153+
{Name: "SCTP_1_SERVICE_PORT", Value: "777"},
154+
{Name: "SCTP_1_PORT", Value: "sctp://1.2.3.4:777"},
155+
{Name: "SCTP_1_PORT_777_SCTP", Value: "sctp://1.2.3.4:777"},
156+
{Name: "SCTP_1_PORT_777_SCTP_PROTO", Value: "sctp"},
157+
{Name: "SCTP_1_PORT_777_SCTP_PORT", Value: "777"},
158+
{Name: "SCTP_1_PORT_777_SCTP_ADDR", Value: "1.2.3.4"},
159+
}
160+
if len(vars) != len(expected) {
161+
t.Errorf("Expected %d env vars, got: %+v", len(expected), vars)
162+
return
163+
}
164+
for i := range expected {
165+
if !reflect.DeepEqual(vars[i], expected[i]) {
166+
t.Errorf("expected %#v, got %#v", vars[i], expected[i])
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)