feat: support random suffix#4
Conversation
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
🤖 AI Code Review
SummaryThis PR changes the suffix generation from second-level timestamp to millisecond-level timestamp with 4 random alphanumeric characters. The implementation includes proper error handling with crypto/rand and fallback to time-based generation. Significant code refactoring improves maintainability. One critical issue found in the fallback logic that could produce shorter-than-expected suffixes. Review Statistics
Critical Issues
Warnings
Suggestions
Positive Feedback
ℹ️ About this reviewThis review was automatically generated using the
|
c375a94 to
8fa6534
Compare
alaudabot
left a comment
There was a problem hiding this comment.
Found 1 critical issue that must be fixed: the fallback logic in generateShortRandomSuffix() may return a string shorter than requested length.
| privilegeNameMap := make(map[string]string, len(resolved.Privileges)) | ||
| if err := resolveUsers(resolved.Users, defaultMode, suffix, state); err != nil { | ||
| return nil, false, err | ||
| } |
There was a problem hiding this comment.
Critical Issue (bug/function-contract): The fallback logic here may return a string shorter than length. When len(fallbackFromTime) <= length, the function returns the entire fallbackFromTime (which is ~19 characters for UnixNano()), violating the function contract that promises exactly length characters.
Suggestion: Always ensure exactly length characters in fallback:
| } | |
| if _, err := randomRead(rawRandomBytes); err != nil { | |
| fallbackFromTime := fmt.Sprintf("%d", time.Now().UnixNano()) | |
| // Pad or truncate to ensure exactly `length` characters | |
| for len(fallbackFromTime) < length { | |
| fallbackFromTime += fallbackFromTime | |
| } | |
| return fallbackFromTime[:length] | |
| } |
| @@ -54,120 +66,168 @@ func (c *Config) resolveNamesWithSuffix(suffix string) (*Config, bool, error) { | |||
| if err != nil { | |||
| return nil, false, fmt.Errorf("invalid config.nameMode: %w", err) | |||
| } | |||
There was a problem hiding this comment.
Suggestion (style/robustness): Consider validating length early for invalid values (e.g., > 100) to fail fast.
将 neuxs-cli 生成的后缀由秒级时间戳改为 毫秒级时间戳 与 4位随机后缀,改动后的用户名长度不会超出 nexus 限制。
已运行单元测试,全部通过。并在 tektoncd-operator 中执行过
make prepare-nexus-data,成功。