Skip to content

Commit

Permalink
fix:代码规范问题处理 (#2939)
Browse files Browse the repository at this point in the history
* fix:代码规范问题处理

* fix:代码规范问题处理优化
  • Loading branch information
LidolLxf authored Feb 1, 2024
1 parent 5091ad4 commit d22ae8a
Show file tree
Hide file tree
Showing 141 changed files with 755 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func HandleIpSchedulerPredicate(extenderArgs schedulerapi.ExtenderArgs) (*schedu
return scheduleResult, nil
}

// get IP Claim
func getIPClaim(namespace, claimKey string) (*BCSNetIPClaim, error) {
claimUnstruct, err := DefaultIpScheduler.ClaimLister.Namespace(namespace).Get(claimKey)
if err != nil {
Expand All @@ -246,13 +247,17 @@ func getIPClaim(namespace, claimKey string) (*BCSNetIPClaim, error) {
return claim, nil
}

// get IP
func getIP(ipName string) (*BCSNetIP, error) {
// Get retrieves a resource from the indexer with the given name
ipUnstruct, err := DefaultIpScheduler.IPLister.Get(ipName)
if err != nil {
blog.Warnf("get BCSNetIP %s failed, err %s", ipName, err.Error())
return nil, fmt.Errorf("get BCSNetIP %s failed, err %s", ipName, err.Error())
}
ip := &BCSNetIP{}
// FromUnstructured converts an object from map[string]interface{} representation into a concrete type.
// It uses encoding/json/Unmarshaler if object implements it or reflection if not.
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(
ipUnstruct.UnstructuredContent(), ip); err != nil {
blog.Warnf("failed to convert unstructured ip %s", ipName)
Expand All @@ -261,6 +266,7 @@ func getIP(ipName string) (*BCSNetIP, error) {
return ip, nil
}

// get Pool
func getPool(poolName string) (*BCSNetPool, error) {
poolUnstruct, err := DefaultIpScheduler.PoolLister.Get(poolName)
if err != nil {
Expand All @@ -276,6 +282,7 @@ func getPool(poolName string) (*BCSNetPool, error) {
return pool, nil
}

// get Pool By Hostname
func getPoolByHostname(hostName string) (*BCSNetPool, error) {
node, err := getNode(hostName)
if err != nil {
Expand Down Expand Up @@ -303,6 +310,7 @@ func getPoolByHostname(hostName string) (*BCSNetPool, error) {
return nil, fmt.Errorf("host %s is not in any net pool", hostName)
}

// get Pod
func getPod(ns, name string) (*v1.Pod, error) {
podUnstruct, err := DefaultIpScheduler.PodLister.Namespace(ns).Get(name)
if err != nil {
Expand All @@ -318,6 +326,7 @@ func getPod(ns, name string) (*v1.Pod, error) {
return pod, nil
}

// get Node
func getNode(name string) (*v1.Node, error) {
nodeUnstruct, err := DefaultIpScheduler.NodeLister.Get(name)
if err != nil {
Expand Down Expand Up @@ -388,6 +397,7 @@ func checkNodeInHosts(node v1.Node, hosts []string) error {
return fmt.Errorf("no available ip")
}

// sync Cached Pool By IP
func syncCachedPoolByIP(ip *BCSNetIP) {
poolName, ok := ip.ObjectMeta.Labels[PodLabelKeyForPool]
if !ok {
Expand All @@ -397,6 +407,7 @@ func syncCachedPoolByIP(ip *BCSNetIP) {
syncCachedPoolIPNum(poolName)
}

// sync Cached Pool IP Num
func syncCachedPoolIPNum(poolName string) {
if DefaultIpScheduler == nil {
blog.Warnf("default scheduler is nil, wait for creation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func InitRouters(ws *restful.WebService, httpServerClient *HttpServerClient) {
func (c *HttpServerClient) AllocateIP(request *restful.Request, response *restful.Response) {
requestID := request.Request.Header.Get("X-Request-Id")
netIPReq := &NetIPAllocateRequest{}
// ReadEntity checks the Accept header and reads the content into the entityPointer.
if err := request.ReadEntity(netIPReq); err != nil {
blog.Errorf("decode json request failed, %s", err.Error())
response.WriteErrorString(http.StatusBadRequest, err.Error())
Expand Down Expand Up @@ -223,6 +224,7 @@ func (c *HttpServerClient) AllocateIP(request *restful.Request, response *restfu
response.WriteEntity(responseData(0, message, true, requestID, data))
}

// allocateNewIPForClaim xxx
func (c *HttpServerClient) allocateNewIPForClaim(
netIPReq *NetIPAllocateRequest, availableIP []*v1.BCSNetIP, ipClaim *v1.BCSNetIPClaim) (
*v1.BCSNetIP, *v1.BCSNetPool, error) {
Expand Down Expand Up @@ -256,6 +258,7 @@ func (c *HttpServerClient) allocateNewIPForClaim(
return targetIP, bcspool, nil
}

// allocateIPByClaim xxx
func (c *HttpServerClient) allocateIPByClaim(
netIPReq *NetIPAllocateRequest, ipClaim *v1.BCSNetIPClaim) (*v1.BCSNetIP, *v1.BCSNetPool, error) {
ipName := ipClaim.Status.BoundedIP
Expand Down Expand Up @@ -284,6 +287,7 @@ func (c *HttpServerClient) allocateIPByClaim(
return bcsNetIP, bcsNetPool, nil
}

// update IP Status
func (c *HttpServerClient) updateIPStatus(ip *v1.BCSNetIP, netIPReq *NetIPAllocateRequest, claimKey, duration string,
fixed bool) error {
ip.Status = v1.BCSNetIPStatus{
Expand All @@ -305,6 +309,7 @@ func (c *HttpServerClient) updateIPStatus(ip *v1.BCSNetIP, netIPReq *NetIPAlloca
return nil
}

// get Available IPs
func (c *HttpServerClient) getAvailableIPs(netPoolList *v1.BCSNetPoolList, netIPReq *NetIPAllocateRequest) (
[]*v1.BCSNetIP, error) {
var availableIP []*v1.BCSNetIP
Expand Down Expand Up @@ -333,6 +338,7 @@ func (c *HttpServerClient) getAvailableIPs(netPoolList *v1.BCSNetPoolList, netIP
return availableIP, nil
}

// get Pool By IP
func (c *HttpServerClient) getPoolByIP(bcsip *v1.BCSNetIP) (*v1.BCSNetPool, error) {
if len(bcsip.GetLabels()) == 0 {
return nil, fmt.Errorf("BCSNetIP %s has no labels", bcsip.GetName())
Expand All @@ -349,6 +355,7 @@ func (c *HttpServerClient) getPoolByIP(bcsip *v1.BCSNetIP) (*v1.BCSNetPool, erro
return netPool, nil
}

// get Claim
func (c *HttpServerClient) getClaim(ns, name string) (*v1.BCSNetIPClaim, error) {
retClaim := &v1.BCSNetIPClaim{}
if err := c.K8SClient.Get(context.Background(), types.NamespacedName{
Expand All @@ -361,6 +368,7 @@ func (c *HttpServerClient) getClaim(ns, name string) (*v1.BCSNetIPClaim, error)
return retClaim, nil
}

// bound Claim IP
func (c *HttpServerClient) boundClaimIP(claim *v1.BCSNetIPClaim, netIP *v1.BCSNetIP) error {
claim.Status.BoundedIP = netIP.Name
claim.Status.Phase = constant.BCSNetIPClaimBoundedStatus
Expand Down Expand Up @@ -409,6 +417,7 @@ func (c *HttpServerClient) DeleteIP(request *restful.Request, response *restful.
claimKey := netIP.Status.IPClaimKey
if len(claimKey) != 0 {
claim := &v1.BCSNetIPClaim{}
// ParseNamespacedNameKey return key by namespace and name
podNamespace, podName, err := utils.ParseNamespacedNameKey(claimKey)
if err != nil {
message := fmt.Sprintf("invalid IPClaimKey %s of BCSNetIP %s instance", claimKey, netIP.GetName())
Expand Down Expand Up @@ -446,6 +455,7 @@ func (c *HttpServerClient) DeleteIP(request *restful.Request, response *restful.
response.WriteEntity(responseData(0, message, true, requestID, netIPReq))
}

// get IP And Pool
func (c *HttpServerClient) getIPAndPool(ip string) (*v1.BCSNetIP, *v1.BCSNetPool, error) {
bcsNetIP := &v1.BCSNetIP{}
if err := c.K8SClient.Get(context.Background(), types.NamespacedName{Name: ip}, bcsNetIP); err != nil {
Expand All @@ -458,6 +468,7 @@ func (c *HttpServerClient) getIPAndPool(ip string) (*v1.BCSNetIP, *v1.BCSNetPool
return bcsNetIP, bcsNetPool, nil
}

// get IP Claim And Duration
func (c *HttpServerClient) getIPClaimAndDuration(namespace, name string) (string, string, error) {
pod := &coreV1.Pod{}
err := c.K8SClient.Get(context.Background(), types.NamespacedName{Name: name, Namespace: namespace}, pod)
Expand All @@ -477,6 +488,7 @@ func (c *HttpServerClient) getIPClaimAndDuration(namespace, name string) (string
return claimValue, claim.Spec.ExpiredDuration, nil
}

// validate Allocate Net IP Req
func validateAllocateNetIPReq(netIPReq *NetIPAllocateRequest) error {
var message string
if netIPReq == nil {
Expand All @@ -497,6 +509,7 @@ func validateAllocateNetIPReq(netIPReq *NetIPAllocateRequest) error {
return nil
}

// validate Delete Net IP Req
func validateDeleteNetIPReq(netIPReq *NetIPDeleteRequest) error {
var message string
if netIPReq == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func NewSdkWrapper() (*SdkWrapper, error) {
return nil, err
}

// NewTokenBucket create new token bucket ratelimiter
sw.throttler = throttle.NewTokenBucket(sw.ratelimitqps, sw.ratelimitbucketSize)
return sw, nil
}
Expand All @@ -100,6 +101,7 @@ func NewSdkWrapper() (*SdkWrapper, error) {
func NewSdkWrapperWithSecretIDKey(credentials []byte) (*SdkWrapper, error) {
sw := &SdkWrapper{}
sw.credentials = credentials
// NewSdkWrapper create a new gcp sdk wrapper
return NewSdkWrapper()
}

Expand Down Expand Up @@ -211,6 +213,8 @@ func (sw *SdkWrapper) CreateNetworkEndpointGroups(project, zone, name, network,
"CreateNetworkEndpointGroups", ret, startTime)
}
sw.tryThrottle()
// Insert: Creates a network endpoint group in the specified project
// using the parameters that are included in the request.
op, err := sw.computeService.NetworkEndpointGroups.Insert(project, zone, &compute.NetworkEndpointGroup{
Name: name,
NetworkEndpointType: "GCE_VM_IP_PORT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func ReportPortAllocate(name, namespace string, allocateSuccess bool) {
}
}

// CleanPortAllocateMetric clean allocate metric
func CleanPortAllocateMetric(name, namespace string) {
portAllocateFailedGauge.Delete(prometheus.Labels{"name": name, "namespace": namespace})
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (n *NodeCache) loadNodeInfoFromConfigmap() {
node := &corev1.Node{}
if err := n.k8sClient.Get(context.Background(), k8stypes.NamespacedName{Name: nodeName},
node); err != nil {
blog.Errorf("get node '%s' failed, err: %s", err.Error())
blog.Errorf("get node failed, err: %s", err.Error())
continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func (s *Server) mutatingNodeWebhook(ar v1.AdmissionReview) (response *v1.Admiss
}
node := &k8scorev1.Node{}
if err := json.Unmarshal(req.Object.Raw, node); err != nil {
blog.Errorf("decode %s to node failed, err %s", string(req.Object.Raw), err.Error)
blog.Errorf("decode %s to node failed, err %s", string(req.Object.Raw), err.Error())
return &v1.AdmissionResponse{Allowed: true}
}
if len(node.Namespace) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (pbr *PortBindingReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
// if entity not found and portbinding found, do clean portbinding
blog.V(3).Infof("clean portbinding %v", req.NamespacedName)
return pbr.cleanPortBinding(portBinding)
} else {
} else { // nolint
// if both entity and portbinding are not found, just return
return ctrl.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (n *NodeExporter) retrieveExternalIP() (string, error) {
func (n *NodeExporter) isRealExternalIP(externalIP, uuid string) bool {
// do connect check
addr := net.JoinHostPort(externalIP, strconv.Itoa(int(n.Opts.ListenPort)))
// nolint
// todo use bk internal service
resp, err := n.HttpClient.Get("http://" + addr + "/node-external-worker/api/v1/health_check")
if err != nil {
Expand All @@ -114,7 +115,7 @@ func (n *NodeExporter) isRealExternalIP(externalIP, uuid string) bool {

respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
blog.Errorf("read body failed, err: %s")
blog.Errorf("read body failed, err: %s", err)
return false
}
blog.Infof("resp: %s, respCode: %d", string(respBody), resp.StatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
*/

// Package httpsvr xxx
package httpsvr

import (
Expand All @@ -29,6 +30,7 @@ type HttpServerClient struct {
UUID string
}

// Init xxx
func (server *HttpServerClient) Init() error {
s := httpserver.NewHttpServer(server.Ops.ListenPort, "0.0.0.0", "")

Expand All @@ -55,6 +57,7 @@ func (server *HttpServerClient) healthCheck(request *restful.Request, response *
_, _ = response.Write(resp)
}

func (server *HttpServerClient) SetUUID(UUID string) {
server.UUID = UUID
// SetUUID set uuid
func (server *HttpServerClient) SetUUID(uUID string) {
server.UUID = uUID
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
*/

// Package options xxx
package options

import (
Expand All @@ -26,6 +27,7 @@ const (
EnvNamePodNamespace = "POD_NAMESPACE"
)

// Options xxx
type Options struct {
// ExternalIPWebURL URL to get external IP
ExternalIPWebURL string
Expand All @@ -50,6 +52,7 @@ type Options struct {
conf.LogConfig
}

// BindFromCommandLine xxx
func (op *Options) BindFromCommandLine() {
var verbosity int

Expand All @@ -75,6 +78,7 @@ func (op *Options) BindFromCommandLine() {

}

// SetFromEnv set form env
func (op *Options) SetFromEnv() {
nodeName := os.Getenv(EnvNameNodeName)
if len(nodeName) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
*/

// Package options xxx
package options

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
*/

// Package webhook xxx
package webhook

import (
Expand Down Expand Up @@ -110,7 +111,7 @@ func (s *AdmissionWebhookServer) registerRouter() {
func (s *AdmissionWebhookServer) check(ctx *gin.Context) {
ar := new(v1.AdmissionReview)
if err := ctx.BindJSON(ar); err != nil {
blog.Errorf("marshal request body failed, err: ", err.Error())
blog.Errorf("marshal request body failed, err: %s", err.Error())
s.webhookAllow(ctx, true, "", "")
return
}
Expand Down
1 change: 1 addition & 0 deletions bcs-scenarios/bcs-gitops-manager/handler/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* limitations under the License.
*/

// Package handler xxx
package handler

import (
Expand Down
4 changes: 2 additions & 2 deletions bcs-scenarios/bcs-gitops-manager/handler/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func (e *BcsGitopsHandler) StartupProject(ctx context.Context, req *pb.ProjectSy
destPro.ObjectMeta.Annotations[common.SecretKey] = secretAnnotation
}

if err := e.option.Storage.CreateProject(ctx, destPro); err != nil {
if err = e.option.Storage.CreateProject(ctx, destPro); err != nil {
return e.startProjectResult(resp, failedCode, "",
errors.Wrapf(err, "create project '%s' to storage failed", project.ProjectCode))
}
if err := e.option.ClusterControl.SyncProject(ctx, project.ProjectCode); err != nil {
if err = e.option.ClusterControl.SyncProject(ctx, project.ProjectCode); err != nil {
return e.startProjectResult(resp, failedCode, "",
errors.Wrapf(err, "sync project '%s' clusters failed", project.ProjectCode))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

var auditClient *audit.Client

// Option gateway and token
type Option struct {
Gateway string
Token string
Expand Down
1 change: 1 addition & 0 deletions bcs-scenarios/bcs-gitops-manager/internal/dao/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const (
tableHistoryManifest = "bcs_gitops_app_history_manifest"
)

// Interface xxx interface
type Interface interface {
Init() error

Expand Down
3 changes: 2 additions & 1 deletion bcs-scenarios/bcs-gitops-manager/internal/dao/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"fmt"
"time"

_ "github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql" // nolint
"github.com/pkg/errors"
"gorm.io/driver/mysql"
"gorm.io/gorm"
Expand All @@ -36,6 +36,7 @@ var (
globalDB *driver
)

// GlobalDB global db
func GlobalDB() Interface {
return globalDB
}
Expand Down
Loading

0 comments on commit d22ae8a

Please sign in to comment.