custom_metrics.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package custommetrics contains support for instrumenting cAdvisor to gather custom metrics from pods.
  14. package custommetrics
  15. import (
  16. "path"
  17. "k8s.io/api/core/v1"
  18. )
  19. const (
  20. // CustomMetricsDefinitionContainerFile is the file in container that stores Custom Metrics definition
  21. CustomMetricsDefinitionContainerFile = "definition.json"
  22. // CustomMetricsDefinitionDir is the dir where Custom Metrics definition is stored
  23. CustomMetricsDefinitionDir = "/etc/custom-metrics"
  24. )
  25. // GetCAdvisorCustomMetricsDefinitionPath returns a path to a cAdvisor-specific custom metrics configuration.
  26. // Alpha implementation.
  27. func GetCAdvisorCustomMetricsDefinitionPath(container *v1.Container) (*string, error) {
  28. // Assumes that the container has Custom Metrics enabled if it has "/etc/custom-metrics" directory
  29. // mounted as a volume. Custom Metrics definition is expected to be in "definition.json".
  30. if container.VolumeMounts != nil {
  31. for _, volumeMount := range container.VolumeMounts {
  32. if path.Clean(volumeMount.MountPath) == path.Clean(CustomMetricsDefinitionDir) {
  33. // TODO: add definition file validation.
  34. definitionPath := path.Clean(path.Join(volumeMount.MountPath, CustomMetricsDefinitionContainerFile))
  35. return &definitionPath, nil
  36. }
  37. }
  38. }
  39. // No Custom Metrics definition available.
  40. return nil, nil
  41. }