driver_validators.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package preview
  2. import (
  3. "fmt"
  4. "github.com/mitchellh/mapstructure"
  5. "github.com/porter-dev/switchboard/pkg/types"
  6. "k8s.io/apimachinery/pkg/util/validation"
  7. )
  8. func commonValidator(resource *types.Resource) (*Source, *Target, error) {
  9. source := &Source{}
  10. err := mapstructure.Decode(resource.Source, source)
  11. if err != nil {
  12. return nil, nil, fmt.Errorf("for resource '%s': error parsing source: %w", resource.Name, err)
  13. }
  14. target := &Target{}
  15. err = mapstructure.Decode(resource.Target, target)
  16. if err != nil {
  17. return nil, nil, fmt.Errorf("for resource '%s': error parsing target: %w", resource.Name, err)
  18. }
  19. return source, target, nil
  20. }
  21. func deployDriverValidator(resource *types.Resource) error {
  22. source, _, err := commonValidator(resource)
  23. if err != nil {
  24. return err
  25. }
  26. if source.Name == "" {
  27. return fmt.Errorf("for resource '%s': source name cannot be empty", resource.Name)
  28. }
  29. if source.Repo == "" {
  30. if source.Name == "web" || source.Name == "worker" || source.Name == "job" {
  31. source.Repo = "https://charts.getporter.dev"
  32. } else {
  33. source.Repo = "https://chart-addons.getporter.dev"
  34. }
  35. }
  36. if source.Repo == "https://charts.getporter.dev" {
  37. appConfig := &ApplicationConfig{}
  38. err = mapstructure.Decode(resource.Config, appConfig)
  39. if err != nil {
  40. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  41. }
  42. if appConfig.Build.Method == "" {
  43. return fmt.Errorf("for resource '%s': build method cannot be empty", resource.Name)
  44. } else if appConfig.Build.Method != "docker" &&
  45. appConfig.Build.Method != "pack" &&
  46. appConfig.Build.Method != "registry" {
  47. return fmt.Errorf("for resource '%s': build method must be one of 'docker', 'pack', or 'registry'", resource.Name)
  48. }
  49. if appConfig.Build.Method == "docker" && appConfig.Build.Dockerfile == "" {
  50. return fmt.Errorf("for resource '%s': dockerfile cannot be empty when using the 'docker' build method",
  51. resource.Name)
  52. } else if appConfig.Build.Method == "registry" && appConfig.Build.Image == "" {
  53. return fmt.Errorf("for resource '%s': image cannot be empty when using the 'registry' build method",
  54. resource.Name)
  55. }
  56. for _, eg := range appConfig.EnvGroups {
  57. if errStrs := validation.IsDNS1123Label(eg.Name); len(errStrs) > 0 {
  58. str := fmt.Sprintf("for resource '%s': invalid characters found in env group '%s' name:",
  59. resource.Name, eg.Name)
  60. for _, errStr := range errStrs {
  61. str += fmt.Sprintf("\n * %s", errStr)
  62. }
  63. return fmt.Errorf("%s", str)
  64. }
  65. }
  66. if len(appConfig.Values) > 0 {
  67. if source.Name == "web" {
  68. err := validateWebChartValues(appConfig.Values)
  69. if err != nil {
  70. return fmt.Errorf("for resource '%s': error validating values for web deployment: %w",
  71. resource.Name, err)
  72. }
  73. } else if source.Name == "worker" {
  74. err := validateWorkerChartValues(appConfig.Values)
  75. if err != nil {
  76. return fmt.Errorf("for resource '%s': error validating values for worker deployment: %w",
  77. resource.Name, err)
  78. }
  79. } else if source.Name == "job" {
  80. err := validateJobChartValues(appConfig.Values)
  81. if err != nil {
  82. return fmt.Errorf("for resource '%s': error validating values for job deployment: %w",
  83. resource.Name, err)
  84. }
  85. }
  86. }
  87. }
  88. return nil
  89. }
  90. func buildImageDriverValidator(resource *types.Resource) error {
  91. _, target, err := commonValidator(resource)
  92. if err != nil {
  93. return err
  94. }
  95. if target.AppName == "" {
  96. return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
  97. } else {
  98. errStrs := validation.IsDNS1123Label(target.AppName)
  99. if len(errStrs) > 0 {
  100. str := fmt.Sprintf("for resource '%s': invalid characters found in app_name:", resource.Name)
  101. for _, errStr := range errStrs {
  102. str += fmt.Sprintf("\n * %s", errStr)
  103. }
  104. return fmt.Errorf("%s", str)
  105. }
  106. }
  107. driverConfig := &BuildDriverConfig{}
  108. err = mapstructure.Decode(resource.Config, driverConfig)
  109. if err != nil {
  110. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  111. }
  112. if driverConfig.Build.Method == "" {
  113. return fmt.Errorf("for resource '%s': build method cannot be empty", resource.Name)
  114. } else if driverConfig.Build.Method != "docker" &&
  115. driverConfig.Build.Method != "pack" &&
  116. driverConfig.Build.Method != "registry" {
  117. return fmt.Errorf("for resource '%s': build method must be one of 'docker', 'pack', or 'registry'", resource.Name)
  118. }
  119. if driverConfig.Build.Method == "docker" && driverConfig.Build.Dockerfile == "" {
  120. return fmt.Errorf("for resource '%s': dockerfile cannot be empty when using the 'docker' build method",
  121. resource.Name)
  122. } else if driverConfig.Build.Method == "registry" && driverConfig.Build.Image == "" {
  123. return fmt.Errorf("for resource '%s': image cannot be empty when using the 'registry' build method",
  124. resource.Name)
  125. }
  126. for _, eg := range driverConfig.EnvGroups {
  127. if errStrs := validation.IsDNS1123Label(eg.Name); len(errStrs) > 0 {
  128. str := fmt.Sprintf("for resource '%s': invalid characters found in env group '%s' name:",
  129. resource.Name, eg.Name)
  130. for _, errStr := range errStrs {
  131. str += fmt.Sprintf("\n * %s", errStr)
  132. }
  133. return fmt.Errorf("%s", str)
  134. }
  135. }
  136. return nil
  137. }
  138. func pushImageDriverValidator(resource *types.Resource) error {
  139. _, target, err := commonValidator(resource)
  140. if err != nil {
  141. return err
  142. }
  143. if target.AppName == "" {
  144. return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
  145. } else {
  146. errStrs := validation.IsDNS1123Label(target.AppName)
  147. if len(errStrs) > 0 {
  148. str := fmt.Sprintf("for resource '%s': invalid characters found in app_name:", resource.Name)
  149. for _, errStr := range errStrs {
  150. str += fmt.Sprintf("\n * %s", errStr)
  151. }
  152. return fmt.Errorf("%s", str)
  153. }
  154. }
  155. driverConfig := &PushDriverConfig{}
  156. err = mapstructure.Decode(resource.Config, driverConfig)
  157. if err != nil {
  158. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  159. }
  160. if driverConfig.Push.Image == "" {
  161. return fmt.Errorf("for resource '%s': image cannot be empty", resource.Name)
  162. }
  163. return nil
  164. }
  165. func updateConfigDriverValidator(resource *types.Resource) error {
  166. source, target, err := commonValidator(resource)
  167. if err != nil {
  168. return err
  169. }
  170. if target.AppName == "" {
  171. return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
  172. } else {
  173. errStrs := validation.IsDNS1123Label(target.AppName)
  174. if len(errStrs) > 0 {
  175. str := fmt.Sprintf("for resource '%s': invalid characters found in app_name:", resource.Name)
  176. for _, errStr := range errStrs {
  177. str += fmt.Sprintf("\n * %s", errStr)
  178. }
  179. return fmt.Errorf("%s", str)
  180. }
  181. }
  182. if source.Repo == "" {
  183. if source.Name == "web" || source.Name == "worker" || source.Name == "job" {
  184. source.Repo = "https://charts.getporter.dev"
  185. } else {
  186. source.Repo = "https://chart-addons.getporter.dev"
  187. }
  188. }
  189. driverConfig := &UpdateConfigDriverConfig{}
  190. err = mapstructure.Decode(resource.Config, driverConfig)
  191. if err != nil {
  192. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  193. }
  194. if driverConfig.UpdateConfig.Image == "" {
  195. return fmt.Errorf("for resource '%s': image cannot be empty", resource.Name)
  196. }
  197. for _, eg := range driverConfig.EnvGroups {
  198. if errStrs := validation.IsDNS1123Label(eg.Name); len(errStrs) > 0 {
  199. str := fmt.Sprintf("for resource '%s': invalid characters found in env group '%s' name:",
  200. resource.Name, eg.Name)
  201. for _, errStr := range errStrs {
  202. str += fmt.Sprintf("\n * %s", errStr)
  203. }
  204. return fmt.Errorf("%s", str)
  205. }
  206. }
  207. if len(driverConfig.Values) > 0 && source.Repo == "https://charts.getporter.dev" {
  208. if source.Name == "web" {
  209. err := validateWebChartValues(driverConfig.Values)
  210. if err != nil {
  211. return fmt.Errorf("for resource '%s': error validating values for web deployment: %w",
  212. resource.Name, err)
  213. }
  214. } else if source.Name == "worker" {
  215. err := validateWorkerChartValues(driverConfig.Values)
  216. if err != nil {
  217. return fmt.Errorf("for resource '%s': error validating values for worker deployment: %w",
  218. resource.Name, err)
  219. }
  220. } else if source.Name == "job" {
  221. err := validateJobChartValues(driverConfig.Values)
  222. if err != nil {
  223. return fmt.Errorf("for resource '%s': error validating values for job deployment: %w",
  224. resource.Name, err)
  225. }
  226. }
  227. }
  228. return nil
  229. }
  230. func randomStringDriverValidator(resource *types.Resource) error {
  231. driverConfig := &RandomStringDriverConfig{}
  232. err := mapstructure.Decode(resource.Config, driverConfig)
  233. if err != nil {
  234. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  235. }
  236. return nil
  237. }
  238. func envGroupDriverValidator(resource *types.Resource) error {
  239. target := &Target{}
  240. err := mapstructure.Decode(resource.Target, target)
  241. if err != nil {
  242. return fmt.Errorf("for resource '%s': error parsing target: %w", resource.Name, err)
  243. }
  244. config := &EnvGroupDriverConfig{}
  245. err = mapstructure.Decode(resource.Config, config)
  246. if err != nil {
  247. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  248. }
  249. for _, eg := range config.EnvGroups {
  250. if errStrs := validation.IsDNS1123Label(eg.Name); len(errStrs) > 0 {
  251. str := fmt.Sprintf("for resource '%s': invalid characters found in env group '%s' name:",
  252. resource.Name, eg.Name)
  253. for _, errStr := range errStrs {
  254. str += fmt.Sprintf("\n * %s", errStr)
  255. }
  256. return fmt.Errorf("%s", str)
  257. }
  258. }
  259. return nil
  260. }
  261. func osEnvDriverValidator(resource *types.Resource) error {
  262. return nil
  263. }