2
0

run.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. package cmd
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strings"
  9. "time"
  10. "github.com/fatih/color"
  11. api "github.com/porter-dev/porter/api/client"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/cli/cmd/utils"
  14. "github.com/spf13/cobra"
  15. batchv1 "k8s.io/api/batch/v1"
  16. v1 "k8s.io/api/core/v1"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/fields"
  20. "k8s.io/apimachinery/pkg/watch"
  21. "k8s.io/kubectl/pkg/util/term"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "k8s.io/client-go/kubernetes"
  25. "k8s.io/client-go/rest"
  26. "k8s.io/client-go/tools/clientcmd"
  27. "k8s.io/client-go/tools/remotecommand"
  28. )
  29. var namespace string
  30. var verbose bool
  31. var existingPod bool
  32. var nonInteractive bool
  33. var containerName string
  34. // runCmd represents the "porter run" base command when called
  35. // without any subcommands
  36. var runCmd = &cobra.Command{
  37. Use: "run [release] -- COMMAND [args...]",
  38. Args: cobra.MinimumNArgs(2),
  39. Short: "Runs a command inside a connected cluster container.",
  40. Run: func(cmd *cobra.Command, args []string) {
  41. err := checkLoginAndRun(args, run)
  42. if err != nil {
  43. os.Exit(1)
  44. }
  45. },
  46. }
  47. // cleanupCmd represents the "porter run cleanup" subcommand
  48. var cleanupCmd = &cobra.Command{
  49. Use: "cleanup",
  50. Args: cobra.NoArgs,
  51. Short: "Delete any lingering ephemeral pods that were created with \"porter run\".",
  52. Run: func(cmd *cobra.Command, args []string) {
  53. err := checkLoginAndRun(args, cleanup)
  54. if err != nil {
  55. os.Exit(1)
  56. }
  57. },
  58. }
  59. func init() {
  60. rootCmd.AddCommand(runCmd)
  61. runCmd.PersistentFlags().StringVar(
  62. &namespace,
  63. "namespace",
  64. "default",
  65. "namespace of release to connect to",
  66. )
  67. runCmd.PersistentFlags().BoolVarP(
  68. &existingPod,
  69. "existing_pod",
  70. "e",
  71. false,
  72. "whether to connect to an existing pod",
  73. )
  74. runCmd.PersistentFlags().BoolVarP(
  75. &verbose,
  76. "verbose",
  77. "v",
  78. false,
  79. "whether to print verbose output",
  80. )
  81. runCmd.PersistentFlags().BoolVar(
  82. &nonInteractive,
  83. "non-interactive",
  84. false,
  85. "whether to run in non-interactive mode",
  86. )
  87. runCmd.PersistentFlags().StringVarP(
  88. &containerName,
  89. "container",
  90. "c",
  91. "",
  92. "name of the container inside pod to run the command in",
  93. )
  94. runCmd.AddCommand(cleanupCmd)
  95. }
  96. func run(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  97. execArgs := args[1:]
  98. color.New(color.FgGreen).Println("Running", strings.Join(execArgs, " "), "for release", args[0])
  99. if nonInteractive {
  100. color.New(color.FgBlue).Println("Using non-interactive mode. The first available pod will be used to run the command.")
  101. }
  102. if len(execArgs) > 0 {
  103. release, err := client.GetRelease(
  104. context.Background(), cliConf.Project, cliConf.Cluster, namespace, args[0],
  105. )
  106. if err != nil {
  107. return fmt.Errorf("error fetching release %s: %w", args[0], err)
  108. }
  109. if release.BuildConfig != nil &&
  110. (strings.Contains(release.BuildConfig.Builder, "heroku") ||
  111. strings.Contains(release.BuildConfig.Builder, "paketo")) &&
  112. execArgs[0] != "/cnb/lifecycle/launcher" &&
  113. execArgs[0] != "launcher" {
  114. // this is a buildpacks release using a heroku builder, prepend the launcher
  115. execArgs = append([]string{"/cnb/lifecycle/launcher"}, execArgs...)
  116. }
  117. }
  118. podsSimple, err := getPods(client, namespace, args[0])
  119. if err != nil {
  120. return fmt.Errorf("Could not retrieve list of pods: %s", err.Error())
  121. }
  122. // if length of pods is 0, throw error
  123. var selectedPod podSimple
  124. if len(podsSimple) == 0 {
  125. return fmt.Errorf("At least one pod must exist in this deployment.")
  126. } else if nonInteractive || len(podsSimple) == 1 {
  127. selectedPod = podsSimple[0]
  128. } else {
  129. podNames := make([]string, 0)
  130. for _, podSimple := range podsSimple {
  131. podNames = append(podNames, podSimple.Name)
  132. }
  133. selectedPodName, err := utils.PromptSelect("Select the pod:", podNames)
  134. if err != nil {
  135. return err
  136. }
  137. // find selected pod
  138. for _, podSimple := range podsSimple {
  139. if selectedPodName == podSimple.Name {
  140. selectedPod = podSimple
  141. }
  142. }
  143. }
  144. var selectedContainerName string
  145. if len(selectedPod.ContainerNames) == 0 {
  146. return fmt.Errorf("At least one container must exist in the selected pod.")
  147. } else if len(selectedPod.ContainerNames) == 1 {
  148. if containerName != "" && containerName != selectedPod.ContainerNames[0] {
  149. return fmt.Errorf("provided container %s does not exist in pod %s", containerName, selectedPod.Name)
  150. }
  151. selectedContainerName = selectedPod.ContainerNames[0]
  152. }
  153. if containerName != "" && selectedContainerName == "" {
  154. // check if provided container name exists in the pod
  155. for _, name := range selectedPod.ContainerNames {
  156. if name == containerName {
  157. selectedContainerName = name
  158. break
  159. }
  160. }
  161. if selectedContainerName == "" {
  162. return fmt.Errorf("provided container %s does not exist in pod %s", containerName, selectedPod.Name)
  163. }
  164. }
  165. if selectedContainerName == "" {
  166. if nonInteractive {
  167. return fmt.Errorf("container name must be specified using the --container flag when using non-interactive mode")
  168. }
  169. selectedContainer, err := utils.PromptSelect("Select the container:", selectedPod.ContainerNames)
  170. if err != nil {
  171. return err
  172. }
  173. selectedContainerName = selectedContainer
  174. }
  175. config := &PorterRunSharedConfig{
  176. Client: client,
  177. }
  178. err = config.setSharedConfig()
  179. if err != nil {
  180. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  181. }
  182. if existingPod {
  183. return executeRun(config, namespace, selectedPod.Name, selectedContainerName, execArgs)
  184. }
  185. return executeRunEphemeral(config, namespace, selectedPod.Name, selectedContainerName, execArgs)
  186. }
  187. func cleanup(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  188. config := &PorterRunSharedConfig{
  189. Client: client,
  190. }
  191. err := config.setSharedConfig()
  192. if err != nil {
  193. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  194. }
  195. proceed, err := utils.PromptSelect(
  196. fmt.Sprintf("You have chosen the '%s' namespace for cleanup. Do you want to proceed?", namespace),
  197. []string{"Yes", "No", "All namespaces"},
  198. )
  199. if err != nil {
  200. return err
  201. }
  202. if proceed == "No" {
  203. return nil
  204. }
  205. var podNames []string
  206. color.New(color.FgGreen).Println("Fetching ephemeral pods for cleanup")
  207. if proceed == "All namespaces" {
  208. namespaces, err := config.Clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
  209. if err != nil {
  210. return err
  211. }
  212. for _, namespace := range namespaces.Items {
  213. if pods, err := getEphemeralPods(namespace.Name, config.Clientset); err == nil {
  214. podNames = append(podNames, pods...)
  215. } else {
  216. return err
  217. }
  218. }
  219. } else {
  220. if pods, err := getEphemeralPods(namespace, config.Clientset); err == nil {
  221. podNames = append(podNames, pods...)
  222. } else {
  223. return err
  224. }
  225. }
  226. if len(podNames) == 0 {
  227. color.New(color.FgBlue).Println("No ephemeral pods to delete")
  228. return nil
  229. }
  230. selectedPods, err := utils.PromptMultiselect("Select ephemeral pods to delete", podNames)
  231. if err != nil {
  232. return err
  233. }
  234. for _, podName := range selectedPods {
  235. color.New(color.FgBlue).Printf("Deleting ephemeral pod: %s\n", podName)
  236. err = config.Clientset.CoreV1().Pods(namespace).Delete(
  237. context.Background(), podName, metav1.DeleteOptions{},
  238. )
  239. if err != nil {
  240. return err
  241. }
  242. }
  243. return nil
  244. }
  245. func getEphemeralPods(namespace string, clientset *kubernetes.Clientset) ([]string, error) {
  246. var podNames []string
  247. pods, err := clientset.CoreV1().Pods(namespace).List(
  248. context.Background(), metav1.ListOptions{LabelSelector: "porter/ephemeral-pod"},
  249. )
  250. if err != nil {
  251. return nil, err
  252. }
  253. for _, pod := range pods.Items {
  254. podNames = append(podNames, pod.Name)
  255. }
  256. return podNames, nil
  257. }
  258. type PorterRunSharedConfig struct {
  259. Client *api.Client
  260. RestConf *rest.Config
  261. Clientset *kubernetes.Clientset
  262. RestClient *rest.RESTClient
  263. }
  264. func (p *PorterRunSharedConfig) setSharedConfig() error {
  265. pID := cliConf.Project
  266. cID := cliConf.Cluster
  267. kubeResp, err := p.Client.GetKubeconfig(context.Background(), pID, cID, cliConf.Kubeconfig)
  268. if err != nil {
  269. return err
  270. }
  271. kubeBytes := kubeResp.Kubeconfig
  272. cmdConf, err := clientcmd.NewClientConfigFromBytes(kubeBytes)
  273. if err != nil {
  274. return err
  275. }
  276. restConf, err := cmdConf.ClientConfig()
  277. if err != nil {
  278. return err
  279. }
  280. restConf.GroupVersion = &schema.GroupVersion{
  281. Group: "api",
  282. Version: "v1",
  283. }
  284. restConf.NegotiatedSerializer = runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{})
  285. p.RestConf = restConf
  286. clientset, err := kubernetes.NewForConfig(restConf)
  287. if err != nil {
  288. return err
  289. }
  290. p.Clientset = clientset
  291. restClient, err := rest.RESTClientFor(restConf)
  292. if err != nil {
  293. return err
  294. }
  295. p.RestClient = restClient
  296. return nil
  297. }
  298. type podSimple struct {
  299. Name string
  300. ContainerNames []string
  301. }
  302. func getPods(client *api.Client, namespace, releaseName string) ([]podSimple, error) {
  303. pID := cliConf.Project
  304. cID := cliConf.Cluster
  305. resp, err := client.GetK8sAllPods(context.TODO(), pID, cID, namespace, releaseName)
  306. if err != nil {
  307. return nil, err
  308. }
  309. pods := *resp
  310. res := make([]podSimple, 0)
  311. for _, pod := range pods {
  312. if pod.Status.Phase == v1.PodRunning {
  313. containerNames := make([]string, 0)
  314. for _, container := range pod.Spec.Containers {
  315. containerNames = append(containerNames, container.Name)
  316. }
  317. res = append(res, podSimple{
  318. Name: pod.ObjectMeta.Name,
  319. ContainerNames: containerNames,
  320. })
  321. }
  322. }
  323. return res, nil
  324. }
  325. func executeRun(config *PorterRunSharedConfig, namespace, name, container string, args []string) error {
  326. req := config.RestClient.Post().
  327. Resource("pods").
  328. Name(name).
  329. Namespace(namespace).
  330. SubResource("exec")
  331. for _, arg := range args {
  332. req.Param("command", arg)
  333. }
  334. req.Param("stdin", "true")
  335. req.Param("stdout", "true")
  336. req.Param("tty", "true")
  337. req.Param("container", container)
  338. t := term.TTY{
  339. In: os.Stdin,
  340. Out: os.Stdout,
  341. Raw: true,
  342. }
  343. size := t.GetSize()
  344. sizeQueue := t.MonitorSize(size)
  345. return t.Safe(func() error {
  346. exec, err := remotecommand.NewSPDYExecutor(config.RestConf, "POST", req.URL())
  347. if err != nil {
  348. return err
  349. }
  350. return exec.Stream(remotecommand.StreamOptions{
  351. Stdin: os.Stdin,
  352. Stdout: os.Stdout,
  353. Stderr: os.Stderr,
  354. Tty: true,
  355. TerminalSizeQueue: sizeQueue,
  356. })
  357. })
  358. }
  359. func executeRunEphemeral(config *PorterRunSharedConfig, namespace, name, container string, args []string) error {
  360. existing, err := getExistingPod(config, name, namespace)
  361. if err != nil {
  362. return err
  363. }
  364. newPod, err := createEphemeralPodFromExisting(config, existing, container, args)
  365. if err != nil {
  366. return err
  367. }
  368. podName := newPod.ObjectMeta.Name
  369. // delete the ephemeral pod no matter what
  370. defer deletePod(config, podName, namespace)
  371. color.New(color.FgYellow).Printf("Waiting for pod %s to be ready...", podName)
  372. if err = waitForPod(config, newPod); err != nil {
  373. color.New(color.FgRed).Println("failed")
  374. return handlePodAttachError(err, config, namespace, podName, container)
  375. }
  376. err = checkForPodDeletionCronJob(config)
  377. if err != nil {
  378. return err
  379. }
  380. // refresh pod info for latest status
  381. newPod, err = config.Clientset.CoreV1().
  382. Pods(newPod.Namespace).
  383. Get(context.Background(), newPod.Name, metav1.GetOptions{})
  384. // pod exited while we were waiting. maybe an error maybe not.
  385. // we dont know if the user wanted an interactive shell or not.
  386. // if it was an error the logs hopefully say so.
  387. if isPodExited(newPod) {
  388. color.New(color.FgGreen).Println("complete!")
  389. var writtenBytes int64
  390. writtenBytes, _ = pipePodLogsToStdout(config, namespace, podName, container, false)
  391. if verbose || writtenBytes == 0 {
  392. color.New(color.FgYellow).Println("Could not get logs. Pod events:")
  393. pipeEventsToStdout(config, namespace, podName, container, false)
  394. }
  395. return nil
  396. }
  397. color.New(color.FgGreen).Println("ready!")
  398. color.New(color.FgYellow).Println("Attempting connection to the container. If you don't see a command prompt, try pressing enter.")
  399. req := config.RestClient.Post().
  400. Resource("pods").
  401. Name(podName).
  402. Namespace(namespace).
  403. SubResource("attach")
  404. req.Param("stdin", "true")
  405. req.Param("stdout", "true")
  406. req.Param("tty", "true")
  407. req.Param("container", container)
  408. t := term.TTY{
  409. In: os.Stdin,
  410. Out: os.Stdout,
  411. Raw: true,
  412. }
  413. size := t.GetSize()
  414. sizeQueue := t.MonitorSize(size)
  415. if err = t.Safe(func() error {
  416. exec, err := remotecommand.NewSPDYExecutor(config.RestConf, "POST", req.URL())
  417. if err != nil {
  418. return err
  419. }
  420. return exec.Stream(remotecommand.StreamOptions{
  421. Stdin: os.Stdin,
  422. Stdout: os.Stdout,
  423. Stderr: os.Stderr,
  424. Tty: true,
  425. TerminalSizeQueue: sizeQueue,
  426. })
  427. }); err != nil {
  428. // ugly way to catch no TTY errors, such as when running command "echo \"hello\""
  429. return handlePodAttachError(err, config, namespace, podName, container)
  430. }
  431. if verbose {
  432. color.New(color.FgYellow).Println("Pod events:")
  433. pipeEventsToStdout(config, namespace, podName, container, false)
  434. }
  435. return err
  436. }
  437. func checkForPodDeletionCronJob(config *PorterRunSharedConfig) error {
  438. // try and create the cron job and all of the other required resources as necessary,
  439. // starting with the service account, then role and then a role binding
  440. err := checkForServiceAccount(config)
  441. if err != nil {
  442. return err
  443. }
  444. err = checkForClusterRole(config)
  445. if err != nil {
  446. return err
  447. }
  448. err = checkForRoleBinding(config)
  449. if err != nil {
  450. return err
  451. }
  452. namespaces, err := config.Clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
  453. if err != nil {
  454. return err
  455. }
  456. for _, namespace := range namespaces.Items {
  457. cronJobs, err := config.Clientset.BatchV1().CronJobs(namespace.Name).List(
  458. context.Background(), metav1.ListOptions{},
  459. )
  460. if err != nil {
  461. return err
  462. }
  463. if namespace.Name == "default" {
  464. for _, cronJob := range cronJobs.Items {
  465. if cronJob.Name == "porter-ephemeral-pod-deletion-cronjob" {
  466. return nil
  467. }
  468. }
  469. } else {
  470. for _, cronJob := range cronJobs.Items {
  471. if cronJob.Name == "porter-ephemeral-pod-deletion-cronjob" {
  472. err = config.Clientset.BatchV1().CronJobs(namespace.Name).Delete(
  473. context.Background(), cronJob.Name, metav1.DeleteOptions{},
  474. )
  475. if err != nil {
  476. return err
  477. }
  478. }
  479. }
  480. }
  481. }
  482. // create the cronjob
  483. cronJob := &batchv1.CronJob{
  484. ObjectMeta: metav1.ObjectMeta{
  485. Name: "porter-ephemeral-pod-deletion-cronjob",
  486. },
  487. Spec: batchv1.CronJobSpec{
  488. Schedule: "0 * * * *",
  489. JobTemplate: batchv1.JobTemplateSpec{
  490. Spec: batchv1.JobSpec{
  491. Template: v1.PodTemplateSpec{
  492. Spec: v1.PodSpec{
  493. ServiceAccountName: "porter-ephemeral-pod-deletion-service-account",
  494. RestartPolicy: v1.RestartPolicyNever,
  495. Containers: []v1.Container{
  496. {
  497. Name: "ephemeral-pods-manager",
  498. Image: "public.ecr.aws/o1j4x7p4/porter-ephemeral-pods-manager:latest",
  499. ImagePullPolicy: v1.PullAlways,
  500. Args: []string{"delete"},
  501. },
  502. },
  503. },
  504. },
  505. },
  506. },
  507. },
  508. }
  509. _, err = config.Clientset.BatchV1().CronJobs("default").Create(
  510. context.Background(), cronJob, metav1.CreateOptions{},
  511. )
  512. if err != nil {
  513. return err
  514. }
  515. return nil
  516. }
  517. func checkForServiceAccount(config *PorterRunSharedConfig) error {
  518. namespaces, err := config.Clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
  519. if err != nil {
  520. return err
  521. }
  522. for _, namespace := range namespaces.Items {
  523. serviceAccounts, err := config.Clientset.CoreV1().ServiceAccounts(namespace.Name).List(
  524. context.Background(), metav1.ListOptions{},
  525. )
  526. if err != nil {
  527. return err
  528. }
  529. if namespace.Name == "default" {
  530. for _, svcAccount := range serviceAccounts.Items {
  531. if svcAccount.Name == "porter-ephemeral-pod-deletion-service-account" {
  532. return nil
  533. }
  534. }
  535. } else {
  536. for _, svcAccount := range serviceAccounts.Items {
  537. if svcAccount.Name == "porter-ephemeral-pod-deletion-service-account" {
  538. err = config.Clientset.CoreV1().ServiceAccounts(namespace.Name).Delete(
  539. context.Background(), svcAccount.Name, metav1.DeleteOptions{},
  540. )
  541. if err != nil {
  542. return err
  543. }
  544. }
  545. }
  546. }
  547. }
  548. serviceAccount := &v1.ServiceAccount{
  549. ObjectMeta: metav1.ObjectMeta{
  550. Name: "porter-ephemeral-pod-deletion-service-account",
  551. },
  552. }
  553. _, err = config.Clientset.CoreV1().ServiceAccounts("default").Create(
  554. context.Background(), serviceAccount, metav1.CreateOptions{},
  555. )
  556. if err != nil {
  557. return err
  558. }
  559. return nil
  560. }
  561. func checkForClusterRole(config *PorterRunSharedConfig) error {
  562. roles, err := config.Clientset.RbacV1().ClusterRoles().List(
  563. context.Background(), metav1.ListOptions{},
  564. )
  565. if err != nil {
  566. return err
  567. }
  568. for _, role := range roles.Items {
  569. if role.Name == "porter-ephemeral-pod-deletion-cluster-role" {
  570. return nil
  571. }
  572. }
  573. role := &rbacv1.ClusterRole{
  574. ObjectMeta: metav1.ObjectMeta{
  575. Name: "porter-ephemeral-pod-deletion-cluster-role",
  576. },
  577. Rules: []rbacv1.PolicyRule{
  578. {
  579. APIGroups: []string{""},
  580. Resources: []string{"pods"},
  581. Verbs: []string{"list", "delete"},
  582. },
  583. {
  584. APIGroups: []string{""},
  585. Resources: []string{"namespaces"},
  586. Verbs: []string{"list"},
  587. },
  588. },
  589. }
  590. _, err = config.Clientset.RbacV1().ClusterRoles().Create(
  591. context.Background(), role, metav1.CreateOptions{},
  592. )
  593. if err != nil {
  594. return err
  595. }
  596. return nil
  597. }
  598. func checkForRoleBinding(config *PorterRunSharedConfig) error {
  599. bindings, err := config.Clientset.RbacV1().ClusterRoleBindings().List(
  600. context.Background(), metav1.ListOptions{},
  601. )
  602. if err != nil {
  603. return err
  604. }
  605. for _, binding := range bindings.Items {
  606. if binding.Name == "porter-ephemeral-pod-deletion-cluster-rolebinding" {
  607. return nil
  608. }
  609. }
  610. binding := &rbacv1.ClusterRoleBinding{
  611. ObjectMeta: metav1.ObjectMeta{
  612. Name: "porter-ephemeral-pod-deletion-cluster-rolebinding",
  613. },
  614. RoleRef: rbacv1.RoleRef{
  615. APIGroup: "rbac.authorization.k8s.io",
  616. Kind: "ClusterRole",
  617. Name: "porter-ephemeral-pod-deletion-cluster-role",
  618. },
  619. Subjects: []rbacv1.Subject{
  620. {
  621. APIGroup: "",
  622. Kind: "ServiceAccount",
  623. Name: "porter-ephemeral-pod-deletion-service-account",
  624. Namespace: "default",
  625. },
  626. },
  627. }
  628. _, err = config.Clientset.RbacV1().ClusterRoleBindings().Create(
  629. context.Background(), binding, metav1.CreateOptions{},
  630. )
  631. if err != nil {
  632. return err
  633. }
  634. return nil
  635. }
  636. func waitForPod(config *PorterRunSharedConfig, pod *v1.Pod) error {
  637. var (
  638. w watch.Interface
  639. err error
  640. ok bool
  641. )
  642. // immediately after creating a pod, the API may return a 404. heuristically 1
  643. // second seems to be plenty.
  644. watchRetries := 3
  645. for i := 0; i < watchRetries; i++ {
  646. selector := fields.OneTermEqualSelector("metadata.name", pod.Name).String()
  647. w, err = config.Clientset.CoreV1().
  648. Pods(pod.Namespace).
  649. Watch(context.Background(), metav1.ListOptions{FieldSelector: selector})
  650. if err == nil {
  651. break
  652. }
  653. time.Sleep(time.Second)
  654. }
  655. if err != nil {
  656. return err
  657. }
  658. defer w.Stop()
  659. for {
  660. select {
  661. case <-time.Tick(time.Second):
  662. // poll every second in case we already missed the ready event while
  663. // creating the listener.
  664. pod, err = config.Clientset.CoreV1().
  665. Pods(pod.Namespace).
  666. Get(context.Background(), pod.Name, metav1.GetOptions{})
  667. if isPodReady(pod) || isPodExited(pod) {
  668. return nil
  669. }
  670. case evt := <-w.ResultChan():
  671. pod, ok = evt.Object.(*v1.Pod)
  672. if !ok {
  673. return fmt.Errorf("unexpected object type: %T", evt.Object)
  674. }
  675. if isPodReady(pod) || isPodExited(pod) {
  676. return nil
  677. }
  678. case <-time.After(time.Second * 10):
  679. return errors.New("timed out waiting for pod")
  680. }
  681. }
  682. }
  683. func isPodReady(pod *v1.Pod) bool {
  684. ready := false
  685. conditions := pod.Status.Conditions
  686. for i := range conditions {
  687. if conditions[i].Type == v1.PodReady {
  688. ready = pod.Status.Conditions[i].Status == v1.ConditionTrue
  689. }
  690. }
  691. return ready
  692. }
  693. func isPodExited(pod *v1.Pod) bool {
  694. return pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed
  695. }
  696. func handlePodAttachError(err error, config *PorterRunSharedConfig, namespace, podName, container string) error {
  697. if verbose {
  698. color.New(color.FgYellow).Fprintf(os.Stderr, "Error: %s\n", err)
  699. }
  700. color.New(color.FgYellow).Fprintln(os.Stderr, "Could not open a shell to this container. Container logs:")
  701. var writtenBytes int64
  702. writtenBytes, _ = pipePodLogsToStdout(config, namespace, podName, container, false)
  703. if verbose || writtenBytes == 0 {
  704. color.New(color.FgYellow).Fprintln(os.Stderr, "Could not get logs. Pod events:")
  705. pipeEventsToStdout(config, namespace, podName, container, false)
  706. }
  707. return err
  708. }
  709. func pipePodLogsToStdout(config *PorterRunSharedConfig, namespace, name, container string, follow bool) (int64, error) {
  710. podLogOpts := v1.PodLogOptions{
  711. Container: container,
  712. Follow: follow,
  713. }
  714. req := config.Clientset.CoreV1().Pods(namespace).GetLogs(name, &podLogOpts)
  715. podLogs, err := req.Stream(
  716. context.Background(),
  717. )
  718. if err != nil {
  719. return 0, err
  720. }
  721. defer podLogs.Close()
  722. return io.Copy(os.Stdout, podLogs)
  723. }
  724. func pipeEventsToStdout(config *PorterRunSharedConfig, namespace, name, container string, follow bool) error {
  725. // update the config in case the operation has taken longer than token expiry time
  726. config.setSharedConfig()
  727. // creates the clientset
  728. resp, err := config.Clientset.CoreV1().Events(namespace).List(
  729. context.TODO(),
  730. metav1.ListOptions{
  731. FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=%s", name, namespace),
  732. },
  733. )
  734. if err != nil {
  735. return err
  736. }
  737. for _, event := range resp.Items {
  738. color.New(color.FgRed).Println(event.Message)
  739. }
  740. return nil
  741. }
  742. func getExistingPod(config *PorterRunSharedConfig, name, namespace string) (*v1.Pod, error) {
  743. return config.Clientset.CoreV1().Pods(namespace).Get(
  744. context.Background(),
  745. name,
  746. metav1.GetOptions{},
  747. )
  748. }
  749. func deletePod(config *PorterRunSharedConfig, name, namespace string) error {
  750. // update the config in case the operation has taken longer than token expiry time
  751. config.setSharedConfig()
  752. err := config.Clientset.CoreV1().Pods(namespace).Delete(
  753. context.Background(),
  754. name,
  755. metav1.DeleteOptions{},
  756. )
  757. if err != nil {
  758. color.New(color.FgRed).Fprintf(os.Stderr, "Could not delete ephemeral pod: %s\n", err.Error())
  759. return err
  760. }
  761. color.New(color.FgGreen).Println("Sucessfully deleted ephemeral pod")
  762. return nil
  763. }
  764. func createEphemeralPodFromExisting(
  765. config *PorterRunSharedConfig,
  766. existing *v1.Pod,
  767. container string,
  768. args []string,
  769. ) (*v1.Pod, error) {
  770. newPod := existing.DeepCopy()
  771. // only copy the pod spec, overwrite metadata
  772. newPod.ObjectMeta = metav1.ObjectMeta{
  773. Name: strings.ToLower(fmt.Sprintf("%s-copy-%s", existing.ObjectMeta.Name, utils.String(4))),
  774. Namespace: existing.ObjectMeta.Namespace,
  775. }
  776. newPod.Status = v1.PodStatus{}
  777. // set restart policy to never
  778. newPod.Spec.RestartPolicy = v1.RestartPolicyNever
  779. // change the command in the pod to the passed in pod command
  780. cmdRoot := args[0]
  781. cmdArgs := make([]string, 0)
  782. // annotate with the ephemeral pod tag
  783. newPod.Labels = make(map[string]string)
  784. newPod.Labels["porter/ephemeral-pod"] = "true"
  785. if len(args) > 1 {
  786. cmdArgs = args[1:]
  787. }
  788. for i := 0; i < len(newPod.Spec.Containers); i++ {
  789. if newPod.Spec.Containers[i].Name == container {
  790. newPod.Spec.Containers[i].Command = []string{cmdRoot}
  791. newPod.Spec.Containers[i].Args = cmdArgs
  792. newPod.Spec.Containers[i].TTY = true
  793. newPod.Spec.Containers[i].Stdin = true
  794. newPod.Spec.Containers[i].StdinOnce = true
  795. }
  796. // remove health checks and probes
  797. newPod.Spec.Containers[i].LivenessProbe = nil
  798. newPod.Spec.Containers[i].ReadinessProbe = nil
  799. newPod.Spec.Containers[i].StartupProbe = nil
  800. }
  801. newPod.Spec.NodeName = ""
  802. // create the pod and return it
  803. return config.Clientset.CoreV1().Pods(existing.ObjectMeta.Namespace).Create(
  804. context.Background(),
  805. newPod,
  806. metav1.CreateOptions{},
  807. )
  808. }