示例#1
0
// Call the network driver's DeleteEndpoint API for all the networks that the
// container belongs to.
func (adptr *PwrStrpAdptr) handlePreStop(req *powerStripRequest) (*powerStripResponse, error) {
	contIDOrName := extractContIDOrName(req.ClientRequest)
	fullContID := adptr.getFullContainerID(contIDOrName)
	if _, ok := adptr.containerNets[fullContID]; !ok {
		log.Errorf("got a stop request for non existent container. contIdOrName: %s Request: %+v",
			contIDOrName, req)
		// let the request be forwarded to docker
	} else {
		// delete the endpoint for every network this container is part of
		for _, net := range adptr.containerNets[fullContID] {
			// in libnetwork netUUID and epUUID are derived by the libnetwork,
			// just deriving a unique string for now.
			netUUID := driverapi.UUID(fmt.Sprintf("%s-%s", net.tenantID, net.netID))
			epUUID := driverapi.UUID(fmt.Sprintf("%s-%s-%s", net.tenantID, net.netID, fullContID))
			err := adptr.driver.DeleteEndpoint(netUUID, epUUID)
			if err != nil {
				log.Errorf("Failed to delete endpoint for net: %+v container: %q with net(s): %+v. Error: %s",
					net, contIDOrName, adptr.containerNets[fullContID], err)
				continue
			}
		}
	}

	return makeClientRequest(req), nil
}
示例#2
0
// Call the network driver's CreateEndpoint API for all networks that the
// container belongs to.
func (adptr *PwrStrpAdptr) handlePostStart(req *powerStripRequest) (*powerStripResponse, error) {
	defer func() { adptr.outstandingContID = "" }()

	// ignore the response if container start failed
	if req.ServerResponse.Code != 204 {
		return makeServerResponse(req), nil
	}

	// should not happen
	if adptr.outstandingContID == "" {
		return nil, core.Errorf("received a container start response, without corresponding start request!")
	}

	// should not happen
	if _, ok := adptr.containerNets[adptr.outstandingContID]; !ok {
		return nil, core.Errorf("received a container start response for unknown container %s",
			adptr.outstandingContID)
	}

	// Now create an endpoint for every network this container is part of
	contID := adptr.outstandingContID
	for _, net := range adptr.containerNets[contID] {
		// in libnetwork netUUID and epUUID are derived by the libnetwork,
		// just deriving a unique string for now.
		netUUID := driverapi.UUID(fmt.Sprintf("%s-%s", net.tenantID, net.netID))
		epUUID := driverapi.UUID(fmt.Sprintf("%s-%s-%s", net.tenantID, net.netID, contID))
		_, err := adptr.driver.CreateEndpoint(netUUID, epUUID, "",
			DriverConfig{net.tenantID, net.netID, contID})
		defer func(netUUID, epUUID driverapi.UUID) {
			if err != nil {
				adptr.driver.DeleteEndpoint(netUUID, epUUID)
			}
		}(netUUID, epUUID)
		if err != nil {
			return nil, core.Errorf("Failed to create endpoint for net: %+v container: %q with net(s): %+v. Error: %s",
				net, contID, adptr.containerNets[contID], err)
		}
	}

	return makeServerResponse(req), nil
}