Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HZ89 committed Dec 21, 2018
1 parent 9fab4dc commit 83b4319
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/backend/client/client.go
Expand Up @@ -150,7 +150,7 @@ func buildCacheController(client *kubernetes.Clientset) *CacheIndexer {
go eventInformer.Run(stopCh)

// create the endpoint watcher
endpointsListWatcher := kcache.NewListWatchFromClient(client.CoreV1().RESTClient(), "endpoint", v1.NamespaceAll, fields.Everything())
endpointsListWatcher := kcache.NewListWatchFromClient(client.CoreV1().RESTClient(), "endpoints", v1.NamespaceAll, fields.Everything())
endpointsIndexer, endpointsinformer := kcache.NewIndexerInformer(endpointsListWatcher, &v1.Endpoints{}, defaultResyncPeriod, kcache.ResourceEventHandlerFuncs{}, kcache.Indexers{})
go endpointsinformer.Run(stopCh)
return &CacheIndexer{
Expand Down
4 changes: 2 additions & 2 deletions src/backend/controllers/kubernetes/service/service.go
Expand Up @@ -58,7 +58,7 @@ func (c *KubeServiceController) GetDetail() {
if err != nil {
c.AbortBadRequestFormat("Cluster")
}
serviceDetail, err := service.GetServiceDetail(manager.Client, manager.Indexer, namespace, namespace)
serviceDetail, err := service.GetServiceDetail(manager.Client, manager.Indexer, namespace, name)
if err != nil {
logs.Error("get kubernetes(%s) namespace(%s) service(%s) detail error: %s", cluster, namespace, name, err.Error())
c.AbortInternalServerError("get kubernetes service detail error.")
Expand Down Expand Up @@ -173,7 +173,7 @@ func (c *KubeServiceController) Get() {
name := c.Ctx.Input.Param(":service")
cli, err := client.Client(cluster)
if err == nil {
result, err := service.GetServiceDetail(cli, name, namespace)
result, err := service.GetService(cli, name, namespace)
if err != nil {
logs.Error("get kubernetes service detail error.", cluster, namespace, name, err)
c.HandleError(err)
Expand Down
81 changes: 81 additions & 0 deletions src/backend/resources/common/endpoint.go
@@ -0,0 +1,81 @@
// Copyright 2017 The Kubernetes Authors.
//
// 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 common

import (
"bytes"

api "k8s.io/api/core/v1"
)

// Endpoint describes an endpoint that is host and a list of available ports for that host.
type Endpoint struct {
// Hostname, either as a domain name or IP address.
Host string `json:"host"`

// List of ports opened for this endpoint on the hostname.
Ports []ServicePort `json:"ports"`
}

// GetExternalEndpoints returns endpoints that are externally reachable for a service.
func GetExternalEndpoints(service *api.Service) []Endpoint {
var externalEndpoints []Endpoint
if service.Spec.Type == api.ServiceTypeLoadBalancer {
for _, ingress := range service.Status.LoadBalancer.Ingress {
externalEndpoints = append(externalEndpoints, getExternalEndpoint(ingress, service.Spec.Ports))
}
}

for _, ip := range service.Spec.ExternalIPs {
externalEndpoints = append(externalEndpoints, Endpoint{
Host: ip,
Ports: GetServicePorts(service.Spec.Ports),
})
}

return externalEndpoints
}

// GetInternalEndpoint returns internal endpoint name for the given service properties, e.g.,
// "my-service.namespace 80/TCP" or "my-service 53/TCP,53/UDP".
func GetInternalEndpoint(serviceName, namespace string, ports []api.ServicePort) Endpoint {
name := serviceName

if namespace != api.NamespaceDefault && len(namespace) > 0 && len(serviceName) > 0 {
bufferName := bytes.NewBufferString(name)
bufferName.WriteString(".")
bufferName.WriteString(namespace)
name = bufferName.String()
}

return Endpoint{
Host: name,
Ports: GetServicePorts(ports),
}
}

// Returns external endpoint name for the given service properties.
func getExternalEndpoint(ingress api.LoadBalancerIngress, ports []api.ServicePort) Endpoint {
var host string
if ingress.Hostname != "" {
host = ingress.Hostname
} else {
host = ingress.IP
}
return Endpoint{
Host: host,
Ports: GetServicePorts(ports),
}
}
38 changes: 38 additions & 0 deletions src/backend/resources/common/serviceport.go
@@ -0,0 +1,38 @@
// Copyright 2017 The Kubernetes Authors.
//
// 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 common

import api "k8s.io/api/core/v1"

// ServicePort is a pair of port and protocol, e.g. a service endpoint.
type ServicePort struct {
// Positive port number.
Port int32 `json:"port"`

// Protocol name, e.g., TCP or UDP.
Protocol api.Protocol `json:"protocol"`

// The port on each node on which service is exposed.
NodePort int32 `json:"nodePort"`
}

// GetServicePorts returns human readable name for the given service ports list.
func GetServicePorts(apiPorts []api.ServicePort) []ServicePort {
var ports []ServicePort
for _, port := range apiPorts {
ports = append(ports, ServicePort{port.Port, port.Protocol, port.NodePort})
}
return ports
}
7 changes: 7 additions & 0 deletions src/backend/resources/common/types.go
Expand Up @@ -64,3 +64,10 @@ func NewObjectMeta(k8SObjectMeta metaV1.ObjectMeta) ObjectMeta {
Annotations: k8SObjectMeta.Annotations,
}
}

// NewTypeMeta creates new type mete for the resource kind.
func NewTypeMeta(kind ResourceKind) TypeMeta {
return TypeMeta{
Kind: kind,
}
}
4 changes: 2 additions & 2 deletions src/backend/resources/service/detail.go
Expand Up @@ -41,7 +41,7 @@ type ServiceDetail struct {
EventList []common.Event `json:"eventList"`

// PodInfos represents list of pods status targeted by same label selector as this service.
PodLists []v1.Pod `json:"podInfos"`
PodList []v1.Pod `json:"podList"`

// Show the value of the SessionAffinity of the Service.
SessionAffinity v1.ServiceAffinity `json:"sessionAffinity"`
Expand Down Expand Up @@ -76,7 +76,7 @@ func toServiceDetail(service *v1.Service, events []common.Event, pods []v1.Pod,
ClusterIP: service.Spec.ClusterIP,
Type: service.Spec.Type,
EventList: events,
PodLists: pods,
PodList: pods,
SessionAffinity: service.Spec.SessionAffinity,
}
}
2 changes: 1 addition & 1 deletion src/backend/resources/service/service.go
Expand Up @@ -23,7 +23,7 @@ func CreateOrUpdateService(cli *kubernetes.Clientset, service *kapi.Service) (*k
return cli.CoreV1().Services(service.Namespace).Update(old)
}

func GetServiceDetail(cli *kubernetes.Clientset, name, namespace string) (*kapi.Service, error) {
func GetService(cli *kubernetes.Clientset, name, namespace string) (*kapi.Service, error) {
service, err := cli.CoreV1().Services(namespace).Get(name, metaV1.GetOptions{})
if err != nil {
return nil, err
Expand Down
Expand Up @@ -7,6 +7,14 @@ import (

func init() {

beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/kubernetes/service:KubeServiceController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/kubernetes/service:KubeServiceController"],
beego.ControllerComments{
Method: "GetDetail",
Router: `/:service/detail/namespaces/:namespace/clusters/:cluster`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Params: nil})

beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/kubernetes/service:KubeServiceController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/kubernetes/service:KubeServiceController"],
beego.ControllerComments{
Method: "Get",
Expand All @@ -31,4 +39,12 @@ func init() {
MethodParams: param.Make(),
Params: nil})

beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/kubernetes/service:KubeServiceController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/kubernetes/service:KubeServiceController"],
beego.ControllerComments{
Method: "List",
Router: `/namespaces/:namespace/clusters/:cluster`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Params: nil})

}

0 comments on commit 83b4319

Please sign in to comment.