From 0ec6bdd2b5963d5f0c12dd25edd8bf7fb77e1e1a Mon Sep 17 00:00:00 2001 From: vikaschoudhary16 Date: Wed, 30 Jan 2019 12:21:13 +0530 Subject: [PATCH] Add image for sample-device-plugin --- test/images/BUILD | 1 + test/images/sample-device-plugin/BASEIMAGE | 5 + test/images/sample-device-plugin/BUILD | 35 +++++++ test/images/sample-device-plugin/Dockerfile | 17 ++++ test/images/sample-device-plugin/Makefile | 25 +++++ test/images/sample-device-plugin/VERSION | 1 + .../sampledeviceplugin.go | 99 +++++++++++++++++++ 7 files changed, 183 insertions(+) create mode 100644 test/images/sample-device-plugin/BASEIMAGE create mode 100644 test/images/sample-device-plugin/BUILD create mode 100644 test/images/sample-device-plugin/Dockerfile create mode 100644 test/images/sample-device-plugin/Makefile create mode 100644 test/images/sample-device-plugin/VERSION create mode 100755 test/images/sample-device-plugin/sampledeviceplugin.go diff --git a/test/images/BUILD b/test/images/BUILD index a7f93f6cf3c..14f64e42f4f 100644 --- a/test/images/BUILD +++ b/test/images/BUILD @@ -31,6 +31,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", diff --git a/test/images/sample-device-plugin/BASEIMAGE b/test/images/sample-device-plugin/BASEIMAGE new file mode 100644 index 00000000000..a6161331615 --- /dev/null +++ b/test/images/sample-device-plugin/BASEIMAGE @@ -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 diff --git a/test/images/sample-device-plugin/BUILD b/test/images/sample-device-plugin/BUILD new file mode 100644 index 00000000000..15e7fc6b2c2 --- /dev/null +++ b/test/images/sample-device-plugin/BUILD @@ -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"], +) diff --git a/test/images/sample-device-plugin/Dockerfile b/test/images/sample-device-plugin/Dockerfile new file mode 100644 index 00000000000..206e0ba645c --- /dev/null +++ b/test/images/sample-device-plugin/Dockerfile @@ -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"] diff --git a/test/images/sample-device-plugin/Makefile b/test/images/sample-device-plugin/Makefile new file mode 100644 index 00000000000..a7921439c67 --- /dev/null +++ b/test/images/sample-device-plugin/Makefile @@ -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 diff --git a/test/images/sample-device-plugin/VERSION b/test/images/sample-device-plugin/VERSION new file mode 100644 index 00000000000..d3827e75a5c --- /dev/null +++ b/test/images/sample-device-plugin/VERSION @@ -0,0 +1 @@ +1.0 diff --git a/test/images/sample-device-plugin/sampledeviceplugin.go b/test/images/sample-device-plugin/sampledeviceplugin.go new file mode 100755 index 00000000000..1c8003db592 --- /dev/null +++ b/test/images/sample-device-plugin/sampledeviceplugin.go @@ -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 {} +}