apply.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. if appConf.InjectBuild {
  517. // use the built image in the values if it is set
  518. // if it contains a $, then the query did not resolve
  519. if appConf.Build.Image != "" && !strings.Contains(appConf.Build.Image, "$") {
  520. imageSpl := strings.Split(appConf.Build.Image, ":")
  521. if len(imageSpl) == 2 {
  522. appConf.Values["image"] = map[string]interface{}{
  523. "repository": imageSpl[0],
  524. "tag": imageSpl[1],
  525. }
  526. } else {
  527. return nil, fmt.Errorf("could not parse image info %s", appConf.Build.Image)
  528. }
  529. }
  530. }
  531. err = updateAgent.UpdateImageAndValues(appConf.Values)
  532. if err != nil {
  533. return nil, err
  534. }
  535. return resource, nil
  536. }
  537. func (d *DeployDriver) assignOutput(resource *switchboardModels.Resource, client *api.Client) error {
  538. release, err := client.GetRelease(
  539. context.Background(),
  540. d.target.Project,
  541. d.target.Cluster,
  542. d.target.Namespace,
  543. resource.Name,
  544. )
  545. if err != nil {
  546. return err
  547. }
  548. d.output = utils.CoalesceValues(d.source.SourceValues, release.Config)
  549. return nil
  550. }
  551. func (d *DeployDriver) Output() (map[string]interface{}, error) {
  552. return d.output, nil
  553. }
  554. func (d *DeployDriver) getApplicationConfig(resource *switchboardModels.Resource) (*previewInt.ApplicationConfig, error) {
  555. populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
  556. RawConf: resource.Config,
  557. LookupTable: *d.lookupTable,
  558. Dependencies: resource.Dependencies,
  559. })
  560. if err != nil {
  561. return nil, err
  562. }
  563. appConf := &previewInt.ApplicationConfig{}
  564. err = mapstructure.Decode(populatedConf, appConf)
  565. if err != nil {
  566. return nil, err
  567. }
  568. if _, ok := resource.Config["waitForJob"]; !ok && d.source.Name == "job" {
  569. // default to true and wait for the job to finish
  570. appConf.WaitForJob = true
  571. }
  572. return appConf, nil
  573. }
  574. func (d *DeployDriver) getAddonConfig(resource *switchboardModels.Resource) (map[string]interface{}, error) {
  575. return drivers.ConstructConfig(&drivers.ConstructConfigOpts{
  576. RawConf: resource.Config,
  577. LookupTable: *d.lookupTable,
  578. Dependencies: resource.Dependencies,
  579. })
  580. }
  581. type DeploymentHook struct {
  582. client *api.Client
  583. resourceGroup *switchboardTypes.ResourceGroup
  584. gitInstallationID, projectID, clusterID, prID, actionID, envID uint
  585. branchFrom, branchInto, namespace, repoName, repoOwner, prName, commitSHA string
  586. }
  587. func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup, namespace string) (*DeploymentHook, error) {
  588. res := &DeploymentHook{
  589. client: client,
  590. resourceGroup: resourceGroup,
  591. namespace: namespace,
  592. }
  593. ghIDStr := os.Getenv("PORTER_GIT_INSTALLATION_ID")
  594. ghID, err := strconv.Atoi(ghIDStr)
  595. if err != nil {
  596. return nil, err
  597. }
  598. res.gitInstallationID = uint(ghID)
  599. prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID")
  600. prID, err := strconv.Atoi(prIDStr)
  601. if err != nil {
  602. return nil, err
  603. }
  604. res.prID = uint(prID)
  605. res.projectID = cliConf.Project
  606. if res.projectID == 0 {
  607. return nil, fmt.Errorf("project id must be set")
  608. }
  609. res.clusterID = cliConf.Cluster
  610. if res.clusterID == 0 {
  611. return nil, fmt.Errorf("cluster id must be set")
  612. }
  613. branchFrom := os.Getenv("PORTER_BRANCH_FROM")
  614. res.branchFrom = branchFrom
  615. branchInto := os.Getenv("PORTER_BRANCH_INTO")
  616. res.branchInto = branchInto
  617. actionIDStr := os.Getenv("PORTER_ACTION_ID")
  618. actionID, err := strconv.Atoi(actionIDStr)
  619. if err != nil {
  620. return nil, err
  621. }
  622. res.actionID = uint(actionID)
  623. repoName := os.Getenv("PORTER_REPO_NAME")
  624. res.repoName = repoName
  625. repoOwner := os.Getenv("PORTER_REPO_OWNER")
  626. res.repoOwner = repoOwner
  627. prName := os.Getenv("PORTER_PR_NAME")
  628. res.prName = prName
  629. commit, err := git.LastCommit()
  630. if err != nil {
  631. return nil, fmt.Errorf(err.Error())
  632. }
  633. res.commitSHA = commit.Sha[:7]
  634. return res, nil
  635. }
  636. func (t *DeploymentHook) isBranchDeploy() bool {
  637. return t.branchFrom != "" && t.branchInto != "" && t.branchFrom == t.branchInto
  638. }
  639. func (t *DeploymentHook) PreApply() error {
  640. if isSystemNamespace(t.namespace) {
  641. color.New(color.FgYellow).Printf("attempting to deploy to system namespace '%s'\n", t.namespace)
  642. }
  643. envList, err := t.client.ListEnvironments(
  644. context.Background(), t.projectID, t.clusterID,
  645. )
  646. if err != nil {
  647. return err
  648. }
  649. envs := *envList
  650. var deplEnv *types.Environment
  651. for _, env := range envs {
  652. if strings.EqualFold(env.GitRepoOwner, t.repoOwner) &&
  653. strings.EqualFold(env.GitRepoName, t.repoName) &&
  654. env.GitInstallationID == t.gitInstallationID {
  655. t.envID = env.ID
  656. deplEnv = env
  657. break
  658. }
  659. }
  660. if t.envID == 0 {
  661. return fmt.Errorf("could not find environment for deployment")
  662. }
  663. nsList, err := t.client.GetK8sNamespaces(
  664. context.Background(), t.projectID, t.clusterID,
  665. )
  666. if err != nil {
  667. return fmt.Errorf("error fetching namespaces: %w", err)
  668. }
  669. found := false
  670. for _, ns := range *nsList {
  671. if ns.Name == t.namespace {
  672. found = true
  673. break
  674. }
  675. }
  676. if !found {
  677. if isSystemNamespace(t.namespace) {
  678. return fmt.Errorf("attempting to deploy to system namespace '%s' which does not exist, please create it "+
  679. "to continue", t.namespace)
  680. }
  681. createNS := &types.CreateNamespaceRequest{
  682. Name: t.namespace,
  683. }
  684. if len(deplEnv.NamespaceLabels) > 0 {
  685. createNS.Labels = deplEnv.NamespaceLabels
  686. }
  687. // create the new namespace
  688. _, err := t.client.CreateNewK8sNamespace(context.Background(), t.projectID, t.clusterID, createNS)
  689. if err != nil && !strings.Contains(err.Error(), "namespace already exists") {
  690. // ignore the error if the namespace already exists
  691. //
  692. // this might happen if someone creates the namespace in between this operation
  693. return fmt.Errorf("error creating namespace: %w", err)
  694. }
  695. }
  696. var deplErr error
  697. if t.isBranchDeploy() {
  698. _, deplErr = t.client.GetDeployment(
  699. context.Background(),
  700. t.projectID, t.clusterID, t.envID,
  701. &types.GetDeploymentRequest{
  702. Branch: t.branchFrom,
  703. },
  704. )
  705. } else {
  706. _, deplErr = t.client.GetDeployment(
  707. context.Background(),
  708. t.projectID, t.clusterID, t.envID,
  709. &types.GetDeploymentRequest{
  710. PRNumber: t.prID,
  711. },
  712. )
  713. }
  714. if deplErr != nil && strings.Contains(deplErr.Error(), "not found") {
  715. // in this case, create the deployment
  716. createReq := &types.CreateDeploymentRequest{
  717. Namespace: t.namespace,
  718. PullRequestID: t.prID,
  719. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  720. ActionID: t.actionID,
  721. },
  722. GitHubMetadata: &types.GitHubMetadata{
  723. PRName: t.prName,
  724. RepoName: t.repoName,
  725. RepoOwner: t.repoOwner,
  726. CommitSHA: t.commitSHA,
  727. PRBranchFrom: t.branchFrom,
  728. PRBranchInto: t.branchInto,
  729. },
  730. }
  731. if t.isBranchDeploy() {
  732. createReq.PullRequestID = 0
  733. }
  734. _, err = t.client.CreateDeployment(
  735. context.Background(),
  736. t.projectID, t.clusterID, createReq,
  737. )
  738. } else if err == nil {
  739. updateReq := &types.UpdateDeploymentByClusterRequest{
  740. RepoOwner: t.repoOwner,
  741. RepoName: t.repoName,
  742. Namespace: t.namespace,
  743. PRNumber: t.prID,
  744. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  745. ActionID: t.actionID,
  746. },
  747. PRBranchFrom: t.branchFrom,
  748. CommitSHA: t.commitSHA,
  749. }
  750. if t.isBranchDeploy() {
  751. updateReq.PRNumber = 0
  752. }
  753. _, err = t.client.UpdateDeployment(context.Background(), t.projectID, t.clusterID, updateReq)
  754. }
  755. return err
  756. }
  757. func (t *DeploymentHook) DataQueries() map[string]interface{} {
  758. res := make(map[string]interface{})
  759. // use the resource group to find all web applications that can have an exposed subdomain
  760. // that we can query for
  761. for _, resource := range t.resourceGroup.Resources {
  762. isWeb := false
  763. if sourceNameInter, exists := resource.Source["name"]; exists {
  764. if sourceName, ok := sourceNameInter.(string); ok {
  765. if sourceName == "web" {
  766. isWeb = true
  767. }
  768. }
  769. }
  770. if isWeb {
  771. // determine if we should query for porter_hosts or just hosts
  772. isCustomDomain := false
  773. ingressMap, err := deploy.GetNestedMap(resource.Config, "values", "ingress")
  774. if err == nil {
  775. enabledVal, enabledExists := ingressMap["enabled"]
  776. customDomVal, customDomExists := ingressMap["custom_domain"]
  777. if enabledExists && customDomExists {
  778. enabled, eOK := enabledVal.(bool)
  779. customDomain, cOK := customDomVal.(bool)
  780. if eOK && cOK && enabled {
  781. if customDomain {
  782. // return the first custom domain when one exists
  783. hostsArr, hostsExists := ingressMap["hosts"]
  784. if hostsExists {
  785. hostsArrVal, hostsArrOk := hostsArr.([]interface{})
  786. if hostsArrOk && len(hostsArrVal) > 0 {
  787. if _, ok := hostsArrVal[0].(string); ok {
  788. res[resource.Name] = fmt.Sprintf("{ .%s.ingress.hosts[0] }", resource.Name)
  789. isCustomDomain = true
  790. }
  791. }
  792. }
  793. }
  794. }
  795. }
  796. }
  797. if !isCustomDomain {
  798. res[resource.Name] = fmt.Sprintf("{ .%s.ingress.porter_hosts[0] }", resource.Name)
  799. }
  800. }
  801. }
  802. return res
  803. }
  804. func (t *DeploymentHook) PostApply(populatedData map[string]interface{}) error {
  805. subdomains := make([]string, 0)
  806. for _, data := range populatedData {
  807. domain, ok := data.(string)
  808. if !ok {
  809. continue
  810. }
  811. if _, err := url.Parse("https://" + domain); err == nil {
  812. subdomains = append(subdomains, "https://"+domain)
  813. }
  814. }
  815. req := &types.FinalizeDeploymentByClusterRequest{
  816. RepoOwner: t.repoOwner,
  817. RepoName: t.repoName,
  818. Subdomain: strings.Join(subdomains, ", "),
  819. }
  820. if t.isBranchDeploy() {
  821. req.Namespace = t.namespace
  822. } else {
  823. req.PRNumber = t.prID
  824. }
  825. for _, res := range t.resourceGroup.Resources {
  826. releaseType := getReleaseType(t.projectID, res)
  827. releaseName := getReleaseName(res)
  828. if releaseType != "" && releaseName != "" {
  829. req.SuccessfulResources = append(req.SuccessfulResources, &types.SuccessfullyDeployedResource{
  830. ReleaseName: releaseName,
  831. ReleaseType: releaseType,
  832. })
  833. }
  834. }
  835. // finalize the deployment
  836. _, err := t.client.FinalizeDeployment(context.Background(), t.projectID, t.clusterID, req)
  837. return err
  838. }
  839. func (t *DeploymentHook) OnError(error) {
  840. var deplErr error
  841. if t.isBranchDeploy() {
  842. _, deplErr = t.client.GetDeployment(
  843. context.Background(),
  844. t.projectID, t.clusterID, t.envID,
  845. &types.GetDeploymentRequest{
  846. Branch: t.branchFrom,
  847. },
  848. )
  849. } else {
  850. _, deplErr = t.client.GetDeployment(
  851. context.Background(),
  852. t.projectID, t.clusterID, t.envID,
  853. &types.GetDeploymentRequest{
  854. PRNumber: t.prID,
  855. },
  856. )
  857. }
  858. // if the deployment exists, throw an error for that deployment
  859. if deplErr == nil {
  860. req := &types.UpdateDeploymentStatusByClusterRequest{
  861. RepoOwner: t.repoOwner,
  862. RepoName: t.repoName,
  863. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  864. ActionID: t.actionID,
  865. },
  866. PRBranchFrom: t.branchFrom,
  867. Status: string(types.DeploymentStatusFailed),
  868. }
  869. if t.isBranchDeploy() {
  870. req.Namespace = t.namespace
  871. } else {
  872. req.PRNumber = t.prID
  873. }
  874. // FIXME: try to use the error with a custom logger
  875. t.client.UpdateDeploymentStatus(context.Background(), t.projectID, t.clusterID, req)
  876. }
  877. }
  878. func (t *DeploymentHook) OnConsolidatedErrors(allErrors map[string]error) {
  879. var deplErr error
  880. if t.isBranchDeploy() {
  881. _, deplErr = t.client.GetDeployment(
  882. context.Background(),
  883. t.projectID, t.clusterID, t.envID,
  884. &types.GetDeploymentRequest{
  885. Branch: t.branchFrom,
  886. },
  887. )
  888. } else {
  889. _, deplErr = t.client.GetDeployment(
  890. context.Background(),
  891. t.projectID, t.clusterID, t.envID,
  892. &types.GetDeploymentRequest{
  893. PRNumber: t.prID,
  894. },
  895. )
  896. }
  897. // if the deployment exists, throw an error for that deployment
  898. if deplErr == nil {
  899. req := &types.FinalizeDeploymentWithErrorsByClusterRequest{
  900. RepoOwner: t.repoOwner,
  901. RepoName: t.repoName,
  902. Errors: make(map[string]string),
  903. }
  904. if t.isBranchDeploy() {
  905. req.Namespace = t.namespace
  906. } else {
  907. req.PRNumber = t.prID
  908. }
  909. for _, res := range t.resourceGroup.Resources {
  910. if _, ok := allErrors[res.Name]; !ok {
  911. req.SuccessfulResources = append(req.SuccessfulResources, &types.SuccessfullyDeployedResource{
  912. ReleaseName: getReleaseName(res),
  913. ReleaseType: getReleaseType(t.projectID, res),
  914. })
  915. }
  916. }
  917. for res, err := range allErrors {
  918. req.Errors[res] = err.Error()
  919. }
  920. // FIXME: handle the error
  921. t.client.FinalizeDeploymentWithErrors(context.Background(), t.projectID, t.clusterID, req)
  922. }
  923. }
  924. type CloneEnvGroupHook struct {
  925. client *api.Client
  926. resGroup *switchboardTypes.ResourceGroup
  927. }
  928. func NewCloneEnvGroupHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup) *CloneEnvGroupHook {
  929. return &CloneEnvGroupHook{
  930. client: client,
  931. resGroup: resourceGroup,
  932. }
  933. }
  934. func (t *CloneEnvGroupHook) PreApply() error {
  935. for _, res := range t.resGroup.Resources {
  936. if res.Driver == "env-group" {
  937. continue
  938. }
  939. appConf := &previewInt.ApplicationConfig{}
  940. err := mapstructure.Decode(res.Config, &appConf)
  941. if err != nil {
  942. continue
  943. }
  944. if appConf != nil && len(appConf.EnvGroups) > 0 {
  945. target, err := preview.GetTarget(res.Name, res.Target)
  946. if err != nil {
  947. return err
  948. }
  949. for _, group := range appConf.EnvGroups {
  950. if group.Name == "" {
  951. return fmt.Errorf("env group name cannot be empty")
  952. }
  953. _, err := t.client.GetEnvGroup(
  954. context.Background(),
  955. target.Project,
  956. target.Cluster,
  957. target.Namespace,
  958. &types.GetEnvGroupRequest{
  959. Name: group.Name,
  960. Version: group.Version,
  961. },
  962. )
  963. if err != nil && err.Error() == "env group not found" {
  964. if group.Namespace == "" {
  965. return fmt.Errorf("env group namespace cannot be empty")
  966. }
  967. color.New(color.FgBlue, color.Bold).
  968. Printf("Env group '%s' does not exist in the target namespace '%s'\n", group.Name, target.Namespace)
  969. color.New(color.FgBlue, color.Bold).
  970. Printf("Cloning env group '%s' from namespace '%s' to target namespace '%s'\n",
  971. group.Name, group.Namespace, target.Namespace)
  972. _, err = t.client.CloneEnvGroup(
  973. context.Background(), target.Project, target.Cluster, group.Namespace,
  974. &types.CloneEnvGroupRequest{
  975. SourceName: group.Name,
  976. TargetNamespace: target.Namespace,
  977. },
  978. )
  979. if err != nil {
  980. return err
  981. }
  982. } else if err != nil {
  983. return err
  984. }
  985. }
  986. }
  987. }
  988. return nil
  989. }
  990. func (t *CloneEnvGroupHook) DataQueries() map[string]interface{} {
  991. return nil
  992. }
  993. func (t *CloneEnvGroupHook) PostApply(map[string]interface{}) error {
  994. return nil
  995. }
  996. func (t *CloneEnvGroupHook) OnError(error) {}
  997. func (t *CloneEnvGroupHook) OnConsolidatedErrors(map[string]error) {}
  998. func getReleaseName(res *switchboardTypes.Resource) string {
  999. // can ignore the error because this method is called once
  1000. // GetTarget has alrealy been called and validated previously
  1001. target, _ := preview.GetTarget(res.Name, res.Target)
  1002. if target.AppName != "" {
  1003. return target.AppName
  1004. }
  1005. return res.Name
  1006. }
  1007. func getReleaseType(projectID uint, res *switchboardTypes.Resource) string {
  1008. // can ignore the error because this method is called once
  1009. // GetSource has alrealy been called and validated previously
  1010. source, _ := preview.GetSource(projectID, res.Name, res.Source)
  1011. if source != nil && source.Name != "" {
  1012. return source.Name
  1013. }
  1014. return ""
  1015. }
  1016. func isSystemNamespace(namespace string) bool {
  1017. return namespace == "cert-manager" || namespace == "ingress-nginx" ||
  1018. namespace == "kube-node-lease" || namespace == "kube-public" ||
  1019. namespace == "kube-system" || namespace == "monitoring" ||
  1020. namespace == "porter-agent-system" || namespace == "default" ||
  1021. namespace == "ingress-nginx-private"
  1022. }
  1023. type ErrorEmitterHook struct{}
  1024. func NewErrorEmitterHook(*api.Client, *switchboardTypes.ResourceGroup) *ErrorEmitterHook {
  1025. return &ErrorEmitterHook{}
  1026. }
  1027. func (t *ErrorEmitterHook) PreApply() error {
  1028. return nil
  1029. }
  1030. func (t *ErrorEmitterHook) DataQueries() map[string]interface{} {
  1031. return nil
  1032. }
  1033. func (t *ErrorEmitterHook) PostApply(map[string]interface{}) error {
  1034. return nil
  1035. }
  1036. func (t *ErrorEmitterHook) OnError(err error) {
  1037. color.New(color.FgRed).Fprintf(os.Stderr, "Errors while building: %s\n", err.Error())
  1038. }
  1039. func (t *ErrorEmitterHook) OnConsolidatedErrors(errMap map[string]error) {
  1040. color.New(color.FgRed).Fprintf(os.Stderr, "Errors while building:\n")
  1041. for resName, err := range errMap {
  1042. color.New(color.FgRed).Fprintf(os.Stderr, " - %s: %s\n", resName, err.Error())
  1043. }
  1044. }