github.phpd.cn/cilium/cilium@v1.6.12/pkg/workloads/crio.go (about)

     1  // Copyright 2018-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package workloads
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/url"
    21  
    22  	"github.com/cilium/cilium/api/v1/models"
    23  	"github.com/cilium/cilium/pkg/endpoint"
    24  )
    25  
    26  const (
    27  	CRIO WorkloadRuntimeType = "crio"
    28  
    29  	// criOEndpoint is the default value for the crio socket
    30  	criOEndpoint = "/var/run/crio/crio.sock"
    31  )
    32  
    33  var (
    34  	criOInstance = &criOModule{
    35  		opts: workloadRuntimeOpts{
    36  			EpOpt: &workloadRuntimeOpt{
    37  				description: "Address of cri-o endpoint",
    38  				value:       criOEndpoint,
    39  			},
    40  		},
    41  	}
    42  )
    43  
    44  type criOModule struct {
    45  	opts workloadRuntimeOpts
    46  }
    47  
    48  func init() {
    49  	registerWorkload(CRIO, criOInstance)
    50  }
    51  
    52  func (c *criOModule) getName() string {
    53  	return string(CRIO)
    54  }
    55  
    56  func (c *criOModule) setConfigDummy() {
    57  }
    58  
    59  func (c *criOModule) setConfig(opts map[string]string) error {
    60  	return setOpts(opts, c.opts)
    61  }
    62  
    63  func (c *criOModule) getConfig() map[string]string {
    64  	return getOpts(c.opts)
    65  }
    66  
    67  func (c *criOModule) newClient() (WorkloadRuntime, error) {
    68  	return newCRIOClient(c.opts)
    69  }
    70  
    71  type criOClient struct {
    72  	cri *criClient
    73  }
    74  
    75  func newCRIOClient(opts workloadRuntimeOpts) (WorkloadRuntime, error) {
    76  	ep := string(opts[EpOpt].value)
    77  	p, err := url.Parse(ep)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	if p.Scheme == "" {
    82  		ep = "unix://" + ep
    83  	}
    84  	rsc, err := newCRIClient(context.WithValue(context.Background(), EpOpt, ep))
    85  	return &criOClient{rsc}, err
    86  }
    87  
    88  // IsRunning returns false if the provided endpoint cannot be associated with a
    89  // running workload. The runtime must be reachable to make this decision.
    90  func (c *criOClient) IsRunning(ep *endpoint.Endpoint) bool {
    91  	return c.cri.IsRunning(ep)
    92  }
    93  
    94  // Status returns the status of the workload runtime
    95  func (c *criOClient) Status() *models.Status {
    96  	if c == nil {
    97  		return workloadStatusDisabled
    98  	}
    99  
   100  	criStatus := c.cri.Status()
   101  	criStatusMsg := fmt.Sprintf("cri-o client: %s - %s", criStatus.State, criStatus.Msg)
   102  
   103  	return &models.Status{
   104  		State: criStatus.State,
   105  		Msg:   criStatusMsg,
   106  	}
   107  }
   108  
   109  // EnableEventListener watches for criO events. Performs the plumbing for
   110  // the containers started or dead.
   111  func (c *criOClient) EnableEventListener() (chan<- *EventMessage, error) {
   112  	return c.cri.EnableEventListener()
   113  }
   114  
   115  func (c *criOClient) processEvents(events chan EventMessage) {
   116  	c.cri.processEvents(events)
   117  }
   118  
   119  func (c *criOClient) handleCreateWorkload(id string, retry bool) {
   120  	c.cri.handleCreateWorkload(id, retry)
   121  }
   122  
   123  // IgnoreRunningWorkloads checks for already running containers and checks
   124  // their IP address, then adds the containers to the list of ignored containers
   125  // and allocates the IPs they are using to prevent future collisions.
   126  func (c *criOClient) IgnoreRunningWorkloads() {
   127  	c.cri.IgnoreRunningWorkloads()
   128  }
   129  
   130  // workloadIDsList returns a list of running workload IDs.
   131  func (c *criOClient) workloadIDsList(ctx context.Context) ([]string, error) {
   132  	return c.cri.workloadIDsList(ctx)
   133  }
   134  
   135  // GetAllInfraContainersPID returns a map that maps container IDs to the PID
   136  // of that container.
   137  func (c *criOClient) GetAllInfraContainersPID() (map[string]int, error) {
   138  	return c.cri.GetAllInfraContainersPID()
   139  }