Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ linters:
- gocyclo
- gosec
- misspell
- modernize
- revive
- whitespace
settings:
Expand All @@ -31,6 +32,14 @@ linters:
gocyclo:
# minimal cyclomatic complexity to report
min-complexity: 15
modernize:
disable:
# Suggest replacing omitempty with omitzero for struct fields.
# Disable this check for now since it introduces too many changes in our existing codebase.
# It suggests changing metav1.ObjectMeta json tags from "metadata,omitempty" to "metadata,omitzero",
# which is not desired as it may break existing behavior.
# See https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#hdr-Analyzer_omitzero for more details.
- omitzero
revive:
rules:
# Disable if-return as it is too strict and not always useful.
Expand Down Expand Up @@ -59,6 +68,15 @@ linters:
- name: indent-error-flow
- name: unreachable-code
- name: var-naming
arguments:
- []
- []
# Disable package name checks which are too strict for our legcy codebase.
# Currently, we have 3 packages named "util" in different directories:
# - pkg/util
# - pkg/karmadactl/util (maybe we can rename this one to "cliutil" in the future)
# - operator/pkg/util (maybe we can rename this one to "oprutil" in the future)
- - skip-package-name-checks: true
- name: redefines-builtin-id
- name: unused-parameter
- name: context-as-argument
Expand Down
10 changes: 5 additions & 5 deletions hack/tools/gencomponentdocs/postprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ func cleanupForInclude(md string) string {
newlines := []string{"---", "title: " + title}
newlines = append(newlines, lines...)

cleanMd := ""
var cleanMd strings.Builder
for i, line := range newlines {
if line == "### SEE ALSO" {
break
}

cleanMd += line
cleanMd.WriteString(line)
if i < len(newlines)-1 {
cleanMd += "\n"
cleanMd.WriteString("\n")
}
}

cleanMd += "###### Auto generated by [spf13/cobra script in Karmada](https://github.com/karmada-io/karmada/tree/master/hack/tools/gencomponentdocs)"
return cleanMd
cleanMd.WriteString("###### Auto generated by [spf13/cobra script in Karmada](https://github.com/karmada-io/karmada/tree/master/hack/tools/gencomponentdocs)")
return cleanMd.String()
}
8 changes: 4 additions & 4 deletions hack/tools/genkarmadactldocs/gen_karmadactl_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

// PrintCLIByTag print custom defined index
func PrintCLIByTag(cmd *cobra.Command, all []*cobra.Command, tag string) string {
var result string
var result strings.Builder
var pl []string
for _, c := range all {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
Expand All @@ -51,10 +51,10 @@ func PrintCLIByTag(cmd *cobra.Command, all []*cobra.Command, tag string) string
}

for _, v := range pl {
result += v
result.WriteString(v)
}
result += "\n"
return result
result.WriteString("\n")
return result.String()
}

// GenMarkdownTreeForIndex generate the index page for karmadactl
Expand Down
2 changes: 1 addition & 1 deletion hack/tools/lifted-gen/lifted-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func newTableWriter(w io.Writer) *tablewriter.Table {
return table
}

func (a *analyzer) errorf(format string, args ...interface{}) {
func (a *analyzer) errorf(format string, args ...any) {
fmt.Fprintf(os.Stderr, "ERROR "+format, args...)
fmt.Fprintln(os.Stderr)
a.failed = true
Expand Down
9 changes: 3 additions & 6 deletions hack/tools/swagger/lib/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package lib
import (
"encoding/json"
"fmt"
"maps"
"net"

"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -59,9 +60,7 @@ type Config struct {
func (c *Config) GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
out := map[string]common.OpenAPIDefinition{}
for _, def := range c.OpenAPIDefinitions {
for k, v := range def(ref) {
out[k] = v
}
maps.Copy(out, def(ref))
}
return out
}
Expand Down Expand Up @@ -125,9 +124,7 @@ func RenderOpenAPISpec(cfg Config) (string, error) {
}
}

for version, s := range storage {
apiGroupInfo.VersionedResourcesStorageMap[version] = s
}
maps.Copy(apiGroupInfo.VersionedResourcesStorageMap, storage)

// Install api to apiserver.
if err := genericServer.InstallAPIGroup(&apiGroupInfo); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion hack/verify-staticcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set -o nounset
set -o pipefail

REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
GOLANGCI_LINT_VER="v2.0.2"
GOLANGCI_LINT_VER="v2.6.2"

cd "${REPO_ROOT}"
source "hack/util.sh"
Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/apis/operator/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func addDefaultingFuncs(scheme *runtime.Scheme) error {
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&Karmada{}, func(obj interface{}) { SetObjectDefaultsKarmada(obj.(*Karmada)) })
scheme.AddTypeDefaultingFunc(&Karmada{}, func(obj any) { SetObjectDefaultsKarmada(obj.(*Karmada)) })
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions operator/pkg/certs/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,17 @@ func TestNewCertificateAuthority(t *testing.T) {
},
}

//nolint:staticcheck
// false positive from SA5011(related information): this check suggests that the pointer can be nil
// See https://github.com/golangci/golangci-lint/issues/5979 for more details.
cert, err := NewCertificateAuthority(cc)
if err != nil {
t.Fatalf("NewCertificateAuthority() returned an error: %v", err)
}

//nolint:staticcheck
// false positive from SA5011: possible nil pointer dereference
// See https://github.com/golangci/golangci-lint/issues/5979 for more details.
if cert == nil {
t.Fatal("NewCertificateAuthority() returned nil cert")
}
Expand Down Expand Up @@ -520,10 +526,16 @@ func TestCreateCertAndKeyFilesWithCA(t *testing.T) {
t.Fatalf("CreateCertAndKeyFilesWithCA() returned an error: %v", err)
}

//nolint:staticcheck
// false positive from SA5011(related information): this check suggests that the pointer can be nil
// See https://github.com/golangci/golangci-lint/issues/5979 for more details.
if cert == nil {
t.Fatal("CreateCertAndKeyFilesWithCA() returned nil cert")
}

//nolint:staticcheck
// false positive from SA5011: possible nil pointer dereference
// See https://github.com/golangci/golangci-lint/issues/5979 for more details.
if cert.cert == nil || cert.key == nil {
t.Error("Expected cert and key to be non-nil")
}
Expand Down
7 changes: 7 additions & 0 deletions operator/pkg/certs/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ func TestAddAndGetCert(t *testing.T) {
store.AddCert(cert)

retrievedCert := store.GetCert("testCert")
//nolint:staticcheck
// false positive from SA5011(related information): this check suggests that the pointer can be nil
// See https://github.com/golangci/golangci-lint/issues/5979 for more details.
if retrievedCert == nil {
t.Fatalf("expected to retrieve cert but got nil")
}

//nolint:staticcheck
// false positive from SA5011: possible nil pointer dereference
// See https://github.com/golangci/golangci-lint/issues/5979 for more details.
if string(retrievedCert.cert) != "certData" {
t.Errorf("expected certData but got %s", string(retrievedCert.cert))
}
Expand Down
8 changes: 2 additions & 6 deletions operator/pkg/controlplane/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package apiserver

import (
"fmt"
"slices"
"testing"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -362,12 +363,7 @@ func TestCreateKarmadaAggregatedAPIServerService(t *testing.T) {

// contains check if a slice contains a specific string.
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
return slices.Contains(slice, item)
}

// verifyDeploymentCreation verifies the creation of a Kubernetes deployment
Expand Down
8 changes: 2 additions & 6 deletions operator/pkg/controlplane/controlplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package controlplane

import (
"fmt"
"slices"
"testing"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -468,10 +469,5 @@ func verifyFeatureGates(container *corev1.Container, featureGates map[string]boo

// contains check if a slice contains a specific string.
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
return slices.Contains(slice, item)
}
8 changes: 2 additions & 6 deletions operator/pkg/controlplane/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package etcd

import (
"fmt"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -432,10 +433,5 @@ func verifyEtcdPeerOrClientService(service *corev1.Service, expectedPorts []core

// contains check if a slice contains a specific string.
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
return slices.Contains(slice, item)
}
8 changes: 2 additions & 6 deletions operator/pkg/controlplane/search/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package search

import (
"fmt"
"slices"
"testing"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -315,10 +316,5 @@ func verifyExtraArgs(container *corev1.Container, extraArgs map[string]string) e

// contains check if a slice contains a specific string.
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
return slices.Contains(slice, item)
}
8 changes: 2 additions & 6 deletions operator/pkg/controlplane/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package webhook

import (
"fmt"
"slices"
"testing"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -314,10 +315,5 @@ func verifyFeatureGates(container *corev1.Container, featureGates map[string]boo

// contains check if a slice contains a specific string.
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
return slices.Contains(slice, item)
}
2 changes: 1 addition & 1 deletion operator/pkg/tasks/init/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func generateComponentKubeconfigSecrets(data InitData, configString string) []*c
return secrets
}

componentList := map[string]interface{}{
componentList := map[string]any{
util.KarmadaAggregatedAPIServerName(data.GetName()): data.Components().KarmadaAggregatedAPIServer,
util.KarmadaControllerManagerName(data.GetName()): data.Components().KarmadaControllerManager,
util.KubeControllerManagerName(data.GetName()): data.Components().KubeControllerManager,
Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/util/apiclient/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (m *MockDiscovery) RESTClient() rest.Interface {
// - An error if pod creation fails.
func CreatePods(client clientset.Interface, namespace string, componentName string, replicaCount int32, labels map[string]string, markRunningState bool) ([]*corev1.Pod, error) {
pods := make([]*corev1.Pod, 0, replicaCount)
for i := int32(0); i < replicaCount; i++ {
for i := range replicaCount {
podName := fmt.Sprintf("%s-pod-%d", componentName, i)
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/util/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ users:
}
base64CACert := base64.StdEncoding.EncodeToString([]byte(caCert))
base64Token := base64.StdEncoding.EncodeToString([]byte(token))
kubeconfigBytes := []byte(fmt.Sprintf(kubeconfig, base64CACert, base64Token))
kubeconfigBytes := fmt.Appendf(nil, kubeconfig, base64CACert, base64Token)

// Create secret with kubeconfig data.
secret := &corev1.Secret{
Expand Down
21 changes: 6 additions & 15 deletions operator/pkg/util/patcher/pather.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package patcher
import (
"errors"
"fmt"
"maps"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -142,9 +143,7 @@ func (p *Patcher) ForDeployment(deployment *appsv1.Deployment) {
overrideArgs["feature-gates"] = buildFeatureGatesArgumentFromMap(baseFeatureGates, p.featureGates)
}

for key, val := range p.extraArgs {
overrideArgs[key] = val
}
maps.Copy(overrideArgs, p.extraArgs)

// the first argument is most often the binary name
command := []string{baseArguments[0]}
Expand Down Expand Up @@ -189,12 +188,8 @@ func buildArgumentListFromMap(baseArguments, overrideArguments map[string]string

argsMap := make(map[string]string)

for k, v := range baseArguments {
argsMap[k] = v
}
for k, v := range overrideArguments {
argsMap[k] = v
}
maps.Copy(argsMap, baseArguments)
maps.Copy(argsMap, overrideArguments)

for k := range argsMap {
keys = append(keys, k)
Expand Down Expand Up @@ -229,12 +224,8 @@ func buildFeatureGatesArgumentFromMap(baseFeatureGates, overrideFeatureGates map

featureGateMap := make(map[string]bool)

for k, v := range baseFeatureGates {
featureGateMap[k] = v
}
for k, v := range overrideFeatureGates {
featureGateMap[k] = v
}
maps.Copy(featureGateMap, baseFeatureGates)
maps.Copy(featureGateMap, overrideFeatureGates)

for k := range featureGateMap {
keys = append(keys, k)
Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/util/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

// ParseTemplate validates and parses passed as argument template
func ParseTemplate(strtmpl string, obj interface{}) ([]byte, error) {
func ParseTemplate(strtmpl string, obj any) ([]byte, error) {
var buf bytes.Buffer
tmpl, err := template.New("template").Parse(strtmpl)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/util/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestParseTemplate(t *testing.T) {
tests := []struct {
name string
template string
args interface{}
args any
want []byte
wantErr bool
errMsg string
Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func DeepEqualTasks(t1, t2 workflow.Task) error {
}

// ContainsAllValues checks if all values in the 'values' slice exist in the 'container' slice or array.
func ContainsAllValues(container interface{}, values interface{}) bool {
func ContainsAllValues(container any, values any) bool {
// Ensure the provided container is a slice or array.
vContainer := reflect.ValueOf(container)
if vContainer.Kind() != reflect.Slice && vContainer.Kind() != reflect.Array {
Expand Down
Loading
Loading