Guest User

Untitled

a guest
Mar 11th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. package model
  2.  
  3. import (
  4. "strings"
  5.  
  6. "k8s.io/api/core/v1"
  7. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  8. )
  9.  
  10. // WARNING: If you add a config value, make sure to
  11. // add it to the parameterize function as well !
  12. type ProtoformConfig struct {
  13.  
  14. // CONTAINER CONFIGS
  15. // These are sed replaced into the config maps for the containers.
  16. PerceptorHost string
  17. PerceptorPort int
  18. AnnotationIntervalSeconds int
  19. DumpIntervalMinutes int
  20. HubHost string
  21. HubUser string
  22. HubUserPassword string
  23. HubPort int
  24.  
  25. // AUTH CONFIGS
  26. // These are given to containers through secrets or other mechanisms.
  27. // Not necessarily a one-to-one text replacement.
  28. // TODO Lets try to have this injected on serviceaccount
  29. // at pod startup, eventually Service accounts.
  30. DockerPasswordOrToken string
  31. DockerUsername string
  32.  
  33. ServiceAccounts map[string]string
  34. }
  35.  
  36. func (p *ProtoformConfig) parameterize(json string) string {
  37. n := 1000
  38. if p.PerceptorHost == "" {
  39. p.PerceptorHost = "perceptor"
  40. }
  41. if p.PerceptorPort == 0 {
  42. p.PerceptorPort = 3001
  43. }
  44. if p.AnnotationIntervalSeconds == 0 {
  45. p.AnnotationIntervalSeconds = 30
  46. }
  47. if p.DumpIntervalMinutes == 0 {
  48. p.DumpIntervalMinutes = 30
  49. }
  50. if p.HubHost == "" {
  51. // meaningless default unless your in same namespace as hub.
  52. p.HubHost = "nginx-webapp-logstash"
  53. }
  54. if p.HubUser == "" {
  55. p.HubUser = "sysadmin"
  56. }
  57. if p.HubUserPassword == "" {
  58. panic("config failing: cannot continue without a hub password!!!")
  59. }
  60. if p.HubPort == 0 {
  61. p.HubPort = 443
  62. }
  63. json = strings.Replace(json, "_1", p.PerceptorHost, n)
  64. json = strings.Replace(json, "_2", string(p.PerceptorPort), n)
  65. json = strings.Replace(json, "_3", string(p.AnnotationIntervalSeconds), n)
  66. json = strings.Replace(json, "_4", string(p.DumpIntervalMinutes), n)
  67. json = strings.Replace(json, "_5", p.HubHost, n)
  68. json = strings.Replace(json, "_6", p.HubUser, n)
  69. json = strings.Replace(json, "_7", p.HubUserPassword, n)
  70. json = strings.Replace(json, "_8", string(p.HubPort), n)
  71.  
  72. return json
  73. }
  74.  
  75. // prometheus.yml
  76. func (p *ProtoformConfig) ToConfigMap() []*v1.ConfigMap {
  77.  
  78. // TODO, parameterize prometheus
  79. // strings.Replace(prometheus_t,
  80. configs := map[string]string{
  81. "prometheus": "prometheus.yml",
  82. "perceptor-scanner-config": "perceptor_scanner_conf.yaml",
  83. "kube-generic-perceiver-config": "perceiver.yaml",
  84. "perceptor-config": "perceptor_conf.yaml",
  85. "openshift-perceiver-config": "perceiver.yaml",
  86. }
  87.  
  88. // Sed replace these. Fine grained control over the json default format
  89. // makes this easier then actually modelling / mutating nested json in golang.
  90. // (I think)? Due to the fct that nested anonymous []string's seem to not be
  91. // "a thing".
  92. defaults := map[string]string{
  93. "prometheus": `{
  94. "global":{"scrape_interval":"5s"},
  95. "scrape_configs":[
  96. {"job_name":"perceptor-scrape",
  97. "scrape_interval":"5s",
  98. "static_configs":[{"targets":["perceptor:3001","perceptor-scanner:3003"]}
  99. ]}]
  100. }`,
  101. "perceptor-scanner-config": `{
  102. "HubHost": "_5",
  103. "HubPort": "_8"
  104. "HubUser": "_6",
  105. "HubUserPassword": "_7"
  106. }`,
  107. "kube-generic-perceiver-config": `{
  108. "PerceptorHost": "_1",
  109. "PerceptorPort": "_2",
  110. "AnnotationIntervalSeconds": "_3",
  111. "DumpIntervalMinutes": "_4"
  112. }`,
  113. "openshift_perceiver_config_t": `{
  114. "PerceptorHost": "_1",
  115. "PerceptorPort": "_2",
  116. "AnnotationIntervalSeconds": "_3",
  117. "DumpIntervalMinutes": "_4"
  118. }`,
  119. }
  120.  
  121. maps := make([]*v1.ConfigMap, len(configs))
  122. x := 0
  123. for config, filename := range configs {
  124. contents := p.parameterize(defaults[config])
  125. maps[x] = &v1.ConfigMap{
  126. ObjectMeta: metav1.ObjectMeta{
  127. Name: "prometheus",
  128. },
  129. Data: map[string]string{
  130. filename: contents,
  131. },
  132. }
  133. x = x + 1
  134. }
  135. return maps
  136. }
Add Comment
Please, Sign In to add comment