代码拉取完成,页面将自动刷新
/*
Copyright 2021 Alibaba Group Holding Limited.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package discovery
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
k8shelper "gitee.com/eezz10001/polardbx-operator/pkg/k8s/helper"
)
type k8sHostDiscovery struct {
kubernetes.Interface
node string
namespace string
selector map[string]string
mu sync.RWMutex
aliveHosts map[string]HostInfo
closeChan chan struct{}
}
func (h *k8sHostDiscovery) IsLocal(name string) bool {
return h.node == name
}
func (h *k8sHostDiscovery) GetHost(name string) (HostInfo, error) {
h.mu.RLock()
defer h.mu.RUnlock()
host, ok := h.aliveHosts[name]
if !ok {
return host, ErrHostNotFound
}
return host, nil
}
func (h *k8sHostDiscovery) GetHosts() (map[string]HostInfo, error) {
h.mu.RLock()
defer h.mu.RUnlock()
// Return the cached hosts, no needs to copy
return h.aliveHosts, nil
}
func (h *k8sHostDiscovery) labelSelector() string {
l := make([]string, 0, len(h.selector))
for k, v := range h.selector {
l = append(l, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(l, ",")
}
func (h *k8sHostDiscovery) sync() error {
hosts := make(map[string]HostInfo)
// Retrieve from k8s
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
hpfsPods, err := h.CoreV1().Pods(h.namespace).List(ctx, metav1.ListOptions{
LabelSelector: h.labelSelector(),
})
if err != nil {
return err
}
for _, p := range hpfsPods.Items {
// Skip those not ready
if !k8shelper.IsPodReady(&p) {
continue
}
c := &p.Spec.Containers[0]
hpfsPort := k8shelper.GetPortFromContainer(c, "hpfs")
if hpfsPort == nil {
continue
}
sshPort := k8shelper.GetPortFromContainer(c, "ssh")
if sshPort == nil {
continue
}
hosts[p.Spec.NodeName] = HostInfo{
NodeName: p.Spec.NodeName,
HpfsHost: p.Status.PodIP,
HpfsPort: uint32(hpfsPort.ContainerPort),
SshPort: uint32(sshPort.ContainerPort),
}
}
// update the cached hosts
h.mu.Lock()
defer h.mu.Unlock()
toPrint := !reflect.DeepEqual(h.aliveHosts, hosts)
h.aliveHosts = hosts
// Output alive hosts
if toPrint {
s, _ := json.Marshal(h.aliveHosts)
fmt.Println("alive hosts: " + string(s))
}
return nil
}
func (h *k8sHostDiscovery) syncLoop() {
for {
select {
case <-time.After(5 * time.Second):
if err := h.sync(); err != nil {
fmt.Printf("failed to sync nodes: %s\n", err.Error())
}
case <-h.closeChan:
return
}
}
}
func (h *k8sHostDiscovery) Close() {
close(h.closeChan)
}
func NewK8sHostDiscovery(node string, namespace string, selector map[string]string) (HostDiscovery, error) {
if len(node) == 0 {
return nil, errors.New("invalid node")
}
if selector == nil || len(selector) == 0 {
return nil, errors.New("invalid selector")
}
if len(namespace) == 0 {
namespace = "default"
}
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
hs := &k8sHostDiscovery{
Interface: clientset,
node: node,
namespace: namespace,
selector: selector,
aliveHosts: make(map[string]HostInfo),
closeChan: make(chan struct{}),
}
// Sync the first time, and then starts the sync loop
if err = hs.sync(); err != nil {
return nil, err
}
go hs.syncLoop()
return hs, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。