Skip to content

Commit

Permalink
adopt static check issues
Browse files Browse the repository at this point in the history
Signed-off-by: RainbowMango <[email protected]>
  • Loading branch information
RainbowMango committed Jan 31, 2023
1 parent aa620d6 commit a1e8f16
Show file tree
Hide file tree
Showing 44 changed files with 211 additions and 113 deletions.
2 changes: 1 addition & 1 deletion cmd/agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ cluster and manifests to the Karmada control plane.`,

var controllers = make(controllerscontext.Initializers)

var controllersDisabledByDefault = sets.NewString(
var controllersDisabledByDefault = sets.New(
"certRotation",
)

Expand Down
3 changes: 3 additions & 0 deletions cmd/aggregated-apiserver/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func (o *Options) Config() (*aggregatedapiserver.Config, error) {
return config, nil
}

// disable `deprecation` check until the underlying genericfilters.BasicLongRunningRequestCheck starts using generic Set.
//
//nolint:staticcheck
func customLongRunningRequestCheck(longRunningVerbs, longRunningSubresources sets.String) apirequest.LongRunningRequestCheck {
return func(r *http.Request, requestInfo *apirequest.RequestInfo) bool {
reqClone := r.Clone(context.Background())
Expand Down
4 changes: 2 additions & 2 deletions cmd/controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ to create regular Kubernetes resources.`,
// and update the flag usage.
genericFlagSet.AddGoFlagSet(flag.CommandLine)
genericFlagSet.Lookup("kubeconfig").Usage = "Path to karmada control plane kubeconfig file."
opts.AddFlags(genericFlagSet, controllers.ControllerNames(), controllersDisabledByDefault.List())
opts.AddFlags(genericFlagSet, controllers.ControllerNames(), sets.List(controllersDisabledByDefault))

// Set klog flags
logsFlagSet := fss.FlagSet("logs")
Expand Down Expand Up @@ -175,7 +175,7 @@ func Run(ctx context.Context, opts *options.Options) error {
var controllers = make(controllerscontext.Initializers)

// controllersDisabledByDefault is the set of controllers which is disabled by default
var controllersDisabledByDefault = sets.NewString(
var controllersDisabledByDefault = sets.New(
"hpa",
)

Expand Down
3 changes: 3 additions & 0 deletions cmd/karmada-search/app/karmada-search.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func config(o *options.Options, outOfTreeRegistryOptions ...Option) (*search.Con
return config, nil
}

// disable `deprecation` check until the underlying genericfilters.BasicLongRunningRequestCheck starts using generic Set.
//
//nolint:staticcheck
func customLongRunningRequestCheck(longRunningVerbs, longRunningSubresources sets.String) request.LongRunningRequestCheck {
return func(r *http.Request, requestInfo *request.RequestInfo) bool {
if requestInfo.APIGroup == "search.karmada.io" && requestInfo.Resource == "proxying" {
Expand Down
4 changes: 2 additions & 2 deletions operator/cmd/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewOperatorCommand(ctx context.Context) *cobra.Command {
// Add the flag(--kubeconfig) that is added by controller-runtime
// (https://github.com/kubernetes-sigs/controller-runtime/blob/v0.11.1/pkg/client/config/config.go#L39).
genericFlagSet.AddGoFlagSet(flag.CommandLine)
o.AddFlags(genericFlagSet, controllers.ControllerNames(), controllersDisabledByDefault.List())
o.AddFlags(genericFlagSet, controllers.ControllerNames(), sets.List(controllersDisabledByDefault))

// Set klog flags
logsFlagSet := fss.FlagSet("logs")
Expand Down Expand Up @@ -112,7 +112,7 @@ func Run(ctx context.Context, o *options.Options) error {
var controllers = make(ctrlctx.Initializers)

// controllersDisabledByDefault is the set of controllers which is disabled by default
var controllersDisabledByDefault = sets.NewString()
var controllersDisabledByDefault = sets.New[string]()

func init() {
controllers["karmada"] = startKarmadaController
Expand Down
4 changes: 2 additions & 2 deletions operator/pkg/controller/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Context struct {
}

// IsControllerEnabled checks if the context's controllers enabled or not
func (c Context) IsControllerEnabled(name string, disabledByDefaultControllers sets.String) bool {
func (c Context) IsControllerEnabled(name string, disabledByDefaultControllers sets.Set[string]) bool {
hasStar := false
for _, ctrl := range c.Controllers {
if ctrl == name {
Expand Down Expand Up @@ -55,7 +55,7 @@ func (i Initializers) ControllerNames() []string {
}

// StartControllers starts a set of controllers with a specified ControllerContext
func (i Initializers) StartControllers(ctx Context, controllersDisabledByDefault sets.String) error {
func (i Initializers) StartControllers(ctx Context, controllersDisabledByDefault sets.Set[string]) error {
for controllerName, initFn := range i {
if !ctx.IsControllerEnabled(controllerName, controllersDisabledByDefault) {
klog.Warningf("%q is disabled", controllerName)
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/policy/v1alpha1/propagation_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestPropagationPolicy_ExplicitPriority(t *testing.T) {
},
{
name: "no priority declared should defaults to zero",
declaredPriority: pointer.Int32Ptr(20),
declaredPriority: pointer.Int32(20),
expectedPriority: 20,
},
}
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestClusterPropagationPolicy_ExplicitPriority(t *testing.T) {
},
{
name: "no priority declared should defaults to zero",
declaredPriority: pointer.Int32Ptr(20),
declaredPriority: pointer.Int32(20),
expectedPriority: 20,
},
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/work/v1alpha2/binding_types_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
GracefulEvictionTasks: []GracefulEvictionTask{
{
FromCluster: "m1",
Replicas: pointer.Int32Ptr(1),
Replicas: pointer.Int32(1),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction",
Producer: EvictionProducerTaintManager,
Expand All @@ -189,7 +189,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
GracefulEvictionTasks: []GracefulEvictionTask{
{
FromCluster: "m2",
Replicas: pointer.Int32Ptr(2),
Replicas: pointer.Int32(2),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction",
Producer: EvictionProducerTaintManager,
Expand All @@ -213,7 +213,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
GracefulEvictionTasks: []GracefulEvictionTask{
{
FromCluster: "m3",
Replicas: pointer.Int32Ptr(3),
Replicas: pointer.Int32(3),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction",
Producer: EvictionProducerTaintManager,
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
},
{
FromCluster: "m3",
Replicas: pointer.Int32Ptr(3),
Replicas: pointer.Int32(3),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction",
Producer: EvictionProducerTaintManager,
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type Context struct {
}

// IsControllerEnabled check if a specified controller enabled or not.
func (c Context) IsControllerEnabled(name string, disabledByDefaultControllers sets.String) bool {
func (c Context) IsControllerEnabled(name string, disabledByDefaultControllers sets.Set[string]) bool {
hasStar := false
for _, ctrl := range c.Opts.Controllers {
if ctrl == name {
Expand Down Expand Up @@ -129,7 +129,7 @@ func (i Initializers) ControllerNames() []string {
}

// StartControllers starts a set of controllers with a specified ControllerContext
func (i Initializers) StartControllers(ctx Context, controllersDisabledByDefault sets.String) error {
func (i Initializers) StartControllers(ctx Context, controllersDisabledByDefault sets.Set[string]) error {
for controllerName, initFn := range i {
if !ctx.IsControllerEnabled(controllerName, controllersDisabledByDefault) {
klog.Warningf("%q is disabled", controllerName)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestContext_IsControllerEnabled(t *testing.T) {
Controllers: tt.controllers,
},
}
if got := c.IsControllerEnabled(tt.controllerName, sets.NewString(tt.disabledByDefaultControllers...)); got != tt.expected {
if got := c.IsControllerEnabled(tt.controllerName, sets.New(tt.disabledByDefaultControllers...)); got != tt.expected {
t.Errorf("expected %v, but got %v", tt.expected, got)
}
})
Expand Down
6 changes: 5 additions & 1 deletion pkg/descheduler/descheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ func NewDescheduler(karmadaClient karmadaclientset.Interface, kubeClient kuberne
}
desched.deschedulerWorker = util.NewAsyncWorker(deschedulerWorkerOptions)

desched.clusterInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
_, err := desched.clusterInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: desched.addCluster,
UpdateFunc: desched.updateCluster,
DeleteFunc: desched.deleteCluster,
})
if err != nil {
klog.Errorf("Failed add handler for Clusters: %v", err)
return nil
}

eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartStructuredLogging(0)
Expand Down
11 changes: 9 additions & 2 deletions pkg/estimator/server/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func addAllEventHandlers(es *AccurateSchedulerEstimatorServer, informerFactory informers.SharedInformerFactory) {
// scheduled pod cache
informerFactory.Core().V1().Pods().Informer().AddEventHandler(
_, err := informerFactory.Core().V1().Pods().Informer().AddEventHandler(
cache.FilteringResourceEventHandler{
FilterFunc: func(obj interface{}) bool {
switch t := obj.(type) {
Expand All @@ -41,13 +41,20 @@ func addAllEventHandlers(es *AccurateSchedulerEstimatorServer, informerFactory i
},
},
)
informerFactory.Core().V1().Nodes().Informer().AddEventHandler(
if err != nil {
klog.Errorf("Failed to add handler for Pods: %v", err)
}

_, err = informerFactory.Core().V1().Nodes().Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: es.addNodeToCache,
UpdateFunc: es.updateNodeInCache,
DeleteFunc: es.deleteNodeFromCache,
},
)
if err != nil {
klog.Errorf("Failed to add handler for Pods: %v", err)
}
}

func (es *AccurateSchedulerEstimatorServer) addPodToCache(obj interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/karmadactl/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func (g *CommandGetOptions) watch(watchObjs []WatchObj) error {

info := infos[0]
mapping := info.ResourceMapping()
outputObjects := utilpointer.BoolPtr(!g.WatchOnly)
outputObjects := utilpointer.Bool(!g.WatchOnly)

printer, err := g.ToPrinter(mapping, outputObjects, g.AllNamespaces, false)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions pkg/resourceinterpreter/defaultinterpreter/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,34 @@ func getDependenciesFromPodTemplate(podObj *corev1.Pod) ([]configv1alpha1.Depend
return dependentObjectRefs, nil
}

func getSecretNames(pod *corev1.Pod) sets.String {
result := sets.NewString()
func getSecretNames(pod *corev1.Pod) sets.Set[string] {
result := sets.New[string]()
lifted.VisitPodSecretNames(pod, func(name string) bool {
result.Insert(name)
return true
})
return result
}

func getServiceAccountNames(pod *corev1.Pod) sets.String {
result := sets.NewString()
func getServiceAccountNames(pod *corev1.Pod) sets.Set[string] {
result := sets.New[string]()
if pod.Spec.ServiceAccountName != "" && pod.Spec.ServiceAccountName != "default" {
result.Insert(pod.Spec.ServiceAccountName)
}
return result
}

func getConfigMapNames(pod *corev1.Pod) sets.String {
result := sets.NewString()
func getConfigMapNames(pod *corev1.Pod) sets.Set[string] {
result := sets.New[string]()
lifted.VisitPodConfigmapNames(pod, func(name string) bool {
result.Insert(name)
return true
})
return result
}

func getPVCNames(pod *corev1.Pod) sets.String {
result := sets.NewString()
func getPVCNames(pod *corev1.Pod) sets.Set[string] {
result := sets.New[string]()
for i := range pod.Spec.Volumes {
volume := pod.Spec.Volumes[i]
if volume.PersistentVolumeClaim != nil {
Expand Down
26 changes: 13 additions & 13 deletions pkg/resourceinterpreter/defaultinterpreter/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func TestGetSecretNames(t *testing.T) {
tests := []struct {
name string
pod *corev1.Pod
expected sets.String
expected sets.Set[string]
}{
{
name: "get secret names from pod",
pod: fakePod,
expected: sets.NewString("fake-foo", "fake-bar"),
expected: sets.New("fake-foo", "fake-bar"),
},
}

Expand All @@ -49,7 +49,7 @@ func TestGetSecretNames(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
res := getSecretNames(tt.pod)
if !reflect.DeepEqual(res, tt.expected) {
if !res.Equal(tt.expected) {
t.Errorf("getSecretNames() = %v, want %v", res, tt.expected)
}
})
Expand Down Expand Up @@ -84,12 +84,12 @@ func TestGetConfigMapNames(t *testing.T) {
tests := []struct {
name string
pod *corev1.Pod
expected sets.String
expected sets.Set[string]
}{
{
name: "get configMap names from pod",
pod: fakePod,
expected: sets.NewString("fake-foo", "fake-bar"),
expected: sets.New("fake-foo", "fake-bar"),
},
}

Expand All @@ -98,7 +98,7 @@ func TestGetConfigMapNames(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
res := getConfigMapNames(tt.pod)
if !reflect.DeepEqual(res, tt.expected) {
if !res.Equal(tt.expected) {
t.Errorf("getConfigMapNames() = %v, want %v", res, tt.expected)
}
})
Expand Down Expand Up @@ -131,12 +131,12 @@ func TestGetPVCNames(t *testing.T) {
tests := []struct {
name string
pod *corev1.Pod
expected sets.String
expected sets.Set[string]
}{
{
name: "get pvc names from pod",
pod: fakePod,
expected: sets.NewString("fake-foo", "fake-bar"),
expected: sets.New("fake-foo", "fake-bar"),
},
}

Expand All @@ -145,7 +145,7 @@ func TestGetPVCNames(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
res := getPVCNames(tt.pod)
if !reflect.DeepEqual(res, tt.expected) {
if !res.Equal(tt.expected) {
t.Errorf("getPVCNames() = %v, want %v", res, tt.expected)
}
})
Expand Down Expand Up @@ -219,24 +219,24 @@ func Test_getServiceAccountNames(t *testing.T) {
tests := []struct {
name string
args args
want sets.String
want sets.Set[string]
}{
{
name: "get ServiceAccountName from pod ",
args: args{pod: &corev1.Pod{Spec: corev1.PodSpec{ServiceAccountName: "test"}}},
want: sets.NewString("test"),
want: sets.New("test"),
},
{
name: "get default ServiceAccountName from pod ",
args: args{pod: &corev1.Pod{Spec: corev1.PodSpec{ServiceAccountName: "default"}}},
want: sets.NewString(),
want: sets.New[string](),
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := getServiceAccountNames(tt.args.pod); !reflect.DeepEqual(got, tt.want) {
if got := getServiceAccountNames(tt.args.pod); !got.Equal(tt.want) {
t.Errorf("getServiceAccountNames() = %v, want %v", got, tt.want)
}
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (s *Snapshot) GetReadyClusters() []*framework.ClusterInfo {
}

// GetReadyClusterNames returns the clusterNames in ready status.
func (s *Snapshot) GetReadyClusterNames() sets.String {
readyClusterNames := sets.NewString()
func (s *Snapshot) GetReadyClusterNames() sets.Set[string] {
readyClusterNames := sets.New[string]()
for _, c := range s.clusterInfoList {
if util.IsClusterReady(&c.Cluster().Status) {
readyClusterNames.Insert(c.Cluster().Name)
Expand Down
Loading

0 comments on commit a1e8f16

Please sign in to comment.