controller.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. Copyright 2016 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 resconsumerctrl
  14. import (
  15. "fmt"
  16. "log"
  17. "net/http"
  18. "net/url"
  19. "strconv"
  20. "sync"
  21. "github.com/spf13/cobra"
  22. "k8s.io/kubernetes/test/images/resource-consumer/common"
  23. )
  24. // CmdResourceConsumerController is used by agnhost Cobra.
  25. var CmdResourceConsumerController = &cobra.Command{
  26. Use: "resource-consumer-controller",
  27. Short: "Starts a HTTP server that spreads requests around resource consumers",
  28. Long: "Starts a HTTP server that spreads requests around resource consumers. The HTTP server has the same endpoints and usage as the one spawned by the \"resource-consumer\" subcommand.",
  29. Args: cobra.MaximumNArgs(0),
  30. Run: main,
  31. }
  32. var (
  33. port int
  34. consumerPort int
  35. consumerServiceName string
  36. consumerServiceNamespace string
  37. )
  38. func init() {
  39. CmdResourceConsumerController.Flags().IntVar(&port, "port", 8080, "Port number.")
  40. CmdResourceConsumerController.Flags().IntVar(&consumerPort, "consumer-port", 8080, "Port number of consumers.")
  41. CmdResourceConsumerController.Flags().StringVar(&consumerServiceName, "consumer-service-name", "resource-consumer", "Name of service containing resource consumers.")
  42. CmdResourceConsumerController.Flags().StringVar(&consumerServiceNamespace, "consumer-service-namespace", "default", "Namespace of service containing resource consumers.")
  43. }
  44. func main(cmd *cobra.Command, args []string) {
  45. mgr := newController()
  46. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), mgr))
  47. }
  48. type controller struct {
  49. responseWriterLock sync.Mutex
  50. waitGroup sync.WaitGroup
  51. }
  52. func newController() *controller {
  53. c := &controller{}
  54. return c
  55. }
  56. func (c *controller) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  57. if req.Method != "POST" {
  58. http.Error(w, common.BadRequest, http.StatusBadRequest)
  59. return
  60. }
  61. // parsing POST request data and URL data
  62. if err := req.ParseForm(); err != nil {
  63. http.Error(w, err.Error(), http.StatusBadRequest)
  64. return
  65. }
  66. // handle consumeCPU
  67. if req.URL.Path == common.ConsumeCPUAddress {
  68. c.handleConsumeCPU(w, req.Form)
  69. return
  70. }
  71. // handle consumeMem
  72. if req.URL.Path == common.ConsumeMemAddress {
  73. c.handleConsumeMem(w, req.Form)
  74. return
  75. }
  76. // handle bumpMetric
  77. if req.URL.Path == common.BumpMetricAddress {
  78. c.handleBumpMetric(w, req.Form)
  79. return
  80. }
  81. http.Error(w, common.UnknownFunction, http.StatusNotFound)
  82. }
  83. func (c *controller) handleConsumeCPU(w http.ResponseWriter, query url.Values) {
  84. // getting string data for consumeCPU
  85. durationSecString := query.Get(common.DurationSecQuery)
  86. millicoresString := query.Get(common.MillicoresQuery)
  87. requestSizeInMillicoresString := query.Get(common.RequestSizeInMillicoresQuery)
  88. if durationSecString == "" || millicoresString == "" || requestSizeInMillicoresString == "" {
  89. http.Error(w, common.NotGivenFunctionArgument, http.StatusBadRequest)
  90. return
  91. }
  92. // convert data (strings to ints) for consumeCPU
  93. durationSec, durationSecError := strconv.Atoi(durationSecString)
  94. millicores, millicoresError := strconv.Atoi(millicoresString)
  95. requestSizeInMillicores, requestSizeInMillicoresError := strconv.Atoi(requestSizeInMillicoresString)
  96. if durationSecError != nil || millicoresError != nil || requestSizeInMillicoresError != nil || requestSizeInMillicores <= 0 {
  97. http.Error(w, common.IncorrectFunctionArgument, http.StatusBadRequest)
  98. return
  99. }
  100. count := millicores / requestSizeInMillicores
  101. rest := millicores - count*requestSizeInMillicores
  102. fmt.Fprintf(w, "RC manager: sending %v requests to consume %v millicores each and 1 request to consume %v millicores\n", count, requestSizeInMillicores, rest)
  103. if count > 0 {
  104. c.waitGroup.Add(count)
  105. c.sendConsumeCPURequests(w, count, requestSizeInMillicores, durationSec)
  106. }
  107. if rest > 0 {
  108. c.waitGroup.Add(1)
  109. go c.sendOneConsumeCPURequest(w, rest, durationSec)
  110. }
  111. c.waitGroup.Wait()
  112. }
  113. func (c *controller) handleConsumeMem(w http.ResponseWriter, query url.Values) {
  114. // getting string data for consumeMem
  115. durationSecString := query.Get(common.DurationSecQuery)
  116. megabytesString := query.Get(common.MegabytesQuery)
  117. requestSizeInMegabytesString := query.Get(common.RequestSizeInMegabytesQuery)
  118. if durationSecString == "" || megabytesString == "" || requestSizeInMegabytesString == "" {
  119. http.Error(w, common.NotGivenFunctionArgument, http.StatusBadRequest)
  120. return
  121. }
  122. // convert data (strings to ints) for consumeMem
  123. durationSec, durationSecError := strconv.Atoi(durationSecString)
  124. megabytes, megabytesError := strconv.Atoi(megabytesString)
  125. requestSizeInMegabytes, requestSizeInMegabytesError := strconv.Atoi(requestSizeInMegabytesString)
  126. if durationSecError != nil || megabytesError != nil || requestSizeInMegabytesError != nil || requestSizeInMegabytes <= 0 {
  127. http.Error(w, common.IncorrectFunctionArgument, http.StatusBadRequest)
  128. return
  129. }
  130. count := megabytes / requestSizeInMegabytes
  131. rest := megabytes - count*requestSizeInMegabytes
  132. fmt.Fprintf(w, "RC manager: sending %v requests to consume %v MB each and 1 request to consume %v MB\n", count, requestSizeInMegabytes, rest)
  133. if count > 0 {
  134. c.waitGroup.Add(count)
  135. c.sendConsumeMemRequests(w, count, requestSizeInMegabytes, durationSec)
  136. }
  137. if rest > 0 {
  138. c.waitGroup.Add(1)
  139. go c.sendOneConsumeMemRequest(w, rest, durationSec)
  140. }
  141. c.waitGroup.Wait()
  142. }
  143. func (c *controller) handleBumpMetric(w http.ResponseWriter, query url.Values) {
  144. // getting string data for handleBumpMetric
  145. metric := query.Get(common.MetricNameQuery)
  146. deltaString := query.Get(common.DeltaQuery)
  147. durationSecString := query.Get(common.DurationSecQuery)
  148. requestSizeCustomMetricString := query.Get(common.RequestSizeCustomMetricQuery)
  149. if durationSecString == "" || metric == "" || deltaString == "" || requestSizeCustomMetricString == "" {
  150. http.Error(w, common.NotGivenFunctionArgument, http.StatusBadRequest)
  151. return
  152. }
  153. // convert data (strings to ints/floats) for handleBumpMetric
  154. durationSec, durationSecError := strconv.Atoi(durationSecString)
  155. delta, deltaError := strconv.Atoi(deltaString)
  156. requestSizeCustomMetric, requestSizeCustomMetricError := strconv.Atoi(requestSizeCustomMetricString)
  157. if durationSecError != nil || deltaError != nil || requestSizeCustomMetricError != nil || requestSizeCustomMetric <= 0 {
  158. http.Error(w, common.IncorrectFunctionArgument, http.StatusBadRequest)
  159. return
  160. }
  161. count := delta / requestSizeCustomMetric
  162. rest := delta - count*requestSizeCustomMetric
  163. fmt.Fprintf(w, "RC manager: sending %v requests to bump custom metric by %v each and 1 request to bump by %v\n", count, requestSizeCustomMetric, rest)
  164. if count > 0 {
  165. c.waitGroup.Add(count)
  166. c.sendConsumeCustomMetric(w, metric, count, requestSizeCustomMetric, durationSec)
  167. }
  168. if rest > 0 {
  169. c.waitGroup.Add(1)
  170. go c.sendOneConsumeCustomMetric(w, metric, rest, durationSec)
  171. }
  172. c.waitGroup.Wait()
  173. }
  174. func (c *controller) sendConsumeCPURequests(w http.ResponseWriter, requests, millicores, durationSec int) {
  175. for i := 0; i < requests; i++ {
  176. go c.sendOneConsumeCPURequest(w, millicores, durationSec)
  177. }
  178. }
  179. func (c *controller) sendConsumeMemRequests(w http.ResponseWriter, requests, megabytes, durationSec int) {
  180. for i := 0; i < requests; i++ {
  181. go c.sendOneConsumeMemRequest(w, megabytes, durationSec)
  182. }
  183. }
  184. func (c *controller) sendConsumeCustomMetric(w http.ResponseWriter, metric string, requests, delta, durationSec int) {
  185. for i := 0; i < requests; i++ {
  186. go c.sendOneConsumeCustomMetric(w, metric, delta, durationSec)
  187. }
  188. }
  189. func createConsumerURL(suffix string) string {
  190. return fmt.Sprintf("http://%s.%s.svc.cluster.local:%d%s", consumerServiceName, consumerServiceNamespace, consumerPort, suffix)
  191. }
  192. // sendOneConsumeCPURequest sends POST request for cpu consumption
  193. func (c *controller) sendOneConsumeCPURequest(w http.ResponseWriter, millicores int, durationSec int) {
  194. defer c.waitGroup.Done()
  195. query := createConsumerURL(common.ConsumeCPUAddress)
  196. _, err := http.PostForm(query, url.Values{common.MillicoresQuery: {strconv.Itoa(millicores)}, common.DurationSecQuery: {strconv.Itoa(durationSec)}})
  197. c.responseWriterLock.Lock()
  198. defer c.responseWriterLock.Unlock()
  199. if err != nil {
  200. fmt.Fprintf(w, "Failed to connect to consumer: %v\n", err)
  201. return
  202. }
  203. fmt.Fprintf(w, "Consumed %d millicores\n", millicores)
  204. }
  205. // sendOneConsumeMemRequest sends POST request for memory consumption
  206. func (c *controller) sendOneConsumeMemRequest(w http.ResponseWriter, megabytes int, durationSec int) {
  207. defer c.waitGroup.Done()
  208. query := createConsumerURL(common.ConsumeMemAddress)
  209. _, err := http.PostForm(query, url.Values{common.MegabytesQuery: {strconv.Itoa(megabytes)}, common.DurationSecQuery: {strconv.Itoa(durationSec)}})
  210. c.responseWriterLock.Lock()
  211. defer c.responseWriterLock.Unlock()
  212. if err != nil {
  213. fmt.Fprintf(w, "Failed to connect to consumer: %v\n", err)
  214. return
  215. }
  216. fmt.Fprintf(w, "Consumed %d megabytes\n", megabytes)
  217. }
  218. // sendOneConsumeCustomMetric sends POST request for custom metric consumption
  219. func (c *controller) sendOneConsumeCustomMetric(w http.ResponseWriter, customMetricName string, delta int, durationSec int) {
  220. defer c.waitGroup.Done()
  221. query := createConsumerURL(common.BumpMetricAddress)
  222. _, err := http.PostForm(query,
  223. url.Values{common.MetricNameQuery: {customMetricName}, common.DurationSecQuery: {strconv.Itoa(durationSec)}, common.DeltaQuery: {strconv.Itoa(delta)}})
  224. c.responseWriterLock.Lock()
  225. defer c.responseWriterLock.Unlock()
  226. if err != nil {
  227. fmt.Fprintf(w, "Failed to connect to consumer: %v\n", err)
  228. return
  229. }
  230. fmt.Fprintf(w, "Bumped metric %s by %d\n", customMetricName, delta)
  231. }