yaml.go 23 KB

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