<commit_before>/*
Copyright 2017 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 deviceplugin

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"sync"

	"github.com/golang/glog"

	"k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	"k8s.io/apimachinery/pkg/util/sets"
	pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1alpha"
	"k8s.io/kubernetes/pkg/kubelet/lifecycle"
	"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)

// ActivePodsFunc is a function that returns a list of pods to reconcile.
type ActivePodsFunc func() []*v1.Pod

// Handler defines the functions used to manage and access device plugin resources.
type Handler interface {
	// Start starts device plugin registration service.
	Start(activePods ActivePodsFunc) error
	// Devices returns all of registered devices keyed by resourceName.
	Devices() map[string][]pluginapi.Device
	// Allocate scans through containers in the pod spec
	// If it finds the container requires device plugin resource, it:
	// 1. Checks whether it already has this information in its cached state.
	// 2. If not, it calls Allocate and populate its cached state afterwards.
	// 3. If there is no cached state and Allocate fails, it returns an error.
	// 4. Otherwise, it updates allocatableResource in nodeInfo if necessary,
	// to make sure it is at least equal to the pod's requested capacity for
	// any registered device plugin resource
	Allocate(node *schedulercache.NodeInfo, attrs *lifecycle.PodAdmitAttributes) error
	// GetDeviceRunContainerOptions checks whether we have cached containerDevices
	// for the passed-in <pod, container> and returns its DeviceRunContainerOptions
	// for the found one. An empty struct is returned in case no cached state is found.
	GetDeviceRunContainerOptions(pod *v1.Pod, container *v1.Container) *DeviceRunContainerOptions
}

// HandlerImpl implements the actual functionality to manage device plugin resources.
type HandlerImpl struct {
	// TODO: consider to change this to RWMutex.
	sync.Mutex
	devicePluginManager Manager
	// activePods is a method for listing active pods on the node
	// so the amount of pluginResources requested by existing pods
	// could be counted when updating allocated devices
	activePods ActivePodsFunc
	// devicePluginManagerMonitorCallback is used for testing only.<commit_msg>Add more comments for HandlerImpl struct<commit_after>/*
Copyright 2017 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 deviceplugin

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"sync"

	"github.com/golang/glog"

	"k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	"k8s.io/apimachinery/pkg/util/sets"
	pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1alpha"
	"k8s.io/kubernetes/pkg/kubelet/lifecycle"
	"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)

// ActivePodsFunc is a function that returns a list of pods to reconcile.
type ActivePodsFunc func() []*v1.Pod

// Handler defines the functions used to manage and access device plugin resources.
type Handler interface {
	// Start starts device plugin registration service.
	Start(activePods ActivePodsFunc) error
	// Devices returns all of registered devices keyed by resourceName.
	Devices() map[string][]pluginapi.Device
	// Allocate scans through containers in the pod spec
	// If it finds the container requires device plugin resource, it:
	// 1. Checks whether it already has this information in its cached state.
	// 2. If not, it calls Allocate and populate its cached state afterwards.
	// 3. If there is no cached state and Allocate fails, it returns an error.
	// 4. Otherwise, it updates allocatableResource in nodeInfo if necessary,
	// to make sure it is at least equal to the pod's requested capacity for
	// any registered device plugin resource
	Allocate(node *schedulercache.NodeInfo, attrs *lifecycle.PodAdmitAttributes) error
	// GetDeviceRunContainerOptions checks whether we have cached containerDevices
	// for the passed-in <pod, container> and returns its DeviceRunContainerOptions
	// for the found one. An empty struct is returned in case no cached state is found.
	GetDeviceRunContainerOptions(pod *v1.Pod, container *v1.Container) *DeviceRunContainerOptions
}

// HandlerImpl implements the actual functionality to manage device plugin resources.
type HandlerImpl struct {
	// TODO: consider to change this to RWMutex.
	sync.Mutex
	// devicePluginManager is an implementation of deviceplugin.Manager interface.
	devicePluginManager Manager
	// activePods is a method for listing active pods on the node
	// so the amount of pluginResources requested by existing pods
	// could be counted when updating allocated devices
	activePods ActivePodsFunc
	// devicePluginManagerMonitorCallback is used for updating devices' states in one time call.
	// e.g. a new device is advertised, two old devices are deleted and a running device fails.
{'fraction_non_alphanumeric': 0.06923988488234298, 'fraction_numerical': 0.007787370915862536, 'mean_word_length': 4.336946702800361, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7244426', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1607, 'n_words': 702}