Skip to content

Commit

Permalink
[WIP] Use utilruntime.Must enhanced check (openkruise#1373)
Browse files Browse the repository at this point in the history
Signed-off-by: acejilam <acejilam@gmail.com>
  • Loading branch information
ls-2018 authored Aug 21, 2023
1 parent 9ccd897 commit e6205a5
Show file tree
Hide file tree
Showing 38 changed files with 158 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ kustomize: ## Download kustomize locally if necessary.

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
golangci-lint: ## Download golangci-lint locally if necessary.
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.42.1)
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2)

GINKGO = $(shell pwd)/bin/ginkgo
ginkgo: ## Download ginkgo locally if necessary.
Expand Down
14 changes: 8 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"time"
_ "time/tzdata" // for AdvancedCronJob Time Zone support

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -69,13 +71,13 @@ var (
)

func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = appsv1alpha1.AddToScheme(clientgoscheme.Scheme)
_ = appsv1beta1.AddToScheme(clientgoscheme.Scheme)
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(appsv1alpha1.AddToScheme(clientgoscheme.Scheme))
utilruntime.Must(appsv1beta1.AddToScheme(clientgoscheme.Scheme))

_ = appsv1alpha1.AddToScheme(scheme)
_ = appsv1beta1.AddToScheme(scheme)
_ = policyv1alpha1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(appsv1beta1.AddToScheme(scheme))
utilruntime.Must(policyv1alpha1.AddToScheme(scheme))
scheme.AddUnversionedTypes(metav1.SchemeGroupVersion, &metav1.UpdateOptions{}, &metav1.DeleteOptions{}, &metav1.CreateOptions{})
// +kubebuilder:scaffold:scheme
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/control/pubcontrol/pub_control_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"testing"
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

appspub "github.com/openkruise/kruise/apis/apps/pub"
policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1"
apps "k8s.io/api/apps/v1"
Expand All @@ -36,9 +38,9 @@ import (

func init() {
scheme = runtime.NewScheme()
_ = policyv1alpha1.AddToScheme(scheme)
_ = corev1.AddToScheme(scheme)
_ = apps.AddToScheme(scheme)
utilruntime.Must(policyv1alpha1.AddToScheme(scheme))
utilruntime.Must(corev1.AddToScheme(scheme))
utilruntime.Must(apps.AddToScheme(scheme))
}

var (
Expand Down
4 changes: 3 additions & 1 deletion pkg/control/sidecarcontrol/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"reflect"
"testing"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/openkruise/kruise/pkg/util"
"github.com/openkruise/kruise/pkg/util/configuration"
Expand All @@ -34,7 +36,7 @@ import (

func init() {
sch = runtime.NewScheme()
_ = corev1.AddToScheme(sch)
utilruntime.Must(corev1.AddToScheme(sch))
}

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"testing"
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/robfig/cron/v3"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -105,8 +107,8 @@ func TestScheduleWithTimeZone(t *testing.T) {
// Test scenario:
func TestReconcileAdvancedJobCreateBroadcastJob(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

// A job
job1 := createJob("job1", broadcastJobTemplate())
Expand Down Expand Up @@ -142,9 +144,9 @@ func TestReconcileAdvancedJobCreateBroadcastJob(t *testing.T) {

func TestReconcileAdvancedJobCreateJob(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = batchv1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(batchv1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

// A job
job1 := createJob("job2", jobTemplate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package broadcastjob
//t := &envtest.Environment{
// CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
//}
//apis.AddToScheme(scheme.Scheme)
//utilruntime.Must(apis.AddToScheme(scheme.Scheme))
//
//var err error
//if cfg, err = t.Start(); err != nil {
Expand Down
42 changes: 22 additions & 20 deletions pkg/controller/broadcastjob/broadcastjob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"reflect"
"testing"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
Expand Down Expand Up @@ -90,8 +92,8 @@ func TestGetNodeToPodMap(t *testing.T) {
// 1 new pod created on 1 node
func TestReconcileJobCreatePodAbsolute(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

p := intstr.FromInt(2)
// A job
Expand Down Expand Up @@ -146,8 +148,8 @@ func TestReconcileJobCreatePodAbsolute(t *testing.T) {
// 1 new pod created on 1 node
func TestReconcileJobCreatePodPercentage(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

p := intstr.FromString("40%")
// A job
Expand Down Expand Up @@ -204,8 +206,8 @@ func TestReconcileJobCreatePodPercentage(t *testing.T) {
// Check only 1 pod is created because the other node is unschedulable
func TestPodsOnUnschedulableNodes(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

p := intstr.FromInt(2)
// A job
Expand Down Expand Up @@ -250,8 +252,8 @@ func TestPodsOnUnschedulableNodes(t *testing.T) {
// 10 pods created with slow start
func TestReconcileJobMultipleBatches(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

p := intstr.FromInt(20)
// A job
Expand Down Expand Up @@ -295,8 +297,8 @@ func TestReconcileJobMultipleBatches(t *testing.T) {
// Check job state is failed
func TestJobFailed(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

// A job
p := intstr.FromInt(10)
Expand Down Expand Up @@ -347,8 +349,8 @@ func TestJobFailed(t *testing.T) {
// check job phase is running
func TestJobFailurePolicyTypeContinue(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

// A job
p := intstr.FromInt(10)
Expand Down Expand Up @@ -394,8 +396,8 @@ func TestJobFailurePolicyTypeContinue(t *testing.T) {
// check job phase is failed
func TestJobFailurePolicyTypeFailFast(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

// A job
p := intstr.FromInt(10)
Expand Down Expand Up @@ -441,8 +443,8 @@ func TestJobFailurePolicyTypeFailFast(t *testing.T) {
// check job phase is paused
func TestJobFailurePolicyPause(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

// A job
p := intstr.FromInt(10)
Expand Down Expand Up @@ -489,8 +491,8 @@ func TestJobFailurePolicyPause(t *testing.T) {
// check job phase is paused, and no new pod is created.
func TestJobSetPaused(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

p := intstr.FromString("50%")
// A job
Expand Down Expand Up @@ -532,8 +534,8 @@ func TestJobSetPaused(t *testing.T) {
// The job should fail after activeDeadline, and active pods will be deleted
func TestJobFailedAfterActiveDeadline(t *testing.T) {
scheme := runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))

// A job
p := intstr.FromInt(10)
Expand Down
8 changes: 5 additions & 3 deletions pkg/controller/cloneset/cloneset_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"sync"
"testing"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/onsi/gomega"
"github.com/openkruise/kruise/apis"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
Expand All @@ -46,8 +48,8 @@ import (

func init() {
testscheme = k8sruntime.NewScheme()
_ = corev1.AddToScheme(testscheme)
_ = appsv1alpha1.AddToScheme(testscheme)
utilruntime.Must(corev1.AddToScheme(testscheme))
utilruntime.Must(appsv1alpha1.AddToScheme(testscheme))
}

var testscheme *k8sruntime.Scheme
Expand All @@ -59,7 +61,7 @@ func TestMain(m *testing.M) {
t := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
}
apis.AddToScheme(scheme.Scheme)
utilruntime.Must(apis.AddToScheme(scheme.Scheme))

var err error
if cfg, err = t.Start(); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/cloneset/revision/cloneset_revision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"reflect"
"testing"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/openkruise/kruise/apis"
clonesettest "github.com/openkruise/kruise/pkg/controller/cloneset/test"
v1 "k8s.io/api/core/v1"
Expand All @@ -29,7 +31,7 @@ import (
)

func TestMain(m *testing.M) {
_ = apis.AddToScheme(scheme.Scheme)
utilruntime.Must(apis.AddToScheme(scheme.Scheme))
}

func TestCreateApplyRevision(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/cloneset/sync/cloneset_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"testing"
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/openkruise/kruise/apis"
appspub "github.com/openkruise/kruise/apis/apps/pub"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
Expand Down Expand Up @@ -81,7 +83,7 @@ func getInt32Pointer(i int32) *int32 {
}

func TestUpdate(t *testing.T) {
apis.AddToScheme(scheme.Scheme)
utilruntime.Must(apis.AddToScheme(scheme.Scheme))
now := metav1.NewTime(time.Unix(time.Now().Add(-time.Hour).Unix(), 0))
cases := []manageCase{
{
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/daemonset/daemonset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/openkruise/kruise/apis"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
kruiseclientset "github.com/openkruise/kruise/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -64,7 +66,7 @@ var (
)

func init() {
_ = apis.AddToScheme(scheme.Scheme)
utilruntime.Must(apis.AddToScheme(scheme.Scheme))
}

func newDaemonSet(name string) *appsv1alpha1.DaemonSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"testing"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -43,8 +45,8 @@ var (

func init() {
testscheme = k8sruntime.NewScheme()
_ = corev1.AddToScheme(testscheme)
_ = appsv1alpha1.AddToScheme(testscheme)
utilruntime.Must(corev1.AddToScheme(testscheme))
utilruntime.Must(appsv1alpha1.AddToScheme(testscheme))
}

func TestReconcile(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/nodepodprobe/node_pod_probe_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"reflect"
"testing"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/openkruise/kruise/pkg/util/controllerfinder"
corev1 "k8s.io/api/core/v1"
Expand All @@ -34,8 +36,8 @@ import (

func init() {
scheme = runtime.NewScheme()
_ = corev1.AddToScheme(scheme)
_ = appsv1alpha1.AddToScheme(scheme)
utilruntime.Must(corev1.AddToScheme(scheme))
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
}

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"testing"
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
appsv1beta1 "github.com/openkruise/kruise/apis/apps/v1beta1"
"github.com/openkruise/kruise/pkg/util/controllerfinder"
Expand Down Expand Up @@ -154,10 +156,10 @@ var (

func init() {
scheme = runtime.NewScheme()
_ = appsv1alpha1.AddToScheme(scheme)
_ = corev1.AddToScheme(scheme)
_ = appsv1.AddToScheme(scheme)
_ = appsv1beta1.AddToScheme(scheme)
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(corev1.AddToScheme(scheme))
utilruntime.Must(appsv1.AddToScheme(scheme))
utilruntime.Must(appsv1beta1.AddToScheme(scheme))
}

func TestReconcilePersistentPodState(t *testing.T) {
Expand Down
Loading

0 comments on commit e6205a5

Please sign in to comment.