Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/blowtorch/cmd/inject.go
2497 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"os"
9
"os/signal"
10
"strconv"
11
"strings"
12
"syscall"
13
14
log "github.com/sirupsen/logrus"
15
"github.com/spf13/cobra"
16
17
"github.com/gitpod-io/gitpod/blowtorch/pkg/dart"
18
)
19
20
// injectCmd represents the inject command
21
var injectCmd = &cobra.Command{
22
Use: "inject <service-name>",
23
Short: "Adds a toxiproxy intermediate for a particular service",
24
Args: cobra.ExactArgs(1),
25
Run: func(cmd *cobra.Command, args []string) {
26
cfg, ns, err := getKubeconfig()
27
if err != nil {
28
log.WithError(err).Fatal("cannot get Kubernetes client config")
29
}
30
31
var opts []dart.InjectOption
32
arflag, err := cmd.Flags().GetStringToString("additional-routes")
33
if err != nil {
34
log.WithError(err).Fatal("bug in blowtorch")
35
}
36
for tps, apss := range arflag {
37
tp, err := strconv.ParseUint(tps, 10, 16)
38
if err != nil {
39
log.WithField("targetPort", tps).WithError(err).Fatal("additional route: target port is not a number")
40
}
41
for _, s := range strings.Split(apss, ",") {
42
s = strings.TrimSpace(s)
43
ap, err := strconv.ParseUint(s, 10, 16)
44
if err != nil {
45
log.WithField("targetPort", tps).WithField("additionalPort", ap).WithError(err).Fatal("additional route: additional port is not a number")
46
}
47
opts = append(opts, dart.WithAdditionalRoute(int(tp), int(ap)))
48
log.WithField("targetPort", tp).WithField("additionalPort", ap).Info("adding additional route")
49
}
50
}
51
52
defer func() {
53
err = dart.Remove(cfg, ns, args[0])
54
if err != nil {
55
log.WithError(err).Fatal("cannot remove toxiproxy")
56
}
57
}()
58
_, err = dart.Inject(cfg, ns, args[0], opts...)
59
if err != nil {
60
log.WithError(err).Fatal("cannot inject toxiproxy")
61
}
62
63
// run until we're told to stop
64
sigChan := make(chan os.Signal, 1)
65
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
66
log.Info("🎯 blowtorch up and running. Stop with SIGINT or CTRL+C")
67
log.Warn("")
68
log.Warn("Note: Don't forget to restart any pod that uses the service you've just replaced.")
69
log.Warn(" Otherwise you might still be using the original service and toxiproxy will")
70
log.Warn(" have no effect.")
71
<-sigChan
72
log.Info("received SIGINT - shutting down")
73
74
},
75
}
76
77
func init() {
78
rootCmd.AddCommand(injectCmd)
79
80
injectCmd.Flags().StringToStringP("additional-routes", "a", make(map[string]string), "add an additional route for a target port, e.g. 8080=9080,9090 where 8080 is the original target port")
81
}
82
83