Skip to content

Commit c4c09d0

Browse files
committed
Merge remote-tracking branch 'origin/main' into ash/restorecon
2 parents 87df9bb + 013b2c7 commit c4c09d0

File tree

109 files changed

+5582
-2435
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+5582
-2435
lines changed

.cursor/rules/clean-code.mdc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
description:
3+
globs: *.go
4+
alwaysApply: false
5+
---
6+
# Clean Code
7+
8+
Essential clean code principles for Go development in Embedded Cluster.
9+
10+
## File Formatting
11+
12+
### End of File
13+
- **Always leave a single newline at the end of every file**
14+
- This ensures proper POSIX compliance and clean git diffs
15+
16+
## Comments
17+
18+
### Keep Comments Concise
19+
- **Comments should be brief and to the point**
20+
- Explain *why*, not *what* the code does
21+
- Avoid redundant comments that just repeat the code
22+
23+
### Comment Quality
24+
25+
- **Write self-explanatory comments that clearly explain the purpose and context**: Explain WHY, not just WHAT the code does
26+
```go
27+
// Good - explains WHY and provides context
28+
// Retry 3 times because API can be temporarily unavailable during deployment
29+
for i := 0; i < 3; i++ {
30+
if err := checkAPIHealth(); err == nil {
31+
break
32+
}
33+
time.Sleep(time.Second * 2)
34+
}
35+
```
36+
37+
### Function Comments
38+
- Use concise godoc format for exported functions
39+
- Focus on purpose and important behavior
40+
- Single line comments for simple functions
41+
42+
### Inline Comments
43+
- Use sparingly for complex logic
44+
- Keep on same line when possible for short explanations

.cursor/rules/frontend-rules.mdc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description:
3-
globs:
3+
globs: web/*.tsx,web/*.ts,web/*.js,web/*.jsx
44
alwaysApply: false
55
---
66
# manager experience (front end) rules and guidelines
@@ -43,3 +43,29 @@ alwaysApply: false
4343

4444

4545
Please validate and run unit tests with `npm run test:unit <path to file>` from the `web` directory.
46+
47+
## React Architecture Patterns
48+
49+
### Context Usage Guidelines
50+
51+
- **Only use React Context when you have props drilling**: Prefer props for simple parent-child communication to avoid unnecessary complexity
52+
```jsx
53+
// Good - simple props for parent-child
54+
<ChildComponent isConnected={connected} />
55+
56+
// Bad - unnecessary Context for simple cases
57+
<ConnectionContext.Provider value={connected}>
58+
<ChildComponent />
59+
</ConnectionContext.Provider>
60+
```
61+
62+
### Component Visibility Control
63+
64+
- **Control component visibility at parent level, not within the component itself**: This creates cleaner conditional rendering patterns
65+
```jsx
66+
// Good - parent controls visibility
67+
{showModal && <Modal onClose={() => setShowModal(false)} />}
68+
69+
// Bad - component controls its own visibility
70+
<Modal visible={showModal} onClose={...} />
71+
```

.cursor/rules/go-best-practices.mdc

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
description:
3+
globs: *.go
4+
alwaysApply: false
5+
---
6+
# Go Best Practices
7+
8+
Follow these best practices when writing Go code in this repository.
9+
10+
## Core Principles
11+
12+
- **Clarity and Simplicity**: Write code that is easy to read, understand, and maintain. Favor explicit over implicit.
13+
- **Dependency Injection**: Use dependency injection to decouple components and enable testing. The functional options pattern is the standard approach.
14+
- **Interface-Driven Design**: Define behavior through interfaces to enable mocking and loose coupling.
15+
- **Explicit Error Handling**: Handle all errors explicitly. Use structured error types when appropriate.
16+
17+
## Architecture Patterns
18+
19+
### Functional Options Pattern
20+
21+
Use the functional options pattern for component initialization. This is the standard across controllers and main components.
22+
23+
### Interface Design
24+
25+
- **Small, Focused Interfaces**: Keep interfaces small and focused on specific behavior
26+
- **Interface Segregation**: Prefer multiple small interfaces over large ones
27+
- **Testability**: All external dependencies should be behind interfaces for mocking
28+
- **Naming**: Use descriptive names that indicate the behavior (e.g., `InstallationManager`, `NetUtils`)
29+
30+
## Error Handling
31+
32+
### Error Wrapping and Context
33+
34+
- **Wrap Errors**: Always add context when propagating errors using `fmt.Errorf("operation failed: %w", err)`
35+
- **Use %w verb for error wrapping**: Use `%w` instead of `%v` when wrapping errors to maintain the error chain
36+
```go
37+
return fmt.Errorf("processing config: %w", err) // Good - contextual, no prefix
38+
return fmt.Errorf("reading config file: %w", err) // Good - specific context
39+
```
40+
- **Preserve Original**: Store the original error for unwrapping when using custom error types
41+
- **Meaningful Messages**: Error messages should be actionable and include relevant context without redundant prefixes
42+
- **Use type-safe error handling**: Check error types instead of string matching to avoid brittle code
43+
```go
44+
if errors.Is(err, context.DeadlineExceeded) { ... } // Good
45+
if strings.Contains(err.Error(), "deadline") { ... } // Bad
46+
```
47+
48+
### Error Message Consistency
49+
50+
- **Go error strings should start with lowercase letter and not end with punctuation**: Follow Go conventions
51+
```go
52+
return errors.New("failed to connect") // Good
53+
return errors.New("Failed to connect.") // Bad
54+
```
55+
56+
## Naming Conventions
57+
58+
### Package Names
59+
- Use short, concise, all-lowercase names
60+
- Avoid stuttering (don't repeat package name in exported types)
61+
- Examples: `types`, `utils`, `install`, `auth`
62+
63+
### Types and Functions
64+
- **Exported Types**: Use `PascalCase` (e.g., `InstallController`, `NetUtils`)
65+
- **Exported Functions**: Use `PascalCase` (e.g., `NewInstallController`, `ValidateConfig`)
66+
- **Internal Functions**: Use `camelCase` (e.g., `processRequest`, `validateInput`)
67+
- **Variables**: Use `camelCase` for all variables
68+
69+
### Interface Naming
70+
- **Behavior-Based**: Name interfaces after what they do (e.g., `Controller`, `Manager`, `NetUtils`)
71+
- **Avoid Generic Names**: Don't use generic suffixes like `Interface` unless necessary
72+
- **Single Method**: For single-method interfaces, consider the "-er" suffix pattern
73+
74+
### Variable Naming Clarity
75+
76+
- **Use distinct variable names for file paths vs configuration objects**: Distinguish between file locations, raw data, and parsed objects
77+
```go
78+
configPath := "/etc/config.yaml" // file location
79+
configBytes := []byte{} // raw data
80+
config := &Config{} // parsed object
81+
```
82+
83+
## Configuration Management
84+
85+
### Input Validation and Defaults
86+
87+
- **Check before setting defaults**: Always verify if user-provided fields are empty before setting defaults to avoid overriding user input
88+
```go
89+
if config.DataDirectory == "" {
90+
config.DataDirectory = "/opt/default"
91+
}
92+
```
93+
94+
## CLI Design
95+
96+
### Flag Naming and Help
97+
98+
- **Ensure all CLI flags appear in help menu with consistent naming patterns**: Use kebab-case for multi-word flags
99+
```go
100+
installCmd.Flags().String("admin-console-namespace", "", "Namespace for admin console") // Good
101+
installCmd.Flags().String("adminconsolenamespace", "", "Namespace for admin console") // Bad
102+
```
103+
104+
## Concurrency and Thread Safety
105+
106+
### Mutex Usage
107+
- Use `sync.RWMutex` for read-heavy workloads
108+
- Keep critical sections small
109+
- Always use defer for unlocking: `defer mu.Unlock()`
110+
111+
### Context Usage
112+
- Pass `context.Context` as the first parameter to functions that may block or need cancellation
113+
- Use `ctx context.Context` as the parameter name
114+
- Don't store contexts in structs; pass them through function calls
115+
116+
## Logging
117+
118+
### Structured Logging with Logrus
119+
120+
- Use `logrus.FieldLogger` interface for dependency injection
121+
- Add contextual fields to log entries for better debugging
122+
- Use appropriate log levels: `Debug`, `Info`, `Warn`, `Error`
123+
124+
### Logging Patterns
125+
126+
- **Error Logging**: Always log errors at the outermost caller (bottom of the stack). All the context from the trace should be included in the message wrapping.
127+
- **Discard Logger**: Use `logger.NewDiscardLogger()` for tests
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
description:
3+
globs: api/*.go
4+
alwaysApply: false
5+
---
6+
# Go Embedded Cluster API Implementation Guidelines
7+
8+
For comprehensive architecture and package structure details, see [api/README.md](mdc:../api/README.md).
9+
10+
## Essential Implementation Patterns
11+
12+
### Error Handling
13+
14+
#### Structured API Errors
15+
16+
Use structured error types for APIs that need to return detailed error information:
17+
18+
```go
19+
type APIError struct {
20+
StatusCode int `json:"status_code,omitempty"`
21+
Message string `json:"message"`
22+
Field string `json:"field,omitempty"`
23+
Errors []*APIError `json:"errors,omitempty"`
24+
err error `json:"-"`
25+
}
26+
```
27+
28+
#### Error Constructor Functions
29+
30+
Create constructor functions for common API error types.
31+
32+
#### API Error Handling Patterns
33+
34+
- Always log errors with context: `a.logError(r, err, "descriptive message")`
35+
- Use structured errors: `types.NewBadRequestError(err)`, `types.NewInternalServerError(err)`
36+
- Wrap errors with context: `fmt.Errorf("operation failed: %w", err)`
37+
38+
#### Specific API Error Types
39+
40+
- **Use specific API error types**: Always use `NewBadRequestError`, `NewInternalServerError`, etc. instead of generic errors for proper HTTP status codes
41+
```go
42+
return NewBadRequestError(errors.New("invalid input")) // Good
43+
return errors.New("invalid input") // Bad
44+
```
45+
46+
- **Return consistent HTTP status codes**: Use appropriate APIError types for different error conditions
47+
```go
48+
return NewBadRequestError(errors.New("invalid input")) // 400
49+
return NewForbiddenError(errors.New("access denied")) // 403
50+
return NewInternalServerError(errors.New("database failed")) // 500
51+
```
52+
53+
### HTTP Handler Patterns
54+
55+
- Use dedicated Handler structs with HTTP method prefixed names: `func (h *Handler) PostConfigureInstallation(w http.ResponseWriter, r *http.Request) {}`
56+
- Include HTTP method in handler names: `Get*`, `Post*`, `Put*`, `Delete*`
57+
- Use helper methods for common operations: `utils.BindJSON`, `utils.JSON`, `utils.JSONError`
58+
- Always validate input and handle errors appropriately
59+
60+
### Request/Response
61+
62+
- Use `a.bindJSON(w, r, &struct)` for parsing JSON requests
63+
- Use `a.json(w, r, statusCode, payload)` for success responses
64+
- Use `a.jsonError(w, r, err)` for error responses
65+
66+
### JSON Tags and Serialization
67+
68+
- Use appropriate JSON tags: `json:"field_name,omitempty"`
69+
- Use `json:"-"` for fields that shouldn't be serialized
70+
- Handle both marshaling and unmarshaling in your types
71+
72+
### Dependencies
73+
74+
- Use functional options pattern for initialization
75+
- Define interfaces for all external dependencies
76+
- Inject dependencies via constructors
77+
78+
### API Documentation
79+
80+
Add Swagger annotations to all handlers for API documentation.
81+
82+
#### Swagger/OpenAPI Requirements
83+
84+
- Use Swagger annotations for HTTP handlers
85+
- Document request/response structures
86+
- Include error response documentation
87+
- Document authentication requirements
88+
89+
## Implementation Quick Reference
90+
91+
### Adding New Functionality
92+
93+
- **New endpoint**: Add handler → create/update controller → define types
94+
- **New business logic**: Add to appropriate controller or create new manager
95+
- **New types**: Add to `api/types/` with proper JSON tags
96+
- **New utilities**: Add to `api/internal/`
97+
98+
### File Naming Conventions
99+
100+
- Handlers: `api/{domain}.go` (e.g., `install.go`, `auth.go`)
101+
- Controllers: `api/controllers/{domain}/controller.go`
102+
- Managers: `api/internal/managers/{domain}/manager.go`
103+
- Types: `api/types/{domain}.go`
104+
105+
### Testing Requirements
106+
107+
- Unit tests: `*_test.go` alongside source files
108+
- Integration tests: `api/integration/*_test.go`
109+
- Use table-driven tests with `testify/assert`
110+
- Mock all external dependencies
111+

0 commit comments

Comments
 (0)