mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
【修改】k8s 配置
This commit is contained in:
132
apps/devops/services/k8s/ingress/ingress.go
Normal file
132
apps/devops/services/k8s/ingress/ingress.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package ingress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"pandax/apps/devops/entity/k8s"
|
||||
k8scommon "pandax/apps/devops/services/k8s/common"
|
||||
"pandax/apps/devops/services/k8s/dataselect"
|
||||
//v1 "k8s.io/api/extensions/v1beta1"
|
||||
v1 "k8s.io/api/networking/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
client "k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
// Ingress - a single ingress returned to the frontend.
|
||||
type Ingress struct {
|
||||
k8s.ObjectMeta `json:"objectMeta"`
|
||||
k8s.TypeMeta `json:"typeMeta"`
|
||||
|
||||
// External endpoints of this ingress.
|
||||
Endpoints []k8scommon.Endpoint `json:"endpoints"`
|
||||
Hosts []string `json:"hosts"`
|
||||
Spec v1.IngressSpec `json:"spec"`
|
||||
Status v1.IngressStatus `json:"status"`
|
||||
}
|
||||
|
||||
// IngressList - response structure for a queried ingress list.
|
||||
type IngressList struct {
|
||||
k8s.ListMeta `json:"listMeta"`
|
||||
|
||||
// Unordered list of Ingresss.
|
||||
Items []Ingress `json:"items"`
|
||||
}
|
||||
|
||||
// GetIngressList returns all ingresses in the given namespace.
|
||||
func GetIngressList(client *client.Clientset, namespace *k8scommon.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*IngressList, error) {
|
||||
//ingressList, err := client.ExtensionsV1beta1().Ingresses(namespace.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
|
||||
ingressList, err := client.NetworkingV1().Ingresses(namespace.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toIngressList(ingressList.Items, dsQuery), nil
|
||||
|
||||
}
|
||||
|
||||
func getEndpoints(ingress *v1.Ingress) []k8scommon.Endpoint {
|
||||
endpoints := make([]k8scommon.Endpoint, 0)
|
||||
if len(ingress.Status.LoadBalancer.Ingress) > 0 {
|
||||
for _, status := range ingress.Status.LoadBalancer.Ingress {
|
||||
endpoint := k8scommon.Endpoint{}
|
||||
if status.Hostname != "" {
|
||||
endpoint.Host = status.Hostname
|
||||
} else if status.IP != "" {
|
||||
endpoint.Host = status.IP
|
||||
}
|
||||
endpoints = append(endpoints, endpoint)
|
||||
}
|
||||
}
|
||||
return endpoints
|
||||
}
|
||||
|
||||
func getHosts(ingress *v1.Ingress) []string {
|
||||
hosts := make([]string, 0)
|
||||
set := make(map[string]struct{})
|
||||
|
||||
for _, rule := range ingress.Spec.Rules {
|
||||
if _, exists := set[rule.Host]; !exists && len(rule.Host) > 0 {
|
||||
hosts = append(hosts, rule.Host)
|
||||
}
|
||||
|
||||
set[rule.Host] = struct{}{}
|
||||
}
|
||||
|
||||
return hosts
|
||||
}
|
||||
|
||||
func toIngress(ingress *v1.Ingress) Ingress {
|
||||
return Ingress{
|
||||
ObjectMeta: k8s.NewObjectMeta(ingress.ObjectMeta),
|
||||
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindIngress),
|
||||
Endpoints: getEndpoints(ingress),
|
||||
Hosts: getHosts(ingress),
|
||||
Spec: ingress.Spec,
|
||||
Status: ingress.Status,
|
||||
}
|
||||
}
|
||||
|
||||
func toIngressList(ingresses []v1.Ingress, dsQuery *dataselect.DataSelectQuery) *IngressList {
|
||||
newIngressList := &IngressList{
|
||||
ListMeta: k8s.ListMeta{TotalItems: len(ingresses)},
|
||||
Items: make([]Ingress, 0),
|
||||
}
|
||||
|
||||
ingresCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(ingresses), dsQuery)
|
||||
ingresses = fromCells(ingresCells)
|
||||
newIngressList.ListMeta = k8s.ListMeta{TotalItems: filteredTotal}
|
||||
|
||||
for _, ingress := range ingresses {
|
||||
newIngressList.Items = append(newIngressList.Items, toIngress(&ingress))
|
||||
}
|
||||
|
||||
return newIngressList
|
||||
}
|
||||
|
||||
func DeleteIngress(client *client.Clientset, namespace string, name string) error {
|
||||
global.Log.Info(fmt.Sprintf("请求删除Ingress: %v, namespace: %v", name, namespace))
|
||||
return client.ExtensionsV1beta1().Ingresses(namespace).Delete(
|
||||
context.TODO(),
|
||||
name,
|
||||
metav1.DeleteOptions{},
|
||||
)
|
||||
}
|
||||
|
||||
func DeleteCollectionIngress(client *client.Clientset, ingressList []k8s.ServiceData) (err error) {
|
||||
global.Log.Info("批量删除Ingress开始")
|
||||
for _, v := range ingressList {
|
||||
global.Log.Info(fmt.Sprintf("delete ingress:%v, ns: %v", v.Name, v.Namespace))
|
||||
err := client.ExtensionsV1beta1().Ingresses(v.Namespace).Delete(
|
||||
context.TODO(),
|
||||
v.Name,
|
||||
metav1.DeleteOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
global.Log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
global.Log.Info("删除ingress已完成")
|
||||
return nil
|
||||
}
|
||||
41
apps/devops/services/k8s/ingress/ingress_common.go
Normal file
41
apps/devops/services/k8s/ingress/ingress_common.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package ingress
|
||||
|
||||
import (
|
||||
"pandax/apps/devops/services/k8s/dataselect"
|
||||
//v1 "k8s.io/api/extensions/v1beta1"
|
||||
v1 "k8s.io/api/networking/v1"
|
||||
)
|
||||
|
||||
// The code below allows to perform complex data section on []extensions.Ingress
|
||||
|
||||
type IngressCell v1.Ingress
|
||||
|
||||
func (self IngressCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue {
|
||||
switch name {
|
||||
case dataselect.NameProperty:
|
||||
return dataselect.StdComparableString(self.ObjectMeta.Name)
|
||||
case dataselect.CreationTimestampProperty:
|
||||
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
|
||||
case dataselect.NamespaceProperty:
|
||||
return dataselect.StdComparableString(self.ObjectMeta.Namespace)
|
||||
default:
|
||||
// if name is not supported then just return a constant dummy value, sort will have no effect.
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func toCells(std []v1.Ingress) []dataselect.DataCell {
|
||||
cells := make([]dataselect.DataCell, len(std))
|
||||
for i := range std {
|
||||
cells[i] = IngressCell(std[i])
|
||||
}
|
||||
return cells
|
||||
}
|
||||
|
||||
func fromCells(cells []dataselect.DataCell) []v1.Ingress {
|
||||
std := make([]v1.Ingress, len(cells))
|
||||
for i := range std {
|
||||
std[i] = v1.Ingress(cells[i].(IngressCell))
|
||||
}
|
||||
return std
|
||||
}
|
||||
46
apps/devops/services/k8s/ingress/ingress_detail.go
Normal file
46
apps/devops/services/k8s/ingress/ingress_detail.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package ingress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"pandax/base/global"
|
||||
|
||||
//v1 "k8s.io/api/extensions/v1beta1"
|
||||
v1 "k8s.io/api/networking/v1"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
client "k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
// IngressDetail API resource provides mechanisms to inject containers with configuration data while keeping
|
||||
// containers agnostic of Kubernetes
|
||||
type IngressDetail struct {
|
||||
// Extends list item structure.
|
||||
Ingress `json:",inline"`
|
||||
|
||||
// Spec is the desired state of the Ingress.
|
||||
Spec v1.IngressSpec `json:"spec"`
|
||||
|
||||
// Status is the current state of the Ingress.
|
||||
Status v1.IngressStatus `json:"status"`
|
||||
}
|
||||
|
||||
// GetIngressDetail returns detailed information about an ingress
|
||||
func GetIngressDetail(client *client.Clientset, namespace, name string) (*IngressDetail, error) {
|
||||
global.Log.Info(fmt.Sprintf("Getting details of %s ingress in %s namespace", name, namespace))
|
||||
|
||||
//rawIngress, err := client.ExtensionsV1beta1().Ingresses(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
|
||||
rawIngress, err := client.NetworkingV1().Ingresses(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return getIngressDetail(rawIngress), nil
|
||||
}
|
||||
|
||||
func getIngressDetail(i *v1.Ingress) *IngressDetail {
|
||||
return &IngressDetail{
|
||||
Ingress: toIngress(i),
|
||||
Spec: i.Spec,
|
||||
Status: i.Status,
|
||||
}
|
||||
}
|
||||
31
apps/devops/services/k8s/ingress/ingress_test.go
Normal file
31
apps/devops/services/k8s/ingress/ingress_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package ingress
|
||||
|
||||
import (
|
||||
"context"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetIngressList(t *testing.T) {
|
||||
|
||||
rules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
overrides := &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{InsecureSkipTLSVerify: true}}
|
||||
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides).ClientConfig()
|
||||
if err != nil {
|
||||
log.Fatalf("Couldn't get Kubernetes default config: %s", err)
|
||||
}
|
||||
|
||||
client, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
ingressList, err := client.ExtensionsV1beta1().Ingresses("").List(context.TODO(), metav1.ListOptions{})
|
||||
|
||||
log.Print(ingressList)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user