-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70508 from dashpole/pod_resources_socket
Add socket-based kubelet pod resources API.
- Loading branch information
Showing
36 changed files
with
1,871 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2018 The Kubernetes 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. | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
KUBE_ROOT="$(cd "$(dirname "${BASH_SOURCE}")/../" && pwd -P)" | ||
POD_RESOURCES_ALPHA="${KUBE_ROOT}/pkg/kubelet/apis/podresources/v1alpha1/" | ||
|
||
source "${KUBE_ROOT}/hack/lib/protoc.sh" | ||
kube::protoc::generate_proto ${POD_RESOURCES_ALPHA} |
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,27 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2018 The Kubernetes 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. | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. | ||
|
||
# NOTE: All output from this script needs to be copied back to the calling | ||
# source tree. This is managed in kube::build::copy_output in build/common.sh. | ||
# If the output set is changed update that function. | ||
|
||
${KUBE_ROOT}/build/run.sh hack/update-generated-pod-resources-dockerized.sh "$@" |
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,45 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2018 The Kubernetes 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. | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. | ||
KUBE_REMOTE_RUNTIME_ROOT="${KUBE_ROOT}/pkg/kubelet/apis/podresources/v1alpha1" | ||
source "${KUBE_ROOT}/hack/lib/init.sh" | ||
|
||
kube::golang::setup_env | ||
|
||
function cleanup { | ||
rm -rf ${KUBE_REMOTE_RUNTIME_ROOT}/_tmp/ | ||
} | ||
|
||
trap cleanup EXIT | ||
|
||
mkdir -p ${KUBE_REMOTE_RUNTIME_ROOT}/_tmp | ||
cp ${KUBE_REMOTE_RUNTIME_ROOT}/api.pb.go ${KUBE_REMOTE_RUNTIME_ROOT}/_tmp/ | ||
|
||
ret=0 | ||
KUBE_VERBOSE=3 "${KUBE_ROOT}/hack/update-generated-pod-resources.sh" | ||
diff -I "gzipped FileDescriptorProto" -I "0x" -Naupr ${KUBE_REMOTE_RUNTIME_ROOT}/_tmp/api.pb.go ${KUBE_REMOTE_RUNTIME_ROOT}/api.pb.go || ret=$? | ||
if [[ $ret -eq 0 ]]; then | ||
echo "Generated pod resources api is up to date." | ||
cp ${KUBE_REMOTE_RUNTIME_ROOT}/_tmp/api.pb.go ${KUBE_REMOTE_RUNTIME_ROOT}/ | ||
else | ||
echo "Generated pod resources api is out of date. Please run hack/update-generated-pod-resources.sh" | ||
exit 1 | ||
fi |
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,48 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"client.go", | ||
"constants.go", | ||
"server.go", | ||
], | ||
importpath = "k8s.io/kubernetes/pkg/kubelet/apis/podresources", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/kubelet/apis/podresources/v1alpha1:go_default_library", | ||
"//pkg/kubelet/util:go_default_library", | ||
"//staging/src/k8s.io/api/core/v1:go_default_library", | ||
"//vendor/google.golang.org/grpc:go_default_library", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["server_test.go"], | ||
embed = [":go_default_library"], | ||
deps = [ | ||
"//pkg/kubelet/apis/podresources/v1alpha1:go_default_library", | ||
"//staging/src/k8s.io/api/core/v1:go_default_library", | ||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", | ||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library", | ||
"//vendor/github.com/stretchr/testify/mock:go_default_library", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "package-srcs", | ||
srcs = glob(["**"]), | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
filegroup( | ||
name = "all-srcs", | ||
srcs = [ | ||
":package-srcs", | ||
"//pkg/kubelet/apis/podresources/v1alpha1:all-srcs", | ||
], | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:public"], | ||
) |
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,44 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 podresources | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
|
||
podresourcesapi "k8s.io/kubernetes/pkg/kubelet/apis/podresources/v1alpha1" | ||
"k8s.io/kubernetes/pkg/kubelet/util" | ||
) | ||
|
||
// GetClient returns a client for the PodResourcesLister grpc service | ||
func GetClient(socket string, connectionTimeout time.Duration, maxMsgSize int) (podresourcesapi.PodResourcesListerClient, *grpc.ClientConn, error) { | ||
addr, dialer, err := util.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.WithDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize))) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("Error dialing socket %s: %v", socket, err) | ||
} | ||
return podresourcesapi.NewPodResourcesListerClient(conn), conn, nil | ||
} |
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,22 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 podresources | ||
|
||
const ( | ||
// Socket is the name of the podresources server socket | ||
Socket = "kubelet" | ||
) |
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,75 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 podresources | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/api/core/v1" | ||
"k8s.io/kubernetes/pkg/kubelet/apis/podresources/v1alpha1" | ||
) | ||
|
||
// DevicesProvider knows how to provide the devices used by the given container | ||
type DevicesProvider interface { | ||
GetDevices(podUID, containerName string) []*v1alpha1.ContainerDevices | ||
} | ||
|
||
// PodsProvider knows how to provide the pods admitted by the node | ||
type PodsProvider interface { | ||
GetPods() []*v1.Pod | ||
} | ||
|
||
// podResourcesServer implements PodResourcesListerServer | ||
type podResourcesServer struct { | ||
podsProvider PodsProvider | ||
devicesProvider DevicesProvider | ||
} | ||
|
||
// NewPodResourcesServer returns a PodResourcesListerServer which lists pods provided by the PodsProvider | ||
// with device information provided by the DevicesProvider | ||
func NewPodResourcesServer(podsProvider PodsProvider, devicesProvider DevicesProvider) v1alpha1.PodResourcesListerServer { | ||
return &podResourcesServer{ | ||
podsProvider: podsProvider, | ||
devicesProvider: devicesProvider, | ||
} | ||
} | ||
|
||
// List returns information about the resources assigned to pods on the node | ||
func (p *podResourcesServer) List(ctx context.Context, req *v1alpha1.ListPodResourcesRequest) (*v1alpha1.ListPodResourcesResponse, error) { | ||
pods := p.podsProvider.GetPods() | ||
podResources := make([]*v1alpha1.PodResources, len(pods)) | ||
|
||
for i, pod := range pods { | ||
pRes := v1alpha1.PodResources{ | ||
Name: pod.Name, | ||
Namespace: pod.Namespace, | ||
Containers: make([]*v1alpha1.ContainerResources, len(pod.Spec.Containers)), | ||
} | ||
|
||
for j, container := range pod.Spec.Containers { | ||
pRes.Containers[j] = &v1alpha1.ContainerResources{ | ||
Name: container.Name, | ||
Devices: p.devicesProvider.GetDevices(string(pod.UID), container.Name), | ||
} | ||
} | ||
podResources[i] = &pRes | ||
} | ||
|
||
return &v1alpha1.ListPodResourcesResponse{ | ||
PodResources: podResources, | ||
}, nil | ||
} |
Oops, something went wrong.