Skip to content

Commit

Permalink
chore: format code about exported function should have comment
Browse files Browse the repository at this point in the history
Signed-off-by: chaosi-zju <[email protected]>
  • Loading branch information
chaosi-zju committed Jan 26, 2025
1 parent a7c02b5 commit 621715d
Show file tree
Hide file tree
Showing 34 changed files with 167 additions and 17 deletions.
1 change: 1 addition & 0 deletions cmd/api/app/router/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/karmada-io/dashboard/pkg/client"
)

// EnsureMemberClusterMiddleware ensures that the member cluster exists.
func EnsureMemberClusterMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
karmadaClient := client.InClusterKarmadaClient()
Expand Down
3 changes: 3 additions & 0 deletions cmd/api/app/router/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ func init() {
})
}

// V1 returns the router group for /api/v1 which for resources in control plane endpoints.
func V1() *gin.RouterGroup {
return v1
}

// Router returns the main Gin engine instance.
func Router() *gin.Engine {
return router
}

// MemberV1 returns the router group for /api/v1/member/:clustername which for resources in specific member cluster.
func MemberV1() *gin.RouterGroup {
return member
}
18 changes: 10 additions & 8 deletions cmd/api/app/routes/cluster/accesscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ const (
// KarmadaAgentServiceAccountName is the name of karmada-agent serviceaccount
KarmadaAgentServiceAccountName = "karmada-agent-sa"
// KarmadaAgentName is the name of karmada-agent
KarmadaAgentName = "karmada-agent"
KarmadaAgentName = "karmada-agent"
// KarmadaAgentImage is the image of karmada-agent
KarmadaAgentImage = "karmada/karmada-agent:latest"
ClusterNamespace = "karmada-cluster"
// ClusterNamespace is the namespace of cluster
ClusterNamespace = "karmada-cluster"
)

var (
Expand All @@ -53,7 +55,7 @@ var (
timeout = 5 * time.Minute
)

type PullModeOption struct {
type pullModeOption struct {
karmadaClient karmadaclientset.Interface
karmadaAgentCfg *clientcmdapi.Config
memberClusterNamespace string
Expand All @@ -63,7 +65,7 @@ type PullModeOption struct {
}

// createSecretAndRBACInMemberCluster create required secrets and rbac in member cluster
func (o PullModeOption) createSecretAndRBACInMemberCluster() error {
func (o pullModeOption) createSecretAndRBACInMemberCluster() error {
configBytes, err := clientcmd.Write(*o.karmadaAgentCfg)
if err != nil {
return fmt.Errorf("failure while serializing karmada-agent kubeConfig. %w", err)
Expand Down Expand Up @@ -149,7 +151,7 @@ func (o PullModeOption) createSecretAndRBACInMemberCluster() error {
}

// makeKarmadaAgentDeployment generate karmada-agent Deployment
func (o PullModeOption) makeKarmadaAgentDeployment() *appsv1.Deployment {
func (o pullModeOption) makeKarmadaAgentDeployment() *appsv1.Deployment {
karmadaAgent := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Expand Down Expand Up @@ -237,7 +239,7 @@ func (o PullModeOption) makeKarmadaAgentDeployment() *appsv1.Deployment {
return karmadaAgent
}

func AccessClusterInPullMode(opts *PullModeOption) error {
func accessClusterInPullMode(opts *pullModeOption) error {
_, exist, err := karmadautil.GetClusterWithKarmadaClient(opts.karmadaClient, opts.memberClusterName)
if err != nil {
return err
Expand Down Expand Up @@ -269,14 +271,14 @@ func AccessClusterInPullMode(opts *PullModeOption) error {
return nil
}

type PushModeOption struct {
type pushModeOption struct {
karmadaClient karmadaclientset.Interface
clusterName string
karmadaRestConfig *rest.Config
memberClusterRestConfig *rest.Config
}

func AccessClusterInPushMode(opts *PushModeOption) error {
func accessClusterInPushMode(opts *pushModeOption) error {
registerOption := karmadautil.ClusterRegisterOption{
ClusterNamespace: ClusterNamespace,
ClusterName: opts.clusterName,
Expand Down
16 changes: 8 additions & 8 deletions cmd/api/app/routes/cluster/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ func handlePostCluster(c *gin.Context) {
common.Fail(c, err)
return
}
opts := &PullModeOption{
opts := &pullModeOption{
karmadaClient: karmadaClient,
karmadaAgentCfg: apiConfig,
memberClusterNamespace: clusterRequest.MemberClusterNamespace,
memberClusterClient: memberClusterClient,
memberClusterName: clusterRequest.MemberClusterName,
memberClusterEndpoint: clusterRequest.MemberClusterEndpoint,
}
if err = AccessClusterInPullMode(opts); err != nil {
klog.ErrorS(err, "AccessClusterInPullMode failed")
if err = accessClusterInPullMode(opts); err != nil {
klog.ErrorS(err, "accessClusterInPullMode failed")
common.Fail(c, err)
} else {
klog.Infof("AccessClusterInPullMode success")
klog.Infof("accessClusterInPullMode success")
common.Success(c, "ok")
}
} else if clusterRequest.SyncMode == v1alpha1.Push {
Expand All @@ -117,18 +117,18 @@ func handlePostCluster(c *gin.Context) {
common.Fail(c, err)
return
}
opts := &PushModeOption{
opts := &pushModeOption{
karmadaClient: karmadaClient,
clusterName: clusterRequest.MemberClusterName,
karmadaRestConfig: restConfig,
memberClusterRestConfig: memberClusterRestConfig,
}
if err := AccessClusterInPushMode(opts); err != nil {
klog.ErrorS(err, "AccessClusterInPushMode failed")
if err := accessClusterInPushMode(opts); err != nil {
klog.ErrorS(err, "accessClusterInPushMode failed")
common.Fail(c, err)
return
}
klog.Infof("AccessClusterInPushMode success")
klog.Infof("accessClusterInPushMode success")
common.Success(c, "ok")
} else {
klog.Errorf("Unknown sync mode %s", clusterRequest.SyncMode)
Expand Down
2 changes: 2 additions & 0 deletions cmd/api/app/routes/config/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import (
"github.com/karmada-io/dashboard/pkg/config"
)

// GetDashboardConfig handles the request to retrieve the dashboard configuration.
func GetDashboardConfig(c *gin.Context) {
dashboardConfig := config.GetDashboardConfig()
common.Success(c, dashboardConfig)
}

// SetDashboardConfig handles the request to update the dashboard configuration.
func SetDashboardConfig(c *gin.Context) {
setDashboardConfigRequest := new(v1.SetDashboardConfigRequest)
if err := c.ShouldBind(setDashboardConfigRequest); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/api/app/routes/overview/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
app = "karmada-controller-manager"
)

// GetControllerManagerVersionInfo returns the version info of karmada-controller-manager.
func GetControllerManagerVersionInfo() (*version.Info, error) {
kubeClient := client.InClusterClient()
restConfig, _, err := client.GetKubeConfig()
Expand Down Expand Up @@ -92,6 +93,7 @@ func GetControllerManagerVersionInfo() (*version.Info, error) {
return parseVersion, nil
}

// ParseVersion parses the version string to version.Info.
func ParseVersion(versionStr string) *version.Info {
versionInfo := &version.Info{}
leftBraceIdx := strings.IndexByte(versionStr, '{')
Expand Down Expand Up @@ -129,6 +131,7 @@ func ParseVersion(versionStr string) *version.Info {
return versionInfo
}

// GetControllerManagerInfo returns the version info of karmada-controller-manager.
func GetControllerManagerInfo() (*v1.KarmadaInfo, error) {
versionInfo, err := GetControllerManagerVersionInfo()
if err != nil {
Expand All @@ -155,6 +158,7 @@ func GetControllerManagerInfo() (*v1.KarmadaInfo, error) {
return karmadaInfo, nil
}

// GetMemberClusterInfo returns the status of member clusters.
func GetMemberClusterInfo(ds *dataselect.DataSelectQuery) (*v1.MemberClusterStatus, error) {
karmadaClient := client.InClusterKarmadaClient()
result, err := cluster.GetClusterList(karmadaClient, ds)
Expand Down Expand Up @@ -187,6 +191,7 @@ func GetMemberClusterInfo(ds *dataselect.DataSelectQuery) (*v1.MemberClusterStat
return memberClusterStatus, nil
}

// GetClusterResourceStatus returns the status of cluster resources.
func GetClusterResourceStatus() (*v1.ClusterResourceStatus, error) {
clusterResourceStatus := &v1.ClusterResourceStatus{}
ctx := context.TODO()
Expand Down
4 changes: 4 additions & 0 deletions cmd/api/app/types/api/v1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ limitations under the License.

package v1

// LoginRequest is the request for login.
type LoginRequest struct {
Token string `json:"token"`
}

// LoginResponse is the response for login.
type LoginResponse struct {
Token string `json:"token"`
}

// User is the user info.
type User struct {
Name string `json:"name,omitempty"`
Authenticated bool `json:"authenticated"`
}

// ServiceAccount is the service account info.
type ServiceAccount struct {
Name string `json:"name"`
UID string `json:"uid"`
Expand Down
12 changes: 12 additions & 0 deletions cmd/api/app/types/api/v1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
corev1 "k8s.io/api/core/v1"
)

// PostClusterRequest is the request body for creating a cluster.
type PostClusterRequest struct {
MemberClusterKubeConfig string `json:"memberClusterKubeconfig" binding:"required"`
SyncMode v1alpha1.ClusterSyncMode `json:"syncMode" binding:"required"`
Expand All @@ -32,26 +33,37 @@ type PostClusterRequest struct {
ClusterZones []string `json:"clusterZones"`
}

// PostClusterResponse is the response body for creating a cluster.
type PostClusterResponse struct {
}

// LabelRequest is the request body for labeling a cluster.
type LableRequest struct {
Key string `json:"key"`
Value string `json:"value"`
}

// TaintRequest is the request body for tainting a cluster.
type TaintRequest struct {
Effect corev1.TaintEffect `json:"effect"`
Key string `json:"key"`
Value string `json:"value"`
}

// PutClusterRequest is the request body for updating a cluster.
type PutClusterRequest struct {
Labels *[]LableRequest `json:"labels"`
Taints *[]TaintRequest `json:"taints"`
}

// PutClusterResponse is the response body for updating a cluster.
type PutClusterResponse struct{}

// DeleteClusterRequest is the request body for deleting a cluster.
type DeleteClusterRequest struct {
MemberClusterName string `uri:"name" binding:"required"`
}

// DeleteClusterResponse is the response body for deleting a cluster.
type DeleteClusterResponse struct {
}
3 changes: 3 additions & 0 deletions cmd/api/app/types/api/v1/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ limitations under the License.

package v1

// CreateDeploymentRequest defines the request structure for creating a deployment.
type CreateDeploymentRequest struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
Content string `json:"content"`
}

// CreateDeploymentResponse defines the response structure for creating a deployment.
type CreateDeploymentResponse struct{}
3 changes: 3 additions & 0 deletions cmd/api/app/types/api/v1/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ limitations under the License.

package v1

// CreateNamesapceRequest is the request body for creating a namespace.
type CreateNamesapceRequest struct {
Name string `json:"name" required:"true"`
SkipAutoPropagation bool `json:"skipAutoPropagation"`
}

// CreateNamesapceResponse is the response body for creating a namespace.
type CreateNamesapceResponse struct{}
6 changes: 6 additions & 0 deletions cmd/api/app/types/api/v1/overridepolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,36 @@ limitations under the License.

package v1

// PostOverridePolicyRequest is the request body for creating an override policy.
type PostOverridePolicyRequest struct {
OverrideData string `json:"overrideData" binding:"required"`
IsClusterScope bool `json:"isClusterScope"`
Namespace string `json:"namespace"`
}

// PostOverridePolicyResponse is the response body for creating an override policy.
type PostOverridePolicyResponse struct {
}

// PutOverridePolicyRequest is the request body for updating an override policy.
type PutOverridePolicyRequest struct {
OverrideData string `json:"overrideData" binding:"required"`
IsClusterScope bool `json:"isClusterScope"`
Namespace string `json:"namespace"`
Name string `json:"name" binding:"required"`
}

// PutOverridePolicyResponse is the response body for updating an override policy.
type PutOverridePolicyResponse struct {
}

// DeleteOverridePolicyRequest is the request body for deleting an override policy.
type DeleteOverridePolicyRequest struct {
IsClusterScope bool `json:"isClusterScope"`
Namespace string `json:"namespace"`
Name string `json:"name" binding:"required"`
}

// DeleteOverridePolicyResponse is the response body for deleting an override policy.
type DeleteOverridePolicyResponse struct {
}
11 changes: 11 additions & 0 deletions cmd/api/app/types/api/v1/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,53 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// OverviewResponse represents the response structure for the overview API.
type OverviewResponse struct {
KarmadaInfo *KarmadaInfo `json:"karmadaInfo"`
MemberClusterStatus *MemberClusterStatus `json:"memberClusterStatus"`
ClusterResourceStatus *ClusterResourceStatus `json:"clusterResourceStatus"`
}

// KarmadaInfo contains information about the Karmada system.
type KarmadaInfo struct {
Version *version.Info `json:"version"`
Status string `json:"status"`
CreateTime metav1.Time `json:"createTime"`
}

// NodeSummary provides a summary of node statistics.
type NodeSummary struct {
TotalNum int32 `json:"totalNum"`
ReadyNum int32 `json:"readyNum"`
}

// CPUSummary provides a summary of CPU resource usage.
type CPUSummary struct {
TotalCPU int64 `json:"totalCPU"`
AllocatedCPU float64 `json:"allocatedCPU"`
}

// MemorySummary provides a summary of memory resource usage.
type MemorySummary struct {
TotalMemory int64 `json:"totalMemory"` // Kib => 8 * KiB
AllocatedMemory float64 `json:"allocatedMemory"`
}

// PodSummary provides a summary of pod statistics.
type PodSummary struct {
TotalPod int64 `json:"totalPod"`
AllocatedPod int64 `json:"allocatedPod"`
}

// MemberClusterStatus represents the status of member clusters.
type MemberClusterStatus struct {
NodeSummary *NodeSummary `json:"nodeSummary"`
CPUSummary *CPUSummary `json:"cpuSummary"`
MemorySummary *MemorySummary `json:"memorySummary"`
PodSummary *PodSummary `json:"podSummary"`
}

// ClusterResourceStatus represents the status of various resources in the cluster.
type ClusterResourceStatus struct {
PropagationPolicyNum int `json:"propagationPolicyNum"`
OverridePolicyNum int `json:"overridePolicyNum"`
Expand Down
Loading

0 comments on commit 621715d

Please sign in to comment.