// CaptureResource is exported for testing func (r *Reporter) CaptureResource(f func(xfer.Request, string, string, string) xfer.Response) func(xfer.Request) xfer.Response { return func(req xfer.Request) xfer.Response { var resource, uid string for _, parser := range []struct { res string f func(string) (string, bool) }{ {report.Deployment, report.ParseDeploymentNodeID}, {report.ReplicaSet, report.ParseReplicaSetNodeID}, } { if u, ok := parser.f(req.NodeID); ok { resource, uid = parser.res, u break } } if resource == "" { return xfer.ResponseErrorf("Invalid ID: %s", req.NodeID) } switch resource { case report.Deployment: var deployment Deployment r.client.WalkDeployments(func(d Deployment) error { if d.UID() == uid { deployment = d } return nil }) if deployment != nil { return f(req, "deployment", deployment.Namespace(), deployment.Name()) } case report.ReplicaSet: var replicaSet ReplicaSet var res string r.client.WalkReplicaSets(func(r ReplicaSet) error { if r.UID() == uid { replicaSet = r res = "replicaset" } return nil }) if replicaSet == nil { r.client.WalkReplicationControllers(func(r ReplicationController) error { if r.UID() == uid { replicaSet = ReplicaSet(r) res = "replicationcontroller" } return nil }) } if replicaSet != nil { return f(req, res, replicaSet.Namespace(), replicaSet.Name()) } } return xfer.ResponseErrorf("%s not found: %s", resource, uid) } }
func captureContainerID(f func(string, xfer.Request) xfer.Response) func(xfer.Request) xfer.Response { return func(req xfer.Request) xfer.Response { containerID, ok := report.ParseContainerNodeID(req.NodeID) if !ok { return xfer.ResponseErrorf("Invalid ID: %s", req.NodeID) } return f(containerID, req) } }
// HandleControlRequest performs a control request. func HandleControlRequest(req xfer.Request) xfer.Response { mtx.Lock() handler, ok := handlers[req.Control] mtx.Unlock() if !ok { return xfer.ResponseErrorf("Control %q not recognised", req.Control) } return handler(req) }
// CapturePod is exported for testing func (r *Reporter) CapturePod(f func(xfer.Request, string, string) xfer.Response) func(xfer.Request) xfer.Response { return func(req xfer.Request) xfer.Response { uid, ok := report.ParsePodNodeID(req.NodeID) if !ok { return xfer.ResponseErrorf("Invalid ID: %s", req.NodeID) } // find pod by UID var pod Pod r.client.WalkPods(func(p Pod) error { if p.UID() == uid { pod = p } return nil }) if pod == nil { return xfer.ResponseErrorf("Pod not found: %s", uid) } return f(req, pod.Namespace(), pod.Name()) } }
func (r *registry) attachContainer(containerID string, req xfer.Request) xfer.Response { c, ok := r.GetContainer(containerID) if !ok { return xfer.ResponseErrorf("Not found: %s", containerID) } hasTTY := c.HasTTY() id, pipe, err := controls.NewPipe(r.pipes, req.AppID) if err != nil { return xfer.ResponseError(err) } local, _ := pipe.Ends() cw, err := r.client.AttachToContainerNonBlocking(docker_client.AttachToContainerOptions{ Container: containerID, RawTerminal: hasTTY, Stream: true, Stdin: true, Stdout: true, Stderr: true, InputStream: local, OutputStream: local, ErrorStream: local, }) if err != nil { return xfer.ResponseError(err) } pipe.OnClose(func() { if err := cw.Close(); err != nil { log.Errorf("Error closing attachment: %v", err) return } log.Infof("Attachment to container %s closed.", containerID) }) go func() { if err := cw.Wait(); err != nil { log.Errorf("Error waiting on exec: %v", err) } pipe.Close() }() return xfer.Response{ Pipe: id, RawTTY: hasTTY, } }