-
Notifications
You must be signed in to change notification settings - Fork 828
Feature: add fetch service list and get service detail from k8s cluster api to backend #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f2bce90
add support fetch service detail and list from k8s cluster
HZ89 a4f8005
bug fix
HZ89 d800b89
rename models.PerMissionTypeIngress to models.PermissionTypeIngress
HZ89 57568bf
Make the "import" order conform to the specification
HZ89 f754bc4
Simplified code
HZ89 a3dacf9
Merge branch 'master' into k8s-service-api
9224725
Merge branch 'master' into k8s-service-api
b462eee
Merge branch 'master' into k8s-service-api
ef033d2
Merge branch 'master' into k8s-service-api
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,78 @@ | ||
| // 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 ( | ||
| "fmt" | ||
|
|
||
| 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 { | ||
|
wilhelmguo marked this conversation as resolved.
|
||
| 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 { | ||
| name = fmt.Sprintf("%s.%s", name, namespace) | ||
| } | ||
|
|
||
| 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), | ||
| } | ||
| } | ||
This file contains hidden or 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,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 | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,74 @@ | ||
| package endpoint | ||
|
|
||
| import ( | ||
| "github.com/Qihoo360/wayne/src/backend/client" | ||
| "github.com/Qihoo360/wayne/src/backend/resources/common" | ||
|
|
||
|
chengyumeng marked this conversation as resolved.
|
||
| "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| type Endpoint struct { | ||
| ObjectMeta common.ObjectMeta `json:"objectMeta"` | ||
| TypeMeta common.TypeMeta `json:"typeMeta"` | ||
|
|
||
| // Hostname, either as a domain name or IP address. | ||
| Host string `json:"host"` | ||
|
|
||
| // Name of the node the endpoint is located | ||
| NodeName *string `json:"nodeName"` | ||
|
|
||
| // Status of the endpoint | ||
| Ready bool `json:"ready"` | ||
|
|
||
| // Array of endpoint ports | ||
| Ports []v1.EndpointPort `json:"ports"` | ||
| } | ||
|
|
||
| func GetServiceEndpointsFromCache(cache *client.CacheIndexer, namespace, name string) (list []Endpoint) { | ||
| return toEndpointList(GetEndpointsFromCache(cache, namespace, name)) | ||
| } | ||
|
|
||
| // GetEndpoints gets endpoints associated to resource with given name. | ||
| func GetEndpointsFromCache(cache *client.CacheIndexer, namespace, name string) (endpointsList []v1.Endpoints) { | ||
| allEndpoints := cache.Endpoints.List() | ||
|
|
||
| for _, v := range allEndpoints { | ||
| endpoints, ok := v.(*v1.Endpoints) | ||
| if !ok { | ||
| continue | ||
| } | ||
| if endpoints.Namespace != namespace { | ||
| continue | ||
| } | ||
| if endpoints.Name != name { | ||
| continue | ||
| } | ||
| endpointsList = append(endpointsList, *endpoints) | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func toEndpointList(endpoints []v1.Endpoints) (list []Endpoint) { | ||
| for _, endpoint := range endpoints { | ||
| for _, subSets := range endpoint.Subsets { | ||
| for _, address := range subSets.Addresses { | ||
| list = append(list, *toEndpoint(address, subSets.Ports, true)) | ||
| } | ||
| for _, notReadyAddress := range subSets.NotReadyAddresses { | ||
| list = append(list, *toEndpoint(notReadyAddress, subSets.Ports, false)) | ||
| } | ||
| } | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func toEndpoint(address v1.EndpointAddress, ports []v1.EndpointPort, ready bool) *Endpoint { | ||
| return &Endpoint{ | ||
| TypeMeta: common.NewTypeMeta(common.ResourceKind("endpoint")), | ||
| Host: address.IP, | ||
| Ports: ports, | ||
| Ready: ready, | ||
| NodeName: address.NodeName, | ||
| } | ||
| } | ||
This file contains hidden or 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,27 @@ | ||
| package service | ||
|
|
||
| import "github.com/Qihoo360/wayne/src/backend/resources/dataselector" | ||
|
|
||
| type ServiceCell Service | ||
|
|
||
| func (self ServiceCell) GetProperty(name dataselector.PropertyName) dataselector.ComparableValue { | ||
| switch name { | ||
| case dataselector.NameProperty: | ||
| return dataselector.StdComparableString(self.ObjectMeta.Name) | ||
| case dataselector.CreationTimestampProperty: | ||
| return dataselector.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time) | ||
| case dataselector.NamespaceProperty: | ||
| return dataselector.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 toCell(std []Service) []dataselector.DataCell { | ||
| cells := make([]dataselector.DataCell, len(std)) | ||
| for i := range std { | ||
| cells[i] = ServiceCell(std[i]) | ||
| } | ||
| return cells | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.