robocanic commented on code in PR #147: URL: https://github.com/apache/dubbo-kubernetes/pull/147#discussion_r1463336879
########## pkg/admin/handlers/v2/resource_handler.go: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 handlersV2 + +import ( + "net/http" + "sort" + + "github.com/apache/dubbo-kubernetes/pkg/admin/errors" + "github.com/apache/dubbo-kubernetes/pkg/admin/model/req" + "github.com/apache/dubbo-kubernetes/pkg/admin/model/resp" + servicesV2 "github.com/apache/dubbo-kubernetes/pkg/admin/services/v2" + "github.com/gin-gonic/gin" + "github.com/vcraescu/go-paginator" + "github.com/vcraescu/go-paginator/adapter" +) + +var resourceService = servicesV2.NewResourceService() + +func SearchApplication(c *gin.Context) { + // request params validation and binding + namespace := c.DefaultQuery(req.Namespace, "") Review Comment: The concept of namesapce is removed in the part of Console. Because the usage of "namespace" can be valid among different users. So the selectors of cache should span multi namespaces. ########## pkg/admin/handlers/v2/resource_handler.go: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 handlersV2 + +import ( + "net/http" + "sort" + + "github.com/apache/dubbo-kubernetes/pkg/admin/errors" + "github.com/apache/dubbo-kubernetes/pkg/admin/model/req" + "github.com/apache/dubbo-kubernetes/pkg/admin/model/resp" + servicesV2 "github.com/apache/dubbo-kubernetes/pkg/admin/services/v2" + "github.com/gin-gonic/gin" + "github.com/vcraescu/go-paginator" + "github.com/vcraescu/go-paginator/adapter" +) + +var resourceService = servicesV2.NewResourceService() + +func SearchApplication(c *gin.Context) { + // request params validation and binding + namespace := c.DefaultQuery(req.Namespace, "") + keywords := c.DefaultQuery(req.Keywords, "") + pageQuery := &req.PageQuery{} + if err := c.ShouldBindQuery(pageQuery); err != nil { + _ = c.Error(errors.NewBizError(resp.InvalidParamCode, err)) + return + } + sortQuery := &req.SortQuery{} + if err := c.ShouldBindQuery(sortQuery); err != nil { + _ = c.Error(errors.NewBizError(resp.InvalidParamCode, err)) + return + } + + // invoke service + appOverviews, err := resourceService.SearchApplications(namespace, keywords) + if err != nil { + _ = c.Error(err) + return + } + + // sort Review Comment: Maybe sort and paging can be extracted in a common module which can be better used repeatedly? ########## pkg/admin/cache/registry/kube/cache.go: ########## @@ -94,6 +94,31 @@ func (c *KubernetesCache) GetApplications(namespace string) ([]*cache.Applicatio return res, nil } +func (c *KubernetesCache) GetApplicationsWithSelector(namespace string, selector selector.Selector) ([]*cache.ApplicationModel, error) { + c.lock.RLock() + defer c.lock.RUnlock() + + applicationSet := make(map[string]struct{}) + deployments, err := c.getCacheLister(namespace).deploymentLister.List(selector.AsLabelsSelector()) + if err != nil { + return nil, err + } + + res := make([]*cache.ApplicationModel, 0) + for _, deployment := range deployments { + if _, ok := deployment.Labels[constant.ApplicationLabel]; ok { + if _, exist := applicationSet[constant.ApplicationLabel]; !exist { + applicationSet[deployment.Labels[constant.ApplicationLabel]] = struct{}{} // mark as exist + res = append(res, &cache.ApplicationModel{ Review Comment: The appName is not the same as the deploymentName. Here should be deployment.Labels[constant.ApplicationLabel] ########## pkg/admin/model/resp/resp.go: ########## @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 resp + +import ( + "github.com/apache/dubbo-kubernetes/pkg/admin/model/req" + "github.com/vcraescu/go-paginator" +) + +type BaseResp struct { + Code Code `json:"code"` + Message string `json:"message"` + Data interface{} `json:"data"` +} + +type SuccessResp struct { + BaseResp +} + +type ErrorResp struct { + BaseResp +} + +type Code int + +// TODO: refactor this +const ( Review Comment: For now, I think the error code can be divided into three level: normal, warn, critical. For example: 200 ~ 210 can be classified to normal, 211 ~ 220 can be classified to warn, 221~230 can be classified to critical, so that the errors can be shown in the front by different colors to tell user the result of important operations. ########## pkg/admin/handlers/v2/resource_handler.go: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 handlersV2 + +import ( + "net/http" + "sort" + + "github.com/apache/dubbo-kubernetes/pkg/admin/errors" + "github.com/apache/dubbo-kubernetes/pkg/admin/model/req" + "github.com/apache/dubbo-kubernetes/pkg/admin/model/resp" + servicesV2 "github.com/apache/dubbo-kubernetes/pkg/admin/services/v2" + "github.com/gin-gonic/gin" + "github.com/vcraescu/go-paginator" + "github.com/vcraescu/go-paginator/adapter" +) + +var resourceService = servicesV2.NewResourceService() + +func SearchApplication(c *gin.Context) { Review Comment: May be the name of this file can be more specified? (Like application_service, instance_service etc.) Because there will be more resources , too many functions in a file will be in a mess to maintain. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
