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

     1  // Copyright 2016-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  	"github.com/cilium/cilium/api/v1/models"
    19  	"github.com/cilium/cilium/pkg/endpoint"
    20  )
    21  
    22  var (
    23  	// defaultClient is the default client initialized by initClient
    24  	defaultClient WorkloadRuntime
    25  )
    26  
    27  func initClient(module workloadModule) error {
    28  	c, err := module.newClient()
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	defaultClient = c
    34  
    35  	return nil
    36  }
    37  
    38  // Client returns the global WorkloadRuntime being used.
    39  func Client() WorkloadRuntime {
    40  	return defaultClient
    41  }
    42  
    43  // IsRunning returns false if the provided endpoint cannot be associated with a
    44  // running workload. The runtime must be reachable to make this decision.
    45  func IsRunning(ep *endpoint.Endpoint) bool {
    46  	if Client() == nil {
    47  		return false
    48  	}
    49  
    50  	return Client().IsRunning(ep)
    51  }
    52  
    53  // Status returns the status of the workload runtime
    54  func Status() *models.Status {
    55  	if Client() == nil {
    56  		return workloadStatusDisabled
    57  	}
    58  	return Client().Status()
    59  }
    60  
    61  // EnableEventListener watches for docker events. Performs the plumbing for the
    62  // containers started or dead.
    63  func EnableEventListener() (eventsCh chan<- *EventMessage, err error) {
    64  	if Client() == nil {
    65  		return nil, nil
    66  	}
    67  	return Client().EnableEventListener()
    68  }
    69  
    70  // IgnoreRunningWorkloads checks for already running containers and checks
    71  // their IP address, then adds the containers to the list of ignored containers
    72  // and allocates the IPs they are using to prevent future collisions.
    73  func IgnoreRunningWorkloads() {
    74  	if Client() == nil {
    75  		return
    76  	}
    77  	Client().IgnoreRunningWorkloads()
    78  }