logging_agent.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2017 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 utils
  14. import (
  15. "fmt"
  16. api_v1 "k8s.io/api/core/v1"
  17. meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/labels"
  19. api "k8s.io/kubernetes/pkg/apis/core"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  22. "k8s.io/utils/integer"
  23. )
  24. // EnsureLoggingAgentDeployment checks that logging agent is present on each
  25. // node and returns an error if that's not true.
  26. func EnsureLoggingAgentDeployment(f *framework.Framework, appName string) error {
  27. agentPods, err := getLoggingAgentPods(f, appName)
  28. if err != nil {
  29. return fmt.Errorf("failed to get logging agent pods: %v", err)
  30. }
  31. agentPerNode := make(map[string]int)
  32. for _, pod := range agentPods.Items {
  33. agentPerNode[pod.Spec.NodeName]++
  34. }
  35. nodeList := framework.GetReadySchedulableNodesOrDie(f.ClientSet)
  36. for _, node := range nodeList.Items {
  37. agentPodsCount, ok := agentPerNode[node.Name]
  38. if !ok {
  39. return fmt.Errorf("node %s doesn't have logging agents, want 1", node.Name)
  40. } else if agentPodsCount != 1 {
  41. return fmt.Errorf("node %s has %d logging agents, want 1", node.Name, agentPodsCount)
  42. }
  43. }
  44. return nil
  45. }
  46. // EnsureLoggingAgentRestartsCount checks that each logging agent was restarted
  47. // no more than maxRestarts times and returns an error if there's a pod which
  48. // exceeds this number of restarts.
  49. func EnsureLoggingAgentRestartsCount(f *framework.Framework, appName string, maxRestarts int) error {
  50. agentPods, err := getLoggingAgentPods(f, appName)
  51. if err != nil {
  52. return fmt.Errorf("failed to get logging agent pods: %v", err)
  53. }
  54. maxRestartCount := 0
  55. for _, pod := range agentPods.Items {
  56. contStatuses := pod.Status.ContainerStatuses
  57. if len(contStatuses) == 0 {
  58. e2elog.Logf("There are no container statuses for pod %s", pod.Name)
  59. continue
  60. }
  61. restartCount := int(contStatuses[0].RestartCount)
  62. maxRestartCount = integer.IntMax(maxRestartCount, restartCount)
  63. e2elog.Logf("Logging agent %s on node %s was restarted %d times",
  64. pod.Name, pod.Spec.NodeName, restartCount)
  65. }
  66. if maxRestartCount > maxRestarts {
  67. return fmt.Errorf("max logging agent restarts was %d, which is more than allowed %d",
  68. maxRestartCount, maxRestarts)
  69. }
  70. return nil
  71. }
  72. func getLoggingAgentPods(f *framework.Framework, appName string) (*api_v1.PodList, error) {
  73. label := labels.SelectorFromSet(labels.Set(map[string]string{"k8s-app": appName}))
  74. options := meta_v1.ListOptions{LabelSelector: label.String()}
  75. return f.ClientSet.CoreV1().Pods(api.NamespaceSystem).List(options)
  76. }