apply.go 32 KB

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