logging_agent.go 3.1 KB

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