"Containerd 1.0 Source Analysis" CTR Containers list Source Analysis _docker

Source: Internet
Author: User

Command:

Ctr Containers-h
NAME:
CTR containers-manage Containers (metadata)


USAGE:
CTR Containers Command [command options] [arguments ...]


COMMANDS:
list, ls list all tasks or those that match a filter
Delete, Del, rm delete an existing container
The label Set and clear labels for a container.


OPTIONS:
--help, h Show help

A. CTR Containers List Command
1.1 The service side received the GRPC request

Func _containers_list_handler (SRV interface{}, CTX context. Context, Dec func (interface{}) error, Interceptor Grpc. Unaryserverinterceptor) (interface{}, error) {in
       : = new (Listcontainersrequest)
       If err: = Dec (in); Err!= Nil {
  return nil, err
       }
       if Interceptor = = Nil {return
              srv. ( Containersserver). List (CTX, in)
       }
       Info: = &grpc. unaryserverinfo{
              Server:     srv,
              fullmethod: "/containerd.services.containers.v1.containers/list",
       }
       Handler: = Func (CTX context. Context, req interface{}) (interface{}, error) {return
              srv. ( Containersserver). List (CTX, req. (*listcontainersrequest))
       } return
       Interceptor (CTX, in, info, handler)
}

1.2 is an important function store in Services/containers/service.go. The List is the implemented interface
Func (S *service) List (CTX context. Context, req *api. Listcontainersrequest) (*api. Listcontainersresponse, error) {
       var resp API. Listcontainersresponse return

       &resp, Errdefs. Togrpc (S.withstoreview (CTX, func) (CTX context. Context, store containers. Store) error {
              containers, err: = store. List (CTX, req. Filters ...)
              If Err!= nil {return
                     err
              }

              resp. containers = Containerstoproto (containers) return
              nil
       })
}

1.3 According to the Withstore function can get the store for metadata. Newcontainerstore, in the path/metadata/containers.go, the containerstore structure is wrapped in a database of operations
Type Containerstore struct {
       tx *bolt. Tx
}

func Newcontainerstore (TX *bolt. TX) containers. Store {return
       &containerstore{
              tx:tx,
       }
}

Getcontainerbucket if empty in the 1.4 list function, it is proved that no container is explained in 1.4.1, and Readcontainer reads the data into container.
Func (S *containerstore) List (CTX context. Context, FS ... string) ([]containers. Container, error) {namespace, err: = namespaces. Namespacerequired (CTX) filter, Err: = Filters.

       Parseall (FS ...) BKT: = Getcontainersbucket (S.tx, namespace) if bkt = = Nil {return nil, nil} var m [] Containers. Container If err: = bkt. ForEach (func (k, v []byte) error {cbkt: = bkt. Bucket (k) if cbkt = = nil {return nil} container: = Contai Ners. Container{id:string (k)} If Err: = Readcontainer (&container, cbkt); Err!= Nil {return errors. Wrap (Err, "failed to read container")} if filter.
       Match (Adaptcontainer (Container)) {m = append (M, container)} return nil }); Err!= Nil {return nil, err} return m, nil}

1.4.1 Getcontainersbucket namespace generally defaults to default,bucketkeyobjectcontainers for contaiers,1.4.1.1 analysis Bolt. TX structure, the database is a bit messy, now only know to operate the database on the line. Embarrassed... Tx. Bucket in 1.4.1.2
Func Getcontainersbucket (TX *bolt. Tx, namespace String) *bolt. Bucket {return
       getbucket (TX, Bucketkeyversion, []byte (namespace), bucketkeyobjectcontainers)
}
Func Getbucket (TX *bolt. Tx, Keys ... []byte) *bolt. Bucket {
       bkt: = Tx. Bucket (Keys[0])

       for _, Key: = Range Keys[1:] {
              if bkt = = Nil {break
              }
              bkt = bkt. Bucket (key)
       } return

       bkt
}

1.4.1.1 Structural Body
Type Tx struct {
       writable       bool
       managed        BOOL
       db             *db
       meta           *meta
       root           Bucket
       pages          map[pgid]*page
       stats          txstats
       commithandlers []func ()

       Writeflag int
Bucket represents a key/value to the existing database
Bucket represents a collection of key/value pairs inside the database.
Type Bucket struct {
       *bucket
       tx       *TX                //The associated transaction  buckets-Map[string]*bucket Subbucket Cache
       Page     *page              //Inline page reference
       rootnode *node              //materialized node for the Root page.
       Nodes    Map[pgid]*node     /Node cache

       //Sets the threshold for filling when nodes split.  By default,
       //The bucket would fill to 50% but it can is useful to increase this
       //amount if you know that your Write workloads are mostly append-only.
       //This are non-persisted across transactions so it must being set in every Tx.
       Fillpercent float64
}

The 1.4.1.2 Bucket function is retrieved by name and returns to the Bucket structure.
Func (TX *tx) Bucket (name []byte) *bucket {return
       tx.root.Bucket (name)
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.