Skip to content

Commit

Permalink
create: Reduce cyclomatic complexity of CreateContainer
Browse files Browse the repository at this point in the history
This complexity exceeded 15 with the previous check.

Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
  • Loading branch information
amshinde committed Jul 3, 2018
1 parent 23311ea commit ba2a32a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,16 +521,8 @@ func (a *agentGRPC) rollbackFailingContainerCreation(ctr *container) {
}

func (a *agentGRPC) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (resp *gpb.Empty, err error) {
if a.sandbox.running == false {
return emptyResp, grpcStatus.Error(codes.FailedPrecondition, "Sandbox not started, impossible to run a new container")
}

if _, err = a.sandbox.getContainer(req.ContainerId); err == nil {
return emptyResp, grpcStatus.Errorf(codes.AlreadyExists, "Container %s already exists, impossible to create", req.ContainerId)
}

if a.pidNsExists(req.OCI) {
return emptyResp, grpcStatus.Errorf(codes.FailedPrecondition, "Unexpected PID namespace received, should have been cleared out", req.ContainerId)
if err := a.createContainerChecks(req); err != nil {
return emptyResp, err
}

// re-scan PCI bus
Expand Down Expand Up @@ -628,6 +620,22 @@ func (a *agentGRPC) CreateContainer(ctx context.Context, req *pb.CreateContainer
return emptyResp, a.postExecProcess(ctr, ctr.initProcess)
}

func (a *agentGRPC) createContainerChecks(req *pb.CreateContainerRequest) (err error) {
if a.sandbox.running == false {
return grpcStatus.Error(codes.FailedPrecondition, "Sandbox not started, impossible to run a new container")
}

if _, err = a.sandbox.getContainer(req.ContainerId); err == nil {
return grpcStatus.Errorf(codes.AlreadyExists, "Container %s already exists, impossible to create", req.ContainerId)
}

if a.pidNsExists(req.OCI) {
return grpcStatus.Errorf(codes.FailedPrecondition, "Unexpected PID namespace received for container %s, should have been cleared out", req.ContainerId)
}

return nil
}

func (a *agentGRPC) pidNsExists(grpcSpec *pb.Spec) bool {
if grpcSpec.Linux != nil {
for _, n := range grpcSpec.Linux.Namespaces {
Expand Down

0 comments on commit ba2a32a

Please sign in to comment.