yaml.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. Sleep *bool `yaml:"sleep,omitempty" validate:"excluded_unless=Type job"`
  166. }
  167. // AutoScaling represents the autoscaling settings for web services
  168. type AutoScaling struct {
  169. Enabled bool `yaml:"enabled"`
  170. MinInstances int `yaml:"minInstances"`
  171. MaxInstances int `yaml:"maxInstances"`
  172. CpuThresholdPercent int `yaml:"cpuThresholdPercent"`
  173. MemoryThresholdPercent int `yaml:"memoryThresholdPercent"`
  174. }
  175. // GPU represents GPU settings for a service
  176. type GPU struct {
  177. Enabled bool `yaml:"enabled"`
  178. GpuCoresNvidia int `yaml:"gpuCoresNvidia"`
  179. }
  180. // Domains are the custom domains for a web service
  181. type Domains struct {
  182. Name string `yaml:"name"`
  183. }
  184. // HealthCheck contains the health check settings
  185. type HealthCheck struct {
  186. Enabled bool `yaml:"enabled"`
  187. HttpPath string `yaml:"httpPath,omitempty"`
  188. Command string `yaml:"command,omitempty"`
  189. TimeoutSeconds int `yaml:"timeoutSeconds,omitempty"`
  190. InitialDelaySeconds *int32 `yaml:"initialDelaySeconds,omitempty"`
  191. }
  192. // ProtoFromApp converts a PorterApp type to a base PorterApp proto type and returns env variables
  193. func ProtoFromApp(ctx context.Context, porterApp PorterApp) (*porterv1.PorterApp, map[string]string, error) {
  194. ctx, span := telemetry.NewSpan(ctx, "build-app-proto")
  195. defer span.End()
  196. appProto := &porterv1.PorterApp{
  197. Name: porterApp.Name,
  198. }
  199. if porterApp.Build != nil {
  200. appProto.Build = &porterv1.Build{
  201. Context: porterApp.Build.Context,
  202. Method: porterApp.Build.Method,
  203. Builder: porterApp.Build.Builder,
  204. Buildpacks: porterApp.Build.Buildpacks,
  205. Dockerfile: porterApp.Build.Dockerfile,
  206. CommitSha: porterApp.Build.CommitSHA,
  207. }
  208. }
  209. if porterApp.Image != nil {
  210. appProto.Image = &porterv1.AppImage{
  211. Repository: porterApp.Image.Repository,
  212. Tag: porterApp.Image.Tag,
  213. }
  214. }
  215. var services []*porterv1.Service
  216. for _, service := range porterApp.Services {
  217. serviceType := protoEnumFromType(service.Name, service)
  218. serviceProto, err := serviceProtoFromConfig(service, serviceType)
  219. if err != nil {
  220. return appProto, nil, telemetry.Error(ctx, span, err, "error casting service config")
  221. }
  222. if service.Name == "" {
  223. return appProto, nil, telemetry.Error(ctx, span, nil, "service found with no name")
  224. }
  225. services = append(services, serviceProto)
  226. }
  227. appProto.ServiceList = services
  228. if porterApp.Predeploy != nil {
  229. predeployProto, err := serviceProtoFromConfig(*porterApp.Predeploy, porterv1.ServiceType_SERVICE_TYPE_JOB)
  230. if err != nil {
  231. return appProto, nil, telemetry.Error(ctx, span, err, "error casting predeploy config")
  232. }
  233. appProto.Predeploy = predeployProto
  234. }
  235. for _, envGroup := range porterApp.EnvGroups {
  236. appProto.EnvGroups = append(appProto.EnvGroups, &porterv1.EnvGroup{
  237. Name: envGroup,
  238. Version: 0, // this will be updated to latest when applied
  239. })
  240. }
  241. if porterApp.EfsStorage != nil {
  242. appProto.EfsStorage = &porterv1.EFS{
  243. Enabled: porterApp.EfsStorage.Enabled,
  244. }
  245. }
  246. if porterApp.AutoRollback != nil {
  247. appProto.AutoRollback = &porterv1.AutoRollback{
  248. Enabled: porterApp.AutoRollback.Enabled,
  249. }
  250. }
  251. for _, requiredApp := range porterApp.RequiredApps {
  252. var targetIdentifier *porterv1.DeploymentTargetIdentifier
  253. if requiredApp.Name == "" {
  254. return appProto, nil, telemetry.Error(ctx, span, nil, "required app specified with no name")
  255. }
  256. if requiredApp.FromTarget != "" {
  257. targetIdentifier = &porterv1.DeploymentTargetIdentifier{
  258. Name: requiredApp.FromTarget,
  259. }
  260. }
  261. appProto.RequiredApps = append(appProto.RequiredApps, &porterv1.RequiredApp{
  262. Name: requiredApp.Name,
  263. FromTarget: targetIdentifier,
  264. })
  265. }
  266. envMap := make(map[string]string)
  267. var envVariables []*porterv1.EnvVariable
  268. for _, envVar := range porterApp.Env {
  269. switch envVar.Source {
  270. case EnvVariableSource_Value:
  271. if !envVar.Value.IsSet {
  272. return appProto, nil, telemetry.Error(ctx, span, nil, "no value set for env variable")
  273. }
  274. envMap[envVar.Key] = envVar.Value.Value
  275. case EnvVariableSource_FromApp:
  276. if !envVar.FromApp.IsSet {
  277. return appProto, nil, telemetry.Error(ctx, span, nil, "no value set for env variable")
  278. }
  279. fromApp, err := EnvVarFromAppToProto(envVar.FromApp.Value)
  280. if err != nil {
  281. return appProto, nil, telemetry.Error(ctx, span, err, "error converting env variable from app to proto")
  282. }
  283. envVariables = append(envVariables, &porterv1.EnvVariable{
  284. Key: envVar.Key,
  285. Source: porterv1.EnvVariableSource_ENV_VARIABLE_SOURCE_FROM_APP,
  286. Definition: &porterv1.EnvVariable_FromApp{
  287. FromApp: fromApp,
  288. },
  289. })
  290. default:
  291. return appProto, nil, telemetry.Error(ctx, span, nil, "invalid definition for env variable")
  292. }
  293. }
  294. appProto.Env = envVariables
  295. return appProto, envMap, nil
  296. }
  297. func protoEnumFromType(name string, service Service) porterv1.ServiceType {
  298. serviceType := porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED
  299. if strings.Contains(name, "web") {
  300. serviceType = porterv1.ServiceType_SERVICE_TYPE_WEB
  301. }
  302. if strings.Contains(name, "wkr") || strings.Contains(name, "worker") {
  303. serviceType = porterv1.ServiceType_SERVICE_TYPE_WORKER
  304. }
  305. if strings.Contains(name, "job") {
  306. serviceType = porterv1.ServiceType_SERVICE_TYPE_JOB
  307. }
  308. switch service.Type {
  309. case "web":
  310. serviceType = porterv1.ServiceType_SERVICE_TYPE_WEB
  311. case "worker":
  312. serviceType = porterv1.ServiceType_SERVICE_TYPE_WORKER
  313. case "job":
  314. serviceType = porterv1.ServiceType_SERVICE_TYPE_JOB
  315. }
  316. return serviceType
  317. }
  318. func serviceProtoFromConfig(service Service, serviceType porterv1.ServiceType) (*porterv1.Service, error) {
  319. serviceProto := &porterv1.Service{
  320. Name: service.Name,
  321. RunOptional: service.Run,
  322. InstancesOptional: service.Instances,
  323. CpuCores: service.CpuCores,
  324. RamMegabytes: int32(service.RamMegabytes),
  325. GpuCoresNvidia: service.GpuCoresNvidia,
  326. Port: int32(service.Port),
  327. SmartOptimization: service.SmartOptimization,
  328. Type: serviceType,
  329. TerminationGracePeriodSeconds: service.TerminationGracePeriodSeconds,
  330. }
  331. if service.GPU != nil {
  332. gpu := &porterv1.GPU{
  333. Enabled: service.GPU.Enabled,
  334. GpuCoresNvidia: int32(service.GPU.GpuCoresNvidia),
  335. }
  336. serviceProto.Gpu = gpu
  337. }
  338. switch serviceType {
  339. default:
  340. return nil, fmt.Errorf("invalid service type '%s'", serviceType)
  341. case porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED:
  342. return nil, errors.New("Service type unspecified")
  343. case porterv1.ServiceType_SERVICE_TYPE_WEB:
  344. webConfig := &porterv1.WebServiceConfig{}
  345. var autoscaling *porterv1.Autoscaling
  346. if service.Autoscaling != nil {
  347. autoscaling = &porterv1.Autoscaling{
  348. Enabled: service.Autoscaling.Enabled,
  349. MinInstances: int32(service.Autoscaling.MinInstances),
  350. MaxInstances: int32(service.Autoscaling.MaxInstances),
  351. CpuThresholdPercent: int32(service.Autoscaling.CpuThresholdPercent),
  352. MemoryThresholdPercent: int32(service.Autoscaling.MemoryThresholdPercent),
  353. }
  354. }
  355. webConfig.Autoscaling = autoscaling
  356. var healthCheck *porterv1.HealthCheck
  357. if service.HealthCheck != nil {
  358. healthCheck = &porterv1.HealthCheck{
  359. Enabled: service.HealthCheck.Enabled,
  360. HttpPath: service.HealthCheck.HttpPath,
  361. Command: service.HealthCheck.Command,
  362. TimeoutSeconds: int32(service.HealthCheck.TimeoutSeconds),
  363. InitialDelaySeconds: service.HealthCheck.InitialDelaySeconds,
  364. }
  365. }
  366. webConfig.HealthCheck = healthCheck
  367. domains := make([]*porterv1.Domain, 0)
  368. for _, domain := range service.Domains {
  369. domains = append(domains, &porterv1.Domain{
  370. Name: domain.Name,
  371. })
  372. }
  373. webConfig.Domains = domains
  374. webConfig.IngressAnnotations = service.IngressAnnotations
  375. if service.Private != nil {
  376. webConfig.Private = service.Private
  377. }
  378. if service.DisableTLS != nil {
  379. webConfig.DisableTls = service.DisableTLS
  380. }
  381. if service.Sleep != nil {
  382. serviceProto.Sleep = service.Sleep
  383. }
  384. serviceProto.Config = &porterv1.Service_WebConfig{
  385. WebConfig: webConfig,
  386. }
  387. case porterv1.ServiceType_SERVICE_TYPE_WORKER:
  388. workerConfig := &porterv1.WorkerServiceConfig{}
  389. var autoscaling *porterv1.Autoscaling
  390. if service.Autoscaling != nil {
  391. autoscaling = &porterv1.Autoscaling{
  392. Enabled: service.Autoscaling.Enabled,
  393. MinInstances: int32(service.Autoscaling.MinInstances),
  394. MaxInstances: int32(service.Autoscaling.MaxInstances),
  395. CpuThresholdPercent: int32(service.Autoscaling.CpuThresholdPercent),
  396. MemoryThresholdPercent: int32(service.Autoscaling.MemoryThresholdPercent),
  397. }
  398. }
  399. workerConfig.Autoscaling = autoscaling
  400. var healthCheck *porterv1.HealthCheck
  401. if service.HealthCheck != nil {
  402. healthCheck = &porterv1.HealthCheck{
  403. Enabled: service.HealthCheck.Enabled,
  404. HttpPath: service.HealthCheck.HttpPath,
  405. Command: service.HealthCheck.Command,
  406. TimeoutSeconds: int32(service.HealthCheck.TimeoutSeconds),
  407. InitialDelaySeconds: service.HealthCheck.InitialDelaySeconds,
  408. }
  409. }
  410. workerConfig.HealthCheck = healthCheck
  411. if service.Sleep != nil {
  412. serviceProto.Sleep = service.Sleep
  413. }
  414. serviceProto.Config = &porterv1.Service_WorkerConfig{
  415. WorkerConfig: workerConfig,
  416. }
  417. case porterv1.ServiceType_SERVICE_TYPE_JOB:
  418. jobConfig := &porterv1.JobServiceConfig{
  419. AllowConcurrentOptional: service.AllowConcurrent,
  420. Cron: service.Cron,
  421. }
  422. if service.SuspendCron != nil {
  423. jobConfig.SuspendCron = service.SuspendCron
  424. }
  425. if service.TimeoutSeconds != 0 {
  426. jobConfig.TimeoutSeconds = int64(service.TimeoutSeconds)
  427. }
  428. serviceProto.Config = &porterv1.Service_JobConfig{
  429. JobConfig: jobConfig,
  430. }
  431. }
  432. return serviceProto, nil
  433. }
  434. // AppFromProto converts a PorterApp proto object into a PorterApp struct
  435. func AppFromProto(appProto *porterv1.PorterApp) (PorterApp, error) {
  436. porterApp := PorterApp{
  437. Version: "v2",
  438. Name: appProto.Name,
  439. }
  440. if appProto.Build != nil {
  441. porterApp.Build = &Build{
  442. Context: appProto.Build.Context,
  443. Method: appProto.Build.Method,
  444. Builder: appProto.Build.Builder,
  445. Buildpacks: appProto.Build.Buildpacks,
  446. Dockerfile: appProto.Build.Dockerfile,
  447. CommitSHA: appProto.Build.CommitSha,
  448. }
  449. }
  450. if appProto.Image != nil {
  451. porterApp.Image = &Image{
  452. Repository: appProto.Image.Repository,
  453. Tag: appProto.Image.Tag,
  454. }
  455. }
  456. uniqueServices := uniqueServices(appProto.Services, appProto.ServiceList) // nolint:staticcheck // temporarily using deprecated field for backwards compatibility
  457. for _, service := range uniqueServices {
  458. appService, err := appServiceFromProto(service)
  459. if err != nil {
  460. return porterApp, err
  461. }
  462. porterApp.Services = append(porterApp.Services, appService)
  463. }
  464. if appProto.Predeploy != nil {
  465. appPredeploy, err := appServiceFromProto(appProto.Predeploy)
  466. if err != nil {
  467. return porterApp, err
  468. }
  469. porterApp.Predeploy = &appPredeploy
  470. }
  471. for _, envGroup := range appProto.EnvGroups {
  472. if envGroup != nil {
  473. porterApp.EnvGroups = append(porterApp.EnvGroups, fmt.Sprintf("%s:v%d", envGroup.Name, envGroup.Version))
  474. }
  475. }
  476. if appProto.EfsStorage != nil {
  477. porterApp.EfsStorage = &EfsStorage{
  478. Enabled: appProto.EfsStorage.Enabled,
  479. }
  480. }
  481. if appProto.AutoRollback != nil {
  482. porterApp.AutoRollback = &AutoRollback{
  483. Enabled: appProto.AutoRollback.Enabled,
  484. }
  485. }
  486. return porterApp, nil
  487. }
  488. func appServiceFromProto(service *porterv1.Service) (Service, error) {
  489. var gpu *GPU
  490. if service.Gpu != nil {
  491. gpu = &GPU{
  492. Enabled: service.Gpu.Enabled,
  493. GpuCoresNvidia: int(service.Gpu.GpuCoresNvidia),
  494. }
  495. }
  496. appService := Service{
  497. Name: service.Name,
  498. Run: service.RunOptional,
  499. Instances: service.InstancesOptional,
  500. CpuCores: service.CpuCores,
  501. RamMegabytes: int(service.RamMegabytes),
  502. GpuCoresNvidia: service.GpuCoresNvidia, // nolint:staticcheck // https://linear.app/porter/issue/POR-2137/support-new-gpu-field-in-porteryaml
  503. Port: int(service.Port),
  504. SmartOptimization: service.SmartOptimization,
  505. GPU: gpu,
  506. TerminationGracePeriodSeconds: service.TerminationGracePeriodSeconds,
  507. Sleep: service.Sleep,
  508. }
  509. switch service.Type {
  510. default:
  511. return appService, fmt.Errorf("invalid service type '%s'", service.Type)
  512. case porterv1.ServiceType_SERVICE_TYPE_UNSPECIFIED:
  513. return appService, errors.New("Service type unspecified")
  514. case porterv1.ServiceType_SERVICE_TYPE_WEB:
  515. webConfig := service.GetWebConfig()
  516. appService.Type = "web"
  517. var autoscaling *AutoScaling
  518. if webConfig.Autoscaling != nil {
  519. autoscaling = &AutoScaling{
  520. Enabled: webConfig.Autoscaling.Enabled,
  521. MinInstances: int(webConfig.Autoscaling.MinInstances),
  522. MaxInstances: int(webConfig.Autoscaling.MaxInstances),
  523. CpuThresholdPercent: int(webConfig.Autoscaling.CpuThresholdPercent),
  524. MemoryThresholdPercent: int(webConfig.Autoscaling.MemoryThresholdPercent),
  525. }
  526. }
  527. appService.Autoscaling = autoscaling
  528. var healthCheck *HealthCheck
  529. if webConfig.HealthCheck != nil {
  530. healthCheck = &HealthCheck{
  531. Enabled: webConfig.HealthCheck.Enabled,
  532. HttpPath: webConfig.HealthCheck.HttpPath,
  533. Command: webConfig.HealthCheck.Command,
  534. TimeoutSeconds: int(webConfig.HealthCheck.TimeoutSeconds),
  535. InitialDelaySeconds: webConfig.HealthCheck.InitialDelaySeconds,
  536. }
  537. }
  538. appService.HealthCheck = healthCheck
  539. domains := make([]Domains, 0)
  540. for _, domain := range webConfig.Domains {
  541. domains = append(domains, Domains{
  542. Name: domain.Name,
  543. })
  544. }
  545. appService.Domains = domains
  546. appService.IngressAnnotations = webConfig.IngressAnnotations
  547. if webConfig.Private != nil {
  548. appService.Private = webConfig.Private
  549. }
  550. if webConfig.DisableTls != nil {
  551. appService.DisableTLS = webConfig.DisableTls
  552. }
  553. case porterv1.ServiceType_SERVICE_TYPE_WORKER:
  554. workerConfig := service.GetWorkerConfig()
  555. appService.Type = "worker"
  556. var autoscaling *AutoScaling
  557. if workerConfig.Autoscaling != nil {
  558. autoscaling = &AutoScaling{
  559. Enabled: workerConfig.Autoscaling.Enabled,
  560. MinInstances: int(workerConfig.Autoscaling.MinInstances),
  561. MaxInstances: int(workerConfig.Autoscaling.MaxInstances),
  562. CpuThresholdPercent: int(workerConfig.Autoscaling.CpuThresholdPercent),
  563. MemoryThresholdPercent: int(workerConfig.Autoscaling.MemoryThresholdPercent),
  564. }
  565. }
  566. appService.Autoscaling = autoscaling
  567. var healthCheck *HealthCheck
  568. if workerConfig.HealthCheck != nil {
  569. healthCheck = &HealthCheck{
  570. Enabled: workerConfig.HealthCheck.Enabled,
  571. HttpPath: workerConfig.HealthCheck.HttpPath,
  572. Command: workerConfig.HealthCheck.Command,
  573. TimeoutSeconds: int(workerConfig.HealthCheck.TimeoutSeconds),
  574. InitialDelaySeconds: workerConfig.HealthCheck.InitialDelaySeconds,
  575. }
  576. }
  577. appService.HealthCheck = healthCheck
  578. case porterv1.ServiceType_SERVICE_TYPE_JOB:
  579. jobConfig := service.GetJobConfig()
  580. appService.Type = "job"
  581. appService.AllowConcurrent = jobConfig.AllowConcurrentOptional
  582. appService.Cron = jobConfig.Cron
  583. appService.SuspendCron = jobConfig.SuspendCron
  584. appService.TimeoutSeconds = int(jobConfig.TimeoutSeconds)
  585. }
  586. return appService, nil
  587. }
  588. func uniqueServices(serviceMap map[string]*porterv1.Service, serviceList []*porterv1.Service) []*porterv1.Service {
  589. if serviceList != nil {
  590. return serviceList
  591. }
  592. // deduplicate services by name, favoring whatever was defined first
  593. uniqueServices := make(map[string]*porterv1.Service)
  594. for name, service := range serviceMap {
  595. service.Name = name
  596. uniqueServices[service.Name] = service
  597. }
  598. mergedServiceList := make([]*porterv1.Service, 0)
  599. for _, service := range uniqueServices {
  600. mergedServiceList = append(mergedServiceList, service)
  601. }
  602. return mergedServiceList
  603. }