/
blue-green.go
175 lines (147 loc) · 4.31 KB
/
blue-green.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cmd
import (
"context"
"fmt"
"strings"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
var blueGreenDeploy bool
func addBlueGreenFlag(cmd *cobra.Command) {
cmd.Flags().BoolVar(&blueGreenDeploy, "blue-green", false, "Enable blue-green deployment")
}
// NewCmdBlueGreen return track command
func NewCmdBlueGreen() *cobra.Command {
blueGreenCmd := &cobra.Command{
Use: "blue-green",
Short: "Commands for blue-green deployment",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
blueGreenDeploy = true
},
}
blueGreenCmd.AddCommand(&cobra.Command{
Use: "color",
Short: "Get current Deployment color",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := initApp()
if err != nil {
return err
}
cmd.SilenceUsage = true
currentColor, err := getCurrentBlueGreenColor(app.Namespace, app.Labels)
if err != nil {
return err
}
fmt.Println(colorize(currentColor))
return nil
},
})
blueGreenCmd.AddCommand(&cobra.Command{
Use: "rollback",
Short: "Rollback Deployment to previous color",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := initApp()
if err != nil {
return err
}
cmd.SilenceUsage = true
fmt.Printf("Check Deployment %s with previous color:\n",
colorize(app.Deployment.BlueGreenColor, app.GetDeploymentName()))
trackTimeout = 1
err = trackReady(app.GetDeploymentName(), app.Namespace)
if err != nil {
return err
}
kcs, err := kubeFactory.KubernetesClientSet()
if err != nil {
return err
}
services, err := kcs.CoreV1().Services(app.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: getSelector(app.Labels),
})
if err != nil {
return err
}
if len(services.Items) > 0 {
for _, service := range services.Items {
fmt.Printf("Patch service %s in [%s] color\n", service.Name, colorize(app.Deployment.BlueGreenColor))
payloadBytes := []byte(`[{
"op": "replace",
"path": "/spec/selector/app.kubernetes.io~1color",
"value": "` + app.Deployment.BlueGreenColor + `"
}]`)
options := metav1.PatchOptions{}
_, err = kcs.CoreV1().Services(app.Namespace).Patch(context.TODO(), service.Name, types.JSONPatchType, payloadBytes, options)
if err != nil {
return err
}
fmt.Println(colorize(app.Deployment.BlueGreenColor, "Rollback is successful"))
}
} else {
return fmt.Errorf("no services found")
}
return nil
},
})
blueGreenCmd.AddCommand(&cobra.Command{
Use: "prune",
Short: "Prune Deployment with previous color",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := initApp()
if err != nil {
return err
}
cmd.SilenceUsage = true
err = deleteDeployment(app.GetDeploymentName(), app.Namespace)
if err != nil {
return err
}
fmt.Printf("Deployment %s pruned\n", colorize(app.Deployment.BlueGreenColor, app.GetDeploymentName()))
return nil
},
})
for _, cmd := range blueGreenCmd.Commands() {
addAppFlags(cmd)
cmd.Flags().MarkHidden("include-namespace")
cmd.Flags().MarkHidden("snapshot")
}
return blueGreenCmd
}
func colorize(s ...string) string {
str := s[0]
if len(s) > 1 {
str = strings.Join(s[1:], " ")
}
if s[0] == "blue" {
return aurora.BrightBlue(str).String()
}
return aurora.Green(str).String()
}
// getTargetBlueGreenColor return the color for target deployment
func getTargetBlueGreenColor(namespace string, labels map[string]string) (string, error) {
color := "blue"
currentColor, _ := getCurrentBlueGreenColor(namespace, labels)
if currentColor == "blue" {
color = "green"
}
return color, nil
}
// getCurrentBlueGreenColor return the color for current deployment
func getCurrentBlueGreenColor(namespace string, labels map[string]string) (string, error) {
kcs, err := kubeFactory.KubernetesClientSet()
if err != nil {
return "", err
}
svc, err := kcs.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: getSelector(labels),
})
if err != nil || len(svc.Items) == 0 {
return "", fmt.Errorf("service not found")
}
if currentColor, ok := svc.Items[0].Spec.Selector["app.kubernetes.io/color"]; ok {
return currentColor, nil
}
return "", fmt.Errorf("color not found")
}