-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent: api: rest: Add Kubernetes information
- Loading branch information
Showing
15 changed files
with
645 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package infra | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
// "k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func GetClientSet() (*kubernetes.Clientset, error) { | ||
|
||
configPath := filepath.Join(os.Getenv("HOME"), ".kube", "config") | ||
config, err := clientcmd.BuildConfigFromFlags("", configPath) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return clientset, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package infra | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/cloud-barista/cm-honeybee/agent/pkg/api/rest/model/onprem/kubernetes" | ||
) | ||
|
||
var kubernetesInfoLock sync.Mutex | ||
|
||
func GetKubernetesInfo() (*kubernetes.Kubernetes, error) { | ||
if !kubernetesInfoLock.TryLock() { | ||
return nil, errors.New("kubernetes info collection is in progress") | ||
} | ||
defer func() { | ||
kubernetesInfoLock.Unlock() | ||
}() | ||
|
||
var i kubernetes.Kubernetes | ||
var err error | ||
|
||
i.Nodes, err = GetNodeInfo() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Workloads, err = GetWorkloadInfo() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &i, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package infra | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cloud-barista/cm-honeybee/agent/pkg/api/rest/common" | ||
"github.com/cloud-barista/cm-honeybee/agent/pkg/api/rest/model/onprem/kubernetes" | ||
|
||
"github.com/jollaman999/utils/logger" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func GetNodeInfo() ([]kubernetes.Node, error) { | ||
clientset, err := GetClientSet() | ||
if err != nil { | ||
logger.Println(logger.ERROR, true, "Kubernetes Connection Error: "+err.Error()) | ||
return []kubernetes.Node{}, err | ||
} | ||
|
||
objects, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
logger.Println(logger.ERROR, true, "Nodes: "+err.Error()) | ||
return []kubernetes.Node{}, err | ||
} | ||
|
||
nodeMap, err := common.Unmarshal(objects) | ||
if err != nil { | ||
logger.Println(logger.ERROR, true, "Error unmarshaling nodes: "+err.Error()) | ||
return []kubernetes.Node{}, err | ||
} | ||
|
||
ObjectCnt := len(objects.Items) | ||
|
||
var nodes []kubernetes.Node | ||
|
||
for i := 0; i < ObjectCnt; i++ { | ||
node := kubernetes.Node{ | ||
Name: common.GoJq(nodeMap, fmt.Sprintf(".items[%d].metadata.name", i)), | ||
Labels: common.GoJq(nodeMap, fmt.Sprintf(".items[%d].metadata.labels", i)), | ||
Addresses: common.GoJq(nodeMap, fmt.Sprintf(".items[%d].status.addresses[]", i)), | ||
NodeInfo: common.GoJq(nodeMap, fmt.Sprintf(".items[%d].status.nodeInfo", i)), | ||
} | ||
nodes = append(nodes, node) | ||
} | ||
|
||
return nodes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package infra | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/cloud-barista/cm-honeybee/agent/pkg/api/rest/common" | ||
|
||
"github.com/jollaman999/utils/logger" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var kindList = []string{ | ||
"namespaces", | ||
"pods", | ||
"services", | ||
"deployments", | ||
"daemonsets", | ||
"replicasets", | ||
"statefulsets", | ||
"job", | ||
"cronjobs", | ||
"ingresses", | ||
"persistentvolumes", | ||
"persistentvolumeclaims", | ||
"storageclasses", | ||
"configmaps", | ||
"servicesaccounts", | ||
"secrets", | ||
"roles", | ||
"rolebindings", | ||
"clusterroles", | ||
"clusterrolebindings", | ||
} | ||
|
||
var clientFilter = map[string]string{ | ||
"namespaces": "CoreV1.Namespaces", | ||
"pods": "CoreV1.Pods.\"\"", | ||
"services": "CoreV1.Services.\"\"", | ||
"configmaps": "CoreV1.ConfigMaps.\"\"", | ||
"servicesaccounts": "CoreV1.ServiceAccounts.\"\"", | ||
"secrets": "CoreV1.Secrets.\"\"", | ||
"persistentvolumes": "CoreV1.PersistentVolumes", | ||
"persistentvolumeclaims": "CoreV1.PersistentVolumeClaims.\"\"", | ||
"deployments": "AppsV1.Deployments.\"\"", | ||
"daemonsets": "AppsV1.DaemonSets.\"\"", | ||
"replicasets": "AppsV1.ReplicaSets.\"\"", | ||
"statefulsets": "AppsV1.StatefulSets.\"\"", | ||
"job": "BatchV1.Jobs.\"\"", | ||
"cronjobs": "BatchV1.CronJobs.\"\"", | ||
"ingresses": "NetworkingV1.Ingresses.\"\"", | ||
"storageclasses": "StorageV1.StorageClasses", | ||
} | ||
|
||
func GetWorkloadInfo() (map[string]interface{}, error) { | ||
workloads := make(map[string]interface{}) | ||
|
||
for _, kind := range kindList { | ||
if clientMethod, exists := clientFilter[kind]; exists { | ||
objects, err := callClientMethod(clientMethod) | ||
if err != nil { | ||
logger.Println(logger.ERROR, true, fmt.Sprintf("Error fetching %s: %s", kind, err.Error())) | ||
continue | ||
} | ||
|
||
processObjects(kind, objects, workloads) | ||
} | ||
} | ||
|
||
return workloads, nil | ||
} | ||
|
||
func callClientMethod(methodName string) (interface{}, error) { | ||
clientset, err := GetClientSet() | ||
if err != nil { | ||
logger.Println(logger.ERROR, true, "Kubernetes Connection Error: "+err.Error()) | ||
return nil, err | ||
} | ||
|
||
methods := strings.Split(methodName, ".") | ||
var result reflect.Value = reflect.ValueOf(clientset) | ||
|
||
for _, method := range methods { | ||
if method == "\"\"" { | ||
// 빈 문자열 인자로 메소드 호출 | ||
result = result.Call([]reflect.Value{reflect.ValueOf("")})[0] | ||
} else { | ||
result = result.MethodByName(method) | ||
if !result.IsValid() { | ||
return nil, fmt.Errorf("invalid method: %s in %s", method, methodName) | ||
} | ||
if result.Type().NumIn() == 0 { | ||
result = result.Call(nil)[0] | ||
} | ||
} | ||
} | ||
|
||
listMethod := result.MethodByName("List") | ||
if !listMethod.IsValid() { | ||
return nil, fmt.Errorf("invalid List method for: %s", methodName) | ||
} | ||
|
||
results := listMethod.Call([]reflect.Value{ | ||
reflect.ValueOf(context.TODO()), | ||
reflect.ValueOf(metav1.ListOptions{}), | ||
}) | ||
|
||
if len(results) != 2 { | ||
return nil, fmt.Errorf("unexpected number of return values") | ||
} | ||
|
||
if !results[1].IsNil() { | ||
return nil, results[1].Interface().(error) | ||
} | ||
|
||
return results[0].Interface(), nil | ||
} | ||
|
||
func processObjects(kind string, objects interface{}, workloads map[string]interface{}) { | ||
|
||
objectMap, err := common.Unmarshal(objects) | ||
if err != nil { | ||
logger.Println(logger.ERROR, true, fmt.Sprintf("Error unmarshaling %s: %s", kind, err.Error())) | ||
return | ||
} | ||
|
||
items := common.GoJq(objectMap, ".items") | ||
objectCnt := 0 | ||
if itemsSlice, ok := items.([]interface{}); ok { | ||
objectCnt = len(itemsSlice) | ||
} | ||
|
||
var newStruct []map[string]interface{} | ||
|
||
for i := 0; i < objectCnt; i++ { | ||
item := make(map[string]interface{}) | ||
|
||
if namespace := common.GoJq(objectMap, fmt.Sprintf(".items[%d].metadata.namespace", i)); namespace != nil { | ||
item["Namespace"] = namespace | ||
} | ||
if name := common.GoJq(objectMap, fmt.Sprintf(".items[%d].metadata.name", i)); name != nil { | ||
item["Name"] = name | ||
} | ||
if svcType := common.GoJq(objectMap, fmt.Sprintf(".items[%d].spec.type", i)); svcType != nil { | ||
item["Type"] = svcType | ||
} | ||
if svcClusterIp := common.GoJq(objectMap, fmt.Sprintf(".items[%d].spec.clusterIP", i)); svcClusterIp != nil { | ||
item["ClusterIP"] = svcClusterIp | ||
} | ||
if status := common.GoJq(objectMap, fmt.Sprintf(".items[%d].status.phase", i)); status != nil { | ||
item["Status"] = status | ||
} | ||
|
||
if len(item) > 0 { | ||
newStruct = append(newStruct, item) | ||
} | ||
} | ||
|
||
workloads[kind] = newStruct | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.