yaml.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 map[string]string `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. return appProto, porterApp.Env, nil
  257. }
  258. func protoEnumFromType(name string, service Service) porterv1.ServiceType {
  259. serviceType := porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED
  260. if strings.Contains(name, "web") {
  261. serviceType = porterv1.ServiceType_SERVICE_TYPE_WEB
  262. }
  263. if strings.Contains(name, "wkr") || strings.Contains(name, "worker") {
  264. serviceType = porterv1.ServiceType_SERVICE_TYPE_WORKER
  265. }
  266. if strings.Contains(name, "job") {
  267. serviceType = porterv1.ServiceType_SERVICE_TYPE_JOB
  268. }
  269. switch service.Type {
  270. case "web":
  271. serviceType = porterv1.ServiceType_SERVICE_TYPE_WEB
  272. case "worker":
  273. serviceType = porterv1.ServiceType_SERVICE_TYPE_WORKER
  274. case "job":
  275. serviceType = porterv1.ServiceType_SERVICE_TYPE_JOB
  276. }
  277. return serviceType
  278. }
  279. func serviceProtoFromConfig(service Service, serviceType porterv1.ServiceType) (*porterv1.Service, error) {
  280. serviceProto := &porterv1.Service{
  281. Name: service.Name,
  282. RunOptional: service.Run,
  283. InstancesOptional: service.Instances,
  284. CpuCores: service.CpuCores,
  285. RamMegabytes: int32(service.RamMegabytes),
  286. GpuCoresNvidia: service.GpuCoresNvidia,
  287. Port: int32(service.Port),
  288. SmartOptimization: service.SmartOptimization,
  289. Type: serviceType,
  290. TerminationGracePeriodSeconds: service.TerminationGracePeriodSeconds,
  291. }
  292. if service.GPU != nil {
  293. gpu := &porterv1.GPU{
  294. Enabled: service.GPU.Enabled,
  295. GpuCoresNvidia: int32(service.GPU.GpuCoresNvidia),
  296. }
  297. serviceProto.Gpu = gpu
  298. }
  299. switch serviceType {
  300. default:
  301. return nil, fmt.Errorf("invalid service type '%s'", serviceType)
  302. case porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED:
  303. return nil, errors.New("Service type unspecified")
  304. case porterv1.ServiceType_SERVICE_TYPE_WEB:
  305. webConfig := &porterv1.WebServiceConfig{}
  306. var autoscaling *porterv1.Autoscaling
  307. if service.Autoscaling != nil {
  308. autoscaling = &porterv1.Autoscaling{
  309. Enabled: service.Autoscaling.Enabled,
  310. MinInstances: int32(service.Autoscaling.MinInstances),
  311. MaxInstances: int32(service.Autoscaling.MaxInstances),
  312. CpuThresholdPercent: int32(service.Autoscaling.CpuThresholdPercent),
  313. MemoryThresholdPercent: int32(service.Autoscaling.MemoryThresholdPercent),
  314. }
  315. }
  316. webConfig.Autoscaling = autoscaling
  317. var healthCheck *porterv1.HealthCheck
  318. if service.HealthCheck != nil {
  319. healthCheck = &porterv1.HealthCheck{
  320. Enabled: service.HealthCheck.Enabled,
  321. HttpPath: service.HealthCheck.HttpPath,
  322. }
  323. }
  324. webConfig.HealthCheck = healthCheck
  325. domains := make([]*porterv1.Domain, 0)
  326. for _, domain := range service.Domains {
  327. domains = append(domains, &porterv1.Domain{
  328. Name: domain.Name,
  329. })
  330. }
  331. webConfig.Domains = domains
  332. webConfig.IngressAnnotations = service.IngressAnnotations
  333. if service.Private != nil {
  334. webConfig.Private = service.Private
  335. }
  336. if service.DisableTLS != nil {
  337. webConfig.DisableTls = service.DisableTLS
  338. }
  339. serviceProto.Config = &porterv1.Service_WebConfig{
  340. WebConfig: webConfig,
  341. }
  342. case porterv1.ServiceType_SERVICE_TYPE_WORKER:
  343. workerConfig := &porterv1.WorkerServiceConfig{}
  344. var autoscaling *porterv1.Autoscaling
  345. if service.Autoscaling != nil {
  346. autoscaling = &porterv1.Autoscaling{
  347. Enabled: service.Autoscaling.Enabled,
  348. MinInstances: int32(service.Autoscaling.MinInstances),
  349. MaxInstances: int32(service.Autoscaling.MaxInstances),
  350. CpuThresholdPercent: int32(service.Autoscaling.CpuThresholdPercent),
  351. MemoryThresholdPercent: int32(service.Autoscaling.MemoryThresholdPercent),
  352. }
  353. }
  354. workerConfig.Autoscaling = autoscaling
  355. serviceProto.Config = &porterv1.Service_WorkerConfig{
  356. WorkerConfig: workerConfig,
  357. }
  358. case porterv1.ServiceType_SERVICE_TYPE_JOB:
  359. jobConfig := &porterv1.JobServiceConfig{
  360. AllowConcurrentOptional: service.AllowConcurrent,
  361. Cron: service.Cron,
  362. }
  363. if service.SuspendCron != nil {
  364. jobConfig.SuspendCron = service.SuspendCron
  365. }
  366. if service.TimeoutSeconds != 0 {
  367. jobConfig.TimeoutSeconds = int64(service.TimeoutSeconds)
  368. }
  369. serviceProto.Config = &porterv1.Service_JobConfig{
  370. JobConfig: jobConfig,
  371. }
  372. }
  373. return serviceProto, nil
  374. }
  375. // AppFromProto converts a PorterApp proto object into a PorterApp struct
  376. func AppFromProto(appProto *porterv1.PorterApp) (PorterApp, error) {
  377. porterApp := PorterApp{
  378. Version: "v2",
  379. Name: appProto.Name,
  380. }
  381. if appProto.Build != nil {
  382. porterApp.Build = &Build{
  383. Context: appProto.Build.Context,
  384. Method: appProto.Build.Method,
  385. Builder: appProto.Build.Builder,
  386. Buildpacks: appProto.Build.Buildpacks,
  387. Dockerfile: appProto.Build.Dockerfile,
  388. CommitSHA: appProto.Build.CommitSha,
  389. }
  390. }
  391. if appProto.Image != nil {
  392. porterApp.Image = &Image{
  393. Repository: appProto.Image.Repository,
  394. Tag: appProto.Image.Tag,
  395. }
  396. }
  397. uniqueServices := uniqueServices(appProto.Services, appProto.ServiceList) // nolint:staticcheck // temporarily using deprecated field for backwards compatibility
  398. for _, service := range uniqueServices {
  399. appService, err := appServiceFromProto(service)
  400. if err != nil {
  401. return porterApp, err
  402. }
  403. porterApp.Services = append(porterApp.Services, appService)
  404. }
  405. if appProto.Predeploy != nil {
  406. appPredeploy, err := appServiceFromProto(appProto.Predeploy)
  407. if err != nil {
  408. return porterApp, err
  409. }
  410. porterApp.Predeploy = &appPredeploy
  411. }
  412. for _, envGroup := range appProto.EnvGroups {
  413. if envGroup != nil {
  414. porterApp.EnvGroups = append(porterApp.EnvGroups, fmt.Sprintf("%s:v%d", envGroup.Name, envGroup.Version))
  415. }
  416. }
  417. if appProto.EfsStorage != nil {
  418. porterApp.EfsStorage = &EfsStorage{
  419. Enabled: appProto.EfsStorage.Enabled,
  420. }
  421. }
  422. return porterApp, nil
  423. }
  424. func appServiceFromProto(service *porterv1.Service) (Service, error) {
  425. var gpu *GPU
  426. if service.Gpu != nil {
  427. gpu = &GPU{
  428. Enabled: service.Gpu.Enabled,
  429. GpuCoresNvidia: int(service.Gpu.GpuCoresNvidia),
  430. }
  431. }
  432. appService := Service{
  433. Name: service.Name,
  434. Run: service.RunOptional,
  435. Instances: service.InstancesOptional,
  436. CpuCores: service.CpuCores,
  437. RamMegabytes: int(service.RamMegabytes),
  438. GpuCoresNvidia: service.GpuCoresNvidia, // nolint:staticcheck // https://linear.app/porter/issue/POR-2137/support-new-gpu-field-in-porteryaml
  439. Port: int(service.Port),
  440. SmartOptimization: service.SmartOptimization,
  441. GPU: gpu,
  442. TerminationGracePeriodSeconds: service.TerminationGracePeriodSeconds,
  443. }
  444. switch service.Type {
  445. default:
  446. return appService, fmt.Errorf("invalid service type '%s'", service.Type)
  447. case porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED:
  448. return appService, errors.New("Service type unspecified")
  449. case porterv1.ServiceType_SERVICE_TYPE_WEB:
  450. webConfig := service.GetWebConfig()
  451. appService.Type = "web"
  452. var autoscaling *AutoScaling
  453. if webConfig.Autoscaling != nil {
  454. autoscaling = &AutoScaling{
  455. Enabled: webConfig.Autoscaling.Enabled,
  456. MinInstances: int(webConfig.Autoscaling.MinInstances),
  457. MaxInstances: int(webConfig.Autoscaling.MaxInstances),
  458. CpuThresholdPercent: int(webConfig.Autoscaling.CpuThresholdPercent),
  459. MemoryThresholdPercent: int(webConfig.Autoscaling.MemoryThresholdPercent),
  460. }
  461. }
  462. appService.Autoscaling = autoscaling
  463. var healthCheck *HealthCheck
  464. if webConfig.HealthCheck != nil {
  465. healthCheck = &HealthCheck{
  466. Enabled: webConfig.HealthCheck.Enabled,
  467. HttpPath: webConfig.HealthCheck.HttpPath,
  468. }
  469. }
  470. appService.HealthCheck = healthCheck
  471. domains := make([]Domains, 0)
  472. for _, domain := range webConfig.Domains {
  473. domains = append(domains, Domains{
  474. Name: domain.Name,
  475. })
  476. }
  477. appService.Domains = domains
  478. appService.IngressAnnotations = webConfig.IngressAnnotations
  479. if webConfig.Private != nil {
  480. appService.Private = webConfig.Private
  481. }
  482. if webConfig.DisableTls != nil {
  483. appService.DisableTLS = webConfig.DisableTls
  484. }
  485. case porterv1.ServiceType_SERVICE_TYPE_WORKER:
  486. workerConfig := service.GetWorkerConfig()
  487. appService.Type = "worker"
  488. var autoscaling *AutoScaling
  489. if workerConfig.Autoscaling != nil {
  490. autoscaling = &AutoScaling{
  491. Enabled: workerConfig.Autoscaling.Enabled,
  492. MinInstances: int(workerConfig.Autoscaling.MinInstances),
  493. MaxInstances: int(workerConfig.Autoscaling.MaxInstances),
  494. CpuThresholdPercent: int(workerConfig.Autoscaling.CpuThresholdPercent),
  495. MemoryThresholdPercent: int(workerConfig.Autoscaling.MemoryThresholdPercent),
  496. }
  497. }
  498. appService.Autoscaling = autoscaling
  499. case porterv1.ServiceType_SERVICE_TYPE_JOB:
  500. jobConfig := service.GetJobConfig()
  501. appService.Type = "job"
  502. appService.AllowConcurrent = jobConfig.AllowConcurrentOptional
  503. appService.Cron = jobConfig.Cron
  504. appService.SuspendCron = jobConfig.SuspendCron
  505. appService.TimeoutSeconds = int(jobConfig.TimeoutSeconds)
  506. }
  507. return appService, nil
  508. }
  509. func uniqueServices(serviceMap map[string]*porterv1.Service, serviceList []*porterv1.Service) []*porterv1.Service {
  510. if serviceList != nil {
  511. return serviceList
  512. }
  513. // deduplicate services by name, favoring whatever was defined first
  514. uniqueServices := make(map[string]*porterv1.Service)
  515. for name, service := range serviceMap {
  516. service.Name = name
  517. uniqueServices[service.Name] = service
  518. }
  519. mergedServiceList := make([]*porterv1.Service, 0)
  520. for _, service := range uniqueServices {
  521. mergedServiceList = append(mergedServiceList, service)
  522. }
  523. return mergedServiceList
  524. }