yaml.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. package v2
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. "gopkg.in/yaml.v2"
  10. )
  11. // AppProtoWithEnv is a struct containing a PorterApp proto object and its environment variables
  12. type AppProtoWithEnv struct {
  13. AppProto *porterv1.PorterApp
  14. Addons []*porterv1.Addon
  15. EnvVariables map[string]string
  16. }
  17. // AppWithPreviewOverrides is a porter app definition with its preview app definition, if it exists
  18. type AppWithPreviewOverrides struct {
  19. AppProtoWithEnv
  20. PreviewApp *AppProtoWithEnv
  21. }
  22. // AppProtoFromYaml converts a Porter YAML file into a PorterApp proto object
  23. func AppProtoFromYaml(ctx context.Context, porterYamlBytes []byte) (AppWithPreviewOverrides, error) {
  24. ctx, span := telemetry.NewSpan(ctx, "v2-app-proto-from-yaml")
  25. defer span.End()
  26. var out AppWithPreviewOverrides
  27. if porterYamlBytes == nil {
  28. return out, telemetry.Error(ctx, span, nil, "porter yaml is nil")
  29. }
  30. porterYaml := &PorterYAML{}
  31. err := yaml.Unmarshal(porterYamlBytes, porterYaml)
  32. if err != nil {
  33. return out, telemetry.Error(ctx, span, err, "error unmarshaling porter yaml")
  34. }
  35. appProto, envVariables, err := ProtoFromApp(ctx, porterYaml.PorterApp)
  36. if err != nil {
  37. return out, telemetry.Error(ctx, span, err, "error converting porter yaml to proto")
  38. }
  39. out.AppProto = appProto
  40. out.EnvVariables = envVariables
  41. var addons []*porterv1.Addon
  42. for _, addon := range porterYaml.Addons {
  43. addonProto, err := ProtoFromAddon(ctx, addon)
  44. if err != nil {
  45. return out, telemetry.Error(ctx, span, err, "error converting addon to proto")
  46. }
  47. addons = append(addons, addonProto)
  48. }
  49. out.Addons = addons
  50. if porterYaml.Previews != nil {
  51. previewConfig := *porterYaml.Previews
  52. previewAppProto, previewEnvVariables, err := ProtoFromApp(ctx, previewConfig.PorterApp)
  53. if err != nil {
  54. return out, telemetry.Error(ctx, span, err, "error converting preview porter yaml to proto")
  55. }
  56. out.PreviewApp = &AppProtoWithEnv{
  57. AppProto: previewAppProto,
  58. EnvVariables: previewEnvVariables,
  59. }
  60. var previewAddons []*porterv1.Addon
  61. for _, addon := range previewConfig.Addons {
  62. addonProto, err := ProtoFromAddon(ctx, addon)
  63. if err != nil {
  64. return out, telemetry.Error(ctx, span, err, "error converting preview addon to proto")
  65. }
  66. previewAddons = append(previewAddons, addonProto)
  67. }
  68. out.PreviewApp.Addons = previewAddons
  69. }
  70. return out, nil
  71. }
  72. // ServiceType is the type of a service in a Porter YAML file
  73. type ServiceType string
  74. const (
  75. // ServiceType_Web is type for web services specified in Porter YAML
  76. ServiceType_Web ServiceType = "web"
  77. // ServiceType_Worker is type for worker services specified in Porter YAML
  78. ServiceType_Worker ServiceType = "worker"
  79. // ServiceType_Job is type for job services specified in Porter YAML
  80. ServiceType_Job ServiceType = "job"
  81. )
  82. // PorterApp represents all the possible fields in a Porter YAML file
  83. type PorterApp struct {
  84. Version string `yaml:"version,omitempty"`
  85. Name string `yaml:"name"`
  86. Services []Service `yaml:"services"`
  87. Image *Image `yaml:"image,omitempty"`
  88. Build *Build `yaml:"build,omitempty"`
  89. Env Env `yaml:"env,omitempty"`
  90. Predeploy *Service `yaml:"predeploy,omitempty"`
  91. EnvGroups []string `yaml:"envGroups,omitempty"`
  92. EfsStorage *EfsStorage `yaml:"efsStorage,omitempty"`
  93. RequiredApps []RequiredApp `yaml:"requiredApps,omitempty"`
  94. }
  95. // PorterAppWithAddons is the definition of a porter app in a Porter YAML file with addons
  96. type PorterAppWithAddons struct {
  97. PorterApp `yaml:",inline"`
  98. Addons []Addon `yaml:"addons,omitempty"`
  99. }
  100. // PorterYAML represents all the possible fields in a Porter YAML file
  101. type PorterYAML struct {
  102. PorterAppWithAddons `yaml:",inline"`
  103. Previews *PorterAppWithAddons `yaml:"previews,omitempty"`
  104. }
  105. // Addon represents an addon that should be installed alongside a Porter app
  106. type Addon struct {
  107. Name string `yaml:"name"`
  108. Type string `yaml:"type"`
  109. EnvGroups []string `yaml:"envGroups,omitempty"`
  110. CpuCores float32 `yaml:"cpuCores,omitempty"`
  111. RamMegabytes int `yaml:"ramMegabytes,omitempty"`
  112. StorageGigabytes float32 `yaml:"storageGigabytes,omitempty"`
  113. }
  114. // RequiredApp specifies another porter app that this app expects to be deployed alongside it
  115. type RequiredApp struct {
  116. Name string `yaml:"name"`
  117. FromTarget string `yaml:"fromTarget"`
  118. }
  119. // EfsStorage represents the EFS storage settings for a Porter app
  120. type EfsStorage struct {
  121. Enabled bool `yaml:"enabled"`
  122. }
  123. // Build represents the build settings for a Porter app
  124. type Build struct {
  125. Context string `yaml:"context,omitempty" validate:"dir"`
  126. Method string `yaml:"method,omitempty" validate:"required,oneof=pack docker registry"`
  127. Builder string `yaml:"builder,omitempty" validate:"required_if=Method pack"`
  128. Buildpacks []string `yaml:"buildpacks,omitempty"`
  129. Dockerfile string `yaml:"dockerfile,omitempty" validate:"required_if=Method docker"`
  130. CommitSHA string `yaml:"commitSha,omitempty"`
  131. }
  132. // Image is the repository and tag for an app's build image
  133. type Image struct {
  134. Repository string `yaml:"repository"`
  135. Tag string `yaml:"tag"`
  136. }
  137. // Service represents a single service in a porter app
  138. type Service struct {
  139. Name string `yaml:"name,omitempty"`
  140. Run *string `yaml:"run,omitempty"`
  141. Type ServiceType `yaml:"type,omitempty" validate:"required, oneof=web worker job"`
  142. Instances *int32 `yaml:"instances,omitempty"`
  143. CpuCores float32 `yaml:"cpuCores,omitempty"`
  144. RamMegabytes int `yaml:"ramMegabytes,omitempty"`
  145. GpuCoresNvidia float32 `yaml:"gpuCoresNvidia,omitempty"`
  146. GPU *GPU `yaml:"gpu,omitempty"`
  147. SmartOptimization *bool `yaml:"smartOptimization,omitempty"`
  148. TerminationGracePeriodSeconds *int32 `yaml:"terminationGracePeriodSeconds,omitempty"`
  149. Port int `yaml:"port,omitempty"`
  150. Autoscaling *AutoScaling `yaml:"autoscaling,omitempty" validate:"excluded_if=Type job"`
  151. Domains []Domains `yaml:"domains,omitempty" validate:"excluded_unless=Type web"`
  152. HealthCheck *HealthCheck `yaml:"healthCheck,omitempty" validate:"excluded_unless=Type web"`
  153. AllowConcurrent *bool `yaml:"allowConcurrent,omitempty" validate:"excluded_unless=Type job"`
  154. Cron string `yaml:"cron,omitempty" validate:"excluded_unless=Type job"`
  155. SuspendCron *bool `yaml:"suspendCron,omitempty" validate:"excluded_unless=Type job"`
  156. TimeoutSeconds int `yaml:"timeoutSeconds,omitempty" validate:"excluded_unless=Type job"`
  157. Private *bool `yaml:"private,omitempty" validate:"excluded_unless=Type web"`
  158. IngressAnnotations map[string]string `yaml:"ingressAnnotations,omitempty" validate:"excluded_unless=Type web"`
  159. DisableTLS *bool `yaml:"disableTLS,omitempty" validate:"excluded_unless=Type web"`
  160. }
  161. // AutoScaling represents the autoscaling settings for web services
  162. type AutoScaling struct {
  163. Enabled bool `yaml:"enabled"`
  164. MinInstances int `yaml:"minInstances"`
  165. MaxInstances int `yaml:"maxInstances"`
  166. CpuThresholdPercent int `yaml:"cpuThresholdPercent"`
  167. MemoryThresholdPercent int `yaml:"memoryThresholdPercent"`
  168. }
  169. // GPU represents GPU settings for a service
  170. type GPU struct {
  171. Enabled bool `yaml:"enabled"`
  172. GpuCoresNvidia int `yaml:"gpuCoresNvidia"`
  173. }
  174. // Domains are the custom domains for a web service
  175. type Domains struct {
  176. Name string `yaml:"name"`
  177. }
  178. // HealthCheck is the health check settings for a web service
  179. type HealthCheck struct {
  180. Enabled bool `yaml:"enabled"`
  181. HttpPath string `yaml:"httpPath"`
  182. }
  183. // ProtoFromApp converts a PorterApp type to a base PorterApp proto type and returns env variables
  184. func ProtoFromApp(ctx context.Context, porterApp PorterApp) (*porterv1.PorterApp, map[string]string, error) {
  185. ctx, span := telemetry.NewSpan(ctx, "build-app-proto")
  186. defer span.End()
  187. appProto := &porterv1.PorterApp{
  188. Name: porterApp.Name,
  189. }
  190. if porterApp.Build != nil {
  191. appProto.Build = &porterv1.Build{
  192. Context: porterApp.Build.Context,
  193. Method: porterApp.Build.Method,
  194. Builder: porterApp.Build.Builder,
  195. Buildpacks: porterApp.Build.Buildpacks,
  196. Dockerfile: porterApp.Build.Dockerfile,
  197. CommitSha: porterApp.Build.CommitSHA,
  198. }
  199. }
  200. if porterApp.Image != nil {
  201. appProto.Image = &porterv1.AppImage{
  202. Repository: porterApp.Image.Repository,
  203. Tag: porterApp.Image.Tag,
  204. }
  205. }
  206. // service map is only needed for backwards compatibility at this time
  207. serviceMap := make(map[string]*porterv1.Service)
  208. var services []*porterv1.Service
  209. for _, service := range porterApp.Services {
  210. serviceType := protoEnumFromType(service.Name, service)
  211. serviceProto, err := serviceProtoFromConfig(service, serviceType)
  212. if err != nil {
  213. return appProto, nil, telemetry.Error(ctx, span, err, "error casting service config")
  214. }
  215. if service.Name == "" {
  216. return appProto, nil, telemetry.Error(ctx, span, nil, "service found with no name")
  217. }
  218. services = append(services, serviceProto)
  219. serviceMap[service.Name] = serviceProto
  220. }
  221. appProto.ServiceList = services
  222. appProto.Services = serviceMap // nolint:staticcheck // temporarily using deprecated field for backwards compatibility
  223. if porterApp.Predeploy != nil {
  224. predeployProto, err := serviceProtoFromConfig(*porterApp.Predeploy, porterv1.ServiceType_SERVICE_TYPE_JOB)
  225. if err != nil {
  226. return appProto, nil, telemetry.Error(ctx, span, err, "error casting predeploy config")
  227. }
  228. appProto.Predeploy = predeployProto
  229. }
  230. for _, envGroup := range porterApp.EnvGroups {
  231. appProto.EnvGroups = append(appProto.EnvGroups, &porterv1.EnvGroup{
  232. Name: envGroup,
  233. Version: 0, // this will be updated to latest when applied
  234. })
  235. }
  236. if porterApp.EfsStorage != nil {
  237. appProto.EfsStorage = &porterv1.EFS{
  238. Enabled: porterApp.EfsStorage.Enabled,
  239. }
  240. }
  241. for _, requiredApp := range porterApp.RequiredApps {
  242. var targetIdentifier *porterv1.DeploymentTargetIdentifier
  243. if requiredApp.Name == "" {
  244. return appProto, nil, telemetry.Error(ctx, span, nil, "required app specified with no name")
  245. }
  246. if requiredApp.FromTarget != "" {
  247. targetIdentifier = &porterv1.DeploymentTargetIdentifier{
  248. Name: requiredApp.FromTarget,
  249. }
  250. }
  251. appProto.RequiredApps = append(appProto.RequiredApps, &porterv1.RequiredApp{
  252. Name: requiredApp.Name,
  253. FromTarget: targetIdentifier,
  254. })
  255. }
  256. envMap := make(map[string]string)
  257. var envVariables []*porterv1.EnvVariable
  258. for _, envVar := range porterApp.Env {
  259. switch envVar.Source {
  260. case EnvVariableSource_Value:
  261. if !envVar.Value.IsSet {
  262. return appProto, nil, telemetry.Error(ctx, span, nil, "no value set for env variable")
  263. }
  264. envMap[envVar.Key] = envVar.Value.Value
  265. case EnvVariableSource_FromApp:
  266. if !envVar.FromApp.IsSet {
  267. return appProto, nil, telemetry.Error(ctx, span, nil, "no value set for env variable")
  268. }
  269. fromApp, err := EnvVarFromAppToProto(envVar.FromApp.Value)
  270. if err != nil {
  271. return appProto, nil, telemetry.Error(ctx, span, err, "error converting env variable from app to proto")
  272. }
  273. envVariables = append(envVariables, &porterv1.EnvVariable{
  274. Key: envVar.Key,
  275. Source: porterv1.EnvVariableSource_ENV_VARIABLE_SOURCE_FROM_APP,
  276. Definition: &porterv1.EnvVariable_FromApp{
  277. FromApp: fromApp,
  278. },
  279. })
  280. default:
  281. return appProto, nil, telemetry.Error(ctx, span, nil, "invalid definition for env variable")
  282. }
  283. }
  284. appProto.Env = envVariables
  285. return appProto, envMap, nil
  286. }
  287. func protoEnumFromType(name string, service Service) porterv1.ServiceType {
  288. serviceType := porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED
  289. if strings.Contains(name, "web") {
  290. serviceType = porterv1.ServiceType_SERVICE_TYPE_WEB
  291. }
  292. if strings.Contains(name, "wkr") || strings.Contains(name, "worker") {
  293. serviceType = porterv1.ServiceType_SERVICE_TYPE_WORKER
  294. }
  295. if strings.Contains(name, "job") {
  296. serviceType = porterv1.ServiceType_SERVICE_TYPE_JOB
  297. }
  298. switch service.Type {
  299. case "web":
  300. serviceType = porterv1.ServiceType_SERVICE_TYPE_WEB
  301. case "worker":
  302. serviceType = porterv1.ServiceType_SERVICE_TYPE_WORKER
  303. case "job":
  304. serviceType = porterv1.ServiceType_SERVICE_TYPE_JOB
  305. }
  306. return serviceType
  307. }
  308. func serviceProtoFromConfig(service Service, serviceType porterv1.ServiceType) (*porterv1.Service, error) {
  309. serviceProto := &porterv1.Service{
  310. Name: service.Name,
  311. RunOptional: service.Run,
  312. InstancesOptional: service.Instances,
  313. CpuCores: service.CpuCores,
  314. RamMegabytes: int32(service.RamMegabytes),
  315. GpuCoresNvidia: service.GpuCoresNvidia,
  316. Port: int32(service.Port),
  317. SmartOptimization: service.SmartOptimization,
  318. Type: serviceType,
  319. TerminationGracePeriodSeconds: service.TerminationGracePeriodSeconds,
  320. }
  321. if service.GPU != nil {
  322. gpu := &porterv1.GPU{
  323. Enabled: service.GPU.Enabled,
  324. GpuCoresNvidia: int32(service.GPU.GpuCoresNvidia),
  325. }
  326. serviceProto.Gpu = gpu
  327. }
  328. switch serviceType {
  329. default:
  330. return nil, fmt.Errorf("invalid service type '%s'", serviceType)
  331. case porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED:
  332. return nil, errors.New("Service type unspecified")
  333. case porterv1.ServiceType_SERVICE_TYPE_WEB:
  334. webConfig := &porterv1.WebServiceConfig{}
  335. var autoscaling *porterv1.Autoscaling
  336. if service.Autoscaling != nil {
  337. autoscaling = &porterv1.Autoscaling{
  338. Enabled: service.Autoscaling.Enabled,
  339. MinInstances: int32(service.Autoscaling.MinInstances),
  340. MaxInstances: int32(service.Autoscaling.MaxInstances),
  341. CpuThresholdPercent: int32(service.Autoscaling.CpuThresholdPercent),
  342. MemoryThresholdPercent: int32(service.Autoscaling.MemoryThresholdPercent),
  343. }
  344. }
  345. webConfig.Autoscaling = autoscaling
  346. var healthCheck *porterv1.HealthCheck
  347. if service.HealthCheck != nil {
  348. healthCheck = &porterv1.HealthCheck{
  349. Enabled: service.HealthCheck.Enabled,
  350. HttpPath: service.HealthCheck.HttpPath,
  351. }
  352. }
  353. webConfig.HealthCheck = healthCheck
  354. domains := make([]*porterv1.Domain, 0)
  355. for _, domain := range service.Domains {
  356. domains = append(domains, &porterv1.Domain{
  357. Name: domain.Name,
  358. })
  359. }
  360. webConfig.Domains = domains
  361. webConfig.IngressAnnotations = service.IngressAnnotations
  362. if service.Private != nil {
  363. webConfig.Private = service.Private
  364. }
  365. if service.DisableTLS != nil {
  366. webConfig.DisableTls = service.DisableTLS
  367. }
  368. serviceProto.Config = &porterv1.Service_WebConfig{
  369. WebConfig: webConfig,
  370. }
  371. case porterv1.ServiceType_SERVICE_TYPE_WORKER:
  372. workerConfig := &porterv1.WorkerServiceConfig{}
  373. var autoscaling *porterv1.Autoscaling
  374. if service.Autoscaling != nil {
  375. autoscaling = &porterv1.Autoscaling{
  376. Enabled: service.Autoscaling.Enabled,
  377. MinInstances: int32(service.Autoscaling.MinInstances),
  378. MaxInstances: int32(service.Autoscaling.MaxInstances),
  379. CpuThresholdPercent: int32(service.Autoscaling.CpuThresholdPercent),
  380. MemoryThresholdPercent: int32(service.Autoscaling.MemoryThresholdPercent),
  381. }
  382. }
  383. workerConfig.Autoscaling = autoscaling
  384. serviceProto.Config = &porterv1.Service_WorkerConfig{
  385. WorkerConfig: workerConfig,
  386. }
  387. case porterv1.ServiceType_SERVICE_TYPE_JOB:
  388. jobConfig := &porterv1.JobServiceConfig{
  389. AllowConcurrentOptional: service.AllowConcurrent,
  390. Cron: service.Cron,
  391. }
  392. if service.SuspendCron != nil {
  393. jobConfig.SuspendCron = service.SuspendCron
  394. }
  395. if service.TimeoutSeconds != 0 {
  396. jobConfig.TimeoutSeconds = int64(service.TimeoutSeconds)
  397. }
  398. serviceProto.Config = &porterv1.Service_JobConfig{
  399. JobConfig: jobConfig,
  400. }
  401. }
  402. return serviceProto, nil
  403. }
  404. // AppFromProto converts a PorterApp proto object into a PorterApp struct
  405. func AppFromProto(appProto *porterv1.PorterApp) (PorterApp, error) {
  406. porterApp := PorterApp{
  407. Version: "v2",
  408. Name: appProto.Name,
  409. }
  410. if appProto.Build != nil {
  411. porterApp.Build = &Build{
  412. Context: appProto.Build.Context,
  413. Method: appProto.Build.Method,
  414. Builder: appProto.Build.Builder,
  415. Buildpacks: appProto.Build.Buildpacks,
  416. Dockerfile: appProto.Build.Dockerfile,
  417. CommitSHA: appProto.Build.CommitSha,
  418. }
  419. }
  420. if appProto.Image != nil {
  421. porterApp.Image = &Image{
  422. Repository: appProto.Image.Repository,
  423. Tag: appProto.Image.Tag,
  424. }
  425. }
  426. uniqueServices := uniqueServices(appProto.Services, appProto.ServiceList) // nolint:staticcheck // temporarily using deprecated field for backwards compatibility
  427. for _, service := range uniqueServices {
  428. appService, err := appServiceFromProto(service)
  429. if err != nil {
  430. return porterApp, err
  431. }
  432. porterApp.Services = append(porterApp.Services, appService)
  433. }
  434. if appProto.Predeploy != nil {
  435. appPredeploy, err := appServiceFromProto(appProto.Predeploy)
  436. if err != nil {
  437. return porterApp, err
  438. }
  439. porterApp.Predeploy = &appPredeploy
  440. }
  441. for _, envGroup := range appProto.EnvGroups {
  442. if envGroup != nil {
  443. porterApp.EnvGroups = append(porterApp.EnvGroups, fmt.Sprintf("%s:v%d", envGroup.Name, envGroup.Version))
  444. }
  445. }
  446. if appProto.EfsStorage != nil {
  447. porterApp.EfsStorage = &EfsStorage{
  448. Enabled: appProto.EfsStorage.Enabled,
  449. }
  450. }
  451. return porterApp, nil
  452. }
  453. func appServiceFromProto(service *porterv1.Service) (Service, error) {
  454. var gpu *GPU
  455. if service.Gpu != nil {
  456. gpu = &GPU{
  457. Enabled: service.Gpu.Enabled,
  458. GpuCoresNvidia: int(service.Gpu.GpuCoresNvidia),
  459. }
  460. }
  461. appService := Service{
  462. Name: service.Name,
  463. Run: service.RunOptional,
  464. Instances: service.InstancesOptional,
  465. CpuCores: service.CpuCores,
  466. RamMegabytes: int(service.RamMegabytes),
  467. GpuCoresNvidia: service.GpuCoresNvidia, // nolint:staticcheck // https://linear.app/porter/issue/POR-2137/support-new-gpu-field-in-porteryaml
  468. Port: int(service.Port),
  469. SmartOptimization: service.SmartOptimization,
  470. GPU: gpu,
  471. TerminationGracePeriodSeconds: service.TerminationGracePeriodSeconds,
  472. }
  473. switch service.Type {
  474. default:
  475. return appService, fmt.Errorf("invalid service type '%s'", service.Type)
  476. case porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED:
  477. return appService, errors.New("Service type unspecified")
  478. case porterv1.ServiceType_SERVICE_TYPE_WEB:
  479. webConfig := service.GetWebConfig()
  480. appService.Type = "web"
  481. var autoscaling *AutoScaling
  482. if webConfig.Autoscaling != nil {
  483. autoscaling = &AutoScaling{
  484. Enabled: webConfig.Autoscaling.Enabled,
  485. MinInstances: int(webConfig.Autoscaling.MinInstances),
  486. MaxInstances: int(webConfig.Autoscaling.MaxInstances),
  487. CpuThresholdPercent: int(webConfig.Autoscaling.CpuThresholdPercent),
  488. MemoryThresholdPercent: int(webConfig.Autoscaling.MemoryThresholdPercent),
  489. }
  490. }
  491. appService.Autoscaling = autoscaling
  492. var healthCheck *HealthCheck
  493. if webConfig.HealthCheck != nil {
  494. healthCheck = &HealthCheck{
  495. Enabled: webConfig.HealthCheck.Enabled,
  496. HttpPath: webConfig.HealthCheck.HttpPath,
  497. }
  498. }
  499. appService.HealthCheck = healthCheck
  500. domains := make([]Domains, 0)
  501. for _, domain := range webConfig.Domains {
  502. domains = append(domains, Domains{
  503. Name: domain.Name,
  504. })
  505. }
  506. appService.Domains = domains
  507. appService.IngressAnnotations = webConfig.IngressAnnotations
  508. if webConfig.Private != nil {
  509. appService.Private = webConfig.Private
  510. }
  511. if webConfig.DisableTls != nil {
  512. appService.DisableTLS = webConfig.DisableTls
  513. }
  514. case porterv1.ServiceType_SERVICE_TYPE_WORKER:
  515. workerConfig := service.GetWorkerConfig()
  516. appService.Type = "worker"
  517. var autoscaling *AutoScaling
  518. if workerConfig.Autoscaling != nil {
  519. autoscaling = &AutoScaling{
  520. Enabled: workerConfig.Autoscaling.Enabled,
  521. MinInstances: int(workerConfig.Autoscaling.MinInstances),
  522. MaxInstances: int(workerConfig.Autoscaling.MaxInstances),
  523. CpuThresholdPercent: int(workerConfig.Autoscaling.CpuThresholdPercent),
  524. MemoryThresholdPercent: int(workerConfig.Autoscaling.MemoryThresholdPercent),
  525. }
  526. }
  527. appService.Autoscaling = autoscaling
  528. case porterv1.ServiceType_SERVICE_TYPE_JOB:
  529. jobConfig := service.GetJobConfig()
  530. appService.Type = "job"
  531. appService.AllowConcurrent = jobConfig.AllowConcurrentOptional
  532. appService.Cron = jobConfig.Cron
  533. appService.SuspendCron = jobConfig.SuspendCron
  534. appService.TimeoutSeconds = int(jobConfig.TimeoutSeconds)
  535. }
  536. return appService, nil
  537. }
  538. func uniqueServices(serviceMap map[string]*porterv1.Service, serviceList []*porterv1.Service) []*porterv1.Service {
  539. if serviceList != nil {
  540. return serviceList
  541. }
  542. // deduplicate services by name, favoring whatever was defined first
  543. uniqueServices := make(map[string]*porterv1.Service)
  544. for name, service := range serviceMap {
  545. service.Name = name
  546. uniqueServices[service.Name] = service
  547. }
  548. mergedServiceList := make([]*porterv1.Service, 0)
  549. for _, service := range uniqueServices {
  550. mergedServiceList = append(mergedServiceList, service)
  551. }
  552. return mergedServiceList
  553. }