提交 6b469285 编写于 作者: Z ZeHuaiWang 提交者: Kubernetes Prow Robot

Reduce code duplication and fix typo (#4424)

* Reduce code duplication and fix typo

* fix apihandler.go

* fix

* fix

* use golint

if block ends with a return statement, so drop this else and outdent its block

* use golint

apihandler.go:1168:2: var sessionId should be sessionID

* fix a space

* fix

* fix
上级 2cab1460
......@@ -141,6 +141,7 @@ const (
ResourceKindEndpoint = "endpoint"
)
// Scalable method return whether ResourceKind is scalable.
func (k ResourceKind) Scalable() bool {
scalable := []ResourceKind{
ResourceKindDeployment,
......@@ -177,6 +178,7 @@ const (
ClientTypePluginsClient = "plugin"
)
// APIMapping is the mapping from resource kind to ClientType and Namespaced.
type APIMapping struct {
// Kubernetes resource name.
Resource string
......@@ -186,7 +188,7 @@ type APIMapping struct {
Namespaced bool
}
// Mapping from resource kind to K8s apiserver API path. This is mostly pluralization, because
// KindToAPIMapping is the mapping from resource kind to K8s apiserver API path. This is mostly pluralization, because
// Kubernetes apiserver uses plural paths and this project singular.
// Must be kept in sync with the list of supported kinds.
// See: https://kubernetes.io/docs/reference/
......@@ -219,14 +221,14 @@ var KindToAPIMapping = map[string]APIMapping{
}
// IsSelectorMatching returns true when an object with the given selector targets the same
// Resources (or subset) that the tested object with the given selector.
func IsSelectorMatching(labelSelector map[string]string, testedObjectLabels map[string]string) bool {
// Resources (or subset) that the target object with the given selector.
func IsSelectorMatching(srcSelector map[string]string, targetObjectLabels map[string]string) bool {
// If service has no selectors, then assume it targets different resource.
if len(labelSelector) == 0 {
if len(srcSelector) == 0 {
return false
}
for label, value := range labelSelector {
if rsValue, ok := testedObjectLabels[label]; !ok || rsValue != value {
for label, value := range srcSelector {
if rsValue, ok := targetObjectLabels[label]; !ok || rsValue != value {
return false
}
}
......@@ -234,18 +236,14 @@ func IsSelectorMatching(labelSelector map[string]string, testedObjectLabels map[
}
// IsLabelSelectorMatching returns true when a resource with the given selector targets the same
// Resources(or subset) that a tested object selector with the given selector.
func IsLabelSelectorMatching(selector map[string]string, labelSelector *v1.LabelSelector) bool {
// If the resource has no selectors, then assume it targets different Pods.
if len(selector) == 0 {
return false
}
for label, value := range selector {
if rsValue, ok := labelSelector.MatchLabels[label]; !ok || rsValue != value {
return false
}
// Resources(or subset) that a target object selector with the given selector.
func IsLabelSelectorMatching(srcSelector map[string]string, targetLabelSelector *v1.LabelSelector) bool {
// Check to see if targetLabelSelector pointer is not nil.
if targetLabelSelector != nil {
targetObjectLabels := targetLabelSelector.MatchLabels
return IsSelectorMatching(srcSelector, targetObjectLabels)
}
return true
return false
}
// ListEverything is a list options used to list all resources without any filtering.
......
......@@ -149,7 +149,7 @@ func CreateHTTPAPIHandler(iManager integration.IntegrationManager, cManager clie
Writes(validation.ProtocolValidity{}))
apiV1Ws.Route(
apiV1Ws.GET("/appdeployment/protocols").
To(apiHandler.handleGetAvailableProcotols).
To(apiHandler.handleGetAvailableProtocols).
Writes(deployment.Protocols{}))
apiV1Ws.Route(
......@@ -939,12 +939,12 @@ func (apiHandler *APIHandler) handleGetReplicaCount(request *restful.Request, re
namespace := request.PathParameter("namespace")
kind := request.PathParameter("kind")
name := request.PathParameter("name")
scaleSpec, err := scaling.GetScaleSpec(cfg, kind, namespace, name)
replicaCounts, err := scaling.GetReplicaCounts(cfg, kind, namespace, name)
if err != nil {
errors.HandleInternalError(response, err)
return
}
response.WriteHeaderAndEntity(http.StatusOK, scaleSpec)
response.WriteHeaderAndEntity(http.StatusOK, replicaCounts)
}
func (apiHandler *APIHandler) handleDeployFromFile(request *restful.Request, response *restful.Response) {
......@@ -1024,7 +1024,7 @@ func (apiHandler *APIHandler) handleProtocolValidity(request *restful.Request, r
response.WriteHeaderAndEntity(http.StatusOK, validation.ValidateProtocol(spec))
}
func (apiHandler *APIHandler) handleGetAvailableProcotols(request *restful.Request, response *restful.Response) {
func (apiHandler *APIHandler) handleGetAvailableProtocols(request *restful.Request, response *restful.Response) {
response.WriteHeaderAndEntity(http.StatusOK, deployment.GetAvailableProtocols())
}
......@@ -1165,7 +1165,7 @@ func (apiHandler *APIHandler) handleGetPodEvents(request *restful.Request, respo
// Handles execute shell API call
func (apiHandler *APIHandler) handleExecShell(request *restful.Request, response *restful.Response) {
sessionId, err := genTerminalSessionId()
sessionID, err := genTerminalSessionId()
if err != nil {
errors.HandleInternalError(response, err)
return
......@@ -1183,13 +1183,13 @@ func (apiHandler *APIHandler) handleExecShell(request *restful.Request, response
return
}
terminalSessions.Set(sessionId, TerminalSession{
id: sessionId,
terminalSessions.Set(sessionID, TerminalSession{
id: sessionID,
bound: make(chan error),
sizeChan: make(chan remotecommand.TerminalSize),
})
go WaitForTerminal(k8sClient, cfg, request, sessionId)
response.WriteHeaderAndEntity(http.StatusOK, TerminalResponse{Id: sessionId})
go WaitForTerminal(k8sClient, cfg, request, sessionID)
response.WriteHeaderAndEntity(http.StatusOK, TerminalResponse{Id: sessionID})
}
func (apiHandler *APIHandler) handleGetDeployments(request *restful.Request, response *restful.Response) {
......
......@@ -34,8 +34,8 @@ type ReplicaCounts struct {
ActualReplicas int32 `json:"actualReplicas"`
}
// GetScaleSpec returns a populated ReplicaCounts object with desired and actual number of replicas.
func GetScaleSpec(cfg *rest.Config, kind, namespace, name string) (*ReplicaCounts, error) {
// GetReplicaCounts returns a populated ReplicaCounts object with desired and actual number of replicas.
func GetReplicaCounts(cfg *rest.Config, kind, namespace, name string) (*ReplicaCounts, error) {
sc, err := getScaleGetter(cfg)
if err != nil {
return nil, err
......@@ -116,7 +116,7 @@ func getGroupResource(kind string) schema.GroupResource {
if gr.Group != "" && gr.Resource != "" {
return gr
} else {
return apps.Resource(kind)
}
return apps.Resource(kind)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册