本文整理匯總了Golang中k8s/io/kubernetes/pkg/client/unversioned.Client.Nodes方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Nodes方法的具體用法?Golang Client.Nodes怎麽用?Golang Client.Nodes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類k8s/io/kubernetes/pkg/client/unversioned.Client
的用法示例。
在下文中一共展示了Client.Nodes方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: WaitForClusterSizeFunc
// WaitForClusterSize waits until the cluster size matches the given function.
func WaitForClusterSizeFunc(c *client.Client, sizeFunc func(int) bool, timeout time.Duration) error {
for start := time.Now(); time.Since(start) < timeout; time.Sleep(20 * time.Second) {
nodes, err := c.Nodes().List(api.ListOptions{FieldSelector: fields.Set{
"spec.unschedulable": "false",
}.AsSelector()})
if err != nil {
glog.Warningf("Failed to list nodes: %v", err)
continue
}
numNodes := len(nodes.Items)
// Filter out not-ready nodes.
framework.FilterNodes(nodes, func(node api.Node) bool {
return framework.IsNodeConditionSetAsExpected(&node, api.NodeReady, true)
})
numReady := len(nodes.Items)
if numNodes == numReady && sizeFunc(numReady) {
glog.Infof("Cluster has reached the desired size")
return nil
}
glog.Infof("Waiting for cluster, current size %d, not ready nodes %d", numNodes, numNodes-numReady)
}
return fmt.Errorf("timeout waiting %v for appropriate cluster size", timeout)
}
示例2: GetNodes
func GetNodes(kubeClient *kclient.Client) ([]kapi.Node, error) {
nodeList, err := kubeClient.Nodes().List(kapi.ListOptions{})
if err != nil {
return nil, fmt.Errorf("Listing nodes in the cluster failed. Error: %s", err)
}
return nodeList.Items, nil
}
示例3: List
func (this *NodeHandler) List(c *client.Client, label labels.Selector, field fields.Selector) (*api.NodeList, error) {
nodes := c.Nodes()
return nodes.List(api.ListOptions{
LabelSelector: label,
FieldSelector: field,
})
}
示例4: Update
// Update updates an existing node api object
// by looking up the given hostname.
// The updated node merges the given slave attribute labels
// and annotations with the found api object.
func Update(
client *client.Client,
hostname string,
slaveAttrLabels,
annotations map[string]string,
) (n *api.Node, err error) {
for i := 0; i < clientRetryCount; i++ {
n, err = client.Nodes().Get(hostname)
if err != nil {
return nil, fmt.Errorf("error getting node %q: %v", hostname, err)
}
if n == nil {
return nil, fmt.Errorf("no node instance returned for %q", hostname)
}
// update labels derived from Mesos slave attributes, keep all other labels
n.Labels = mergeMaps(
filterMap(n.Labels, IsNotSlaveAttributeLabel),
slaveAttrLabels,
)
n.Annotations = mergeMaps(n.Annotations, annotations)
n, err = client.Nodes().Update(n)
if err == nil && !errors.IsConflict(err) {
return n, nil
}
log.Infof("retry %d/%d: error updating node %v err %v", i, clientRetryCount, n, err)
time.Sleep(time.Duration(i) * clientRetryInterval)
}
return nil, err
}
示例5: updateNodes
func updateNodes(kubeClient *kube.Client, cPort int) (hostIPtoNodeMap map[string]kubeAPI.Node, nodeIPs []string) {
hostIPtoNodeMap = make(map[string]kubeAPI.Node, 2)
nodeIPs = make([]string, 0, 2)
nodeList, apiErr := kubeClient.Nodes().List(kubeLabels.Everything(), kubeFields.Everything())
if apiErr != nil {
glog.Errorf("Failed to list kubernetes nodes. Error: %v\n", apiErr)
} else {
for _, node := range nodeList.Items {
var hostIP string
for _, nodeAddress := range node.Status.Addresses {
switch nodeAddress.Type {
case kubeAPI.NodeInternalIP:
hostIP = nodeAddress.Address
break
case kubeAPI.NodeLegacyHostIP:
hostIP = nodeAddress.Address
}
}
if hostIP != "" {
hostIP = fmt.Sprintf("http://%v:%v", hostIP, cPort)
nodeIPs = append(nodeIPs, hostIP)
hostIPtoNodeMap[hostIP] = node
}
}
}
return hostIPtoNodeMap, nodeIPs
}
示例6: addIngressInfraLabel
func addIngressInfraLabel(c *k8sclient.Client, ns string) string {
nodeClient := c.Nodes()
nodes, err := nodeClient.List(api.ListOptions{})
if err != nil {
util.Errorf("\nUnable to find any nodes: %s\n", err)
}
changed := false
hasExistingExposeIPLabel, externalNodeName := hasExistingLabel(nodes, externalIPLabel)
if externalNodeName != "" {
return externalNodeName
}
if !hasExistingExposeIPLabel && len(nodes.Items) > 0 {
for _, node := range nodes.Items {
if !node.Spec.Unschedulable {
changed = addLabelIfNotExist(&node.ObjectMeta, externalIPLabel, "true")
if changed {
_, err = nodeClient.Update(&node)
if err != nil {
printError("Failed to label node with ", err)
}
return node.Name
}
}
}
}
if !changed && !hasExistingExposeIPLabel {
util.Warnf("Unable to add label for ingress controller to run on a specific node, please add manually: kubectl label node [your node name] %s=true", externalIPLabel)
}
return ""
}
示例7: updateNodeLabels
// updates labels of nodes given by nodeNames.
// In case a given label already exists, it overwrites it. If label to remove doesn't exist
// it silently ignores it.
// TODO: migrate to use framework.AddOrUpdateLabelOnNode/framework.RemoveLabelOffNode
func updateNodeLabels(c *client.Client, nodeNames sets.String, toAdd, toRemove map[string]string) {
const maxRetries = 5
for nodeName := range nodeNames {
var node *api.Node
var err error
for i := 0; i < maxRetries; i++ {
node, err = c.Nodes().Get(nodeName)
if err != nil {
framework.Logf("Error getting node %s: %v", nodeName, err)
continue
}
if toAdd != nil {
for k, v := range toAdd {
node.ObjectMeta.Labels[k] = v
}
}
if toRemove != nil {
for k := range toRemove {
delete(node.ObjectMeta.Labels, k)
}
}
_, err = c.Nodes().Update(node)
if err != nil {
framework.Logf("Error updating node %s: %v", nodeName, err)
} else {
break
}
}
Expect(err).NotTo(HaveOccurred())
}
}
示例8: addTaint
func addTaint(client *kube_client.Client, node *kube_api.Node, value string) error {
taints, err := kube_api.GetTaintsFromNodeAnnotations(node.Annotations)
if err != nil {
return err
}
taint := kube_api.Taint{
Key: criticalAddonsOnlyTaintKey,
Value: value,
Effect: kube_api.TaintEffectNoSchedule,
}
taints = append(taints, taint)
taintsJson, err := json.Marshal(taints)
if err != nil {
return err
}
if node.Annotations == nil {
node.Annotations = make(map[string]string)
}
node.Annotations[kube_api.TaintsAnnotationKey] = string(taintsJson)
_, err = client.Nodes().Update(node)
if err != nil {
return err
}
return nil
}
示例9: getClusterNodesIP
// getClusterNodesIP returns the IP address of each node in the kubernetes cluster
func getClusterNodesIP(kubeClient *unversioned.Client, nodeSelector string) (clusterNodes []string) {
listOpts := api.ListOptions{}
if nodeSelector != "" {
label, err := labels.Parse(nodeSelector)
if err != nil {
glog.Fatalf("'%v' is not a valid selector: %v", nodeSelector, err)
}
listOpts.LabelSelector = label
}
nodes, err := kubeClient.Nodes().List(listOpts)
if err != nil {
glog.Fatalf("Error getting running nodes: %v", err)
}
for _, nodo := range nodes.Items {
nodeIP, err := node.GetNodeHostIP(&nodo)
if err == nil {
clusterNodes = append(clusterNodes, nodeIP.String())
}
}
sort.Strings(clusterNodes)
return
}
示例10: clearNodeLabels
func clearNodeLabels(c *client.Client) error {
nodeClient := c.Nodes()
nodeList, err := nodeClient.List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for _, node := range nodeList.Items {
if len(node.Labels) != 0 {
node.Labels = map[string]string{}
var newNode *api.Node
err = wait.Poll(updateRetryPeriod, updateRetryTimeout, func() (bool, error) {
newNode, err = nodeClient.Update(&node)
if err == nil {
return true, err
}
if se, ok := err.(*apierrs.StatusError); ok && se.ErrStatus.Reason == unversioned.StatusReasonConflict {
Logf("failed to update node due to resource version conflict")
return false, nil
}
return false, err
})
if err != nil {
return err
} else if len(newNode.Labels) != 0 {
return fmt.Errorf("Could not make node labels nil.")
}
}
}
return nil
}
示例11: CheckCadvisorHealthOnAllNodes
func CheckCadvisorHealthOnAllNodes(c *client.Client, timeout time.Duration) {
// It should be OK to list unschedulable Nodes here.
By("getting list of nodes")
nodeList, err := c.Nodes().List(api.ListOptions{})
framework.ExpectNoError(err)
var errors []error
retries := maxRetries
for {
errors = []error{}
for _, node := range nodeList.Items {
// cadvisor is not accessible directly unless its port (4194 by default) is exposed.
// Here, we access '/stats/' REST endpoint on the kubelet which polls cadvisor internally.
statsResource := fmt.Sprintf("api/v1/proxy/nodes/%s/stats/", node.Name)
By(fmt.Sprintf("Querying stats from node %s using url %s", node.Name, statsResource))
_, err = c.Get().AbsPath(statsResource).Timeout(timeout).Do().Raw()
if err != nil {
errors = append(errors, err)
}
}
if len(errors) == 0 {
return
}
if retries--; retries <= 0 {
break
}
framework.Logf("failed to retrieve kubelet stats -\n %v", errors)
time.Sleep(sleepDuration)
}
framework.Failf("Failed after retrying %d times for cadvisor to be healthy on all nodes. Errors:\n%v", maxRetries, errors)
}
示例12: NewMetricsGrabber
func NewMetricsGrabber(c *client.Client, kubelets bool, scheduler bool, controllers bool, apiServer bool) (*MetricsGrabber, error) {
registeredMaster := false
masterName := ""
nodeList, err := c.Nodes().List(api.ListOptions{})
if err != nil {
return nil, err
}
if len(nodeList.Items) < 1 {
glog.Warning("Can't find any Nodes in the API server to grab metrics from")
}
for _, node := range nodeList.Items {
if system.IsMasterNode(&node) {
registeredMaster = true
masterName = node.Name
break
}
}
if !registeredMaster {
scheduler = false
controllers = false
glog.Warningf("Master node is not registered. Grabbing metrics from Scheduler and ControllerManager is disabled.")
}
return &MetricsGrabber{
client: c,
grabFromApiServer: apiServer,
grabFromControllerManager: controllers,
grabFromKubelets: kubelets,
grabFromScheduler: scheduler,
masterName: masterName,
registeredMaster: registeredMaster,
}, nil
}
示例13: externalMetrics
func externalMetrics(kubeClient *kube.Client, metrics *Metrics) error {
nodeList, err := kubeClient.Nodes().List(kube_api.ListOptions{})
if err != nil {
return fmt.Errorf("externalMetrics: unable to retrieve node list from k8s")
}
metrics.Nodes = int64(len(nodeList.Items))
return nil
}
示例14: NewResourceUsageGatherer
func NewResourceUsageGatherer(c *client.Client, options ResourceGathererOptions) (*containerResourceGatherer, error) {
g := containerResourceGatherer{
client: c,
stopCh: make(chan struct{}),
containerIDToNameMap: make(map[string]string),
containerIDs: make([]string, 0),
options: options,
}
if options.inKubemark {
g.workerWg.Add(1)
g.workers = append(g.workers, resourceGatherWorker{
inKubemark: true,
stopCh: g.stopCh,
wg: &g.workerWg,
finished: false,
})
} else {
pods, err := c.Pods("kube-system").List(api.ListOptions{})
if err != nil {
Logf("Error while listing Pods: %v", err)
return nil, err
}
for _, pod := range pods.Items {
for _, container := range pod.Status.ContainerStatuses {
containerID := strings.TrimPrefix(container.ContainerID, "docker:/")
g.containerIDToNameMap[containerID] = pod.Name + "/" + container.Name
g.containerIDs = append(g.containerIDs, containerID)
}
}
nodeList, err := c.Nodes().List(api.ListOptions{})
if err != nil {
Logf("Error while listing Nodes: %v", err)
return nil, err
}
for _, node := range nodeList.Items {
if !options.masterOnly || system.IsMasterNode(&node) {
g.workerWg.Add(1)
g.workers = append(g.workers, resourceGatherWorker{
c: c,
nodeName: node.Name,
wg: &g.workerWg,
containerIDToNameMap: g.containerIDToNameMap,
containerIDs: g.containerIDs,
stopCh: g.stopCh,
finished: false,
inKubemark: false,
})
if options.masterOnly {
break
}
}
}
}
return &g, nil
}
示例15: pickNode
func pickNode(c *client.Client) (string, error) {
nodes, err := c.Nodes().List(labels.Everything(), fields.Everything())
if err != nil {
return "", err
}
if len(nodes.Items) == 0 {
return "", fmt.Errorf("no nodes exist, can't test node proxy")
}
return nodes.Items[0].Name, nil
}