Java源码示例:io.kubernetes.client.openapi.models.V1NodeList

示例1
private SharedIndexInformer<V1Node> createNodeInformer(SharedInformerFactory sharedInformerFactory) {
    return sharedInformerFactory.sharedIndexInformerFor(
            (CallGeneratorParams params) -> coreV1Api.listNodeCall(
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    params.resourceVersion,
                    params.timeoutSeconds,
                    params.watch,
                    null
            ),
            V1Node.class,
            V1NodeList.class,
            configuration.getKubeApiServerIntegratorRefreshIntervalMs()
    );
}
 
示例2
public static void main(String[] args) throws IOException {

    CoreV1Api coreV1Api = new CoreV1Api();
    ApiClient apiClient = coreV1Api.getApiClient();
    OkHttpClient httpClient =
        apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
    apiClient.setHttpClient(httpClient);

    // instantiating an informer-factory, and there should be only one informer-factory globally.
    SharedInformerFactory informerFactory = new SharedInformerFactory();
    // registering node-informer into the informer-factory.
    SharedIndexInformer<V1Node> nodeInformer =
        informerFactory.sharedIndexInformerFor(
            (CallGeneratorParams params) -> {
              return coreV1Api.listNodeCall(
                  null,
                  null,
                  null,
                  null,
                  null,
                  null,
                  params.resourceVersion,
                  params.timeoutSeconds,
                  params.watch,
                  null);
            },
            V1Node.class,
            V1NodeList.class);
    informerFactory.startAllRegisteredInformers();

    EventBroadcaster eventBroadcaster = new LegacyEventBroadcaster(coreV1Api);

    // nodeReconciler prints node information on events
    NodePrintingReconciler nodeReconciler =
        new NodePrintingReconciler(
            nodeInformer,
            eventBroadcaster.newRecorder(
                new V1EventSource().host("localhost").component("node-printer")));

    // Use builder library to construct a default controller.
    Controller controller =
        ControllerBuilder.defaultBuilder(informerFactory)
            .watch(
                (workQueue) ->
                    ControllerBuilder.controllerWatchBuilder(V1Node.class, workQueue)
                        .withWorkQueueKeyFunc(
                            (V1Node node) ->
                                new Request(node.getMetadata().getName())) // optional, default to
                        .withOnAddFilter(
                            (V1Node createdNode) ->
                                createdNode
                                    .getMetadata()
                                    .getName()
                                    .startsWith("docker-")) // optional, set onAdd filter
                        .withOnUpdateFilter(
                            (V1Node oldNode, V1Node newNode) ->
                                newNode
                                    .getMetadata()
                                    .getName()
                                    .startsWith("docker-")) // optional, set onUpdate filter
                        .withOnDeleteFilter(
                            (V1Node deletedNode, Boolean stateUnknown) ->
                                deletedNode
                                    .getMetadata()
                                    .getName()
                                    .startsWith("docker-")) // optional, set onDelete filter
                        .build())
            .withReconciler(nodeReconciler) // required, set the actual reconciler
            .withName("node-printing-controller") // optional, set name for controller
            .withWorkerCount(4) // optional, set worker thread count
            .withReadyFunc(
                nodeInformer
                    ::hasSynced) // optional, only starts controller when the cache has synced up
            .build();

    // Use builder library to manage one or multiple controllers.
    ControllerManager controllerManager =
        ControllerBuilder.controllerManagerBuilder(informerFactory)
            .addController(controller)
            .build();

    LeaderElectingController leaderElectingController =
        new LeaderElectingController(
            new LeaderElector(
                new LeaderElectionConfig(
                    new EndpointsLock("kube-system", "leader-election", "foo"),
                    Duration.ofMillis(10000),
                    Duration.ofMillis(8000),
                    Duration.ofMillis(5000))),
            controllerManager);

    leaderElectingController.run();
  }
 
示例3
/**
 * get NodeInfoUtils objects for the nodes on this cluster
 *
 * @return the NodeInfoUtils object list. If it can not get the list from K8s master, return null.
 */
public ArrayList<JobMasterAPI.NodeInfo> getNodeInfo(String rackLabelKey,
                                                    String datacenterLabelKey) {

  V1NodeList nodeList = null;
  try {
    nodeList = coreApi.listNode(null, null, null, null, null, null, null, null, null);
  } catch (ApiException e) {
    LOG.log(Level.SEVERE, "Exception when getting NodeList.", e);
    return null;
  }

  ArrayList<JobMasterAPI.NodeInfo> nodeInfoList = new ArrayList<>();
  for (V1Node node : nodeList.getItems()) {
    List<V1NodeAddress> addressList = node.getStatus().getAddresses();
    for (V1NodeAddress nodeAddress : addressList) {
      if ("InternalIP".equalsIgnoreCase(nodeAddress.getType())) {
        String nodeIP = nodeAddress.getAddress();
        String rackName = null;
        String datacenterName = null;

        // get labels
        Map<String, String> labelMap = node.getMetadata().getLabels();
        for (String key : labelMap.keySet()) {
          if (key.equalsIgnoreCase(rackLabelKey)) {
            rackName = labelMap.get(key);
          }
          if (key.equalsIgnoreCase(datacenterLabelKey)) {
            datacenterName = labelMap.get(key);
          }
        }

        JobMasterAPI.NodeInfo nodeInfo =
            NodeInfoUtils.createNodeInfo(nodeIP, rackName, datacenterName);
        nodeInfoList.add(nodeInfo);
        break;
      }
    }
  }

  return nodeInfoList;
}