yaml.go 22 KB

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