apply.go 33 KB

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