(ctx devspacecontext.Context, arch, addr, sshHost string, selector targetselector.TargetSelector, parent *tomb.Tomb)
| 145 | } |
| 146 | |
| 147 | func startSSHWithRestart(ctx devspacecontext.Context, arch, addr, sshHost string, selector targetselector.TargetSelector, parent *tomb.Tomb) error { |
| 148 | if ctx.IsDone() { |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | // find target container |
| 153 | container, err := selector.SelectSingleContainer(ctx.Context(), ctx.KubeClient(), ctx.Log()) |
| 154 | if err != nil { |
| 155 | return errors.Wrap(err, "error selecting container") |
| 156 | } else if container == nil { |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | // make sure the DevSpace helper binary is injected |
| 161 | err = inject.InjectDevSpaceHelper(ctx.Context(), ctx.KubeClient(), container.Pod, container.Container.Name, arch, ctx.Log()) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | // get host key |
| 167 | hostKey, err := getHostKey() |
| 168 | if err != nil { |
| 169 | return errors.Wrap(err, "generate host key") |
| 170 | } |
| 171 | |
| 172 | // get public key |
| 173 | publicKey, err := getPublicKey() |
| 174 | if err != nil { |
| 175 | return errors.Wrap(err, "generate key pair") |
| 176 | } |
| 177 | |
| 178 | // get command |
| 179 | command := []string{inject.DevSpaceHelperContainerPath, "ssh", "--authorized-key", publicKey, "--host-key", hostKey} |
| 180 | if addr != "" { |
| 181 | command = append(command, "--address", addr) |
| 182 | } |
| 183 | |
| 184 | // start ssh server |
| 185 | parent.Go(func() error { |
| 186 | writer := ctx.Log().Writer(logrus.DebugLevel, false) |
| 187 | defer writer.Close() |
| 188 | for !ctx.IsDone() { |
| 189 | buffer := &bytes.Buffer{} |
| 190 | multiWriter := io.MultiWriter(writer, buffer) |
| 191 | err = ctx.KubeClient().ExecStream(ctx.Context(), &kubectl.ExecStreamOptions{ |
| 192 | Pod: container.Pod, |
| 193 | Container: container.Container.Name, |
| 194 | Command: command, |
| 195 | Stdout: multiWriter, |
| 196 | Stderr: multiWriter, |
| 197 | SubResource: kubectl.SubResourceExec, |
| 198 | }) |
| 199 | if err != nil { |
| 200 | select { |
| 201 | case <-ctx.Context().Done(): |
| 202 | return nil |
| 203 | case <-time.After(time.Second * 2): |
| 204 | // check if context is done |
no test coverage detected