github.com/imran-kn/cilium-fork@v1.6.9/pkg/client/endpoint.go (about)

     1  // Copyright 2016-2017 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 client
    16  
    17  import (
    18  	"github.com/cilium/cilium/api/v1/client/endpoint"
    19  	"github.com/cilium/cilium/api/v1/models"
    20  	"github.com/cilium/cilium/pkg/api"
    21  	pkgEndpointID "github.com/cilium/cilium/pkg/endpoint/id"
    22  	"github.com/cilium/cilium/pkg/labels"
    23  )
    24  
    25  // EndpointList returns a list of all endpoints
    26  func (c *Client) EndpointList() ([]*models.Endpoint, error) {
    27  	resp, err := c.Endpoint.GetEndpoint(nil)
    28  	if err != nil {
    29  		return nil, Hint(err)
    30  	}
    31  	return resp.Payload, nil
    32  }
    33  
    34  // EndpointGet returns endpoint by ID
    35  func (c *Client) EndpointGet(id string) (*models.Endpoint, error) {
    36  	params := endpoint.NewGetEndpointIDParams().WithID(id).WithTimeout(api.ClientTimeout)
    37  	resp, err := c.Endpoint.GetEndpointID(params)
    38  	if err != nil {
    39  		/* Since plugins rely on checking the error type, we don't wrap this
    40  		 * with Hint(...)
    41  		 */
    42  		return nil, err
    43  	}
    44  	return resp.Payload, nil
    45  }
    46  
    47  // EndpointCreate creates a new endpoint
    48  func (c *Client) EndpointCreate(ep *models.EndpointChangeRequest) error {
    49  	id := pkgEndpointID.NewCiliumID(ep.ID)
    50  	params := endpoint.NewPutEndpointIDParams().WithID(id).WithEndpoint(ep).WithTimeout(api.ClientTimeout)
    51  	_, err := c.Endpoint.PutEndpointID(params)
    52  	return Hint(err)
    53  }
    54  
    55  // EndpointPatch modifies the endpoint
    56  func (c *Client) EndpointPatch(id string, ep *models.EndpointChangeRequest) error {
    57  	params := endpoint.NewPatchEndpointIDParams().WithID(id).WithEndpoint(ep).WithTimeout(api.ClientTimeout)
    58  	_, err := c.Endpoint.PatchEndpointID(params)
    59  	return Hint(err)
    60  }
    61  
    62  // EndpointDelete deletes endpoint
    63  func (c *Client) EndpointDelete(id string) error {
    64  	params := endpoint.NewDeleteEndpointIDParams().WithID(id).WithTimeout(api.ClientTimeout)
    65  	_, _, err := c.Endpoint.DeleteEndpointID(params)
    66  	return Hint(err)
    67  }
    68  
    69  // EndpointLogGet returns endpoint log
    70  func (c *Client) EndpointLogGet(id string) (models.EndpointStatusLog, error) {
    71  	params := endpoint.NewGetEndpointIDLogParams().WithID(id).WithTimeout(api.ClientTimeout)
    72  	resp, err := c.Endpoint.GetEndpointIDLog(params)
    73  	if err != nil {
    74  		return nil, Hint(err)
    75  	}
    76  	return resp.Payload, nil
    77  }
    78  
    79  // EndpointHealthGet returns endpoint healthz
    80  func (c *Client) EndpointHealthGet(id string) (*models.EndpointHealth, error) {
    81  	params := endpoint.NewGetEndpointIDHealthzParams().WithID(id).WithTimeout(api.ClientTimeout)
    82  	resp, err := c.Endpoint.GetEndpointIDHealthz(params)
    83  	if err != nil {
    84  		return nil, Hint(err)
    85  	}
    86  	return resp.Payload, nil
    87  }
    88  
    89  // EndpointConfigGet returns endpoint configuration
    90  func (c *Client) EndpointConfigGet(id string) (*models.EndpointConfigurationStatus, error) {
    91  	params := endpoint.NewGetEndpointIDConfigParams().WithID(id).WithTimeout(api.ClientTimeout)
    92  	resp, err := c.Endpoint.GetEndpointIDConfig(params)
    93  	if err != nil {
    94  		return nil, Hint(err)
    95  	}
    96  	return resp.Payload, nil
    97  }
    98  
    99  // EndpointConfigPatch modifies endpoint configuration
   100  func (c *Client) EndpointConfigPatch(id string, cfg *models.EndpointConfigurationSpec) error {
   101  	params := endpoint.NewPatchEndpointIDConfigParams().WithID(id).WithTimeout(api.ClientTimeout)
   102  	if cfg != nil {
   103  		params.SetEndpointConfiguration(cfg)
   104  	}
   105  
   106  	_, err := c.Endpoint.PatchEndpointIDConfig(params)
   107  	return Hint(err)
   108  }
   109  
   110  // EndpointLabelsGet returns endpoint label configuration
   111  func (c *Client) EndpointLabelsGet(id string) (*models.LabelConfiguration, error) {
   112  	params := endpoint.NewGetEndpointIDLabelsParams().WithID(id).WithTimeout(api.ClientTimeout)
   113  	resp, err := c.Endpoint.GetEndpointIDLabels(params)
   114  	if err != nil {
   115  		return nil, Hint(err)
   116  	}
   117  	return resp.Payload, nil
   118  }
   119  
   120  // EndpointLabelsPut modifies endpoint label configuration
   121  // add: List of labels to add and enable. If the label is an orchestration
   122  // system label which has been disabled before, it will be removed from
   123  // the disabled list and readded to the orchestration list. Otherwise
   124  // it will be added to the custom label list.
   125  //
   126  // delete: List of labels to delete. If the label is an orchestration system
   127  // label, then it will be deleted from the orchestration list and
   128  // added to the disabled list. Otherwise it will be removed from the
   129  // custom list.
   130  //
   131  func (c *Client) EndpointLabelsPatch(id string, toAdd, toDelete models.Labels) error {
   132  	currentCfg, err := c.EndpointLabelsGet(id)
   133  	if err != nil {
   134  		return err
   135  	}
   136  
   137  	userLbl := labels.NewLabelsFromModel(currentCfg.Status.Realized.User)
   138  	for _, lbl := range toAdd {
   139  		lblParsed := labels.ParseLabel(lbl)
   140  		if _, found := userLbl[lblParsed.Key]; !found {
   141  			userLbl[lblParsed.Key] = lblParsed
   142  		}
   143  	}
   144  	for _, lbl := range toDelete {
   145  		lblParsed := labels.ParseLabel(lbl)
   146  		if _, found := userLbl[lblParsed.Key]; found {
   147  			delete(userLbl, lblParsed.Key)
   148  		}
   149  	}
   150  	currentCfg.Spec.User = userLbl.GetModel()
   151  
   152  	params := endpoint.NewPatchEndpointIDLabelsParams().WithID(id).WithTimeout(api.ClientTimeout)
   153  	_, err = c.Endpoint.PatchEndpointIDLabels(params.WithConfiguration(currentCfg.Spec))
   154  	return Hint(err)
   155  }