mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-18 16:21:13 +00:00
Move duplicated logic into single function.
This commit is contained in:
parent
0c68c6307a
commit
25ab3b695e
@ -19,6 +19,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/coreos/go-etcd/etcd"
|
"github.com/coreos/go-etcd/etcd"
|
||||||
|
|
||||||
@ -96,20 +97,32 @@ func (registry *EtcdRegistry) listEtcdNode(key string) ([]*etcd.Node, error) {
|
|||||||
return result.Node.Nodes, nil
|
return result.Node.Nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *EtcdRegistry) listPodsForMachine(machine string) ([]api.Pod, error) {
|
// Extract a go object per etcd node into a slice.
|
||||||
pods := []api.Pod{}
|
func (r *EtcdRegistry) extractList(key string, slicePtr interface{}) error {
|
||||||
key := "/registry/hosts/" + machine + "/pods"
|
nodes, err := r.listEtcdNode(key)
|
||||||
nodes, err := registry.listEtcdNode(key)
|
if err != nil {
|
||||||
for _, node := range nodes {
|
return err
|
||||||
pod := api.Pod{}
|
|
||||||
err = json.Unmarshal([]byte(node.Value), &pod)
|
|
||||||
if err != nil {
|
|
||||||
return pods, err
|
|
||||||
}
|
|
||||||
pod.CurrentState.Host = machine
|
|
||||||
pods = append(pods, pod)
|
|
||||||
}
|
}
|
||||||
return pods, err
|
pv := reflect.ValueOf(slicePtr)
|
||||||
|
if pv.Type().Kind() != reflect.Ptr || pv.Type().Elem().Kind() != reflect.Slice {
|
||||||
|
// This should not happen at runtime.
|
||||||
|
panic("need ptr to slice")
|
||||||
|
}
|
||||||
|
v := pv.Elem()
|
||||||
|
for _, node := range nodes {
|
||||||
|
obj := reflect.New(v.Type().Elem())
|
||||||
|
err = json.Unmarshal([]byte(node.Value), obj.Interface())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
v.Set(reflect.Append(v, obj.Elem()))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (registry *EtcdRegistry) listPodsForMachine(machine string) (pods []api.Pod, err error) {
|
||||||
|
err = registry.extractList("/registry/hosts/"+machine+"/pods", &pods)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *EtcdRegistry) GetPod(podID string) (*api.Pod, error) {
|
func (registry *EtcdRegistry) GetPod(podID string) (*api.Pod, error) {
|
||||||
@ -262,17 +275,8 @@ func isEtcdNotFound(err error) bool {
|
|||||||
|
|
||||||
func (registry *EtcdRegistry) ListControllers() ([]api.ReplicationController, error) {
|
func (registry *EtcdRegistry) ListControllers() ([]api.ReplicationController, error) {
|
||||||
var controllers []api.ReplicationController
|
var controllers []api.ReplicationController
|
||||||
key := "/registry/controllers"
|
err := registry.extractList("/registry/controllers", &controllers)
|
||||||
nodes, err := registry.listEtcdNode(key)
|
return controllers, err
|
||||||
for _, node := range nodes {
|
|
||||||
var controller api.ReplicationController
|
|
||||||
err = json.Unmarshal([]byte(node.Value), &controller)
|
|
||||||
if err != nil {
|
|
||||||
return controllers, err
|
|
||||||
}
|
|
||||||
controllers = append(controllers, controller)
|
|
||||||
}
|
|
||||||
return controllers, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeControllerKey(id string) string {
|
func makeControllerKey(id string) string {
|
||||||
@ -323,21 +327,9 @@ func makeServiceKey(name string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (registry *EtcdRegistry) ListServices() (api.ServiceList, error) {
|
func (registry *EtcdRegistry) ListServices() (api.ServiceList, error) {
|
||||||
nodes, err := registry.listEtcdNode("/registry/services/specs")
|
var list api.ServiceList
|
||||||
if err != nil {
|
err := registry.extractList("/registry/services/specs", &list.Items)
|
||||||
return api.ServiceList{}, err
|
return list, err
|
||||||
}
|
|
||||||
|
|
||||||
var services []api.Service
|
|
||||||
for _, node := range nodes {
|
|
||||||
var svc api.Service
|
|
||||||
err := json.Unmarshal([]byte(node.Value), &svc)
|
|
||||||
if err != nil {
|
|
||||||
return api.ServiceList{}, err
|
|
||||||
}
|
|
||||||
services = append(services, svc)
|
|
||||||
}
|
|
||||||
return api.ServiceList{Items: services}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *EtcdRegistry) CreateService(svc api.Service) error {
|
func (registry *EtcdRegistry) CreateService(svc api.Service) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user