2015-05-12 00:04:36 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-05-12 00:04:36 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 00:09:53 +00:00
|
|
|
package cadvisor
|
2015-05-12 00:04:36 +00:00
|
|
|
|
2015-05-30 00:32:34 +00:00
|
|
|
import (
|
2017-11-14 19:43:08 +00:00
|
|
|
goruntime "runtime"
|
|
|
|
|
2016-11-30 07:27:27 +00:00
|
|
|
cadvisorapi "github.com/google/cadvisor/info/v1"
|
2017-05-25 19:29:19 +00:00
|
|
|
cadvisorapi2 "github.com/google/cadvisor/info/v2"
|
2017-06-22 17:25:57 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2017-08-17 18:28:15 +00:00
|
|
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
2017-11-08 22:34:54 +00:00
|
|
|
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
|
2017-08-17 18:28:15 +00:00
|
|
|
"k8s.io/kubernetes/pkg/features"
|
2017-11-14 19:43:08 +00:00
|
|
|
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
|
2015-05-30 00:32:34 +00:00
|
|
|
)
|
|
|
|
|
2016-11-30 07:27:27 +00:00
|
|
|
func CapacityFromMachineInfo(info *cadvisorapi.MachineInfo) v1.ResourceList {
|
2016-11-18 20:50:58 +00:00
|
|
|
c := v1.ResourceList{
|
|
|
|
v1.ResourceCPU: *resource.NewMilliQuantity(
|
2015-10-10 00:09:53 +00:00
|
|
|
int64(info.NumCores*1000),
|
|
|
|
resource.DecimalSI),
|
2016-11-18 20:50:58 +00:00
|
|
|
v1.ResourceMemory: *resource.NewQuantity(
|
2016-01-06 23:36:48 +00:00
|
|
|
int64(info.MemoryCapacity),
|
2015-10-10 00:09:53 +00:00
|
|
|
resource.BinarySI),
|
|
|
|
}
|
2017-08-17 18:28:15 +00:00
|
|
|
|
|
|
|
// if huge pages are enabled, we report them as a schedulable resource on the node
|
|
|
|
if utilfeature.DefaultFeatureGate.Enabled(features.HugePages) {
|
|
|
|
for _, hugepagesInfo := range info.HugePages {
|
|
|
|
pageSizeBytes := int64(hugepagesInfo.PageSize * 1024)
|
|
|
|
hugePagesBytes := pageSizeBytes * int64(hugepagesInfo.NumPages)
|
|
|
|
pageSizeQuantity := resource.NewQuantity(pageSizeBytes, resource.BinarySI)
|
|
|
|
c[v1helper.HugePageResourceName(*pageSizeQuantity)] = *resource.NewQuantity(hugePagesBytes, resource.BinarySI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-10 00:09:53 +00:00
|
|
|
return c
|
2015-05-12 00:04:36 +00:00
|
|
|
}
|
2017-05-25 19:29:19 +00:00
|
|
|
|
2017-08-18 04:42:19 +00:00
|
|
|
func EphemeralStorageCapacityFromFsInfo(info cadvisorapi2.FsInfo) v1.ResourceList {
|
2017-05-25 19:29:19 +00:00
|
|
|
c := v1.ResourceList{
|
2017-08-18 04:42:19 +00:00
|
|
|
v1.ResourceEphemeralStorage: *resource.NewQuantity(
|
2017-05-30 19:41:31 +00:00
|
|
|
int64(info.Capacity),
|
|
|
|
resource.BinarySI),
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2017-11-14 19:43:08 +00:00
|
|
|
|
|
|
|
// CRI integrations should get container metrics via CRI. Docker
|
|
|
|
// uses the built-in cadvisor to gather such metrics on Linux for
|
|
|
|
// historical reasons.
|
|
|
|
// cri-o relies on cadvisor as a temporary workaround. The code should
|
|
|
|
// be removed. Related issue:
|
|
|
|
// https://github.com/kubernetes/kubernetes/issues/51798
|
|
|
|
// UsingLegacyCadvisorStats returns true if container stats are provided by cadvisor instead of through the CRI
|
|
|
|
func UsingLegacyCadvisorStats(runtime, runtimeEndpoint string) bool {
|
|
|
|
return runtime == kubetypes.RktContainerRuntime ||
|
|
|
|
(runtime == kubetypes.DockerContainerRuntime && goruntime.GOOS == "linux") ||
|
|
|
|
runtimeEndpoint == "/var/run/crio.sock"
|
|
|
|
}
|