The goal of this guide is to manage the complexity, keep a consistent code style and prevent common mistakes. New code should follow the guides below and reviewers should check if new PRs follow the rules.
Always use camelcase to name variables and functions.
Bad | Good |
---|---|
var command_line string |
var commandLine string |
All error that not expected should be handled with error log. No error should be skipped silently.
Bad | Good |
---|---|
kubeClient, _ := kubernetes.NewForConfig(cfg) |
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Errorf("init kubernetes client failed %v", err)
return err
} |
We prefer use if err := somefunction(); err != nil {}
to check error in one line.
Bad | Good |
---|---|
err := c.initNodeRoutes()
if err != nil {
klog.Fatalf("failed to initialize node routes: %v", err)
} |
if err := c.initNodeRoutes(); err != nil {
klog.Fatalf("failed to initialize node routes: %v", err)
} |
The length of one function should not exceed 100 lines.
When err occurs in the function, it should be returned to the caller not skipped silently.
Bad | Good |
---|---|
func startHandle() {
if err = some(); err != nil {
klog.Errorf(err)
}
return
} |
func startHandle() error {
if err = some(); err != nil {
klog.Errorf(err)
return err
}
return nil
} |
When adding a new CRD to Kube-OVN, you should consider things below to avoid common bugs.
- The new feature should be disabled for performance and stability reasons.
- The
install.sh
,charts
andyamls
should install the new CRD. - The
cleanup.sh
should clean the CRD and all the related resources. - The
gc.go
should check the inconsistent resource and do the cleanup. - The add/update/delete event can be triggered many times during the lifecycle, the handler should be reentrant.