bluegreen.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "time"
  7. v2 "github.com/porter-dev/porter/cli/cmd/v2"
  8. "github.com/fatih/color"
  9. api "github.com/porter-dev/porter/api/client"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/cli/cmd/deploy"
  12. "github.com/spf13/cobra"
  13. appsv1 "k8s.io/api/apps/v1"
  14. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  15. intstrutil "k8s.io/apimachinery/pkg/util/intstr"
  16. )
  17. var deployCmd = &cobra.Command{
  18. Use: "deploy",
  19. }
  20. var bluegreenCmd = &cobra.Command{
  21. Use: "blue-green-switch",
  22. Short: "Automatically switches the traffic of a blue-green deployment once the new application is ready.",
  23. Run: func(cmd *cobra.Command, args []string) {
  24. err := checkLoginAndRun(args, bluegreenSwitch)
  25. if err != nil {
  26. os.Exit(1)
  27. }
  28. },
  29. }
  30. func init() {
  31. rootCmd.AddCommand(deployCmd)
  32. deployCmd.AddCommand(bluegreenCmd)
  33. bluegreenCmd.PersistentFlags().StringVar(
  34. &app,
  35. "app",
  36. "",
  37. "Application in the Porter dashboard",
  38. )
  39. bluegreenCmd.MarkPersistentFlagRequired("app")
  40. bluegreenCmd.PersistentFlags().StringVar(
  41. &tag,
  42. "tag",
  43. "",
  44. "The image tag to switch traffic to.",
  45. )
  46. bluegreenCmd.PersistentFlags().StringVar(
  47. &namespace,
  48. "namespace",
  49. "",
  50. "The namespace of the jobs.",
  51. )
  52. }
  53. func bluegreenSwitch(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  54. ctx := context.Background()
  55. project, err := client.GetProject(ctx, cliConf.Project)
  56. if err != nil {
  57. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  58. }
  59. if project.ValidateApplyV2 {
  60. err = v2.BlueGreenSwitch(ctx)
  61. if err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. // get the web release
  67. webRelease, err := client.GetRelease(context.Background(), cliConf.Project, cliConf.Cluster, namespace, app)
  68. if err != nil {
  69. return err
  70. }
  71. // if this application is not a web chart, throw an error
  72. if webRelease.Chart.Name() != "web" {
  73. return fmt.Errorf("target application is not a web chart")
  74. }
  75. currActiveImage := deploy.GetCurrActiveBlueGreenImage(webRelease.Config)
  76. sharedConf := &PorterRunSharedConfig{
  77. Client: client,
  78. }
  79. err = sharedConf.setSharedConfig()
  80. if err != nil {
  81. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  82. }
  83. // if no job exists with the given revision, wait up to 30 minutes
  84. timeWait := time.Now().Add(30 * time.Minute)
  85. prevRefresh := time.Now()
  86. success := false
  87. color.New(color.FgGreen).Printf("Waiting for the new version of the application %s to be ready\n", app)
  88. for time.Now().Before(timeWait) {
  89. // refresh the client every 10 minutes
  90. if time.Now().After(prevRefresh.Add(10 * time.Minute)) {
  91. err = sharedConf.setSharedConfig()
  92. if err != nil {
  93. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  94. }
  95. prevRefresh = time.Now()
  96. }
  97. depls, err := sharedConf.Clientset.AppsV1().Deployments(namespace).List(
  98. context.Background(),
  99. metav1.ListOptions{
  100. LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s", app),
  101. },
  102. )
  103. if err != nil {
  104. return fmt.Errorf("could not get deployments: %s", err.Error())
  105. }
  106. foundDeployment := false
  107. // get the deployment which matches the new image tag
  108. for _, depl := range depls.Items {
  109. if depl.ObjectMeta.Name == fmt.Sprintf("%s-web-%s", app, tag) || depl.ObjectMeta.Name == fmt.Sprintf("%s-%s", app, tag) {
  110. foundDeployment = true
  111. // determine if the deployment has an appropriate number of ready replicas
  112. minUnavailable := *(depl.Spec.Replicas) - getMaxUnavailable(depl)
  113. // if the number of ready replicas is greater than the number of min unavailable,
  114. // the controller is ready for a traffic switch
  115. if minUnavailable <= depl.Status.ReadyReplicas {
  116. // push the deployment
  117. color.New(color.FgGreen).Printf("Switching traffic for app %s\n", app)
  118. deployAgent, err := updateGetAgent(client)
  119. if err != nil {
  120. return err
  121. }
  122. if currActiveImage == "" {
  123. err = deployAgent.UpdateImageAndValues(map[string]interface{}{
  124. "bluegreen": map[string]interface{}{
  125. "enabled": true,
  126. "disablePrimaryDeployment": true,
  127. "activeImageTag": tag,
  128. "imageTags": []string{tag},
  129. },
  130. })
  131. } else {
  132. err = deployAgent.UpdateImageAndValues(map[string]interface{}{
  133. "bluegreen": map[string]interface{}{
  134. "enabled": true,
  135. "disablePrimaryDeployment": true,
  136. "activeImageTag": tag,
  137. "imageTags": []string{currActiveImage, tag},
  138. },
  139. })
  140. }
  141. if err != nil {
  142. return err
  143. } else {
  144. success = true
  145. }
  146. }
  147. }
  148. }
  149. if !foundDeployment {
  150. return fmt.Errorf("target deployment not found. Did you specify the correct tag?")
  151. }
  152. if success {
  153. break
  154. }
  155. // otherwise, return no error
  156. time.Sleep(2 * time.Second)
  157. }
  158. if !success {
  159. return fmt.Errorf("new application was not ready within 30 minutes")
  160. }
  161. // wait 30 seconds before removing old deployment
  162. time.Sleep(30 * time.Second)
  163. deployAgent, err := updateGetAgent(client)
  164. if err != nil {
  165. return err
  166. }
  167. err = deployAgent.UpdateImageAndValues(map[string]interface{}{
  168. "bluegreen": map[string]interface{}{
  169. "enabled": true,
  170. "disablePrimaryDeployment": true,
  171. "activeImageTag": tag,
  172. "imageTags": []string{tag},
  173. },
  174. })
  175. return nil
  176. }
  177. func getMaxUnavailable(deployment appsv1.Deployment) int32 {
  178. if deployment.Spec.Strategy.Type != appsv1.RollingUpdateDeploymentStrategyType || *(deployment.Spec.Replicas) == 0 {
  179. return int32(0)
  180. }
  181. desired := *(deployment.Spec.Replicas)
  182. maxUnavailable := deployment.Spec.Strategy.RollingUpdate.MaxUnavailable
  183. unavailable, err := intstrutil.GetScaledValueFromIntOrPercent(intstrutil.ValueOrDefault(maxUnavailable, intstrutil.FromInt(0)), int(desired), false)
  184. if err != nil {
  185. return 0
  186. }
  187. return int32(unavailable)
  188. }