apply.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "github.com/cli/cli/git"
  13. "github.com/fatih/color"
  14. "github.com/mitchellh/mapstructure"
  15. api "github.com/porter-dev/porter/api/client"
  16. "github.com/porter-dev/porter/api/types"
  17. "github.com/porter-dev/porter/cli/cmd/config"
  18. "github.com/porter-dev/porter/cli/cmd/deploy"
  19. "github.com/porter-dev/porter/cli/cmd/deploy/wait"
  20. "github.com/porter-dev/porter/cli/cmd/preview"
  21. previewInt "github.com/porter-dev/porter/internal/integrations/preview"
  22. "github.com/porter-dev/porter/internal/templater/utils"
  23. "github.com/porter-dev/switchboard/pkg/drivers"
  24. switchboardModels "github.com/porter-dev/switchboard/pkg/models"
  25. "github.com/porter-dev/switchboard/pkg/parser"
  26. switchboardTypes "github.com/porter-dev/switchboard/pkg/types"
  27. switchboardWorker "github.com/porter-dev/switchboard/pkg/worker"
  28. "github.com/rs/zerolog"
  29. "github.com/spf13/cobra"
  30. )
  31. // applyCmd represents the "porter apply" base command when called
  32. // with a porter.yaml file as an argument
  33. var applyCmd = &cobra.Command{
  34. Use: "apply",
  35. Short: "Applies a configuration to an application",
  36. Long: fmt.Sprintf(`
  37. %s
  38. Applies a configuration to an application by either creating a new one or updating an existing
  39. one. For example:
  40. %s
  41. This command will apply the configuration contained in porter.yaml to the requested project and
  42. cluster either provided inside the porter.yaml file or through environment variables. Note that
  43. environment variables will always take precendence over values specified in the porter.yaml file.
  44. By default, this command expects to be run from a local git repository.
  45. The following are the environment variables that can be used to set certain values while
  46. applying a configuration:
  47. PORTER_CLUSTER Cluster ID that contains the project
  48. PORTER_PROJECT Project ID that contains the application
  49. PORTER_NAMESPACE The Kubernetes namespace that the application belongs to
  50. PORTER_SOURCE_NAME Name of the source Helm chart
  51. PORTER_SOURCE_REPO The URL of the Helm charts registry
  52. PORTER_SOURCE_VERSION The version of the Helm chart to use
  53. PORTER_TAG The Docker image tag to use (like the git commit hash)
  54. `,
  55. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter apply\":"),
  56. color.New(color.FgGreen, color.Bold).Sprintf("porter apply -f porter.yaml"),
  57. ),
  58. Run: func(cmd *cobra.Command, args []string) {
  59. err := checkLoginAndRun(args, apply)
  60. if err != nil {
  61. if strings.Contains(err.Error(), "Forbidden") {
  62. color.New(color.FgRed).Fprintf(os.Stderr, "You may have to update your GitHub secret token")
  63. }
  64. os.Exit(1)
  65. }
  66. },
  67. }
  68. // applyValidateCmd represents the "porter apply validate" command when called
  69. // with a porter.yaml file as an argument
  70. var applyValidateCmd = &cobra.Command{
  71. Use: "validate",
  72. Short: "Validates a porter.yaml",
  73. Run: func(*cobra.Command, []string) {
  74. err := applyValidate()
  75. if err != nil {
  76. color.New(color.FgRed).Fprintf(os.Stderr, "Error: %s\n", err.Error())
  77. os.Exit(1)
  78. } else {
  79. color.New(color.FgGreen).Printf("The porter.yaml file is valid!\n")
  80. }
  81. },
  82. }
  83. var porterYAML string
  84. func init() {
  85. rootCmd.AddCommand(applyCmd)
  86. applyCmd.AddCommand(applyValidateCmd)
  87. applyCmd.PersistentFlags().StringVarP(&porterYAML, "file", "f", "", "path to porter.yaml")
  88. applyCmd.MarkFlagRequired("file")
  89. }
  90. func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  91. if _, ok := os.LookupEnv("PORTER_VALIDATE_YAML"); ok {
  92. err := applyValidate()
  93. if err != nil {
  94. return err
  95. }
  96. }
  97. fileBytes, err := ioutil.ReadFile(porterYAML)
  98. if err != nil {
  99. return fmt.Errorf("error reading porter.yaml: %w", err)
  100. }
  101. resGroup, err := parser.ParseRawBytes(fileBytes)
  102. if err != nil {
  103. return fmt.Errorf("error parsing porter.yaml: %w", err)
  104. }
  105. basePath, err := os.Getwd()
  106. if err != nil {
  107. return fmt.Errorf("error getting working directory: %w", err)
  108. }
  109. worker := switchboardWorker.NewWorker()
  110. worker.RegisterDriver("deploy", NewDeployDriver)
  111. worker.RegisterDriver("build-image", preview.NewBuildDriver)
  112. worker.RegisterDriver("push-image", preview.NewPushDriver)
  113. worker.RegisterDriver("update-config", preview.NewUpdateConfigDriver)
  114. worker.RegisterDriver("random-string", preview.NewRandomStringDriver)
  115. worker.RegisterDriver("env-group", preview.NewEnvGroupDriver)
  116. worker.RegisterDriver("os-env", preview.NewOSEnvDriver)
  117. worker.SetDefaultDriver("deploy")
  118. if hasDeploymentHookEnvVars() {
  119. deplNamespace := os.Getenv("PORTER_NAMESPACE")
  120. if deplNamespace == "" {
  121. return fmt.Errorf("namespace must be set by PORTER_NAMESPACE")
  122. }
  123. deploymentHook, err := NewDeploymentHook(client, resGroup, deplNamespace)
  124. if err != nil {
  125. return fmt.Errorf("error creating deployment hook: %w", err)
  126. }
  127. worker.RegisterHook("deployment", deploymentHook)
  128. }
  129. cloneEnvGroupHook := NewCloneEnvGroupHook(client, resGroup)
  130. worker.RegisterHook("cloneenvgroup", cloneEnvGroupHook)
  131. return worker.Apply(resGroup, &switchboardTypes.ApplyOpts{
  132. BasePath: basePath,
  133. })
  134. }
  135. func applyValidate() error {
  136. fileBytes, err := ioutil.ReadFile(porterYAML)
  137. if err != nil {
  138. return fmt.Errorf("error reading porter.yaml: %w", err)
  139. }
  140. validationErrors := previewInt.Validate(string(fileBytes))
  141. if len(validationErrors) > 0 {
  142. errString := "the following error(s) were found while validating the porter.yaml file:"
  143. for _, err := range validationErrors {
  144. errString += "\n- " + strings.ReplaceAll(err.Error(), "\n\n*", "\n *")
  145. }
  146. return fmt.Errorf(errString)
  147. }
  148. return nil
  149. }
  150. func hasDeploymentHookEnvVars() bool {
  151. if ghIDStr := os.Getenv("PORTER_GIT_INSTALLATION_ID"); ghIDStr == "" {
  152. return false
  153. }
  154. if prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID"); prIDStr == "" {
  155. return false
  156. }
  157. if branchFrom := os.Getenv("PORTER_BRANCH_FROM"); branchFrom == "" {
  158. return false
  159. }
  160. if branchInto := os.Getenv("PORTER_BRANCH_INTO"); branchInto == "" {
  161. return false
  162. }
  163. if actionIDStr := os.Getenv("PORTER_ACTION_ID"); actionIDStr == "" {
  164. return false
  165. }
  166. if repoName := os.Getenv("PORTER_REPO_NAME"); repoName == "" {
  167. return false
  168. }
  169. if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner == "" {
  170. return false
  171. }
  172. if prName := os.Getenv("PORTER_PR_NAME"); prName == "" {
  173. return false
  174. }
  175. return true
  176. }
  177. type DeployDriver struct {
  178. source *previewInt.Source
  179. target *previewInt.Target
  180. output map[string]interface{}
  181. lookupTable *map[string]drivers.Driver
  182. logger *zerolog.Logger
  183. }
  184. func NewDeployDriver(resource *switchboardModels.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
  185. driver := &DeployDriver{
  186. lookupTable: opts.DriverLookupTable,
  187. logger: opts.Logger,
  188. output: make(map[string]interface{}),
  189. }
  190. source, err := preview.GetSource(resource.Name, resource.Source)
  191. if err != nil {
  192. return nil, err
  193. }
  194. driver.source = source
  195. target, err := preview.GetTarget(resource.Name, resource.Target)
  196. if err != nil {
  197. return nil, err
  198. }
  199. driver.target = target
  200. return driver, nil
  201. }
  202. func (d *DeployDriver) ShouldApply(_ *switchboardModels.Resource) bool {
  203. return true
  204. }
  205. func (d *DeployDriver) Apply(resource *switchboardModels.Resource) (*switchboardModels.Resource, error) {
  206. client := config.GetAPIClient()
  207. _, err := client.GetRelease(
  208. context.Background(),
  209. d.target.Project,
  210. d.target.Cluster,
  211. d.target.Namespace,
  212. resource.Name,
  213. )
  214. shouldCreate := err != nil
  215. if err != nil {
  216. color.New(color.FgYellow).Printf("Could not read release %s/%s (%s): attempting creation\n", d.target.Namespace, resource.Name, err.Error())
  217. }
  218. if d.source.IsApplication {
  219. return d.applyApplication(resource, client, shouldCreate)
  220. }
  221. return d.applyAddon(resource, client, shouldCreate)
  222. }
  223. // Simple apply for addons
  224. func (d *DeployDriver) applyAddon(resource *switchboardModels.Resource, client *api.Client, shouldCreate bool) (*switchboardModels.Resource, error) {
  225. addonConfig, err := d.getAddonConfig(resource)
  226. if err != nil {
  227. return nil, fmt.Errorf("error getting addon config for resource %s: %w", resource.Name, err)
  228. }
  229. if shouldCreate {
  230. err := client.DeployAddon(
  231. context.Background(),
  232. d.target.Project,
  233. d.target.Cluster,
  234. d.target.Namespace,
  235. &types.CreateAddonRequest{
  236. CreateReleaseBaseRequest: &types.CreateReleaseBaseRequest{
  237. RepoURL: d.source.Repo,
  238. TemplateName: d.source.Name,
  239. TemplateVersion: d.source.Version,
  240. Values: addonConfig,
  241. Name: resource.Name,
  242. },
  243. },
  244. )
  245. if err != nil {
  246. return nil, fmt.Errorf("error creating addon from resource %s: %w", resource.Name, err)
  247. }
  248. } else {
  249. bytes, err := json.Marshal(addonConfig)
  250. if err != nil {
  251. return nil, fmt.Errorf("error marshalling addon config from resource %s: %w", resource.Name, err)
  252. }
  253. err = client.UpgradeRelease(
  254. context.Background(),
  255. d.target.Project,
  256. d.target.Cluster,
  257. d.target.Namespace,
  258. resource.Name,
  259. &types.UpgradeReleaseRequest{
  260. Values: string(bytes),
  261. },
  262. )
  263. if err != nil {
  264. return nil, fmt.Errorf("error updating addon from resource %s: %w", resource.Name, err)
  265. }
  266. }
  267. if err = d.assignOutput(resource, client); err != nil {
  268. return nil, err
  269. }
  270. return resource, nil
  271. }
  272. func (d *DeployDriver) applyApplication(resource *switchboardModels.Resource, client *api.Client, shouldCreate bool) (*switchboardModels.Resource, error) {
  273. if resource == nil {
  274. return nil, fmt.Errorf("nil resource")
  275. }
  276. resourceName := resource.Name
  277. appConfig, err := d.getApplicationConfig(resource)
  278. if err != nil {
  279. return nil, err
  280. }
  281. fullPath, err := filepath.Abs(appConfig.Build.Context)
  282. if err != nil {
  283. return nil, fmt.Errorf("for resource %s, error getting absolute path for config.build.context: %w", resourceName,
  284. err)
  285. }
  286. tag := os.Getenv("PORTER_TAG")
  287. if tag == "" {
  288. color.New(color.FgYellow).Printf("for resource %s, since PORTER_TAG is not set, the Docker image tag will default to"+
  289. " the git repo SHA", resourceName)
  290. commit, err := git.LastCommit()
  291. if err != nil {
  292. return nil, fmt.Errorf("for resource %s, error getting last git commit: %w", resourceName, err)
  293. }
  294. tag = commit.Sha[:7]
  295. color.New(color.FgYellow).Printf("for resource %s, using tag %s\n", resourceName, tag)
  296. }
  297. // if the method is registry and a tag is defined, we use the provided tag
  298. if appConfig.Build.Method == "registry" {
  299. imageSpl := strings.Split(appConfig.Build.Image, ":")
  300. if len(imageSpl) == 2 {
  301. tag = imageSpl[1]
  302. }
  303. if tag == "" {
  304. tag = "latest"
  305. }
  306. }
  307. sharedOpts := &deploy.SharedOpts{
  308. ProjectID: d.target.Project,
  309. ClusterID: d.target.Cluster,
  310. Namespace: d.target.Namespace,
  311. LocalPath: fullPath,
  312. LocalDockerfile: appConfig.Build.Dockerfile,
  313. OverrideTag: tag,
  314. Method: deploy.DeployBuildType(appConfig.Build.Method),
  315. EnvGroups: appConfig.EnvGroups,
  316. UseCache: appConfig.Build.UseCache,
  317. }
  318. if appConfig.Build.UseCache {
  319. // set the docker config so that pack caching can use the repo credentials
  320. err := config.SetDockerConfig(client)
  321. if err != nil {
  322. return nil, err
  323. }
  324. }
  325. if shouldCreate {
  326. resource, err = d.createApplication(resource, client, sharedOpts, appConfig)
  327. if err != nil {
  328. return nil, fmt.Errorf("error creating app from resource %s: %w", resourceName, err)
  329. }
  330. } else if !appConfig.OnlyCreate {
  331. resource, err = d.updateApplication(resource, client, sharedOpts, appConfig)
  332. if err != nil {
  333. return nil, fmt.Errorf("error updating application from resource %s: %w", resourceName, err)
  334. }
  335. } else {
  336. color.New(color.FgYellow).Printf("Skipping creation for resource %s as onlyCreate is set to true\n", resourceName)
  337. }
  338. if err = d.assignOutput(resource, client); err != nil {
  339. return nil, err
  340. }
  341. if d.source.Name == "job" && appConfig.WaitForJob && (shouldCreate || !appConfig.OnlyCreate) {
  342. color.New(color.FgYellow).Printf("Waiting for job '%s' to finish\n", resourceName)
  343. err = wait.WaitForJob(client, &wait.WaitOpts{
  344. ProjectID: d.target.Project,
  345. ClusterID: d.target.Cluster,
  346. Namespace: d.target.Namespace,
  347. Name: resourceName,
  348. })
  349. if err != nil && appConfig.OnlyCreate {
  350. deleteJobErr := client.DeleteRelease(
  351. context.Background(),
  352. d.target.Project,
  353. d.target.Cluster,
  354. d.target.Namespace,
  355. resourceName,
  356. )
  357. if deleteJobErr != nil {
  358. return nil, fmt.Errorf("error deleting job %s with waitForJob and onlyCreate set to true: %w",
  359. resourceName, deleteJobErr)
  360. }
  361. } else if err != nil {
  362. return nil, fmt.Errorf("error waiting for job %s: %w", resourceName, err)
  363. }
  364. }
  365. return resource, err
  366. }
  367. func (d *DeployDriver) createApplication(resource *switchboardModels.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *previewInt.ApplicationConfig) (*switchboardModels.Resource, error) {
  368. // create new release
  369. color.New(color.FgGreen).Printf("Creating %s release: %s\n", d.source.Name, resource.Name)
  370. regList, err := client.ListRegistries(context.Background(), d.target.Project)
  371. if err != nil {
  372. return nil, fmt.Errorf("for resource %s, error listing registries: %w", resource.Name, err)
  373. }
  374. var registryURL string
  375. if len(*regList) == 0 {
  376. return nil, fmt.Errorf("no registry found")
  377. } else {
  378. registryURL = (*regList)[0].URL
  379. }
  380. color.New(color.FgBlue).Printf("for resource %s, using registry %s\n", resource.Name, registryURL)
  381. // attempt to get repo suffix from environment variables
  382. var repoSuffix string
  383. if repoName := os.Getenv("PORTER_REPO_NAME"); repoName != "" {
  384. if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner != "" {
  385. repoSuffix = strings.ToLower(strings.ReplaceAll(fmt.Sprintf("%s-%s", repoOwner, repoName), "_", "-"))
  386. }
  387. }
  388. createAgent := &deploy.CreateAgent{
  389. Client: client,
  390. CreateOpts: &deploy.CreateOpts{
  391. SharedOpts: sharedOpts,
  392. Kind: d.source.Name,
  393. ReleaseName: resource.Name,
  394. RegistryURL: registryURL,
  395. RepoSuffix: repoSuffix,
  396. },
  397. }
  398. var buildConfig *types.BuildConfig
  399. if appConf.Build.Builder != "" {
  400. buildConfig = &types.BuildConfig{
  401. Builder: appConf.Build.Builder,
  402. Buildpacks: appConf.Build.Buildpacks,
  403. }
  404. }
  405. var subdomain string
  406. if appConf.Build.Method == "registry" {
  407. subdomain, err = createAgent.CreateFromRegistry(appConf.Build.Image, appConf.Values)
  408. } else {
  409. // if useCache is set, create the image repository first
  410. if appConf.Build.UseCache {
  411. regID, imageURL, err := createAgent.GetImageRepoURL(resource.Name, sharedOpts.Namespace)
  412. if err != nil {
  413. return nil, err
  414. }
  415. err = client.CreateRepository(
  416. context.Background(),
  417. sharedOpts.ProjectID,
  418. regID,
  419. &types.CreateRegistryRepositoryRequest{
  420. ImageRepoURI: imageURL,
  421. },
  422. )
  423. if err != nil {
  424. return nil, err
  425. }
  426. }
  427. subdomain, err = createAgent.CreateFromDocker(appConf.Values, sharedOpts.OverrideTag, buildConfig)
  428. }
  429. if err != nil {
  430. return nil, err
  431. }
  432. return resource, handleSubdomainCreate(subdomain, err)
  433. }
  434. func (d *DeployDriver) updateApplication(resource *switchboardModels.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *previewInt.ApplicationConfig) (*switchboardModels.Resource, error) {
  435. color.New(color.FgGreen).Println("Updating existing release:", resource.Name)
  436. if len(appConf.Build.Env) > 0 {
  437. sharedOpts.AdditionalEnv = appConf.Build.Env
  438. }
  439. updateAgent, err := deploy.NewDeployAgent(client, resource.Name, &deploy.DeployOpts{
  440. SharedOpts: sharedOpts,
  441. Local: appConf.Build.Method != "registry",
  442. })
  443. if err != nil {
  444. return nil, err
  445. }
  446. // if the build method is registry, we do not trigger a build
  447. if appConf.Build.Method != "registry" {
  448. buildEnv, err := updateAgent.GetBuildEnv(&deploy.GetBuildEnvOpts{
  449. UseNewConfig: true,
  450. NewConfig: appConf.Values,
  451. })
  452. if err != nil {
  453. return nil, err
  454. }
  455. err = updateAgent.SetBuildEnv(buildEnv)
  456. if err != nil {
  457. return nil, err
  458. }
  459. var buildConfig *types.BuildConfig
  460. if appConf.Build.Builder != "" {
  461. buildConfig = &types.BuildConfig{
  462. Builder: appConf.Build.Builder,
  463. Buildpacks: appConf.Build.Buildpacks,
  464. }
  465. }
  466. err = updateAgent.Build(buildConfig)
  467. if err != nil {
  468. return nil, err
  469. }
  470. if !appConf.Build.UseCache {
  471. err = updateAgent.Push()
  472. if err != nil {
  473. return nil, err
  474. }
  475. }
  476. }
  477. err = updateAgent.UpdateImageAndValues(appConf.Values)
  478. if err != nil {
  479. return nil, err
  480. }
  481. return resource, nil
  482. }
  483. func (d *DeployDriver) assignOutput(resource *switchboardModels.Resource, client *api.Client) error {
  484. release, err := client.GetRelease(
  485. context.Background(),
  486. d.target.Project,
  487. d.target.Cluster,
  488. d.target.Namespace,
  489. resource.Name,
  490. )
  491. if err != nil {
  492. return err
  493. }
  494. d.output = utils.CoalesceValues(d.source.SourceValues, release.Config)
  495. return nil
  496. }
  497. func (d *DeployDriver) Output() (map[string]interface{}, error) {
  498. return d.output, nil
  499. }
  500. func (d *DeployDriver) getApplicationConfig(resource *switchboardModels.Resource) (*previewInt.ApplicationConfig, error) {
  501. populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
  502. RawConf: resource.Config,
  503. LookupTable: *d.lookupTable,
  504. Dependencies: resource.Dependencies,
  505. })
  506. if err != nil {
  507. return nil, err
  508. }
  509. appConf := &previewInt.ApplicationConfig{}
  510. err = mapstructure.Decode(populatedConf, appConf)
  511. if err != nil {
  512. return nil, err
  513. }
  514. if _, ok := resource.Config["waitForJob"]; !ok && d.source.Name == "job" {
  515. // default to true and wait for the job to finish
  516. appConf.WaitForJob = true
  517. }
  518. return appConf, nil
  519. }
  520. func (d *DeployDriver) getAddonConfig(resource *switchboardModels.Resource) (map[string]interface{}, error) {
  521. return drivers.ConstructConfig(&drivers.ConstructConfigOpts{
  522. RawConf: resource.Config,
  523. LookupTable: *d.lookupTable,
  524. Dependencies: resource.Dependencies,
  525. })
  526. }
  527. type DeploymentHook struct {
  528. client *api.Client
  529. resourceGroup *switchboardTypes.ResourceGroup
  530. gitInstallationID, projectID, clusterID, prID, actionID, envID uint
  531. branchFrom, branchInto, namespace, repoName, repoOwner, prName, commitSHA string
  532. }
  533. func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup, namespace string) (*DeploymentHook, error) {
  534. res := &DeploymentHook{
  535. client: client,
  536. resourceGroup: resourceGroup,
  537. namespace: namespace,
  538. }
  539. ghIDStr := os.Getenv("PORTER_GIT_INSTALLATION_ID")
  540. ghID, err := strconv.Atoi(ghIDStr)
  541. if err != nil {
  542. return nil, err
  543. }
  544. res.gitInstallationID = uint(ghID)
  545. prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID")
  546. prID, err := strconv.Atoi(prIDStr)
  547. if err != nil {
  548. return nil, err
  549. }
  550. res.prID = uint(prID)
  551. res.projectID = cliConf.Project
  552. if res.projectID == 0 {
  553. return nil, fmt.Errorf("project id must be set")
  554. }
  555. res.clusterID = cliConf.Cluster
  556. if res.clusterID == 0 {
  557. return nil, fmt.Errorf("cluster id must be set")
  558. }
  559. branchFrom := os.Getenv("PORTER_BRANCH_FROM")
  560. res.branchFrom = branchFrom
  561. branchInto := os.Getenv("PORTER_BRANCH_INTO")
  562. res.branchInto = branchInto
  563. actionIDStr := os.Getenv("PORTER_ACTION_ID")
  564. actionID, err := strconv.Atoi(actionIDStr)
  565. if err != nil {
  566. return nil, err
  567. }
  568. res.actionID = uint(actionID)
  569. repoName := os.Getenv("PORTER_REPO_NAME")
  570. res.repoName = repoName
  571. repoOwner := os.Getenv("PORTER_REPO_OWNER")
  572. res.repoOwner = repoOwner
  573. prName := os.Getenv("PORTER_PR_NAME")
  574. res.prName = prName
  575. commit, err := git.LastCommit()
  576. if err != nil {
  577. return nil, fmt.Errorf(err.Error())
  578. }
  579. res.commitSHA = commit.Sha[:7]
  580. return res, nil
  581. }
  582. func (t *DeploymentHook) PreApply() error {
  583. if isSystemNamespace(t.namespace) {
  584. color.New(color.FgYellow).Printf("attempting to deploy to system namespace '%s'\n", t.namespace)
  585. }
  586. envList, err := t.client.ListEnvironments(
  587. context.Background(), t.projectID, t.clusterID,
  588. )
  589. if err != nil {
  590. return err
  591. }
  592. envs := *envList
  593. var deplEnv *types.Environment
  594. for _, env := range envs {
  595. if strings.EqualFold(env.GitRepoOwner, t.repoOwner) &&
  596. strings.EqualFold(env.GitRepoName, t.repoName) &&
  597. env.GitInstallationID == t.gitInstallationID {
  598. t.envID = env.ID
  599. deplEnv = env
  600. break
  601. }
  602. }
  603. if t.envID == 0 {
  604. return fmt.Errorf("could not find environment for deployment")
  605. }
  606. nsList, err := t.client.GetK8sNamespaces(
  607. context.Background(), t.projectID, t.clusterID,
  608. )
  609. if err != nil {
  610. return fmt.Errorf("error fetching namespaces: %w", err)
  611. }
  612. found := false
  613. for _, ns := range *nsList {
  614. if ns.Name == t.namespace {
  615. found = true
  616. break
  617. }
  618. }
  619. if !found {
  620. if isSystemNamespace(t.namespace) {
  621. return fmt.Errorf("attempting to deploy to system namespace '%s' which does not exist, please create it "+
  622. "to continue", t.namespace)
  623. }
  624. createNS := &types.CreateNamespaceRequest{
  625. Name: t.namespace,
  626. }
  627. if len(deplEnv.NamespaceAnnotations) > 0 {
  628. createNS.Annotations = deplEnv.NamespaceAnnotations
  629. }
  630. // create the new namespace
  631. _, err := t.client.CreateNewK8sNamespace(context.Background(), t.projectID, t.clusterID, createNS)
  632. if err != nil && !strings.Contains(err.Error(), "namespace already exists") {
  633. // ignore the error if the namespace already exists
  634. //
  635. // this might happen if someone creates the namespace in between this operation
  636. return fmt.Errorf("error creating namespace: %w", err)
  637. }
  638. }
  639. // attempt to read the deployment -- if it doesn't exist, create it
  640. _, err = t.client.GetDeployment(
  641. context.Background(),
  642. t.projectID, t.clusterID, t.envID,
  643. &types.GetDeploymentRequest{
  644. PRNumber: t.prID,
  645. },
  646. )
  647. if err != nil && strings.Contains(err.Error(), "not found") {
  648. // in this case, create the deployment
  649. _, err = t.client.CreateDeployment(
  650. context.Background(),
  651. t.projectID, t.gitInstallationID, t.clusterID,
  652. t.repoOwner, t.repoName,
  653. &types.CreateDeploymentRequest{
  654. Namespace: t.namespace,
  655. PullRequestID: t.prID,
  656. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  657. ActionID: t.actionID,
  658. },
  659. GitHubMetadata: &types.GitHubMetadata{
  660. PRName: t.prName,
  661. RepoName: t.repoName,
  662. RepoOwner: t.repoOwner,
  663. CommitSHA: t.commitSHA,
  664. PRBranchFrom: t.branchFrom,
  665. PRBranchInto: t.branchInto,
  666. },
  667. },
  668. )
  669. } else if err == nil {
  670. _, err = t.client.UpdateDeployment(
  671. context.Background(),
  672. t.projectID, t.gitInstallationID, t.clusterID,
  673. t.repoOwner, t.repoName,
  674. &types.UpdateDeploymentRequest{
  675. Namespace: t.namespace,
  676. PRNumber: t.prID,
  677. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  678. ActionID: t.actionID,
  679. },
  680. PRBranchFrom: t.branchFrom,
  681. CommitSHA: t.commitSHA,
  682. },
  683. )
  684. }
  685. return err
  686. }
  687. func (t *DeploymentHook) DataQueries() map[string]interface{} {
  688. res := make(map[string]interface{})
  689. // use the resource group to find all web applications that can have an exposed subdomain
  690. // that we can query for
  691. for _, resource := range t.resourceGroup.Resources {
  692. isWeb := false
  693. if sourceNameInter, exists := resource.Source["name"]; exists {
  694. if sourceName, ok := sourceNameInter.(string); ok {
  695. if sourceName == "web" {
  696. isWeb = true
  697. }
  698. }
  699. }
  700. if isWeb {
  701. // determine if we should query for porter_hosts or just hosts
  702. isCustomDomain := false
  703. ingressMap, err := deploy.GetNestedMap(resource.Config, "values", "ingress")
  704. if err == nil {
  705. enabledVal, enabledExists := ingressMap["enabled"]
  706. customDomVal, customDomExists := ingressMap["custom_domain"]
  707. if enabledExists && customDomExists {
  708. enabled, eOK := enabledVal.(bool)
  709. customDomain, cOK := customDomVal.(bool)
  710. if eOK && cOK && enabled {
  711. if customDomain {
  712. // return the first custom domain when one exists
  713. hostsArr, hostsExists := ingressMap["hosts"]
  714. if hostsExists {
  715. hostsArrVal, hostsArrOk := hostsArr.([]interface{})
  716. if hostsArrOk && len(hostsArrVal) > 0 {
  717. if _, ok := hostsArrVal[0].(string); ok {
  718. res[resource.Name] = fmt.Sprintf("{ .%s.ingress.hosts[0] }", resource.Name)
  719. isCustomDomain = true
  720. }
  721. }
  722. }
  723. }
  724. }
  725. }
  726. }
  727. if !isCustomDomain {
  728. res[resource.Name] = fmt.Sprintf("{ .%s.ingress.porter_hosts[0] }", resource.Name)
  729. }
  730. }
  731. }
  732. return res
  733. }
  734. func (t *DeploymentHook) PostApply(populatedData map[string]interface{}) error {
  735. subdomains := make([]string, 0)
  736. for _, data := range populatedData {
  737. domain, ok := data.(string)
  738. if !ok {
  739. continue
  740. }
  741. if _, err := url.Parse("https://" + domain); err == nil {
  742. subdomains = append(subdomains, "https://"+domain)
  743. }
  744. }
  745. req := &types.FinalizeDeploymentRequest{
  746. PRNumber: t.prID,
  747. Subdomain: strings.Join(subdomains, ", "),
  748. }
  749. for _, res := range t.resourceGroup.Resources {
  750. releaseType := getReleaseType(res)
  751. releaseName := getReleaseName(res)
  752. if releaseType != "" && releaseName != "" {
  753. req.SuccessfulResources = append(req.SuccessfulResources, &types.SuccessfullyDeployedResource{
  754. ReleaseName: releaseName,
  755. ReleaseType: releaseType,
  756. })
  757. }
  758. }
  759. // finalize the deployment
  760. _, err := t.client.FinalizeDeployment(
  761. context.Background(),
  762. t.projectID, t.gitInstallationID, t.clusterID,
  763. t.repoOwner, t.repoName, req,
  764. )
  765. return err
  766. }
  767. func (t *DeploymentHook) OnError(error) {
  768. // if the deployment exists, throw an error for that deployment
  769. _, err := t.client.GetDeployment(
  770. context.Background(),
  771. t.projectID, t.clusterID, t.envID,
  772. &types.GetDeploymentRequest{
  773. PRNumber: t.prID,
  774. },
  775. )
  776. if err == nil {
  777. // FIXME: try to use the error with a custom logger
  778. t.client.UpdateDeploymentStatus(
  779. context.Background(),
  780. t.projectID, t.gitInstallationID, t.clusterID,
  781. t.repoOwner, t.repoName,
  782. &types.UpdateDeploymentStatusRequest{
  783. PRNumber: t.prID,
  784. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  785. ActionID: t.actionID,
  786. },
  787. PRBranchFrom: t.branchFrom,
  788. Status: string(types.DeploymentStatusFailed),
  789. },
  790. )
  791. }
  792. }
  793. func (t *DeploymentHook) OnConsolidatedErrors(allErrors map[string]error) {
  794. // if the deployment exists, throw an error for that deployment
  795. _, getDeplErr := t.client.GetDeployment(
  796. context.Background(),
  797. t.projectID, t.clusterID, t.envID,
  798. &types.GetDeploymentRequest{
  799. PRNumber: t.prID,
  800. },
  801. )
  802. if getDeplErr == nil {
  803. req := &types.FinalizeDeploymentWithErrorsRequest{
  804. PRNumber: t.prID,
  805. Errors: make(map[string]string),
  806. }
  807. for _, res := range t.resourceGroup.Resources {
  808. if _, ok := allErrors[res.Name]; !ok {
  809. req.SuccessfulResources = append(req.SuccessfulResources, &types.SuccessfullyDeployedResource{
  810. ReleaseName: getReleaseName(res),
  811. ReleaseType: getReleaseType(res),
  812. })
  813. }
  814. }
  815. for res, err := range allErrors {
  816. req.Errors[res] = err.Error()
  817. }
  818. // FIXME: handle the error
  819. t.client.FinalizeDeploymentWithErrors(
  820. context.Background(),
  821. t.projectID, t.gitInstallationID, t.clusterID,
  822. t.repoOwner, t.repoName,
  823. req,
  824. )
  825. }
  826. }
  827. type CloneEnvGroupHook struct {
  828. client *api.Client
  829. resGroup *switchboardTypes.ResourceGroup
  830. }
  831. func NewCloneEnvGroupHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup) *CloneEnvGroupHook {
  832. return &CloneEnvGroupHook{
  833. client: client,
  834. resGroup: resourceGroup,
  835. }
  836. }
  837. func (t *CloneEnvGroupHook) PreApply() error {
  838. for _, res := range t.resGroup.Resources {
  839. if res.Driver == "env-group" {
  840. continue
  841. }
  842. appConf := &previewInt.ApplicationConfig{}
  843. err := mapstructure.Decode(res.Config, &appConf)
  844. if err != nil {
  845. continue
  846. }
  847. if appConf != nil && len(appConf.EnvGroups) > 0 {
  848. target, err := preview.GetTarget(res.Name, res.Target)
  849. if err != nil {
  850. return err
  851. }
  852. for _, group := range appConf.EnvGroups {
  853. if group.Name == "" {
  854. return fmt.Errorf("env group name cannot be empty")
  855. }
  856. _, err := t.client.GetEnvGroup(
  857. context.Background(),
  858. target.Project,
  859. target.Cluster,
  860. target.Namespace,
  861. &types.GetEnvGroupRequest{
  862. Name: group.Name,
  863. Version: group.Version,
  864. },
  865. )
  866. if err != nil && err.Error() == "env group not found" {
  867. if group.Namespace == "" {
  868. return fmt.Errorf("env group namespace cannot be empty")
  869. }
  870. color.New(color.FgBlue, color.Bold).
  871. Printf("Env group '%s' does not exist in the target namespace '%s'\n", group.Name, target.Namespace)
  872. color.New(color.FgBlue, color.Bold).
  873. Printf("Cloning env group '%s' from namespace '%s' to target namespace '%s'\n",
  874. group.Name, group.Namespace, target.Namespace)
  875. _, err = t.client.CloneEnvGroup(
  876. context.Background(), target.Project, target.Cluster, group.Namespace,
  877. &types.CloneEnvGroupRequest{
  878. Name: group.Name,
  879. Namespace: target.Namespace,
  880. },
  881. )
  882. if err != nil {
  883. return err
  884. }
  885. } else if err != nil {
  886. return err
  887. }
  888. }
  889. }
  890. }
  891. return nil
  892. }
  893. func (t *CloneEnvGroupHook) DataQueries() map[string]interface{} {
  894. return nil
  895. }
  896. func (t *CloneEnvGroupHook) PostApply(map[string]interface{}) error {
  897. return nil
  898. }
  899. func (t *CloneEnvGroupHook) OnError(error) {}
  900. func (t *CloneEnvGroupHook) OnConsolidatedErrors(map[string]error) {}
  901. func getReleaseName(res *switchboardTypes.Resource) string {
  902. // can ignore the error because this method is called once
  903. // GetTarget has alrealy been called and validated previously
  904. target, _ := preview.GetTarget(res.Name, res.Target)
  905. if target.AppName != "" {
  906. return target.AppName
  907. }
  908. return res.Name
  909. }
  910. func getReleaseType(res *switchboardTypes.Resource) string {
  911. // can ignore the error because this method is called once
  912. // GetSource has alrealy been called and validated previously
  913. source, _ := preview.GetSource(res.Name, res.Source)
  914. if source != nil && source.Name != "" {
  915. return source.Name
  916. }
  917. return ""
  918. }
  919. func isSystemNamespace(namespace string) bool {
  920. return namespace == "cert-manager" || namespace == "ingress-nginx" ||
  921. namespace == "kube-node-lease" || namespace == "kube-public" ||
  922. namespace == "kube-system" || namespace == "monitoring" ||
  923. namespace == "porter-agent-system" || namespace == "default" ||
  924. namespace == "ingress-nginx-private"
  925. }