Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions bcs-services/bcs-platform-manager/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/component"
"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/component/cmdb"
"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/config"
cronClient "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/cron/client"
cronServer "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/cron/server"
"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/discovery"
"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/storage"
"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/utils"
Expand Down Expand Up @@ -63,23 +65,36 @@ func runCmd(cmd *cobra.Command, cmdFunc CommandFunc) {
// runAPIServer 启动api服务
func runAPIServer(ctx context.Context, g *run.Group, opt *option) error {
addrIPv6 := utils.GetIPv6AddrFromEnv()
server, err := api.NewAPIServer(ctx, config.G.Base.BindAddress, config.G.Base.HttpPort, addrIPv6)
if err != nil {
return errors.Wrap(err, "apiserver")
}

sd, err := discovery.NewServiceDiscovery(ctx, platformManager, version.BcsVersion,
config.G.Base.BindAddress, config.G.Base.HttpPort, addrIPv6)
if err != nil {
return err
}

server, err := api.NewAPIServer(ctx, config.G.Base.BindAddress, config.G.Base.HttpPort, addrIPv6, sd.TLSConfig)
if err != nil {
return errors.Wrap(err, "apiserver")
}

InitClient()

// init cron task
scheduler, err := cronClient.NewScheduler()
if err != nil {
return err
}

// start scheduler
g.Add(func() error { return scheduler.Run() }, func(error) { scheduler.Shutdown() })
// 启动 apiserver
g.Add(server.Run, func(err error) { _ = server.Close(); component.GetAuditClient().Close() })
g.Add(sd.Run, func(error) {})

// start asynq server
err = cronServer.NewAsynqServer()
if err != nil {
return err
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion bcs-services/bcs-platform-manager/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
// Used for flags.
cfgFile string
appName = "bcs-platform-manager"
platformManager = "bcsplatformmanager"
platformManager = "platformmanager"

rootCmd = &cobra.Command{
Use: appName,
Expand Down
9 changes: 7 additions & 2 deletions bcs-services/bcs-platform-manager/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.0

replace (
configcenter => github.com/Tencent/bk-cmdb v0.0.0-20250710063351-e7cb8e8963fa
github.com/Tencent/bk-bcs/bcs-common => github.com/LidolLxf/bk-bcs/bcs-common v0.0.0-20260520092228-899beff9282c
k8s.io/client-go => k8s.io/client-go v0.32.2
)

Expand All @@ -23,15 +24,18 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.2
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
github.com/hibiken/asynq v0.25.1
github.com/mitchellh/go-homedir v1.1.0
github.com/oklog/run v1.1.0
github.com/parnurzeal/gorequest v0.2.16
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.20.1
github.com/stretchr/testify v1.10.0
github.com/swaggo/http-swagger v1.3.4
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.49
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses v1.1.0
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms v1.1.49
go-micro.dev/v4 v4.10.2
go.mongodb.org/mongo-driver v1.9.1
go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo v0.33.0
Expand Down Expand Up @@ -138,11 +142,12 @@ require (
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
Expand Down
279 changes: 279 additions & 0 deletions bcs-services/bcs-platform-manager/pkg/api/cloud/vpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 cloud cloud operate
package cloud

import (
"context"

cluproto "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi/clustermanager"

"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/component/bcs/clustermanager"
"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/types"
)

// ListCloudVpcsPage 获取云VPC分页列表
// @Summary 获取云VPC分页列表
// @Tags Cloud
// @Produce json
// @Success 200 {array} types.ListCloudVpcsPageResponse
// @Router /clouds/{cloudID}/vpcs/page [get]
func ListCloudVpcsPage(
c context.Context, req *types.ListCloudVpcsPageRequest) (*types.ListCloudVpcsPageResponse, error) {
sr, err := clustermanager.ListCloudVpcsPage(c, &cluproto.ListCloudVpcsPageRequest{
CloudID: req.CloudID,
Region: req.Region,
AccountID: req.AccountID,
VpcID: req.VpcID,
ResourceGroupName: req.ResourceGroupName,
VpcName: req.VpcName,
Offset: req.Offset,
Limit: req.Limit,
})
if err != nil {
return nil, err
}
cloudVpcs := make([]types.CloudVpcs, 0)
for _, vpc := range sr.Data {
cloudVpcs = append(cloudVpcs, types.CloudVpcs{
VpcName: vpc.VpcName,
VpcID: vpc.VpcID,
Region: vpc.Region,
OverlayCidr: vpc.OverlayCidr,
AvailableOverlayIpNum: vpc.AvailableOverlayIpNum,
AvailableOverlayCidr: vpc.AvailableOverlayCidr,
TotalOverlayIpNum: vpc.TotalOverlayIpNum,
UnderlayCidr: vpc.UnderlayCidr,
AvailableUnderlayIpNum: vpc.AvailableUnderlayIpNum,
AvailableUnderlayCidr: vpc.AvailableUnderlayCidr,
TotalUnderlayIpNum: vpc.TotalUnderlayIpNum,
OverlayIpUsageRate: calculateUsageRate(vpc.AvailableOverlayIpNum, vpc.TotalOverlayIpNum),
UnderlayIpUsageRate: calculateUsageRate(vpc.AvailableUnderlayIpNum, vpc.TotalUnderlayIpNum),
CreateTime: vpc.CreateTime,
OverlayIPCidr: convertOverlayIPCidr(vpc.OverlayIPCidr),
})
}
return &types.ListCloudVpcsPageResponse{
Total: sr.Total,
CloudVpcs: cloudVpcs,
}, nil
}

// calculateUsageRate calculate usage rate
func calculateUsageRate(available, total uint32) float64 {
if total == 0 {
return 0
}
return 1 - (float64(available) / float64(total))
}

// convertOverlayIPCidr convert overlay ip cidr from proto to types
func convertOverlayIPCidr(proto []*cluproto.OverlayIPCidr) []types.OverlayIPCidr {
overlayIPCidr := make([]types.OverlayIPCidr, 0)
for _, ipCidr := range proto {
overlayIPCidr = append(overlayIPCidr, types.OverlayIPCidr{
Cidr: ipCidr.Cidr,
IpNum: ipCidr.IpNum,
})
}
return overlayIPCidr
}

// ListCloudVpcCluster 获取云VPC关联的集群列表
// @Summary 获取云VPC关联的集群列表
// @Tags Cloud
// @Produce json
// @Success 200 {array} types.ListCloudVpcClusterResponse
// @Router /clouds/{cloudID}/vpc/{vpcID}/cluster [get]
func ListCloudVpcCluster(
c context.Context, req *types.ListCloudVpcClusterRequest) (*types.ListCloudVpcClusterResponse, error) {
sr, err := clustermanager.ListCloudVpcCluster(c, &cluproto.ListCloudVpcClusterRequest{
CloudID: req.CloudID,
Region: req.Region,
AccountID: req.AccountID,
VpcID: req.VpcID,
Offset: req.Offset,
Limit: req.Limit,
})
if err != nil {
return nil, err
}
cloudCluster := make([]types.CloudCluster, 0)
for _, vpc := range sr.Data {
cloudCluster = append(cloudCluster, types.CloudCluster{
ClusterID: vpc.ClusterID,
OverlayIPCidr: convertOverlayIPCidr(vpc.OverlayIPCidr),
})
}
return &types.ListCloudVpcClusterResponse{
Total: sr.Total,
CloudCluster: cloudCluster,
}, nil
}

// UpdateCloudVpcs 更新云VPC
// @Summary 更新云VPC
// @Tags Cloud
// @Produce json
// @Success 200 {array} types.UpdateCloudVpcsResponse
// @Router /clouds/{cloudID}/vpc/{vpcID} [put]
func UpdateCloudVpcs(
c context.Context, req *types.UpdateCloudVpcsRequest) (*types.UpdateCloudVpcsResponse, error) {
_, err := clustermanager.UpdateCloudVpcs(c, &cluproto.UpdateCloudVpcsRequest{
CloudID: req.CloudID,
Region: req.Region,
AccountID: req.AccountID,
VpcID: req.VpcID,
ResourceGroupName: req.ResourceGroupName,
VpcName: req.VpcName,
})
if err != nil {
return nil, err
}
return &types.UpdateCloudVpcsResponse{}, nil
}

// ListCloudSubnets 获取云子网列表
// @Summary 获取云子网列表
// @Tags Cloud
// @Produce json
// @Success 200 {array} types.ListCloudSubnetsResponse
// @Router /clouds/{cloudID}/vpc/{vpcID}/subnets [get]
func ListCloudSubnets(
c context.Context, req *types.ListCloudSubnetsRequest) (*types.ListCloudSubnetsResponse, error) {
cloudSubnets, err := clustermanager.ListCloudSubnets(c, &cluproto.ListCloudSubnetsRequest{
CloudID: req.CloudID,
Region: req.Region,
AccountID: req.AccountID,
VpcID: req.VpcID,
})
if err != nil {
return nil, err
}

cloudSubnetsData := make([]types.ListCloudSubnets, 0)
for _, vpc := range cloudSubnets {
cloudSubnetsData = append(cloudSubnetsData, types.ListCloudSubnets{
SubnetName: vpc.SubnetName,
SubnetID: vpc.SubnetID,
VpcID: vpc.VpcID,
CidrRange: vpc.CidrRange,
Ipv6CidrRange: vpc.Ipv6CidrRange,
Zone: vpc.Zone,
AvailableIPAddressCount: vpc.AvailableIPAddressCount,
ZoneName: vpc.ZoneName,
Cluster: convertCluster(vpc.Cluster),
HwNeutronSubnetID: vpc.HwNeutronSubnetID,
TotalIpAddressCount: vpc.TotalIpAddressCount,
})
}

return &types.ListCloudSubnetsResponse{
Total: uint32(len(cloudSubnetsData)),
Subnets: cloudSubnetsData,
}, nil
}

// CreateCloudSubnets 创建云子网
// @Summary 创建云子网
// @Tags Cloud
// @Produce json
// @Success 200 {array} types.CreateCloudSubnetsResponse
// @Router /clouds/{cloudID}/vpc/{vpcID}/subnets [post]
func CreateCloudSubnets(
c context.Context, req *types.CreateCloudSubnetsRequest) (*types.CreateCloudSubnetsResponse, error) {
cloudSubnets, err := clustermanager.CreateCloudSubnets(c, &cluproto.CreateCloudSubnetsRequest{
CloudID: req.CloudID,
Region: req.Region,
AccountID: req.AccountID,
VpcID: req.VpcID,
SubnetName: req.SubnetName,
CidrBlock: req.CidrBlock,
Zone: req.Zone,
})
if err != nil {
return nil, err
}

return &types.CreateCloudSubnetsResponse{
Subnet: types.CloudSubnets{
SubnetName: cloudSubnets.Data.SubnetName,
SubnetID: cloudSubnets.Data.SubnetID,
VpcID: cloudSubnets.Data.VpcID,
CidrRange: cloudSubnets.Data.CidrRange,
Ipv6CidrRange: cloudSubnets.Data.Ipv6CidrRange,
Zone: cloudSubnets.Data.Zone,
AvailableIPAddressCount: cloudSubnets.Data.AvailableIPAddressCount,
ZoneName: cloudSubnets.Data.ZoneName,
Cluster: convertCluster(cloudSubnets.Data.Cluster),
HwNeutronSubnetID: cloudSubnets.Data.HwNeutronSubnetID,
TotalIpAddressCount: cloudSubnets.Data.TotalIpAddressCount,
},
}, nil
}

func convertCluster(proto *cluproto.ClusterInfo) types.ClusterInfo {
if proto != nil {
return types.ClusterInfo{
ClusterName: proto.ClusterName,
ClusterID: proto.ClusterID,
}
}
return types.ClusterInfo{
ClusterName: "",
ClusterID: "",
}
}

// UpdateCloudSubnets 更新云子网
// @Summary 更新云子网
// @Tags Cloud
// @Produce json
// @Success 200 {array} types.UpdateCloudSubnetsResponse
// @Router /clouds/{cloudID}/vpc/{vpcID}/subnets [put]
func UpdateCloudSubnets(
c context.Context, req *types.UpdateCloudSubnetsRequest) (*types.UpdateCloudSubnetsResponse, error) {
_, err := clustermanager.UpdateCloudSubnets(c, &cluproto.UpdateCloudSubnetsRequest{
CloudID: req.CloudID,
Region: req.Region,
AccountID: req.AccountID,
SubnetName: req.SubnetName,
SubnetID: req.SubnetID,
})
if err != nil {
return nil, err
}

return &types.UpdateCloudSubnetsResponse{}, nil
}

// DeleteCloudSubnets 删除云子网
// @Summary 删除云子网
// @Tags Cloud
// @Produce json
// @Success 200 {array} types.DeleteCloudSubnetsResponse
// @Router /clouds/{cloudID}/vpc/{vpcID}/subnets [delete]
func DeleteCloudSubnets(
c context.Context, req *types.DeleteCloudSubnetsRequest) (*types.DeleteCloudSubnetsResponse, error) {
_, err := clustermanager.DeleteCloudSubnets(c, &cluproto.DeleteCloudSubnetsRequest{
CloudID: req.CloudID,
Region: req.Region,
AccountID: req.AccountID,
SubnetID: req.SubnetID,
})
if err != nil {
return nil, err
}

return &types.DeleteCloudSubnetsResponse{}, nil
}
Loading
Loading