logging_pod.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "strconv"
  16. "time"
  17. "fmt"
  18. api_v1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  23. imageutils "k8s.io/kubernetes/test/utils/image"
  24. )
  25. const (
  26. // Amount of requested cores for logging container in millicores
  27. loggingContainerCPURequest = 10
  28. // Amount of requested memory for logging container in bytes
  29. loggingContainerMemoryRequest = 10 * 1024 * 1024
  30. // Name of the container used for logging tests
  31. loggingContainerName = "logging-container"
  32. )
  33. // LoggingPod is an interface of a pod that can be started and that logs
  34. // something to its stdout, possibly indefinitely.
  35. type LoggingPod interface {
  36. // Name equals to the Kubernetes pod name.
  37. Name() string
  38. // Start method controls when the logging pod is started in the cluster.
  39. Start(f *framework.Framework) error
  40. }
  41. // StartAndReturnSelf is a helper method to start a logging pod and
  42. // immediately return it.
  43. func StartAndReturnSelf(p LoggingPod, f *framework.Framework) (LoggingPod, error) {
  44. err := p.Start(f)
  45. return p, err
  46. }
  47. // FiniteLoggingPod is a logging pod that emits a known number of log lines.
  48. type FiniteLoggingPod interface {
  49. LoggingPod
  50. // ExpectedLinesNumber returns the number of lines that are
  51. // expected to be ingested from this pod.
  52. ExpectedLineCount() int
  53. }
  54. var _ FiniteLoggingPod = &loadLoggingPod{}
  55. type loadLoggingPod struct {
  56. name string
  57. nodeName string
  58. expectedLinesCount int
  59. runDuration time.Duration
  60. }
  61. // NewLoadLoggingPod returns a logging pod that generates totalLines random
  62. // lines over period of length loggingDuration. Lines generated by this
  63. // pod are numbered and have well-defined structure.
  64. func NewLoadLoggingPod(podName string, nodeName string, totalLines int,
  65. loggingDuration time.Duration) FiniteLoggingPod {
  66. return &loadLoggingPod{
  67. name: podName,
  68. nodeName: nodeName,
  69. expectedLinesCount: totalLines,
  70. runDuration: loggingDuration,
  71. }
  72. }
  73. func (p *loadLoggingPod) Name() string {
  74. return p.name
  75. }
  76. func (p *loadLoggingPod) Start(f *framework.Framework) error {
  77. e2elog.Logf("Starting load logging pod %s", p.name)
  78. f.PodClient().Create(&api_v1.Pod{
  79. ObjectMeta: meta_v1.ObjectMeta{
  80. Name: p.name,
  81. },
  82. Spec: api_v1.PodSpec{
  83. RestartPolicy: api_v1.RestartPolicyNever,
  84. Containers: []api_v1.Container{
  85. {
  86. Name: loggingContainerName,
  87. Image: imageutils.GetE2EImage(imageutils.LogsGenerator),
  88. Env: []api_v1.EnvVar{
  89. {
  90. Name: "LOGS_GENERATOR_LINES_TOTAL",
  91. Value: strconv.Itoa(p.expectedLinesCount),
  92. },
  93. {
  94. Name: "LOGS_GENERATOR_DURATION",
  95. Value: p.runDuration.String(),
  96. },
  97. },
  98. Resources: api_v1.ResourceRequirements{
  99. Requests: api_v1.ResourceList{
  100. api_v1.ResourceCPU: *resource.NewMilliQuantity(
  101. loggingContainerCPURequest,
  102. resource.DecimalSI),
  103. api_v1.ResourceMemory: *resource.NewQuantity(
  104. loggingContainerMemoryRequest,
  105. resource.BinarySI),
  106. },
  107. },
  108. },
  109. },
  110. NodeName: p.nodeName,
  111. },
  112. })
  113. return framework.WaitForPodNameRunningInNamespace(f.ClientSet, p.name, f.Namespace.Name)
  114. }
  115. func (p *loadLoggingPod) ExpectedLineCount() int {
  116. return p.expectedLinesCount
  117. }
  118. // NewRepeatingLoggingPod returns a logging pod that each second prints
  119. // line value to its stdout.
  120. func NewRepeatingLoggingPod(podName string, line string) LoggingPod {
  121. cmd := []string{
  122. "/bin/sh",
  123. "-c",
  124. fmt.Sprintf("while :; do echo '%s'; sleep 1; done", line),
  125. }
  126. return NewExecLoggingPod(podName, cmd)
  127. }
  128. var _ LoggingPod = &execLoggingPod{}
  129. type execLoggingPod struct {
  130. name string
  131. cmd []string
  132. }
  133. // NewExecLoggingPod returns a logging pod that produces logs through
  134. // executing a command, passed in cmd.
  135. func NewExecLoggingPod(podName string, cmd []string) LoggingPod {
  136. return &execLoggingPod{
  137. name: podName,
  138. cmd: cmd,
  139. }
  140. }
  141. func (p *execLoggingPod) Name() string {
  142. return p.name
  143. }
  144. func (p *execLoggingPod) Start(f *framework.Framework) error {
  145. e2elog.Logf("Starting repeating logging pod %s", p.name)
  146. f.PodClient().Create(&api_v1.Pod{
  147. ObjectMeta: meta_v1.ObjectMeta{
  148. Name: p.name,
  149. },
  150. Spec: api_v1.PodSpec{
  151. Containers: []api_v1.Container{
  152. {
  153. Name: loggingContainerName,
  154. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  155. Command: p.cmd,
  156. Resources: api_v1.ResourceRequirements{
  157. Requests: api_v1.ResourceList{
  158. api_v1.ResourceCPU: *resource.NewMilliQuantity(
  159. loggingContainerCPURequest,
  160. resource.DecimalSI),
  161. api_v1.ResourceMemory: *resource.NewQuantity(
  162. loggingContainerMemoryRequest,
  163. resource.BinarySI),
  164. },
  165. },
  166. },
  167. },
  168. },
  169. })
  170. return framework.WaitForPodNameRunningInNamespace(f.ClientSet, p.name, f.Namespace.Name)
  171. }