Skip to content

Commit 899c4f6

Browse files
committed
introduce node resource estimation as the default estimator plugin
Signed-off-by: zhzhuang-zju <[email protected]>
1 parent b574778 commit 899c4f6

File tree

5 files changed

+329
-31
lines changed

5 files changed

+329
-31
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
title: Karmada Code Style Guide
3+
---
4+
5+
Karmada is primarily developed in Go and follows the standard Go community coding conventions. Automated style checks are
6+
enforced via [golangci-lint](https://github.com/karmada-io/karmada/blob/e2c4b596a5da442fc0dbeab9f9063d8db8669208/.github/workflows/ci.yml#L18-L35).
7+
However, the resulting code can still look and feel very differently among different developers. To ensure consistency
8+
across the codebase, this guide defines additional rules beyond linter defaults.
9+
10+
Adhering to this guide helps new contributors onboard quickly, reduces PR review iterations, shortens maintainer review
11+
cycles, and ultimately speeds up merge velocity.
12+
13+
## Code Documentation and Commenting
14+
15+
- All exported functions, methods, structs, and interfaces **must** be documented with clear and concise comments describing their purpose and behavior.
16+
17+
WRONG
18+
```go
19+
func GetAnnotationValue(annotations map[string]string, annotationKey string) string {
20+
if annotations == nil {
21+
return ""
22+
}
23+
return annotations[annotationKey]
24+
}
25+
```
26+
27+
RIGHT
28+
```go
29+
// GetAnnotationValue retrieves the value via 'annotationKey' (if it exists), otherwise an empty string is returned.
30+
func GetAnnotationValue(annotations map[string]string, annotationKey string) string {
31+
if annotations == nil {
32+
return ""
33+
}
34+
return annotations[annotationKey]
35+
}
36+
```
37+
38+
- Comments in the body of the code are highly encouraged, but they should explain the intention of the code as opposed to just calling out the obvious.
39+
40+
WRONG
41+
```go
42+
// continue if the cluster is deleting
43+
if !c.Cluster().DeletionTimestamp.IsZero() {
44+
klog.V(4).Infof("Cluster %q is deleting, skip it", c.Cluster().Name)
45+
continue
46+
```
47+
48+
RIGHT
49+
```go
50+
// When cluster is deleting, we will clean up the scheduled results in the cluster.
51+
// So we should not schedule resource to the deleting cluster.
52+
if !c.Cluster().DeletionTimestamp.IsZero() {
53+
klog.V(4).Infof("Cluster %q is deleting, skip it", c.Cluster().Name)
54+
continue
55+
```
56+
57+
## Interface Compliance
58+
59+
- Any struct that explicitly implements an interface must include a compile-time interface compliance check using the following pattern:
60+
```go
61+
var _ InterfaceName = &StructName{}
62+
```
63+
This assertion should be placed in the same file as the struct definition (typically near the type declaration or at the top of the file) to ensure:
64+
1. Compilation fails immediately if the struct does not fully implement the interface;
65+
2. The interface contract is explicitly declared, improving readability and maintainability.
66+
67+
RIGHT
68+
```go
69+
// Check if our workloadInterpreter implements necessary interface
70+
var _ interpreter.Handler = &workloadInterpreter{}
71+
72+
// workloadInterpreter explore resource with request operation.
73+
type workloadInterpreter struct {
74+
decoder *interpreter.Decoder
75+
}
76+
```
77+
78+
## Function Definitions
79+
- When a function has many parameters, consider encapsulating them into a struct to improve readability and maintainability.
80+
- Function signatures should preferably be written on a single line, including the parameter list and return types.
81+
82+
WRONG
83+
```go
84+
func Foo(
85+
bar string,
86+
baz int) error
87+
```
88+
89+
RIGHT
90+
```go
91+
func Foo(bar string, baz int) error
92+
```
93+
94+
- If a function accepts a `context.Context` parameter, it must be the first argument.
95+
96+
## Secure Coding Specifications
97+
98+
- It is prohibited to have authentication credentials that cannot be modified (e.g., hard-coded passwords in process binaries).
99+
- If implemented using interpreted languages (such as Shell/Python/Perl scripts, JSP, HTML, etc.), for functions that do not meet the requirement of undisclosed interfaces and need to be cleaned up, they must be completely deleted. It is strictly prohibited to use forms such as comment lines to merely disable the functions.
100+
- It is prohibited to use private cryptographic algorithms for encryption and decryption, including:
101+
- Cryptographic algorithms designed independently without being evaluated by professional institutions;
102+
- Self-defined data conversion algorithms executed through methods such as deformation/character shifting/replacement;
103+
- Pseudo-encryption implementations that use encoding methods (such as Base64 encoding) to achieve the purpose of data encryption.
104+
Note: In scenarios other than encryption and decryption, the use of encoding methods such as Base64 or algorithms such as deformation/shifting/replacement for legitimate business purposes does not violate this provision.
105+
- The random numbers used in cryptographic algorithms must be secure random numbers in the cryptographic sense.
106+
- It is prohibited to print authentication credentials (passwords/private keys/pre-shared keys) in plain text in system-stored logs, debugging information, and error prompts.
107+
108+
## Directory-Specific Style Guides
109+
110+
In addition to the general rules above, certain directories have specific style requirements due to their unique responsibilities. This section documents those directory-specific conventions.
111+
112+
### CHANGELOG
113+
114+
#### Location
115+
The `CHANGELOG` files are located under `docs/CHANGELOG/` in the Karmada repository.
116+
117+
#### Responsibilities
118+
Records release notes for each version, including new features, bug fixes, deprecations, and other significant changes.
119+
120+
#### Code Style
121+
- Release notes for the same version should be grouped by category (e.g., Features, Bug Fixes, Deprecations).
122+
- Each release note follows one of two formats, depending on whether it relates to a specific component:
123+
```markdown
124+
- `Component`: Description ([#pr_ID](pr_link), @author)
125+
- Description ([#pr_ID](pr_link), @author)
126+
```
127+
> Note: If the author is `dependabot`, the `, @author` part should be omitted.
128+
129+
RIGHT
130+
```markdown
131+
- `karmada-controller-manager`: Fixed the issue that a workload propagated with `duplicated mode` can bypass quota checks during scale up. ([#6474](https://github.com/karmada-io/karmada/pull/6474), @zhzhuang-zju)
132+
```
133+
134+
- Component names must be wrapped in backticks (e.g., `karmada-controller-manager`).
135+
- Within each category, release notes should be grouped by component to improve readability.
136+
137+
WRONG
138+
```markdown
139+
- `karmada-controller-manager`: Fixed the issue that reporting repeat EndpointSlice resources leads to duplicate backend IPs. ([#6693](https://github.com/karmada-io/karmada/pull/6693), @XiShanYongYe-Chang)
140+
- `karmada-interpreter-webhook-example`: Fixed write response error for broken HTTP connection issue. ([#6680](https://github.com/karmada-io/karmada/pull/6680), @tdn21)
141+
- `karmada-controller-manager`: Fixed the issue that the relevant fields in rb and pp are inconsistent. ([#6714](https://github.com/karmada-io/karmada/pull/6714), @zhzhuang-zju)
142+
```
143+
144+
RIGHT
145+
```markdown
146+
- `karmada-controller-manager`: Fixed the issue that reporting repeat EndpointSlice resources leads to duplicate backend IPs. ([#6693](https://github.com/karmada-io/karmada/pull/6693), @XiShanYongYe-Chang)
147+
- `karmada-controller-manager`: Fixed the issue that the relevant fields in rb and pp are inconsistent. ([#6714](https://github.com/karmada-io/karmada/pull/6714), @zhzhuang-zju)
148+
- `karmada-interpreter-webhook-example`: Fixed write response error for broken HTTP connection issue. ([#6680](https://github.com/karmada-io/karmada/pull/6680), @tdn21)
149+
```
150+
151+
- Tense usage
152+
- `Deprecations`: Use present perfect tense (e.g., “has been deprecated”).
153+
- All other categories (features, fixes, etc.): Use simple past tense (e.g., “Fixed…”, “Added…”, “Removed…”).
154+
- Only when describing a newly introduced capability or behavioral changes, you may use present tense constructions like `now support` or `no longer relies`.
155+
156+
WRONG
157+
```markdown
158+
- `karmada-controller-manager`: Fix the bug that xxx. ([#prID](pr_link), @author)
159+
```
160+
161+
RIGHT
162+
```markdown
163+
- `karmada-controller-manager`: Fixed the bug that xxx. ([#prID](pr_link), @author)
164+
```

docs/developers/secure-coding-specifications.md

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
title: Karmada 代码风格指南
3+
---
4+
5+
Karmada 主要使用 Go 语言开发,遵循 Go 社区通用代码风格,并通过 [golangci-lint](https://github.com/karmada-io/karmada/blob/e2c4b596a5da442fc0dbeab9f9063d8db8669208/.github/workflows/ci.yml#L18-L35)
6+
工具进行自动化检查。然而,不同开发者编写的代码的风格可能最终大相径庭。我们旨在补充若干强制性规范,以统一 Karmada 代码的风格,从而帮助提高整体可读性。
7+
8+
遵循本指南有助于新贡献者快速融入项目,减少 PR 往返,并缩短维护者的审核周期,从而加速合入流程。
9+
10+
## 代码注释
11+
12+
- 所有导出的函数、方法、结构体和接口都必须有注释,注释应简洁明了地描述其功能和用途。
13+
14+
错误示例
15+
```go
16+
func GetAnnotationValue(annotations map[string]string, annotationKey string) string {
17+
if annotations == nil {
18+
return ""
19+
}
20+
return annotations[annotationKey]
21+
}
22+
```
23+
24+
正确示例
25+
```go
26+
// GetAnnotationValue retrieves the value via 'annotationKey' (if it exists), otherwise an empty string is returned.
27+
func GetAnnotationValue(annotations map[string]string, annotationKey string) string {
28+
if annotations == nil {
29+
return ""
30+
}
31+
return annotations[annotationKey]
32+
}
33+
```
34+
35+
- 鼓励在代码内部添加注释,但应解释设计意图或复杂逻辑,而非解释显而易见的操作。
36+
37+
错误示例
38+
```go
39+
// continue if the cluster is deleting
40+
if !c.Cluster().DeletionTimestamp.IsZero() {
41+
klog.V(4).Infof("Cluster %q is deleting, skip it", c.Cluster().Name)
42+
continue
43+
```
44+
45+
正确示例
46+
```go
47+
// When cluster is deleting, we will clean up the scheduled results in the cluster.
48+
// So we should not schedule resource to the deleting cluster.
49+
if !c.Cluster().DeletionTimestamp.IsZero() {
50+
klog.V(4).Infof("Cluster %q is deleting, skip it", c.Cluster().Name)
51+
continue
52+
```
53+
54+
## 接口合规性
55+
56+
- 所有显式实现接口的结构体,必须在代码中添加编译时接口合规性检查,以确保完整实现接口契约。
57+
使用以下模式:
58+
```go
59+
var _ InterfaceName = &StructName{}
60+
```
61+
此断言应置于结构体定义所在的文件中(通常靠近类型声明或文件顶部),确保:
62+
63+
1.若结构体未完整实现接口,编译阶段即报错;
64+
65+
2.显式表达该结构体的接口契约,提升代码可读性与可维护性。
66+
67+
正确示例
68+
```go
69+
// Check if our workloadInterpreter implements necessary interface
70+
var _ interpreter.Handler = &workloadInterpreter{}
71+
72+
// workloadInterpreter explore resource with request operation.
73+
type workloadInterpreter struct {
74+
decoder *interpreter.Decoder
75+
}
76+
```
77+
78+
## 函数定义
79+
- 函数参数过多时,建议使用结构体封装参数,以提高代码可读性和可维护性。
80+
- 函数签名应优先写在单行,包括参数列表和返回值。
81+
82+
错误示例
83+
```go
84+
func Foo(
85+
bar string,
86+
baz int) error
87+
```
88+
89+
正确示例
90+
```go
91+
func Foo(bar string, baz int) error
92+
```
93+
94+
- 当接受 `context.Context` 参数时,必须将其作为函数的第一个参数。
95+
96+
## 安全编码规范
97+
98+
- 禁止硬编码密码、私钥、API Token 等认证凭据。
99+
- 如果采用解释性语言(如 Shell/Python/Perl 脚本、JSP、HTML等)实现,对于不满足未公开接口并需要清理的功能,必须彻底删除,严禁使用注释行等形式仅使功能失效。
100+
- 禁止使用私有或未经验证的加密算法。以下行为均属违规:
101+
- 使用未经专业机构评估的自研加密算法;
102+
- 通过字符移位、替换、变形等方式实现的“自定义加解密”;
103+
- 将 Base64、Hex 等编码方式当作“加密”手段使用。
104+
注:在非加密场景(如数据序列化)中使用 Base64 或类似转换是允许的。
105+
- 密码学操作必须使用安全随机数。
106+
- 禁止在日志或错误信息中明文输出凭证。
107+
108+
## 目录专属风格指南
109+
110+
除了上述通用代码风格指南外,某些目录可能有特定的代码风格要求。此章节将列出这些目录及其特定的代码风格指南。
111+
112+
### CHANGELOG
113+
114+
#### 位置
115+
`CHANGELOG` 文件位于 Karmada 仓库的 `docs/CHANGELOG` 目录下。
116+
117+
#### 作用
118+
记录每个版本的发布说明(release notes),包括新增功能、问题修复和其他重要变更。
119+
120+
#### 格式规范
121+
- 同版本的 release notes 按照类别组合。
122+
- 每条 release note 可按是否涉及具体组件而分为两种格式:
123+
```markdown
124+
- `组件名`: 内容 ([#pr编号](pr链接), @pr作者)
125+
- 内容 ([#pr编号](pr链接), @pr作者)
126+
```
127+
注:如果 pr作者为 `dependabot`,可省略 `, @pr作者` 部分。
128+
129+
正确示例
130+
```markdown
131+
- `karmada-controller-manager`: Fixed the issue that a workload propagated with `duplicated mode` can bypass quota checks during scale up. ([#6474](https://github.com/karmada-io/karmada/pull/6474), @zhzhuang-zju)
132+
```
133+
134+
- 组件名使用反引号标记,例如 `karmada-controller-manager`
135+
- 同一类别的 release notes 按照组件归类,提高可读性。
136+
137+
错误示例
138+
```markdown
139+
- `karmada-controller-manager`: Fixed the bug that XXXXX. ([#6446](https://github.com/karmada-io/karmada/pull/6446), @XiShanYongYe-Chang)
140+
- `helm`: Fixed the issue where `helm upgrade` failed to update Karmada's static resources properly. ([#6395](https://github.com/karmada-io/karmada/pull/6395), @deefreak)
141+
- `karmada-controller-manager`: Fixed the issue that `taint-manager` didn't honour`--no-execute-taint-eviction-purge-mode` when evicting `ClusterResourceBinding`. ([#6491](https://github.com/karmada-io/karmada/pull/6491), @XiShanYongYe-Chang)
142+
```
143+
144+
正确示例
145+
```markdown
146+
- `karmada-controller-manager`: Fixed the bug that cluster can not be unjoined in case of the `--enable-taint-manager=false` or the feature gate `Failover` is disabled. ([#6446](https://github.com/karmada-io/karmada/pull/6446), @XiShanYongYe-Chang)
147+
- `karmada-controller-manager`: Fixed the issue that `taint-manager` didn't honour`--no-execute-taint-eviction-purge-mode` when evicting `ClusterResourceBinding`. ([#6491](https://github.com/karmada-io/karmada/pull/6491), @XiShanYongYe-Chang)
148+
- `helm`: Fixed the issue where `helm upgrade` failed to update Karmada's static resources properly. ([#6395](https://github.com/karmada-io/karmada/pull/6395), @deefreak)
149+
```
150+
151+
- 注意时态使用
152+
- 废弃(Deprecation)类 release note 使用现在完成时态,表示某个功能已经被废弃。
153+
- 其它类别统一使用一般过去时,表示某个功能已经被添加或修复。
154+
- 仅当描述“从当前版本起引入的新能力或行为变更”时,可使用 `now supports``no longer relies` 等一般现在时结构。
155+
156+
错误示例
157+
```markdown
158+
- `karmada-controller-manager`: Fix the bug that xxx. ([#prID](pr_link), @author)
159+
```
160+
161+
正确示例
162+
```markdown
163+
- `karmada-controller-manager`: Fixed the bug that xxx. ([#prID](pr_link), @author)
164+
```

i18n/zh/docusaurus-plugin-content-docs/current/developers/secure-coding-specifications.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

sidebars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ module.exports = {
247247
"developers/customize-karmada-scheduler",
248248
"developers/document-releasing",
249249
"developers/add-new-api",
250-
"developers/secure-coding-specifications"
250+
"developers/code-style-guide"
251251
],
252252
},
253253
{

0 commit comments

Comments
 (0)