apply.go 35 KB

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