|
| 1 | +package websocket |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "math/rand" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "time" |
| 11 | + |
| 12 | + gwebsocket "github.com/gorilla/websocket" |
| 13 | + "github.com/pkg/errors" |
| 14 | + "github.com/replicatedhq/embedded-cluster/pkg/extensions" |
| 15 | + "github.com/replicatedhq/embedded-cluster/pkg/manager" |
| 16 | + "github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig" |
| 17 | + "github.com/replicatedhq/embedded-cluster/pkg/upgrade" |
| 18 | + "github.com/replicatedhq/embedded-cluster/pkg/versions" |
| 19 | + "github.com/replicatedhq/embedded-cluster/pkg/websocket/types" |
| 20 | + "github.com/sirupsen/logrus" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 24 | +) |
| 25 | + |
| 26 | +var wsDialer = &gwebsocket.Dialer{ |
| 27 | + HandshakeTimeout: 10 * time.Second, |
| 28 | +} |
| 29 | + |
| 30 | +func ConnectToKOTSWebSocket(ctx context.Context, kcli client.Client) { |
| 31 | + for { |
| 32 | + if err := attemptConnection(ctx, kcli); err != nil { |
| 33 | + logrus.Errorf("Connection attempt to KOTS failed: %v, retrying in 10 seconds...", err) |
| 34 | + time.Sleep(10 * time.Second) |
| 35 | + continue |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func attemptConnection(ctx context.Context, kcli client.Client) error { |
| 41 | + endpoint, err := getKOTSEndpoint(ctx, kcli) |
| 42 | + if err != nil { |
| 43 | + return errors.Wrap(err, "get kots endpoint") |
| 44 | + } |
| 45 | + |
| 46 | + hostname, err := os.Hostname() |
| 47 | + if err != nil { |
| 48 | + return errors.Wrap(err, "get hostname") |
| 49 | + } |
| 50 | + var node corev1.Node |
| 51 | + if err := kcli.Get(ctx, k8stypes.NamespacedName{Name: hostname}, &node); err != nil { |
| 52 | + return errors.Wrap(err, "get node") |
| 53 | + } |
| 54 | + |
| 55 | + wsURL := fmt.Sprintf("ws://%s/ec-ws?nodeName=%s&version=%s", endpoint, url.QueryEscape(node.Name), url.QueryEscape(versions.Version)) |
| 56 | + logrus.Infof("connecting to KOTS WebSocket server on %s", wsURL) |
| 57 | + u, err := url.Parse(wsURL) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("parse websocket url: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + conn, _, err := wsDialer.Dial(u.String(), nil) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("connect to websocket server: %w", err) |
| 65 | + } |
| 66 | + defer conn.Close() |
| 67 | + |
| 68 | + logrus.Info("Successfully connected to KOTS WebSocket server") |
| 69 | + |
| 70 | + // ping server on a regular interval to make sure it's still connected |
| 71 | + go pingWSServer(ctx, conn) |
| 72 | + |
| 73 | + // listen to server messages |
| 74 | + return listenToWSServer(ctx, conn, endpoint) |
| 75 | +} |
| 76 | + |
| 77 | +func pingWSServer(ctx context.Context, conn *gwebsocket.Conn) error { |
| 78 | + for { |
| 79 | + select { |
| 80 | + case <-ctx.Done(): |
| 81 | + return nil |
| 82 | + case <-time.After(time.Second * time.Duration(5+rand.Intn(16))): // 5-20 seconds |
| 83 | + pingMsg := fmt.Sprintf("%x", rand.Int()) |
| 84 | + if err := conn.WriteControl(gwebsocket.PingMessage, []byte(pingMsg), time.Now().Add(1*time.Second)); err != nil { |
| 85 | + return errors.Wrap(err, "send ping message") |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func listenToWSServer(ctx context.Context, conn *gwebsocket.Conn, endpoint string) error { |
| 92 | + for { |
| 93 | + _, message, err := conn.ReadMessage() // receive messages, including ping/pong |
| 94 | + if err != nil { |
| 95 | + return errors.Wrap(err, "read message") |
| 96 | + } |
| 97 | + |
| 98 | + var msg types.Message |
| 99 | + if err := json.Unmarshal(message, &msg); err != nil { |
| 100 | + logrus.Errorf("failed to unmarshal message: %s: %s", err, string(message)) |
| 101 | + continue |
| 102 | + } |
| 103 | + if err := msg.Validate(); err != nil { |
| 104 | + logrus.Errorf("invalid message: %s", err.Error()) |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + logrus.Infof("Processing message with command=%s app=%s version=%s step=%s", msg.Command, msg.AppSlug, msg.VersionLabel, msg.StepID) |
| 109 | + |
| 110 | + // ensure the environment is set up correctly |
| 111 | + os.Setenv("KUBECONFIG", runtimeconfig.PathToKubeConfig()) |
| 112 | + os.Setenv("TMPDIR", runtimeconfig.EmbeddedClusterTmpSubDir()) |
| 113 | + |
| 114 | + // create a reporter for this step |
| 115 | + stepReporter := NewStepReporter(ctx, endpoint, msg.AppSlug, msg.VersionLabel, msg.StepID) |
| 116 | + stepReporter.Started() |
| 117 | + |
| 118 | + if err := processWSMessage(ctx, msg); err != nil { |
| 119 | + logrus.Errorf("failed to process message: %s", err.Error()) |
| 120 | + stepReporter.Failed(fmt.Sprintf("failed to process message: %s", err.Error())) |
| 121 | + continue |
| 122 | + } |
| 123 | + |
| 124 | + // this doesn't get called for the upgrade manager step because the manager restarts. |
| 125 | + // kots marks the step as complete when the new manager connects to it. |
| 126 | + logrus.Infof("Successfully proccessed message with command=%s app=%s version=%s step=%s", msg.Command, msg.AppSlug, msg.VersionLabel, msg.StepID) |
| 127 | + stepReporter.Complete() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func processWSMessage(ctx context.Context, msg types.Message) error { |
| 132 | + switch msg.Command { |
| 133 | + case types.CommandUpgradeManager: |
| 134 | + d := types.UpgradeManagerData{} |
| 135 | + if err := json.Unmarshal([]byte(msg.Data), &d); err != nil { |
| 136 | + return errors.Wrapf(err, "unmarshal data: %s", string(msg.Data)) |
| 137 | + } |
| 138 | + if err := d.Validate(); err != nil { |
| 139 | + return errors.Wrap(err, "invalid data") |
| 140 | + } |
| 141 | + |
| 142 | + if err := manager.Upgrade(ctx, manager.UpgradeOptions{ |
| 143 | + AppSlug: msg.AppSlug, |
| 144 | + VersionLabel: msg.VersionLabel, |
| 145 | + LicenseID: d.LicenseID, |
| 146 | + LicenseEndpoint: d.LicenseEndpoint, |
| 147 | + }); err != nil { |
| 148 | + return errors.Wrap(err, "upgrade manager") |
| 149 | + } |
| 150 | + |
| 151 | + case types.CommandUpgradeCluster: |
| 152 | + d := types.UpgradeClusterData{} |
| 153 | + if err := json.Unmarshal([]byte(msg.Data), &d); err != nil { |
| 154 | + return errors.Wrapf(err, "unmarshal data: %s", string(msg.Data)) |
| 155 | + } |
| 156 | + if err := d.Validate(); err != nil { |
| 157 | + return errors.Wrap(err, "invalid data") |
| 158 | + } |
| 159 | + if err := upgrade.Upgrade(ctx, &d.Installation); err != nil { |
| 160 | + return errors.Wrap(err, "upgrade cluster") |
| 161 | + } |
| 162 | + |
| 163 | + case types.CommandAddExtension: |
| 164 | + d := types.ExtensionData{} |
| 165 | + if err := json.Unmarshal([]byte(msg.Data), &d); err != nil { |
| 166 | + return errors.Wrapf(err, "unmarshal data: %s", string(msg.Data)) |
| 167 | + } |
| 168 | + if err := extensions.Add(ctx, d.Repos, d.Chart); err != nil { |
| 169 | + return errors.Wrap(err, "add extension") |
| 170 | + } |
| 171 | + |
| 172 | + case types.CommandUpgradeExtension: |
| 173 | + d := types.ExtensionData{} |
| 174 | + if err := json.Unmarshal([]byte(msg.Data), &d); err != nil { |
| 175 | + return errors.Wrapf(err, "unmarshal data: %s", string(msg.Data)) |
| 176 | + } |
| 177 | + if err := extensions.Upgrade(ctx, d.Repos, d.Chart); err != nil { |
| 178 | + return errors.Wrap(err, "upgrade extension") |
| 179 | + } |
| 180 | + |
| 181 | + case types.CommandRemoveExtension: |
| 182 | + d := types.ExtensionData{} |
| 183 | + if err := json.Unmarshal([]byte(msg.Data), &d); err != nil { |
| 184 | + return errors.Wrapf(err, "unmarshal data: %s", string(msg.Data)) |
| 185 | + } |
| 186 | + if err := extensions.Remove(ctx, d.Repos, d.Chart); err != nil { |
| 187 | + return errors.Wrap(err, "remove extension") |
| 188 | + } |
| 189 | + default: |
| 190 | + return errors.Errorf("unknown command: %s", msg.Command) |
| 191 | + } |
| 192 | + |
| 193 | + return nil |
| 194 | +} |
0 commit comments