dotcloud/docker

View on GitHub
daemon/container_operations.go

Summary

Maintainability
F
1 wk
Test Coverage

File container_operations.go has 926 lines of code (exceeds 500 allowed). Consider refactoring.
Open

// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.21

package daemon // import "github.com/docker/docker/daemon"

Severity: Major
Found in daemon/container_operations.go - About 1 day to fix

    Method Daemon.buildSandboxOptions has a Cognitive Complexity of 55 (exceeds 20 allowed). Consider refactoring.
    Open

    func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnetwork.SandboxOption, error) {
        var sboxOptions []libnetwork.SandboxOption
        sboxOptions = append(sboxOptions, libnetwork.OptionHostname(ctr.Config.Hostname), libnetwork.OptionDomainname(ctr.Config.Domainname))
    
        if ctr.HostConfig.NetworkMode.IsHost() {
    Severity: Minor
    Found in daemon/container_operations.go - About 6 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Method Daemon.findAndAttachNetwork has a Cognitive Complexity of 48 (exceeds 20 allowed). Consider refactoring.
    Open

    func (daemon *Daemon) findAndAttachNetwork(ctr *container.Container, idOrName string, epConfig *networktypes.EndpointSettings) (*libnetwork.Network, *networktypes.NetworkingConfig, error) {
        id := getNetworkID(idOrName, epConfig)
    
        n, err := daemon.FindNetwork(id)
        if err != nil {
    Severity: Minor
    Found in daemon/container_operations.go - About 4 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Method Daemon.buildSandboxOptions has 141 lines of code (exceeds 50 allowed). Consider refactoring.
    Open

    func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnetwork.SandboxOption, error) {
        var sboxOptions []libnetwork.SandboxOption
        sboxOptions = append(sboxOptions, libnetwork.OptionHostname(ctr.Config.Hostname), libnetwork.OptionDomainname(ctr.Config.Domainname))
    
        if ctr.HostConfig.NetworkMode.IsHost() {
    Severity: Major
    Found in daemon/container_operations.go - About 4 hrs to fix

      Method Daemon.connectToNetwork has a Cognitive Complexity of 37 (exceeds 20 allowed). Consider refactoring.
      Open

      func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container, idOrName string, endpointConfig *network.EndpointSettings, updateSettings bool) (retErr error) {
          containerName := strings.TrimPrefix(ctr.Name, "/")
          ctx, span := otel.Tracer("").Start(ctx, "daemon.connectToNetwork", trace.WithAttributes(
              attribute.String("container.ID", ctr.ID),
              attribute.String("container.name", containerName),
      Severity: Minor
      Found in daemon/container_operations.go - About 3 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method Daemon.connectToNetwork has 96 lines of code (exceeds 50 allowed). Consider refactoring.
      Open

      func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container, idOrName string, endpointConfig *network.EndpointSettings, updateSettings bool) (retErr error) {
          containerName := strings.TrimPrefix(ctr.Name, "/")
          ctx, span := otel.Tracer("").Start(ctx, "daemon.connectToNetwork", trace.WithAttributes(
              attribute.String("container.ID", ctr.ID),
              attribute.String("container.name", containerName),
      Severity: Major
      Found in daemon/container_operations.go - About 2 hrs to fix

        Function validateEndpointSettings has a Cognitive Complexity of 31 (exceeds 20 allowed). Consider refactoring.
        Open

        func validateEndpointSettings(nw *libnetwork.Network, nwName string, epConfig *networktypes.EndpointSettings) error {
            if epConfig == nil {
                return nil
            }
        
        
        Severity: Minor
        Found in daemon/container_operations.go - About 2 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Method Daemon.allocateNetwork has a Cognitive Complexity of 31 (exceeds 20 allowed). Consider refactoring.
        Open

        func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container) (retErr error) {
            if daemon.netController == nil {
                return nil
            }
        
        
        Severity: Minor
        Found in daemon/container_operations.go - About 2 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Method Daemon.findAndAttachNetwork has 65 lines of code (exceeds 50 allowed). Consider refactoring.
        Open

        func (daemon *Daemon) findAndAttachNetwork(ctr *container.Container, idOrName string, epConfig *networktypes.EndpointSettings) (*libnetwork.Network, *networktypes.NetworkingConfig, error) {
            id := getNetworkID(idOrName, epConfig)
        
            n, err := daemon.FindNetwork(id)
            if err != nil {
        Severity: Minor
        Found in daemon/container_operations.go - About 1 hr to fix

          Method Daemon.connectToNetwork has 16 return statements (exceeds 4 allowed).
          Open

          func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container, idOrName string, endpointConfig *network.EndpointSettings, updateSettings bool) (retErr error) {
              containerName := strings.TrimPrefix(ctr.Name, "/")
              ctx, span := otel.Tracer("").Start(ctx, "daemon.connectToNetwork", trace.WithAttributes(
                  attribute.String("container.ID", ctr.ID),
                  attribute.String("container.name", containerName),
          Severity: Major
          Found in daemon/container_operations.go - About 1 hr to fix

            Method Daemon.allocateNetwork has 61 lines of code (exceeds 50 allowed). Consider refactoring.
            Open

            func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container) (retErr error) {
                if daemon.netController == nil {
                    return nil
                }
            
            
            Severity: Minor
            Found in daemon/container_operations.go - About 1 hr to fix

              Function validateEndpointSettings has 56 lines of code (exceeds 50 allowed). Consider refactoring.
              Open

              func validateEndpointSettings(nw *libnetwork.Network, nwName string, epConfig *networktypes.EndpointSettings) error {
                  if epConfig == nil {
                      return nil
                  }
              
              
              Severity: Minor
              Found in daemon/container_operations.go - About 1 hr to fix

                Method Daemon.allocateNetwork has 9 return statements (exceeds 4 allowed).
                Open

                func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container) (retErr error) {
                    if daemon.netController == nil {
                        return nil
                    }
                
                
                Severity: Major
                Found in daemon/container_operations.go - About 55 mins to fix

                  Method Daemon.disconnectFromNetwork has 9 return statements (exceeds 4 allowed).
                  Open

                  func (daemon *Daemon) disconnectFromNetwork(ctx context.Context, ctr *container.Container, n *libnetwork.Network, force bool) error {
                      var (
                          ep   *libnetwork.Endpoint
                          sbox *libnetwork.Sandbox
                      )
                  Severity: Major
                  Found in daemon/container_operations.go - About 55 mins to fix

                    Method Daemon.findAndAttachNetwork has 7 return statements (exceeds 4 allowed).
                    Open

                    func (daemon *Daemon) findAndAttachNetwork(ctr *container.Container, idOrName string, epConfig *networktypes.EndpointSettings) (*libnetwork.Network, *networktypes.NetworkingConfig, error) {
                        id := getNetworkID(idOrName, epConfig)
                    
                        n, err := daemon.FindNetwork(id)
                        if err != nil {
                    Severity: Major
                    Found in daemon/container_operations.go - About 45 mins to fix

                      Avoid deeply nested control flow statements.
                      Open

                                          if retryCount >= 5 {
                                              return nil, nil, fmt.Errorf("could not find network %s after successful attachment", idOrName)
                                          }
                      Severity: Major
                      Found in daemon/container_operations.go - About 45 mins to fix

                        Method Daemon.DisconnectFromNetwork has 7 return statements (exceeds 4 allowed).
                        Open

                        func (daemon *Daemon) DisconnectFromNetwork(ctx context.Context, ctr *container.Container, networkName string, force bool) error {
                            n, err := daemon.FindNetwork(networkName)
                            ctr.Lock()
                            defer ctr.Unlock()
                        
                        
                        Severity: Major
                        Found in daemon/container_operations.go - About 45 mins to fix

                          Method Daemon.connectToNetwork has 6 arguments (exceeds 4 allowed). Consider refactoring.
                          Open

                          func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container, idOrName string, endpointConfig *network.EndpointSettings, updateSettings bool) (retErr error) {
                          Severity: Minor
                          Found in daemon/container_operations.go - About 45 mins to fix

                            Method Daemon.buildSandboxOptions has 7 return statements (exceeds 4 allowed).
                            Open

                            func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnetwork.SandboxOption, error) {
                                var sboxOptions []libnetwork.SandboxOption
                                sboxOptions = append(sboxOptions, libnetwork.OptionHostname(ctr.Config.Hostname), libnetwork.OptionDomainname(ctr.Config.Domainname))
                            
                                if ctr.HostConfig.NetworkMode.IsHost() {
                            Severity: Major
                            Found in daemon/container_operations.go - About 45 mins to fix

                              Method Daemon.initializeNetworking has 6 return statements (exceeds 4 allowed).
                              Open

                              func (daemon *Daemon) initializeNetworking(ctx context.Context, cfg *config.Config, ctr *container.Container) error {
                                  if ctr.HostConfig.NetworkMode.IsContainer() {
                                      // we need to get the hosts files from the container to join
                                      nc, err := daemon.getNetworkedContainer(ctr.ID, ctr.HostConfig.NetworkMode.ConnectedContainer())
                                      if err != nil {
                              Severity: Major
                              Found in daemon/container_operations.go - About 40 mins to fix

                                Method Daemon.updateNetworkSettings has 5 return statements (exceeds 4 allowed).
                                Open

                                func (daemon *Daemon) updateNetworkSettings(ctr *container.Container, n *libnetwork.Network, endpointConfig *networktypes.EndpointSettings) error {
                                    if ctr.NetworkSettings == nil {
                                        ctr.NetworkSettings = &network.Settings{}
                                    }
                                    if ctr.NetworkSettings.Networks == nil {
                                Severity: Major
                                Found in daemon/container_operations.go - About 35 mins to fix

                                  Method Daemon.releaseNetwork has 5 return statements (exceeds 4 allowed).
                                  Open

                                  func (daemon *Daemon) releaseNetwork(ctx context.Context, ctr *container.Container) {
                                      ctx = context.WithoutCancel(ctx)
                                  
                                      start := time.Now()
                                      // If live-restore is enabled, the daemon cleans up dead containers when it starts up. In that case, the
                                  Severity: Major
                                  Found in daemon/container_operations.go - About 35 mins to fix

                                    Function updateJoinInfo has 5 return statements (exceeds 4 allowed).
                                    Open

                                    func updateJoinInfo(networkSettings *network.Settings, n *libnetwork.Network, ep *libnetwork.Endpoint) error {
                                        if ep == nil {
                                            return errors.New("invalid enppoint whhile building portmap info")
                                        }
                                    
                                    
                                    Severity: Major
                                    Found in daemon/container_operations.go - About 35 mins to fix

                                      Method Daemon.updateNetwork has 5 return statements (exceeds 4 allowed).
                                      Open

                                      func (daemon *Daemon) updateNetwork(cfg *config.Config, ctr *container.Container) error {
                                          var (
                                              start = time.Now()
                                              ctrl  = daemon.netController
                                              sid   = ctr.NetworkSettings.SandboxID
                                      Severity: Major
                                      Found in daemon/container_operations.go - About 35 mins to fix

                                        Method Daemon.getNetworkedContainer has 5 return statements (exceeds 4 allowed).
                                        Open

                                        func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID string) (*container.Container, error) {
                                            nc, err := daemon.GetContainer(connectedContainerID)
                                            if err != nil {
                                                return nil, err
                                            }
                                        Severity: Major
                                        Found in daemon/container_operations.go - About 35 mins to fix

                                          Similar blocks of code found in 2 locations. Consider refactoring.
                                          Open

                                              for _, port := range ports {
                                                  portProto := types.ParseProtocol(port.Proto())
                                                  portNum := uint16(port.Int())
                                                  exposedPorts = append(exposedPorts, types.TransportPort{
                                                      Proto: portProto,
                                          Severity: Major
                                          Found in daemon/container_operations.go and 1 other location - About 3 hrs to fix
                                          daemon/network.go on lines 956..988

                                          Duplicated Code

                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                          Tuning

                                          This issue has a mass of 291.

                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                          Refactorings

                                          Further Reading

                                          Similar blocks of code found in 2 locations. Consider refactoring.
                                          Open

                                              if ctr.HostConfig.PortBindings != nil {
                                                  for p, b := range ctr.HostConfig.PortBindings {
                                                      bindings[p] = []nat.PortBinding{}
                                                      for _, bb := range b {
                                                          bindings[p] = append(bindings[p], nat.PortBinding{
                                          Severity: Minor
                                          Found in daemon/container_operations.go and 1 other location - About 35 mins to fix
                                          daemon/network.go on lines 933..943

                                          Duplicated Code

                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                          Tuning

                                          This issue has a mass of 106.

                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                          Refactorings

                                          Further Reading

                                          There are no issues that match your filters.

                                          Category
                                          Status