mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 22:46:12 +00:00
Merge pull request #73511 from vikaschoudhary16/image-only
Add image for sample-device-plugin
This commit is contained in:
commit
713eaf450f
@ -32,6 +32,7 @@ filegroup(
|
||||
"//test/images/porter:all-srcs",
|
||||
"//test/images/resource-consumer:all-srcs",
|
||||
"//test/images/sample-apiserver:all-srcs",
|
||||
"//test/images/sample-device-plugin:all-srcs",
|
||||
"//test/images/serve-hostname:all-srcs",
|
||||
"//test/images/test-webserver:all-srcs",
|
||||
"//test/images/webhook:all-srcs",
|
||||
|
5
test/images/sample-device-plugin/BASEIMAGE
Normal file
5
test/images/sample-device-plugin/BASEIMAGE
Normal file
@ -0,0 +1,5 @@
|
||||
amd64=alpine:3.8
|
||||
arm=arm32v6/alpine:3.8
|
||||
arm64=arm64v8/alpine:3.8
|
||||
ppc64le=ppc64le/alpine:3.8
|
||||
s390x=s390x/alpine:3.8
|
35
test/images/sample-device-plugin/BUILD
Normal file
35
test/images/sample-device-plugin/BUILD
Normal file
@ -0,0 +1,35 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["sampledeviceplugin.go"],
|
||||
importpath = "k8s.io/kubernetes/test/images/sample-device-plugin",
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
"//pkg/kubelet/apis/deviceplugin/v1beta1:go_default_library",
|
||||
"//pkg/kubelet/cm/devicemanager:go_default_library",
|
||||
"//vendor/k8s.io/klog:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_binary(
|
||||
name = "sample-device-plugin",
|
||||
embed = [":go_default_library"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
17
test/images/sample-device-plugin/Dockerfile
Normal file
17
test/images/sample-device-plugin/Dockerfile
Normal file
@ -0,0 +1,17 @@
|
||||
# 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.
|
||||
|
||||
FROM BASEIMAGE
|
||||
ADD sampledeviceplugin /sampledeviceplugin
|
||||
ENTRYPOINT ["/sampledeviceplugin", "-alsologtostderr"]
|
25
test/images/sample-device-plugin/Makefile
Normal file
25
test/images/sample-device-plugin/Makefile
Normal file
@ -0,0 +1,25 @@
|
||||
# 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.
|
||||
|
||||
SRCS=sampledeviceplugin
|
||||
ARCH ?= amd64
|
||||
TARGET ?= $(CURDIR)
|
||||
GOLANG_VERSION ?= latest
|
||||
SRC_DIR = $(notdir $(shell pwd))
|
||||
export
|
||||
|
||||
bin:
|
||||
../image-util.sh bin $(SRCS)
|
||||
|
||||
.PHONY: bin
|
1
test/images/sample-device-plugin/VERSION
Normal file
1
test/images/sample-device-plugin/VERSION
Normal file
@ -0,0 +1 @@
|
||||
1.0
|
99
test/images/sample-device-plugin/sampledeviceplugin.go
Executable file
99
test/images/sample-device-plugin/sampledeviceplugin.go
Executable file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"k8s.io/klog"
|
||||
pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
|
||||
dm "k8s.io/kubernetes/pkg/kubelet/cm/devicemanager"
|
||||
)
|
||||
|
||||
const (
|
||||
resourceName = "example.com/resource"
|
||||
)
|
||||
|
||||
// stubAllocFunc creates and returns allocation response for the input allocate request
|
||||
func stubAllocFunc(r *pluginapi.AllocateRequest, devs map[string]pluginapi.Device) (*pluginapi.AllocateResponse, error) {
|
||||
var responses pluginapi.AllocateResponse
|
||||
for _, req := range r.ContainerRequests {
|
||||
response := &pluginapi.ContainerAllocateResponse{}
|
||||
for _, requestID := range req.DevicesIDs {
|
||||
dev, ok := devs[requestID]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid allocation request with non-existing device %s", requestID)
|
||||
}
|
||||
|
||||
if dev.Health != pluginapi.Healthy {
|
||||
return nil, fmt.Errorf("invalid allocation request with unhealthy device: %s", requestID)
|
||||
}
|
||||
|
||||
// create fake device file
|
||||
fpath := filepath.Join("/tmp", dev.ID)
|
||||
|
||||
// clean first
|
||||
if err := os.RemoveAll(fpath); err != nil {
|
||||
return nil, fmt.Errorf("failed to clean fake device file from previous run: %s", err)
|
||||
}
|
||||
|
||||
f, err := os.Create(fpath)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
return nil, fmt.Errorf("failed to create fake device file: %s", err)
|
||||
}
|
||||
|
||||
f.Close()
|
||||
|
||||
response.Mounts = append(response.Mounts, &pluginapi.Mount{
|
||||
ContainerPath: fpath,
|
||||
HostPath: fpath,
|
||||
})
|
||||
}
|
||||
responses.ContainerResponses = append(responses.ContainerResponses, response)
|
||||
}
|
||||
|
||||
return &responses, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
devs := []*pluginapi.Device{
|
||||
{ID: "Dev-1", Health: pluginapi.Healthy},
|
||||
{ID: "Dev-2", Health: pluginapi.Healthy},
|
||||
}
|
||||
|
||||
pluginSocksDir := os.Getenv("PLUGIN_SOCK_DIR")
|
||||
klog.Infof("pluginSocksDir: %s", pluginSocksDir)
|
||||
if pluginSocksDir == "" {
|
||||
klog.Errorf("Empty pluginSocksDir")
|
||||
return
|
||||
}
|
||||
socketPath := pluginSocksDir + "/dp." + fmt.Sprintf("%d", time.Now().Unix())
|
||||
|
||||
dp1 := dm.NewDevicePluginStub(devs, socketPath, resourceName, false)
|
||||
if err := dp1.Start(); err != nil {
|
||||
panic(err)
|
||||
|
||||
}
|
||||
dp1.SetAllocFunc(stubAllocFunc)
|
||||
if err := dp1.Register(pluginapi.KubeletSocket, resourceName, pluginapi.DevicePluginPath); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
select {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user