2
0

apply.go 32 KB

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