diff --git a/Gopkg.toml b/Gopkg.toml index 9ebd0461..9447936f 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -33,10 +33,6 @@ ignored = ["k8s.io/code-generator"] non-go = false unused-packages = false -[[constraint]] - branch = "master" - name = "github.com/golang/glog" - [[constraint]] name = "google.golang.org/grpc" version = "1.11.0" diff --git a/Makefile b/Makefile index f9905ca4..92daf959 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ ifndef WHAT @$(GO) test -race -coverprofile=coverage.txt -covermode=atomic $(pkgs) else @cd $(WHAT) && \ - $(GO) test -v -cover -coverprofile cover.out -args -logtostderr -v 2 || rc=1; \ + $(GO) test -v -cover -coverprofile cover.out || rc=1; \ $(GO) tool cover -html=cover.out -o coverage.html; \ rm cover.out; \ echo "Coverage report: file://$$(realpath coverage.html)"; \ diff --git a/build/docker/intel-fpga-plugin.Dockerfile b/build/docker/intel-fpga-plugin.Dockerfile index 76c116b2..ef180195 100644 --- a/build/docker/intel-fpga-plugin.Dockerfile +++ b/build/docker/intel-fpga-plugin.Dockerfile @@ -7,4 +7,4 @@ RUN chmod a+x /go/bin/fpga_plugin FROM alpine COPY --from=builder /go/bin/fpga_plugin /usr/bin/intel_fpga_device_plugin -CMD ["/usr/bin/intel_fpga_device_plugin", "-logtostderr"] +CMD ["/usr/bin/intel_fpga_device_plugin"] diff --git a/cmd/fpga_admissionwebhook/controller.go b/cmd/fpga_admissionwebhook/controller.go index 1fb76026..d808b214 100644 --- a/cmd/fpga_admissionwebhook/controller.go +++ b/cmd/fpga_admissionwebhook/controller.go @@ -18,7 +18,6 @@ import ( "fmt" "time" - "github.com/golang/glog" "github.com/pkg/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -31,6 +30,7 @@ import ( clientset "github.com/intel/intel-device-plugins-for-kubernetes/pkg/client/clientset/versioned" informers "github.com/intel/intel-device-plugins-for-kubernetes/pkg/client/informers/externalversions" listers "github.com/intel/intel-device-plugins-for-kubernetes/pkg/client/listers/fpga.intel.com/v1" + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) const ( @@ -89,7 +89,7 @@ func (c *controller) run(threadiness int) error { defer runtime.HandleCrash() defer c.queue.ShutDown() - glog.Info("Starting controller") + fmt.Println("Starting controller") go c.informerFactory.Start(c.stopCh) @@ -153,7 +153,7 @@ func (c *controller) processNextWorkItem() bool { // Finally, if no error occurs we Forget this item so it does not // get queued again until another change happens. c.queue.Forget(obj) - glog.V(2).Infof("Successfully synced '%s'", key) + debug.Printf("Successfully synced '%s'", key) return nil }(obj) @@ -180,7 +180,7 @@ func (c *controller) syncAfHandler(key string) error { // processing. if k8serrors.IsNotFound(err) { runtime.HandleError(errors.Errorf("accelerated function '%s' in work queue no longer exists", key)) - glog.V(2).Infof("AF '%s' no longer exists", key) + debug.Printf("AF '%s' no longer exists", key) c.patcher.removeAf(name) return nil } @@ -188,7 +188,7 @@ func (c *controller) syncAfHandler(key string) error { return err } - glog.V(2).Info("Received ", af) + debug.Print("Received", af) c.patcher.addAf(af) return nil } @@ -208,7 +208,7 @@ func (c *controller) syncRegionHandler(key string) error { // processing. if k8serrors.IsNotFound(err) { runtime.HandleError(errors.Errorf("FPGA region '%s' in work queue no longer exists", key)) - glog.V(2).Infof("Region '%s' no longer exists", key) + debug.Printf("Region '%s' no longer exists", key) c.patcher.removeRegion(name) return nil } @@ -216,7 +216,7 @@ func (c *controller) syncRegionHandler(key string) error { return err } - glog.V(2).Info("Received ", region) + debug.Print("Received", region) c.patcher.addRegion(region) return nil } diff --git a/cmd/fpga_admissionwebhook/controller_test.go b/cmd/fpga_admissionwebhook/controller_test.go index 9d54b5e2..46b88091 100644 --- a/cmd/fpga_admissionwebhook/controller_test.go +++ b/cmd/fpga_admissionwebhook/controller_test.go @@ -25,6 +25,7 @@ import ( v1 "github.com/intel/intel-device-plugins-for-kubernetes/pkg/apis/fpga.intel.com/v1" listers "github.com/intel/intel-device-plugins-for-kubernetes/pkg/client/listers/fpga.intel.com/v1" + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) type fakeAfNamespaceLister struct { @@ -32,6 +33,10 @@ type fakeAfNamespaceLister struct { err error } +func init() { + debug.Activate() +} + func (nl *fakeAfNamespaceLister) Get(name string) (*v1.AcceleratorFunction, error) { return nl.af, nl.err } diff --git a/cmd/fpga_admissionwebhook/fpga_admissionwebhook.go b/cmd/fpga_admissionwebhook/fpga_admissionwebhook.go index c463f9e5..46d55d28 100644 --- a/cmd/fpga_admissionwebhook/fpga_admissionwebhook.go +++ b/cmd/fpga_admissionwebhook/fpga_admissionwebhook.go @@ -24,7 +24,6 @@ import ( "os" "strings" - "github.com/golang/glog" "github.com/pkg/errors" "k8s.io/api/admission/v1beta1" @@ -36,6 +35,8 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" + + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) const ( @@ -71,11 +72,11 @@ func getTLSConfig(certFile string, keyFile string) *tls.Config { func mutatePods(ar v1beta1.AdmissionReview, p *patcher) *v1beta1.AdmissionResponse { var ops []string - glog.V(2).Info("mutating pods") + debug.Print("mutating pods") podResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"} if ar.Request.Resource != podResource { - glog.Errorf("expect resource to be %s", podResource) + fmt.Printf("WARNING: Unexpected resource type %s\n", ar.Request.Resource) return nil } @@ -128,7 +129,7 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) { } if len(body) == 0 { - glog.Error("No body in request") + debug.Print("No body in request") w.WriteHeader(http.StatusBadRequest) return } @@ -136,16 +137,16 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) { // verify the content type is accurate contentType := r.Header.Get("Content-Type") if contentType != "application/json" { - glog.Errorf("contentType=%s, expect application/json", contentType) + debug.Printf("contentType=%s, expect application/json", contentType) w.WriteHeader(http.StatusBadRequest) return } - glog.V(2).Info(fmt.Sprintf("handling request: %s", string(body))) + debug.Printf("handling request: %s", string(body)) ar := v1beta1.AdmissionReview{} deserializer := codecs.UniversalDeserializer() if _, _, err := deserializer.Decode(body, nil, &ar); err != nil { - glog.Error(err) + fmt.Printf("ERROR: %+v\n", err) reviewResponse = toAdmissionResponse(err) } else { if ar.Request == nil { @@ -156,7 +157,7 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) { reviewResponse = admit(ar) } } - glog.V(2).Info(fmt.Sprintf("sending response: %v", reviewResponse)) + debug.Print("sending response", reviewResponse) response := v1beta1.AdmissionReview{} if reviewResponse != nil { @@ -172,11 +173,11 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) { resp, err := json.Marshal(response) if err != nil { - glog.Error(err) + fmt.Println("ERROR:", err) return } if _, err := w.Write(resp); err != nil { - glog.Error(err) + fmt.Println("ERROR:", err) } } @@ -201,6 +202,7 @@ func main() { var mode string var config *rest.Config var err error + var debugEnabled bool flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file") flag.StringVar(&master, "master", "", "master url") @@ -208,25 +210,30 @@ func main() { "File containing the x509 Certificate for HTTPS. (CA cert, if any, concatenated after server cert).") flag.StringVar(&keyFile, "tls-private-key-file", keyFile, "File containing the x509 private key matching --tls-cert-file.") flag.StringVar(&mode, "mode", preprogrammed, fmt.Sprintf("webhook mode: '%s' (default) or '%s'", preprogrammed, orchestrated)) + flag.BoolVar(&debugEnabled, "debug", false, "enable debug output") flag.Parse() + if debugEnabled { + debug.Activate() + } + if certFile == "" { - glog.Error("TLS certificate file is not set") + fmt.Println("TLS certificate file is not set") os.Exit(1) } if keyFile == "" { - glog.Error("TLS private key is not set") + fmt.Println("TLS private key is not set") os.Exit(1) } if _, err = os.Stat(certFile); err != nil { - glog.Error("TLS certificate not found") + fmt.Println("TLS certificate not found") os.Exit(1) } if _, err = os.Stat(keyFile); err != nil { - glog.Error("TLS private key not found") + fmt.Println("TLS private key not found") os.Exit(1) } @@ -236,7 +243,7 @@ func main() { config, err = clientcmd.BuildConfigFromFlags(master, kubeconfig) } if err != nil { - glog.Error("Failed to get cluster config ", err) + fmt.Println("Failed to get cluster config ", err) os.Exit(1) } @@ -253,7 +260,7 @@ func main() { http.HandleFunc("/pods", makePodsHandler(patcher)) - glog.V(2).Info("Webhook started") + debug.Print("Webhook started") server := &http.Server{ Addr: ":443", diff --git a/cmd/fpga_admissionwebhook/fpga_admissionwebhook_test.go b/cmd/fpga_admissionwebhook/fpga_admissionwebhook_test.go index 1f6b7e8e..b2ec9c59 100644 --- a/cmd/fpga_admissionwebhook/fpga_admissionwebhook_test.go +++ b/cmd/fpga_admissionwebhook/fpga_admissionwebhook_test.go @@ -28,8 +28,14 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) +func init() { + debug.Activate() +} + func fakeMutatePods(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse { reviewResponse := v1beta1.AdmissionResponse{} return &reviewResponse diff --git a/cmd/fpga_admissionwebhook/patcher_test.go b/cmd/fpga_admissionwebhook/patcher_test.go index faeb8f38..f4f595a8 100644 --- a/cmd/fpga_admissionwebhook/patcher_test.go +++ b/cmd/fpga_admissionwebhook/patcher_test.go @@ -22,8 +22,13 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" fpgav1 "github.com/intel/intel-device-plugins-for-kubernetes/pkg/apis/fpga.intel.com/v1" + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) +func init() { + debug.Activate() +} + func TestPatcherStorageFunctions(t *testing.T) { af := &fpgav1.AcceleratorFunction{ ObjectMeta: metav1.ObjectMeta{ diff --git a/cmd/fpga_crihook/main_test.go b/cmd/fpga_crihook/main_test.go index abfde06d..ad2be2ba 100644 --- a/cmd/fpga_crihook/main_test.go +++ b/cmd/fpga_crihook/main_test.go @@ -22,8 +22,14 @@ import ( "k8s.io/utils/exec" fakeexec "k8s.io/utils/exec/testing" + + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) +func init() { + debug.Activate() +} + func TestGetFPGAParams(t *testing.T) { tcases := []struct { stdinJSON string diff --git a/cmd/fpga_plugin/fpga_plugin.go b/cmd/fpga_plugin/fpga_plugin.go index 8d2a7c9d..b239ac8d 100644 --- a/cmd/fpga_plugin/fpga_plugin.go +++ b/cmd/fpga_plugin/fpga_plugin.go @@ -24,7 +24,6 @@ import ( "strings" "time" - "github.com/golang/glog" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -35,6 +34,7 @@ import ( utilnode "k8s.io/kubernetes/pkg/util/node" dpapi "github.com/intel/intel-device-plugins-for-kubernetes/internal/deviceplugin" + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) const ( @@ -237,7 +237,7 @@ func (dp *devicePlugin) getDevNode(devName string) (string, error) { func (dp *devicePlugin) scanFPGAs() (dpapi.DeviceTree, error) { var devices []device - glog.V(2).Info("Start new FPGA scan") + debug.Print("Start new FPGA scan") fpgaFiles, err := ioutil.ReadDir(dp.sysfsDir) if err != nil { @@ -340,13 +340,19 @@ func main() { var master string var config *rest.Config var err error + var debugEnabled bool flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file") flag.StringVar(&master, "master", "", "master url") flag.StringVar(&mode, "mode", string(afMode), fmt.Sprintf("device plugin mode: '%s' (default), '%s' or '%s'", afMode, regionMode, regionDevelMode)) + flag.BoolVar(&debugEnabled, "debug", false, "enable debug output") flag.Parse() + if debugEnabled { + debug.Activate() + } + if kubeconfig == "" { config, err = rest.InClusterConfig() } else { @@ -370,7 +376,7 @@ func main() { } if nodeMode, ok := node.ObjectMeta.Annotations["fpga.intel.com/device-plugin-mode"]; ok { - glog.Info("Overriding mode to ", nodeMode) + fmt.Println("Overriding mode to ", nodeMode) mode = nodeMode } @@ -379,7 +385,7 @@ func main() { fatal(err) } - glog.Info("FPGA device plugin started in ", mode, " mode") + fmt.Println("FPGA device plugin started in ", mode, " mode") manager := dpapi.NewManager(namespace, plugin) manager.Run() diff --git a/cmd/fpga_plugin/fpga_plugin_test.go b/cmd/fpga_plugin/fpga_plugin_test.go index 26d1a283..dbec735a 100644 --- a/cmd/fpga_plugin/fpga_plugin_test.go +++ b/cmd/fpga_plugin/fpga_plugin_test.go @@ -29,8 +29,13 @@ import ( pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1" dpapi "github.com/intel/intel-device-plugins-for-kubernetes/internal/deviceplugin" + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) +func init() { + debug.Activate() +} + func createTestDirs(devfs, sysfs string, devfsDirs, sysfsDirs []string, sysfsFiles map[string][]byte) error { var err error diff --git a/cmd/gpu_plugin/gpu_plugin.go b/cmd/gpu_plugin/gpu_plugin.go index dfd5166e..f7b2ec96 100644 --- a/cmd/gpu_plugin/gpu_plugin.go +++ b/cmd/gpu_plugin/gpu_plugin.go @@ -24,12 +24,12 @@ import ( "strings" "time" - "github.com/golang/glog" "github.com/pkg/errors" pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1" dpapi "github.com/intel/intel-device-plugins-for-kubernetes/internal/deviceplugin" + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) const ( @@ -88,7 +88,7 @@ func (dp *devicePlugin) scan() (dpapi.DeviceTree, error) { if dp.gpuDeviceReg.MatchString(f.Name()) { dat, err := ioutil.ReadFile(path.Join(dp.sysfsDir, f.Name(), "device/vendor")) if err != nil { - glog.Warning("Skipping. Can't read vendor file: ", err) + fmt.Println("WARNING: Skipping. Can't read vendor file: ", err) continue } @@ -110,7 +110,7 @@ func (dp *devicePlugin) scan() (dpapi.DeviceTree, error) { continue } - glog.V(2).Info("Adding ", devPath, " to GPU ", f.Name()) + debug.Printf("Adding %s to GPU %s", devPath, f.Name()) nodes = append(nodes, devPath) } @@ -134,16 +134,22 @@ func (dp *devicePlugin) scan() (dpapi.DeviceTree, error) { func main() { var sharedDevNum int + var debugEnabled bool flag.IntVar(&sharedDevNum, "shared-dev-num", 1, "number of containers sharing the same GPU device") + flag.BoolVar(&debugEnabled, "debug", false, "enable debug output") flag.Parse() + if debugEnabled { + debug.Activate() + } + if sharedDevNum < 1 { - glog.Error("The number of containers sharing the same GPU must greater than zero") + fmt.Println("The number of containers sharing the same GPU must greater than zero") os.Exit(1) } - glog.Info("GPU device plugin started") + fmt.Println("GPU device plugin started") plugin := newDevicePlugin(sysfsDrmDirectory, devfsDriDirectory, sharedDevNum) manager := dpapi.NewManager(namespace, plugin) diff --git a/cmd/gpu_plugin/gpu_plugin_test.go b/cmd/gpu_plugin/gpu_plugin_test.go index 5278a125..b0ae7028 100644 --- a/cmd/gpu_plugin/gpu_plugin_test.go +++ b/cmd/gpu_plugin/gpu_plugin_test.go @@ -21,8 +21,14 @@ import ( "path" "testing" "time" + + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) +func init() { + debug.Activate() +} + func TestScan(t *testing.T) { tmpdir := fmt.Sprintf("/tmp/gpuplugin-test-%d", time.Now().Unix()) sysfs := path.Join(tmpdir, "sysfs") diff --git a/cmd/qat_plugin/README.md b/cmd/qat_plugin/README.md index 09b49290..d1854509 100644 --- a/cmd/qat_plugin/README.md +++ b/cmd/qat_plugin/README.md @@ -35,7 +35,7 @@ $ ls /var/lib/kubelet/device-plugins/kubelet.sock ### Deploy QAT device plugin directly on the host: ``` $ sudo $GOPATH/src/github.com/intel/intel-device-plugins-for-kubernetes/cmd/qat_plugin/qat_plugin \ --dpdk-driver igb_uio -kernel-vf-drivers dh895xccvf -max-num-devices 10 -v 10 logtostderr +-dpdk-driver igb_uio -kernel-vf-drivers dh895xccvf -max-num-devices 10 -debug QAT device plugin started Discovered Devices below: 03:01.0 device: corresponding DPDK device detected is uio0 diff --git a/cmd/qat_plugin/qat_plugin.go b/cmd/qat_plugin/qat_plugin.go index a8487ced..504cc7fb 100644 --- a/cmd/qat_plugin/qat_plugin.go +++ b/cmd/qat_plugin/qat_plugin.go @@ -25,12 +25,12 @@ import ( "strings" "time" - "github.com/golang/glog" "github.com/pkg/errors" pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1" "github.com/intel/intel-device-plugins-for-kubernetes/internal/deviceplugin" + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) const ( @@ -263,9 +263,14 @@ func main() { dpdkDriver := flag.String("dpdk-driver", "igb_uio", "DPDK Device driver for configuring the QAT device") kernelVfDrivers := flag.String("kernel-vf-drivers", "dh895xccvf,c6xxvf,c3xxxvf,d15xxvf", "Comma separated VF Device Driver of the QuickAssist Devices in the system. Devices supported: DH895xCC,C62x,C3xxx and D15xx") maxNumDevices := flag.Int("max-num-devices", 32, "maximum number of QAT devices to be provided to the QuickAssist device plugin") + debugEnabled := flag.Bool("debug", false, "enable debug output") flag.Parse() fmt.Println("QAT device plugin started") + if *debugEnabled { + debug.Activate() + } + if !isValidDpdkDeviceDriver(*dpdkDriver) { fmt.Println("Wrong DPDK device driver:", *dpdkDriver) os.Exit(1) diff --git a/cmd/qat_plugin/qat_plugin_test.go b/cmd/qat_plugin/qat_plugin_test.go index bcac7e9a..2529ba90 100644 --- a/cmd/qat_plugin/qat_plugin_test.go +++ b/cmd/qat_plugin/qat_plugin_test.go @@ -23,8 +23,14 @@ import ( "time" "github.com/pkg/errors" + + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) +func init() { + debug.Activate() +} + func createTestFiles(prefix string, dirs []string, files map[string][]byte) error { for _, dir := range dirs { err := os.MkdirAll(path.Join(prefix, dir), 0755) diff --git a/deployments/fpga_admissionwebhook/deployment-tpl.yaml b/deployments/fpga_admissionwebhook/deployment-tpl.yaml index 05549a1b..44b29037 100644 --- a/deployments/fpga_admissionwebhook/deployment-tpl.yaml +++ b/deployments/fpga_admissionwebhook/deployment-tpl.yaml @@ -21,9 +21,7 @@ spec: - -tls-cert-file=/etc/webhook/certs/cert.pem - -tls-private-key-file=/etc/webhook/certs/key.pem - -mode={MODE} - - -alsologtostderr - - -v=2 - - 2>&1 + - -debug volumeMounts: - name: webhook-certs mountPath: /etc/webhook/certs diff --git a/internal/deviceplugin/manager.go b/internal/deviceplugin/manager.go index 458dd861..7581a363 100644 --- a/internal/deviceplugin/manager.go +++ b/internal/deviceplugin/manager.go @@ -19,9 +19,9 @@ import ( "os" "reflect" - "github.com/golang/glog" - pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1" + + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) // updateInfo contains info for added, updated and deleted devices. @@ -107,7 +107,7 @@ func (m *Manager) Run() { } func (m *Manager) handleUpdate(update updateInfo) { - glog.V(2).Info("Received dev updates: ", update) + debug.Print("Received dev updates:", update) for devType, devices := range update.Added { var postAllocate func(*pluginapi.AllocateResponse) error diff --git a/internal/deviceplugin/manager_test.go b/internal/deviceplugin/manager_test.go index fa360846..52f0d4c6 100644 --- a/internal/deviceplugin/manager_test.go +++ b/internal/deviceplugin/manager_test.go @@ -18,8 +18,14 @@ import ( "testing" pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1" + + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) +func init() { + debug.Activate() +} + func TestNotify(t *testing.T) { tcases := []struct { name string diff --git a/internal/deviceplugin/server.go b/internal/deviceplugin/server.go index 6b269279..e9b2191e 100644 --- a/internal/deviceplugin/server.go +++ b/internal/deviceplugin/server.go @@ -24,11 +24,12 @@ import ( "time" "github.com/fsnotify/fsnotify" - "github.com/golang/glog" "github.com/pkg/errors" "google.golang.org/grpc" pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1" + + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" ) // devicePluginServer maintains a gRPC server satisfying @@ -69,7 +70,7 @@ func (srv *server) sendDevices(stream pluginapi.DevicePlugin_ListAndWatchServer) for id, device := range srv.devices { resp.Devices = append(resp.Devices, &pluginapi.Device{id, device.State}) } - glog.V(2).Info("Sending to kubelet ", resp.Devices) + debug.Print("Sending to kubelet", resp.Devices) if err := stream.Send(resp); err != nil { srv.Stop() return errors.Wrapf(err, "Cannot update device list") @@ -79,7 +80,7 @@ func (srv *server) sendDevices(stream pluginapi.DevicePlugin_ListAndWatchServer) } func (srv *server) ListAndWatch(empty *pluginapi.Empty, stream pluginapi.DevicePlugin_ListAndWatchServer) error { - glog.V(2).Info("Started ListAndWatch for ", srv.devType) + debug.Print("Started ListAndWatch for", srv.devType) if err := srv.sendDevices(stream); err != nil { return err @@ -187,7 +188,7 @@ func (srv *server) setupAndServe(namespace string, devicePluginPath string, kube // Starts device plugin service. go func() { - fmt.Printf("device-plugin start server at: %s\n", pluginSocket) + fmt.Printf("Start server for %s at: %s\n", srv.devType, pluginSocket) srv.grpcServer.Serve(lis) }() @@ -201,16 +202,14 @@ func (srv *server) setupAndServe(namespace string, devicePluginPath string, kube if err != nil { return err } - fmt.Println("device-plugin registered") + fmt.Printf("Device plugin for %s registered\n", srv.devType) // Kubelet removes plugin socket when it (re)starts // plugin must restart in this case if err = watchFile(pluginSocket); err != nil { return err } - fmt.Printf("socket %s removed, restarting", pluginSocket) - - fmt.Println("stop GRPC server") + fmt.Printf("Socket %s removed, restarting\n", pluginSocket) srv.grpcServer.Stop() os.Remove(pluginSocket) diff --git a/internal/deviceplugin/server_test.go b/internal/deviceplugin/server_test.go index d58a32e9..7231d263 100644 --- a/internal/deviceplugin/server_test.go +++ b/internal/deviceplugin/server_test.go @@ -29,6 +29,7 @@ import ( pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1" + "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" "github.com/pkg/errors" ) @@ -47,6 +48,10 @@ type kubeletStub struct { server *grpc.Server } +func init() { + debug.Activate() +} + // newKubeletStub returns an initialized kubeletStub for testing purpose. func newKubeletStub(socket string) *kubeletStub { return &kubeletStub{ diff --git a/pkg/debug/debug.go b/pkg/debug/debug.go new file mode 100644 index 00000000..d58b7180 --- /dev/null +++ b/pkg/debug/debug.go @@ -0,0 +1,57 @@ +// Copyright 2018 Intel Corporation. All Rights Reserved. +// +// 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 debug + +import ( + "fmt" + "runtime" + "strings" +) + +const ( + prefix = "DEBUG" +) + +var ( + isEnabled bool +) + +func getFileAndLine() string { + _, file, line, ok := runtime.Caller(2) + if !ok { + return "???:0" + } + parts := strings.Split(file, "/") + return fmt.Sprintf("%s:%d", parts[len(parts)-1], line) +} + +// Activate activates debugging output +func Activate() { + isEnabled = true +} + +// Print prints its arguments with fmt.Println() if debug output is activated +func Print(obj ...interface{}) { + if isEnabled { + fmt.Println(append([]interface{}{prefix, getFileAndLine()}, obj...)...) + } +} + +// Printf prints its arguments with fmt.Printf() if debug output is activated +func Printf(pattern string, obj ...interface{}) { + if isEnabled { + fmt.Printf(strings.Join([]string{prefix, getFileAndLine(), pattern + "\n"}, " "), obj...) + } +}