This document provides guidelines and best practices for AI agents contributing to the Antrea project. Antrea is a Kubernetes networking solution that leverages Open vSwitch as the networking data plane to provide networking and security services for Kubernetes clusters.
- Code Quality and Style
- Go Coding Conventions
- Bash Coding Conventions
- Logging Guidelines
- Git and Commit Guidelines
- File Management
- Copyright and Licensing
- Testing
- Documentation
- Write clear, maintainable, and idiomatic Go code
- Follow established patterns and conventions within the codebase
- Prioritize code readability and simplicity
- Ensure all code is properly tested
- Document complex logic and design decisions
- No trailing whitespaces: Never introduce trailing whitespaces in any files, whether it is code or documentation
- File endings: Always end files with a newline character
- Consistent indentation: Match the existing code style in the file/package
- Line length: Keep lines reasonably short (typically under 120 characters for code and under 100 characters for documentation)
Follow the Go coding conventions as outlined in the Go Code Review Comments and additional project-specific guidelines:
- Use
gofmt: Rungofmton all Go code to ensure consistent formatting - Import organization: Use
goimportsto automatically manage imports - Variable naming:
- Use short names for local variables with limited scope
- Use descriptive names for variables with broader scope
- Follow Go naming conventions (camelCase for unexported, PascalCase for exported)
- Error handling: Always handle errors appropriately, never ignore them with
_ - Comments:
- Write complete sentences for documentation comments
- Start comments with the name of the item being described
- End comments with a period
- Interfaces: Define interfaces at the point of use, not implementation
- Context: Pass
context.Contextas the first parameter to functions that need it
- Receiver names: Use consistent, short receiver names (typically 1-2 letters)
- Package names: Use short, lowercase names without underscores
- Error strings: Don't capitalize error strings or end them with punctuation
- Empty slices: Prefer
var t []stringovert := []string{}
- Shebang: For all Bash scripts, prefer
#!/usr/bin/env bashover alternatives like#!/bin/bashfor better portability - Error handling: Use
set -o errexitandset -o pipefailto catch errors early - Variable quoting: Always quote variables to prevent word splitting and pathname expansion
- Function naming: Use lowercase with underscores for function names
- Comments: Include clear comments explaining complex logic or non-obvious behavior
Antrea uses structured logging with the k8s.io/klog/v2 module. Follow these guidelines:
- Use
klog.InfoS()orklog.Info(): For general informational messages - Use
klog.ErrorS()orklog.Error(): For error conditions - DO NOT use
klog.Warning()orklog.Warningf(): These are deprecated
- Prefer structured logging with key-value pairs
- Use consistent key names across the codebase
- Follow Kubernetes logging guidelines from the community documentation
// Good: Structured logging
klog.InfoS("Processing network policy", "policy", policyName, "namespace", namespace)
// Good: Simple info logging
klog.Info("Starting Antrea agent")
// Good: Error with context
klog.ErrorS(err, "Failed to create OVS bridge", "bridge", bridgeName)
// Bad: Using deprecated Warning
klog.Warning("This is deprecated") // Don't do thisFollow the guidelines from How to Write a Git Commit Message for all commits:
-
Subject line:
- Limit to 50 characters
- Use imperative mood ("Add feature" not "Added feature")
- Don't end with a period
- Capitalize the first letter
-
Body (if needed):
- Wrap at 72 characters
- Explain what and why, not how
- Separate from subject with a blank line
-
Examples:
Fix memory leak in flow exporter The connection tracking module was not properly releasing memory when flows expired, causing gradual memory growth in long-running agents.
- All commits must be signed: Use
git commit -sto sign your commits - Clear descriptions: Write clear, concise commit messages
- Atomic commits: Each commit should represent a single logical change
- No merge commits: Use rebase to maintain a linear history
- Follow the existing package structure in
pkg/ - Place test files alongside the code they test
- Use appropriate file naming conventions (
*_test.gofor tests,doc.gofor package documentation)
All new Go source files must include the Apache 2.0 license header:
// Copyright 2025 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.All new shell scripts must include the license header:
# Copyright 2025 Antrea Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.- Use the current year for new files
- Look at existing files in the codebase for examples of the correct format
- Write tests for all new functionality
- Maintain or improve test coverage
- Follow existing test patterns in the codebase
- Use table-driven tests where appropriate
- Provide meaningful test failure messages
- Place unit tests in
*_test.gofiles alongside the code being tested - Follow the existing patterns for integration and e2e tests
- Document all exported functions, types, and packages
- Use complete sentences in documentation comments
- Start documentation with the name of the item being documented
- Provide examples for complex functionality
- Write clear, concise comments for complex logic
- Explain the "why" not just the "what"
- Keep comments up-to-date with code changes
- Use TODO comments sparingly and include context
- Linting: All markdown files should be checked with
markdownlint - Exceptions: Files listed in
hack/.markdownlint-ignoreare exempt from linting - Validation: Use
make markdownlintto check all markdown files in the project - Auto-fix: Basic issues can be fixed with
make markdownlint-fix - Standards: Follow standard markdown formatting and style conventions