From 44f99e6f73aac34268bd5a3694b6378e224ef010 Mon Sep 17 00:00:00 2001 From: "dongyun.xzh" Date: Fri, 10 Feb 2023 16:07:37 +0800 Subject: [PATCH 1/7] Validate port allocating policy before setting up Signed-off-by: dongyun.xzh --- pkg/ddc/base/portallocator/port_allocator.go | 26 ++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/ddc/base/portallocator/port_allocator.go b/pkg/ddc/base/portallocator/port_allocator.go index 54519dc6dd0..c7876476364 100644 --- a/pkg/ddc/base/portallocator/port_allocator.go +++ b/pkg/ddc/base/portallocator/port_allocator.go @@ -17,6 +17,8 @@ limitations under the License. package portallocator import ( + "fmt" + "github.com/go-logr/logr" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/util/net" @@ -31,6 +33,15 @@ const ( BitMap AllocatePolicy = "bitmap" ) +func ValidateEnum(allocatePolicyStr string) (AllocatePolicy, error) { + switch AllocatePolicy(allocatePolicyStr) { + case Random, BitMap: + return AllocatePolicy(allocatePolicyStr), nil + default: + return AllocatePolicy(allocatePolicyStr), fmt.Errorf("runtime-port-allocator can only be random or bitmap") + } +} + type BatchAllocatorInterface interface { Allocate(int) error @@ -61,8 +72,13 @@ type RuntimePortAllocator struct { var rpa *RuntimePortAllocator // SetupRuntimePortAllocator instantiates the global singleton rpa, use BitMap port allocating policy -func SetupRuntimePortAllocator(client client.Client, pr *net.PortRange, getReservedPorts func(client client.Client) (ports []int, err error)) { - SetupRuntimePortAllocatorWithType(client, pr, BitMap, getReservedPorts) +func SetupRuntimePortAllocator(client client.Client, pr *net.PortRange, allocatePolicyStr string, getReservedPorts func(client client.Client) (ports []int, err error)) error { + policy, err := ValidateEnum(allocatePolicyStr) + if err != nil { + return err + } + SetupRuntimePortAllocatorWithType(client, pr, policy, getReservedPorts) + return nil } // SetupRuntimePortAllocatorWithType instantiates the global singleton rpa with specified port allocating policy @@ -90,11 +106,7 @@ func (alloc *RuntimePortAllocator) createAndRestorePortAllocator() (err error) { case BitMap: alloc.pa, err = newBitMapAllocator(alloc.pr, alloc.log) default: - err = errors.New("allocate-port-policy can only be random or bitmap") - } - - if err != nil { - return err + return errors.New("runtime-port-allocator can only be random or bitmap") } // policy should check reserved ports From a1d21a837d17363fd84aa2abdd4357dadd7676cb Mon Sep 17 00:00:00 2001 From: "dongyun.xzh" Date: Fri, 10 Feb 2023 16:08:09 +0800 Subject: [PATCH 2/7] Expose configurable option for port allocating policy Signed-off-by: dongyun.xzh --- cmd/alluxio/app/alluxio.go | 9 +++++++-- cmd/goosefs/app/goosefs.go | 9 ++++++++- cmd/jindo/app/jindo.go | 11 +++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/alluxio/app/alluxio.go b/cmd/alluxio/app/alluxio.go index 595719f787b..475b6123401 100644 --- a/cmd/alluxio/app/alluxio.go +++ b/cmd/alluxio/app/alluxio.go @@ -74,7 +74,7 @@ func init() { alluxioCmd.Flags().StringVar(&portRange, "runtime-node-port-range", "20000-25000", "Set available port range for Alluxio") alluxioCmd.Flags().IntVar(&maxConcurrentReconciles, "runtime-workers", 3, "Set max concurrent workers for AlluxioRuntime controller") alluxioCmd.Flags().StringVarP(&pprofAddr, "pprof-addr", "", "", "The address for pprof to use while exporting profiling results") - alluxioCmd.Flags().StringVar(&portAllocatePolicy, "port-allocate-policy", "bitmap", "Set port allocating policy, available choice is bitmap or random(default bitmap).") + alluxioCmd.Flags().StringVar(&portAllocatePolicy, "port-allocate-policy", "random", "Set port allocating policy, available choice is bitmap or random(default random).") } func handle() { @@ -128,7 +128,12 @@ func handle() { } setupLog.Info("port range parsed", "port range", pr.String()) - portallocator.SetupRuntimePortAllocatorWithType(mgr.GetClient(), pr, portallocator.AllocatePolicy(portAllocatePolicy), alluxio.GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(mgr.GetClient(), pr, portAllocatePolicy, alluxio.GetReservedPorts) + if err != nil { + setupLog.Error(err, "failed to setup runtime port allocator") + os.Exit(1) + } + setupLog.Info("Set up runtime port allocator", "policy", portAllocatePolicy) setupLog.Info("starting alluxioruntime-controller") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { diff --git a/cmd/goosefs/app/goosefs.go b/cmd/goosefs/app/goosefs.go index 2f1e2ede793..e943dbffde6 100644 --- a/cmd/goosefs/app/goosefs.go +++ b/cmd/goosefs/app/goosefs.go @@ -49,6 +49,7 @@ var ( portRange string maxConcurrentReconciles int pprofAddr string + portAllocatePolicy string ) var cmd = &cobra.Command{ @@ -73,6 +74,7 @@ func init() { startCmd.Flags().StringVarP(&leaderElectionNamespace, "leader-election-namespace", "", "fluid-system", "The namespace in which the leader election resource will be created.") startCmd.Flags().BoolVarP(&development, "development", "", true, "Enable development mode for fluid controller.") startCmd.Flags().StringVar(&portRange, "runtime-node-port-range", "20000-25000", "Set available port range for GooseFS") + startCmd.Flags().StringVar(&portAllocatePolicy, "port-allocate-policy", "random", "Set port allocating policy, available choice is bitmap or random(default random).") startCmd.Flags().StringVarP(&pprofAddr, "pprof-addr", "", "", "The address for pprof to use while exporting profiling results") startCmd.Flags().IntVar(&maxConcurrentReconciles, "runtime-workers", 3, "Set max concurrent workers for GooseFSRuntime controller") cmd.AddCommand(startCmd) @@ -129,7 +131,12 @@ func handle() { } setupLog.Info("port range parsed", "port range", pr.String()) - portallocator.SetupRuntimePortAllocator(mgr.GetClient(), pr, goosefs.GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(mgr.GetClient(), pr, portAllocatePolicy, goosefs.GetReservedPorts) + if err != nil { + setupLog.Error(err, "failed to setup runtime port allocator") + os.Exit(1) + } + setupLog.Info("Set up runtime port allocator", "policy", portAllocatePolicy) setupLog.Info("starting goosefsruntime-controller") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { diff --git a/cmd/jindo/app/jindo.go b/cmd/jindo/app/jindo.go index c9c308e4177..25a2aa765d4 100644 --- a/cmd/jindo/app/jindo.go +++ b/cmd/jindo/app/jindo.go @@ -52,9 +52,10 @@ var ( development bool // The new mode eventDriven bool - portRange string maxConcurrentReconciles int pprofAddr string + portRange string + portAllocatePolicy string ) var jindoCmd = &cobra.Command{ @@ -74,6 +75,7 @@ func init() { jindoCmd.Flags().StringVarP(&leaderElectionNamespace, "leader-election-namespace", "", "fluid-system", "The namespace in which the leader election resource will be created.") jindoCmd.Flags().BoolVarP(&development, "development", "", true, "Enable development mode for fluid controller.") jindoCmd.Flags().StringVar(&portRange, "runtime-node-port-range", "18000-19999", "Set available port range for Jindo") + jindoCmd.Flags().StringVar(&portAllocatePolicy, "port-allocate-policy", "random", "Set port allocating policy, available choice is bitmap or random(default random).") jindoCmd.Flags().IntVar(&maxConcurrentReconciles, "runtime-workers", 3, "Set max concurrent workers for JindoRuntime controller") jindoCmd.Flags().BoolVar(&eventDriven, "event-driven", true, "The reconciler's loop strategy. if it's false, it indicates period driven.") jindoCmd.Flags().StringVarP(&pprofAddr, "pprof-addr", "", "", "The address for pprof to use while exporting profiling results") @@ -130,7 +132,12 @@ func handle() { } setupLog.Info("port range parsed", "port range", pr.String()) - portallocator.SetupRuntimePortAllocator(mgr.GetClient(), pr, jindo.GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(mgr.GetClient(), pr, portAllocatePolicy, jindo.GetReservedPorts) + if err != nil { + setupLog.Error(err, "failed to setup runtime port allocator") + os.Exit(1) + } + setupLog.Info("Set up runtime port allocator", "policy", portAllocatePolicy) setupLog.Info("starting jindoruntime-controller") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { From d9633cdc6804b2aabc153e690165f1c8f8f72b47 Mon Sep 17 00:00:00 2001 From: "dongyun.xzh" Date: Fri, 10 Feb 2023 16:28:29 +0800 Subject: [PATCH 3/7] Fix unit tests Signed-off-by: dongyun.xzh --- cmd/dataset/app/dataset.go | 2 +- .../v1alpha1/databackup/databackup_controller.go | 2 +- pkg/controllers/v1alpha1/dataload/dataload_controller.go | 2 +- pkg/controllers/v1alpha1/dataset/dataset_controller.go | 2 +- pkg/ddc/alluxio/master_internal_test.go | 4 ++-- pkg/ddc/alluxio/shutdown_engine_test.go | 2 +- pkg/ddc/alluxio/shutdown_test.go | 2 +- pkg/ddc/alluxio/transform_test.go | 2 +- pkg/ddc/base/portallocator/port_allocator_test.go | 6 +++--- pkg/ddc/eac/master_internal_test.go | 4 ++-- pkg/ddc/eac/shutdown_test.go | 2 +- pkg/ddc/eac/transform_port_test.go | 6 +++--- pkg/ddc/eac/transform_test.go | 2 +- pkg/ddc/goosefs/master_internal_test.go | 4 ++-- pkg/ddc/goosefs/shutdown_test.go | 2 +- pkg/ddc/jindo/master_internal_test.go | 4 ++-- pkg/ddc/jindofsx/master_internal_test.go | 4 ++-- pkg/ddc/jindofsx/transform_test.go | 2 +- 18 files changed, 27 insertions(+), 27 deletions(-) diff --git a/cmd/dataset/app/dataset.go b/cmd/dataset/app/dataset.go index 66b1b39094b..f4bcb8bfb26 100644 --- a/cmd/dataset/app/dataset.go +++ b/cmd/dataset/app/dataset.go @@ -28,7 +28,6 @@ import ( "github.com/fluid-cloudnative/fluid/pkg/ddc/alluxio" "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "github.com/fluid-cloudnative/fluid/pkg/utils" - "sigs.k8s.io/controller-runtime/pkg/controller" "github.com/spf13/cobra" zapOpt "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -36,6 +35,7 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/log/zap" ) diff --git a/pkg/controllers/v1alpha1/databackup/databackup_controller.go b/pkg/controllers/v1alpha1/databackup/databackup_controller.go index 4faec46ac4d..a4a04b576fe 100644 --- a/pkg/controllers/v1alpha1/databackup/databackup_controller.go +++ b/pkg/controllers/v1alpha1/databackup/databackup_controller.go @@ -27,7 +27,6 @@ import ( cdatabackup "github.com/fluid-cloudnative/fluid/pkg/databackup" "github.com/fluid-cloudnative/fluid/pkg/utils" "github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient" - "sigs.k8s.io/controller-runtime/pkg/controller" "github.com/go-logr/logr" "github.com/pkg/errors" v1 "k8s.io/api/core/v1" @@ -38,6 +37,7 @@ import ( "k8s.io/client-go/util/retry" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller" ) const controllerName string = "DataBackupController" diff --git a/pkg/controllers/v1alpha1/dataload/dataload_controller.go b/pkg/controllers/v1alpha1/dataload/dataload_controller.go index b5d552e816d..6a09df826e4 100644 --- a/pkg/controllers/v1alpha1/dataload/dataload_controller.go +++ b/pkg/controllers/v1alpha1/dataload/dataload_controller.go @@ -29,12 +29,12 @@ import ( cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime" "github.com/fluid-cloudnative/fluid/pkg/utils" "github.com/fluid-cloudnative/fluid/pkg/utils/jindo" - "sigs.k8s.io/controller-runtime/pkg/controller" "github.com/pkg/errors" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/controller" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" diff --git a/pkg/controllers/v1alpha1/dataset/dataset_controller.go b/pkg/controllers/v1alpha1/dataset/dataset_controller.go index 131a938c014..47ee550aefb 100644 --- a/pkg/controllers/v1alpha1/dataset/dataset_controller.go +++ b/pkg/controllers/v1alpha1/dataset/dataset_controller.go @@ -25,8 +25,8 @@ import ( "github.com/fluid-cloudnative/fluid/pkg/common" "github.com/fluid-cloudnative/fluid/pkg/controllers/deploy" "github.com/fluid-cloudnative/fluid/pkg/ddc/base" - "sigs.k8s.io/controller-runtime/pkg/controller" v1 "k8s.io/api/core/v1" + "sigs.k8s.io/controller-runtime/pkg/controller" "github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient" diff --git a/pkg/ddc/alluxio/master_internal_test.go b/pkg/ddc/alluxio/master_internal_test.go index 078a5ef4ec9..7a06efcabf3 100644 --- a/pkg/ddc/alluxio/master_internal_test.go +++ b/pkg/ddc/alluxio/master_internal_test.go @@ -113,7 +113,7 @@ func TestSetupMasterInternal(t *testing.T) { }, }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) @@ -239,7 +239,7 @@ func TestGenerateAlluxioValueFile(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) diff --git a/pkg/ddc/alluxio/shutdown_engine_test.go b/pkg/ddc/alluxio/shutdown_engine_test.go index 860d2ffbe21..358d2be9e73 100644 --- a/pkg/ddc/alluxio/shutdown_engine_test.go +++ b/pkg/ddc/alluxio/shutdown_engine_test.go @@ -127,7 +127,7 @@ func TestShutdown(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-21000") - portallocator.SetupRuntimePortAllocator(nil, pr, dummy) + portallocator.SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) var testCase = []struct { expectedWorkers int32 diff --git a/pkg/ddc/alluxio/shutdown_test.go b/pkg/ddc/alluxio/shutdown_test.go index c48caffeb81..f4e24e15024 100644 --- a/pkg/ddc/alluxio/shutdown_test.go +++ b/pkg/ddc/alluxio/shutdown_test.go @@ -335,7 +335,7 @@ func TestAlluxioEngineReleasePorts(t *testing.T) { Log: fake.NullLogger(), } - portallocator.SetupRuntimePortAllocator(client, pr, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) allocator, _ := portallocator.GetRuntimePortAllocator() patch1 := ApplyMethod(reflect.TypeOf(allocator), "ReleaseReservedPorts", func(_ *portallocator.RuntimePortAllocator, ports []int) { diff --git a/pkg/ddc/alluxio/transform_test.go b/pkg/ddc/alluxio/transform_test.go index aaff92b7488..732c015c703 100644 --- a/pkg/ddc/alluxio/transform_test.go +++ b/pkg/ddc/alluxio/transform_test.go @@ -647,7 +647,7 @@ func TestAlluxioEngine_allocateSinglePort(t *testing.T) { func TestAlluxioEngine_allocatePorts(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-21000") - portallocator.SetupRuntimePortAllocator(nil, pr, dummy) + portallocator.SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) type fields struct { runtime *datav1alpha1.AlluxioRuntime name string diff --git a/pkg/ddc/base/portallocator/port_allocator_test.go b/pkg/ddc/base/portallocator/port_allocator_test.go index 974675a63aa..339f57879da 100644 --- a/pkg/ddc/base/portallocator/port_allocator_test.go +++ b/pkg/ddc/base/portallocator/port_allocator_test.go @@ -33,7 +33,7 @@ var errDummy = func(client client.Client) (ports []int, err error) { func TestRuntimePortAllocatorWithError(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-21000") - SetupRuntimePortAllocator(nil, pr, errDummy) + SetupRuntimePortAllocator(nil, pr, "bitmap", errDummy) _, err := GetRuntimePortAllocator() if err == nil { @@ -43,7 +43,7 @@ func TestRuntimePortAllocatorWithError(t *testing.T) { func TestRuntimePortAllocator(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-21000") - SetupRuntimePortAllocator(nil, pr, dummy) + SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) allocator, err := GetRuntimePortAllocator() if err != nil { @@ -71,7 +71,7 @@ func TestRuntimePortAllocator(t *testing.T) { func TestRuntimePortAllocatorRelease(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-20010") - SetupRuntimePortAllocator(nil, pr, dummy) + SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) preservedPorts, _ := dummy(nil) diff --git a/pkg/ddc/eac/master_internal_test.go b/pkg/ddc/eac/master_internal_test.go index a3aca796a95..be6513273ef 100644 --- a/pkg/ddc/eac/master_internal_test.go +++ b/pkg/ddc/eac/master_internal_test.go @@ -117,7 +117,7 @@ func TestSetupMasterInternal(t *testing.T) { runtime: eacruntime, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) // check release found err := gohook.Hook(helm.CheckRelease, mockExecCheckReleaseCommonFound, nil) @@ -244,7 +244,7 @@ func TestGenerateEACValueFile(t *testing.T) { runtime: eacruntime, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockCreateConfigMap, nil) if err != nil { diff --git a/pkg/ddc/eac/shutdown_test.go b/pkg/ddc/eac/shutdown_test.go index 658ceba17d5..fc2e150dce1 100644 --- a/pkg/ddc/eac/shutdown_test.go +++ b/pkg/ddc/eac/shutdown_test.go @@ -311,7 +311,7 @@ func TestEACEngineReleasePorts(t *testing.T) { Log: fake.NullLogger(), } - portallocator.SetupRuntimePortAllocator(client, pr, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) allocator, _ := portallocator.GetRuntimePortAllocator() patch1 := ApplyMethod(reflect.TypeOf(allocator), "ReleaseReservedPorts", func(_ *portallocator.RuntimePortAllocator, ports []int) { diff --git a/pkg/ddc/eac/transform_port_test.go b/pkg/ddc/eac/transform_port_test.go index 80aaa74d920..9e09dc8e723 100644 --- a/pkg/ddc/eac/transform_port_test.go +++ b/pkg/ddc/eac/transform_port_test.go @@ -35,7 +35,7 @@ func TestTransformPortForMaster(t *testing.T) { for _, test := range tests { client := fake.NewFakeClientWithScheme(testScheme, test.runtime.DeepCopy()) engine := &EACEngine{Log: fake.NullLogger()} - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) err := engine.transformPortForMaster(test.runtime, test.eacValue) if err != nil { t.Errorf("unexpected err %v", err) @@ -70,7 +70,7 @@ func TestTransformPortForFuse(t *testing.T) { for _, test := range tests { client := fake.NewFakeClientWithScheme(testScheme, test.runtime.DeepCopy()) engine := &EACEngine{Log: fake.NullLogger()} - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) err := engine.transformPortForFuse(test.runtime, test.eacValue) if err != nil { t.Errorf("unexpected err %v", err) @@ -105,7 +105,7 @@ func TestTransformPortForWorker(t *testing.T) { for _, test := range tests { client := fake.NewFakeClientWithScheme(testScheme, test.runtime.DeepCopy()) engine := &EACEngine{Log: fake.NullLogger()} - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) err := engine.transformPortForWorker(test.runtime, test.eacValue) if err != nil { t.Errorf("unexpected err %v", err) diff --git a/pkg/ddc/eac/transform_test.go b/pkg/ddc/eac/transform_test.go index dab35335f75..68375ef8c6f 100644 --- a/pkg/ddc/eac/transform_test.go +++ b/pkg/ddc/eac/transform_test.go @@ -59,7 +59,7 @@ func TestEACEngine_transform(t *testing.T) { ctrl.SetLogger(zap.New(func(o *zap.Options) { o.Development = true })) - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) _, err := engine.transform(test.runtime) if err != nil { t.Errorf("error %v", err) diff --git a/pkg/ddc/goosefs/master_internal_test.go b/pkg/ddc/goosefs/master_internal_test.go index 852b7cb0377..ec669115e28 100644 --- a/pkg/ddc/goosefs/master_internal_test.go +++ b/pkg/ddc/goosefs/master_internal_test.go @@ -184,7 +184,7 @@ func TestSetupMasterInternal(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) @@ -400,7 +400,7 @@ func TestGenerateGooseFSValueFile(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) diff --git a/pkg/ddc/goosefs/shutdown_test.go b/pkg/ddc/goosefs/shutdown_test.go index 8fadb2740d4..0c3efd9f1cf 100644 --- a/pkg/ddc/goosefs/shutdown_test.go +++ b/pkg/ddc/goosefs/shutdown_test.go @@ -467,7 +467,7 @@ func TestGooseFSEngineReleasePorts(t *testing.T) { Log: fake.NullLogger(), } - portallocator.SetupRuntimePortAllocator(client, pr, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) allocator, _ := portallocator.GetRuntimePortAllocator() patch1 := ApplyMethod(reflect.TypeOf(allocator), "ReleaseReservedPorts", func(_ *portallocator.RuntimePortAllocator, ports []int) { diff --git a/pkg/ddc/jindo/master_internal_test.go b/pkg/ddc/jindo/master_internal_test.go index 0d0f8983318..7722642b92e 100644 --- a/pkg/ddc/jindo/master_internal_test.go +++ b/pkg/ddc/jindo/master_internal_test.go @@ -111,7 +111,7 @@ func TestSetupMasterInternal(t *testing.T) { }, }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) @@ -234,7 +234,7 @@ func TestGenerateJindoValueFile(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) diff --git a/pkg/ddc/jindofsx/master_internal_test.go b/pkg/ddc/jindofsx/master_internal_test.go index 1e471be02b0..ce95f11ed40 100644 --- a/pkg/ddc/jindofsx/master_internal_test.go +++ b/pkg/ddc/jindofsx/master_internal_test.go @@ -111,7 +111,7 @@ func TestSetupMasterInternal(t *testing.T) { }, }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) @@ -234,7 +234,7 @@ func TestGenerateJindoValueFile(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) diff --git a/pkg/ddc/jindofsx/transform_test.go b/pkg/ddc/jindofsx/transform_test.go index 831e16574e8..8f9416200ef 100644 --- a/pkg/ddc/jindofsx/transform_test.go +++ b/pkg/ddc/jindofsx/transform_test.go @@ -576,7 +576,7 @@ func TestJindoFSxEngine_transform(t *testing.T) { Log: fake.NullLogger(), } tt.args.runtime = tt.fields.runtime - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, GetReservedPorts) + portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) _, err := e.transform(tt.args.runtime) if (err != nil) != tt.wantErr { t.Errorf("JindoFSxEngine.transform() error = %v, wantErr %v", err, tt.wantErr) From b9bcb29ef1f044c036f564325ae9427396494dcf Mon Sep 17 00:00:00 2001 From: "dongyun.xzh" Date: Fri, 10 Feb 2023 16:35:47 +0800 Subject: [PATCH 4/7] Fix static check Signed-off-by: dongyun.xzh --- pkg/ddc/base/portallocator/port_allocator.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/ddc/base/portallocator/port_allocator.go b/pkg/ddc/base/portallocator/port_allocator.go index c7876476364..70ee255050c 100644 --- a/pkg/ddc/base/portallocator/port_allocator.go +++ b/pkg/ddc/base/portallocator/port_allocator.go @@ -106,7 +106,11 @@ func (alloc *RuntimePortAllocator) createAndRestorePortAllocator() (err error) { case BitMap: alloc.pa, err = newBitMapAllocator(alloc.pr, alloc.log) default: - return errors.New("runtime-port-allocator can only be random or bitmap") + err = errors.New("runtime-port-allocator can only be random or bitmap") + } + + if err != nil { + return err } // policy should check reserved ports From 4a7479cbfb0916cac88efb570a4545b8c7149996 Mon Sep 17 00:00:00 2001 From: "dongyun.xzh" Date: Fri, 10 Feb 2023 17:03:57 +0800 Subject: [PATCH 5/7] Fix static check Signed-off-by: dongyun.xzh --- pkg/ddc/alluxio/master_internal_test.go | 14 +++++++++---- pkg/ddc/alluxio/shutdown_engine_test.go | 5 ++++- pkg/ddc/alluxio/shutdown_test.go | 5 ++++- pkg/ddc/alluxio/transform_test.go | 5 ++++- .../base/portallocator/port_allocator_test.go | 19 +++++++++++++---- pkg/ddc/eac/master_internal_test.go | 15 ++++++++----- pkg/ddc/eac/shutdown_test.go | 5 ++++- pkg/ddc/eac/transform_port_test.go | 21 +++++++++++++------ pkg/ddc/eac/transform_test.go | 7 +++++-- pkg/ddc/goosefs/master_internal_test.go | 15 ++++++++----- pkg/ddc/goosefs/shutdown_test.go | 5 ++++- pkg/ddc/jindo/master_internal_test.go | 14 +++++++++---- pkg/ddc/jindofsx/master_internal_test.go | 14 +++++++++---- pkg/ddc/jindofsx/transform_test.go | 7 +++++-- 14 files changed, 110 insertions(+), 41 deletions(-) diff --git a/pkg/ddc/alluxio/master_internal_test.go b/pkg/ddc/alluxio/master_internal_test.go index 7a06efcabf3..02eb662b20b 100644 --- a/pkg/ddc/alluxio/master_internal_test.go +++ b/pkg/ddc/alluxio/master_internal_test.go @@ -113,8 +113,11 @@ func TestSetupMasterInternal(t *testing.T) { }, }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) } @@ -239,8 +242,11 @@ func TestGenerateAlluxioValueFile(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/alluxio/shutdown_engine_test.go b/pkg/ddc/alluxio/shutdown_engine_test.go index 358d2be9e73..e7a7f9b18a5 100644 --- a/pkg/ddc/alluxio/shutdown_engine_test.go +++ b/pkg/ddc/alluxio/shutdown_engine_test.go @@ -127,7 +127,10 @@ func TestShutdown(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-21000") - portallocator.SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) + err = portallocator.SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) + if err != nil { + t.Fatalf("failed to set up runtime port allocator due to %v", err) + } var testCase = []struct { expectedWorkers int32 diff --git a/pkg/ddc/alluxio/shutdown_test.go b/pkg/ddc/alluxio/shutdown_test.go index f4e24e15024..665ee637107 100644 --- a/pkg/ddc/alluxio/shutdown_test.go +++ b/pkg/ddc/alluxio/shutdown_test.go @@ -335,7 +335,10 @@ func TestAlluxioEngineReleasePorts(t *testing.T) { Log: fake.NullLogger(), } - portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) + err := portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } allocator, _ := portallocator.GetRuntimePortAllocator() patch1 := ApplyMethod(reflect.TypeOf(allocator), "ReleaseReservedPorts", func(_ *portallocator.RuntimePortAllocator, ports []int) { diff --git a/pkg/ddc/alluxio/transform_test.go b/pkg/ddc/alluxio/transform_test.go index 732c015c703..86b61c51485 100644 --- a/pkg/ddc/alluxio/transform_test.go +++ b/pkg/ddc/alluxio/transform_test.go @@ -647,7 +647,10 @@ func TestAlluxioEngine_allocateSinglePort(t *testing.T) { func TestAlluxioEngine_allocatePorts(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-21000") - portallocator.SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) + err := portallocator.SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) + if err != nil { + t.Fatal(err.Error()) + } type fields struct { runtime *datav1alpha1.AlluxioRuntime name string diff --git a/pkg/ddc/base/portallocator/port_allocator_test.go b/pkg/ddc/base/portallocator/port_allocator_test.go index 339f57879da..6b07cf76757 100644 --- a/pkg/ddc/base/portallocator/port_allocator_test.go +++ b/pkg/ddc/base/portallocator/port_allocator_test.go @@ -33,9 +33,12 @@ var errDummy = func(client client.Client) (ports []int, err error) { func TestRuntimePortAllocatorWithError(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-21000") - SetupRuntimePortAllocator(nil, pr, "bitmap", errDummy) + err := SetupRuntimePortAllocator(nil, pr, "bitmap", errDummy) + if err != nil { + t.Fatalf("failed to setup runtime port allocator due to %v", err) + } - _, err := GetRuntimePortAllocator() + _, err = GetRuntimePortAllocator() if err == nil { t.Errorf("Expecetd error when GetRuntimePortAllocator") } @@ -43,7 +46,11 @@ func TestRuntimePortAllocatorWithError(t *testing.T) { func TestRuntimePortAllocator(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-21000") - SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) + err := SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) + if err != nil { + t.Errorf("get non-nil err when GetRuntimePortAllocator") + return + } allocator, err := GetRuntimePortAllocator() if err != nil { @@ -71,7 +78,11 @@ func TestRuntimePortAllocator(t *testing.T) { func TestRuntimePortAllocatorRelease(t *testing.T) { pr := net.ParsePortRangeOrDie("20000-20010") - SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) + err := SetupRuntimePortAllocator(nil, pr, "bitmap", dummy) + if err != nil { + t.Errorf("get non-nil err when GetRuntimePortAllocator") + return + } preservedPorts, _ := dummy(nil) diff --git a/pkg/ddc/eac/master_internal_test.go b/pkg/ddc/eac/master_internal_test.go index be6513273ef..abaafc68806 100644 --- a/pkg/ddc/eac/master_internal_test.go +++ b/pkg/ddc/eac/master_internal_test.go @@ -117,10 +117,12 @@ func TestSetupMasterInternal(t *testing.T) { runtime: eacruntime, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } // check release found - err := gohook.Hook(helm.CheckRelease, mockExecCheckReleaseCommonFound, nil) + err = gohook.Hook(helm.CheckRelease, mockExecCheckReleaseCommonFound, nil) if err != nil { t.Fatal(err.Error()) } @@ -244,9 +246,12 @@ func TestGenerateEACValueFile(t *testing.T) { runtime: eacruntime, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockCreateConfigMap, nil) + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockCreateConfigMap, nil) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/eac/shutdown_test.go b/pkg/ddc/eac/shutdown_test.go index fc2e150dce1..0a256b0abdb 100644 --- a/pkg/ddc/eac/shutdown_test.go +++ b/pkg/ddc/eac/shutdown_test.go @@ -311,7 +311,10 @@ func TestEACEngineReleasePorts(t *testing.T) { Log: fake.NullLogger(), } - portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) + err := portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) + if err != nil { + t.Fatalf("failed to set up runtime port allocator due to %v", err) + } allocator, _ := portallocator.GetRuntimePortAllocator() patch1 := ApplyMethod(reflect.TypeOf(allocator), "ReleaseReservedPorts", func(_ *portallocator.RuntimePortAllocator, ports []int) { diff --git a/pkg/ddc/eac/transform_port_test.go b/pkg/ddc/eac/transform_port_test.go index 9e09dc8e723..56a03cd346d 100644 --- a/pkg/ddc/eac/transform_port_test.go +++ b/pkg/ddc/eac/transform_port_test.go @@ -35,8 +35,11 @@ func TestTransformPortForMaster(t *testing.T) { for _, test := range tests { client := fake.NewFakeClientWithScheme(testScheme, test.runtime.DeepCopy()) engine := &EACEngine{Log: fake.NullLogger()} - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - err := engine.transformPortForMaster(test.runtime, test.eacValue) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatalf("failed to set up runtime port allocator due to %v", err) + } + err = engine.transformPortForMaster(test.runtime, test.eacValue) if err != nil { t.Errorf("unexpected err %v", err) } @@ -70,8 +73,11 @@ func TestTransformPortForFuse(t *testing.T) { for _, test := range tests { client := fake.NewFakeClientWithScheme(testScheme, test.runtime.DeepCopy()) engine := &EACEngine{Log: fake.NullLogger()} - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - err := engine.transformPortForFuse(test.runtime, test.eacValue) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = engine.transformPortForFuse(test.runtime, test.eacValue) if err != nil { t.Errorf("unexpected err %v", err) } @@ -105,8 +111,11 @@ func TestTransformPortForWorker(t *testing.T) { for _, test := range tests { client := fake.NewFakeClientWithScheme(testScheme, test.runtime.DeepCopy()) engine := &EACEngine{Log: fake.NullLogger()} - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - err := engine.transformPortForWorker(test.runtime, test.eacValue) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = engine.transformPortForWorker(test.runtime, test.eacValue) if err != nil { t.Errorf("unexpected err %v", err) } diff --git a/pkg/ddc/eac/transform_test.go b/pkg/ddc/eac/transform_test.go index 68375ef8c6f..88ed75b5e6f 100644 --- a/pkg/ddc/eac/transform_test.go +++ b/pkg/ddc/eac/transform_test.go @@ -59,8 +59,11 @@ func TestEACEngine_transform(t *testing.T) { ctrl.SetLogger(zap.New(func(o *zap.Options) { o.Development = true })) - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - _, err := engine.transform(test.runtime) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + _, err = engine.transform(test.runtime) if err != nil { t.Errorf("error %v", err) } diff --git a/pkg/ddc/goosefs/master_internal_test.go b/pkg/ddc/goosefs/master_internal_test.go index ec669115e28..f9020c29c3b 100644 --- a/pkg/ddc/goosefs/master_internal_test.go +++ b/pkg/ddc/goosefs/master_internal_test.go @@ -184,9 +184,12 @@ func TestSetupMasterInternal(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { @@ -400,9 +403,11 @@ func TestGenerateGooseFSValueFile(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) - - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { diff --git a/pkg/ddc/goosefs/shutdown_test.go b/pkg/ddc/goosefs/shutdown_test.go index 0c3efd9f1cf..4f133c8e80c 100644 --- a/pkg/ddc/goosefs/shutdown_test.go +++ b/pkg/ddc/goosefs/shutdown_test.go @@ -467,7 +467,10 @@ func TestGooseFSEngineReleasePorts(t *testing.T) { Log: fake.NullLogger(), } - portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) + err := portallocator.SetupRuntimePortAllocator(client, pr, "bitmap", GetReservedPorts) + if err != nil { + t.Fatalf("Failed to set up runtime port allocator due to %v", err) + } allocator, _ := portallocator.GetRuntimePortAllocator() patch1 := ApplyMethod(reflect.TypeOf(allocator), "ReleaseReservedPorts", func(_ *portallocator.RuntimePortAllocator, ports []int) { diff --git a/pkg/ddc/jindo/master_internal_test.go b/pkg/ddc/jindo/master_internal_test.go index 7722642b92e..56402e4c9ee 100644 --- a/pkg/ddc/jindo/master_internal_test.go +++ b/pkg/ddc/jindo/master_internal_test.go @@ -111,8 +111,11 @@ func TestSetupMasterInternal(t *testing.T) { }, }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) } @@ -234,8 +237,11 @@ func TestGenerateJindoValueFile(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/jindofsx/master_internal_test.go b/pkg/ddc/jindofsx/master_internal_test.go index ce95f11ed40..b2acf82b148 100644 --- a/pkg/ddc/jindofsx/master_internal_test.go +++ b/pkg/ddc/jindofsx/master_internal_test.go @@ -111,8 +111,11 @@ func TestSetupMasterInternal(t *testing.T) { }, }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) } @@ -234,8 +237,11 @@ func TestGenerateJindoValueFile(t *testing.T) { }, } - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) - err := gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatal(err.Error()) + } + err = gohook.Hook(kubectl.CreateConfigMapFromFile, mockExecCreateConfigMapFromFileErr, nil) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/jindofsx/transform_test.go b/pkg/ddc/jindofsx/transform_test.go index 8f9416200ef..ef5daad9632 100644 --- a/pkg/ddc/jindofsx/transform_test.go +++ b/pkg/ddc/jindofsx/transform_test.go @@ -576,8 +576,11 @@ func TestJindoFSxEngine_transform(t *testing.T) { Log: fake.NullLogger(), } tt.args.runtime = tt.fields.runtime - portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) - _, err := e.transform(tt.args.runtime) + err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + if err != nil { + t.Fatalf("failed to set up runtime port allocator due to %v", err) + } + _, err = e.transform(tt.args.runtime) if (err != nil) != tt.wantErr { t.Errorf("JindoFSxEngine.transform() error = %v, wantErr %v", err, tt.wantErr) return From acdfb895978876d906a21ee501de4c469eb5321c Mon Sep 17 00:00:00 2001 From: "dongyun.xzh" Date: Mon, 13 Feb 2023 10:28:53 +0800 Subject: [PATCH 6/7] Fix comments for `SetupRuntimePortAllocator()` Signed-off-by: dongyun.xzh --- pkg/ddc/base/portallocator/port_allocator.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/ddc/base/portallocator/port_allocator.go b/pkg/ddc/base/portallocator/port_allocator.go index 70ee255050c..dcb297ebc07 100644 --- a/pkg/ddc/base/portallocator/port_allocator.go +++ b/pkg/ddc/base/portallocator/port_allocator.go @@ -71,7 +71,8 @@ type RuntimePortAllocator struct { // rpa is a global singleton of type RuntimePortAllocator var rpa *RuntimePortAllocator -// SetupRuntimePortAllocator instantiates the global singleton rpa, use BitMap port allocating policy +// SetupRuntimePortAllocator instantiates the global singleton rpa, set up port allocating policy according to the given allocatePolicyStr. +// Currently the valid policies are either "random" or "bitmap". func SetupRuntimePortAllocator(client client.Client, pr *net.PortRange, allocatePolicyStr string, getReservedPorts func(client client.Client) (ports []int, err error)) error { policy, err := ValidateEnum(allocatePolicyStr) if err != nil { From c75fc4dc3b1805779a293fc38bd624f4813d9eb7 Mon Sep 17 00:00:00 2001 From: "dongyun.xzh" Date: Mon, 13 Feb 2023 10:39:36 +0800 Subject: [PATCH 7/7] Support configurable port allocate policy Signed-off-by: dongyun.xzh --- .../fluid/templates/controller/goosefsruntime_controller.yaml | 1 + .../fluid/templates/controller/jindoruntime_controller.yaml | 1 + charts/fluid/fluid/values.yaml | 4 +++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/charts/fluid/fluid/templates/controller/goosefsruntime_controller.yaml b/charts/fluid/fluid/templates/controller/goosefsruntime_controller.yaml index e03ad2f4c15..00287a8d21e 100644 --- a/charts/fluid/fluid/templates/controller/goosefsruntime_controller.yaml +++ b/charts/fluid/fluid/templates/controller/goosefsruntime_controller.yaml @@ -52,6 +52,7 @@ spec: - --runtime-workers={{ .Values.runtime.goosefs.runtimeWorkers }} - --pprof-addr=:6060 - --enable-leader-election + - --port-allocate-policy={{ .Values.runtime.goosefs.portAllocatePolicy }} env: {{- if .Values.workdir }} - name: FLUID_WORKDIR diff --git a/charts/fluid/fluid/templates/controller/jindoruntime_controller.yaml b/charts/fluid/fluid/templates/controller/jindoruntime_controller.yaml index 702efb8c58d..aed70ca866a 100644 --- a/charts/fluid/fluid/templates/controller/jindoruntime_controller.yaml +++ b/charts/fluid/fluid/templates/controller/jindoruntime_controller.yaml @@ -50,6 +50,7 @@ spec: - --runtime-workers={{ .Values.runtime.jindo.runtimeWorkers }} - --pprof-addr=:6060 - --enable-leader-election + - --port-allocate-policy={{ .Values.runtime.jindo.portAllocatePolicy }} env: {{- if .Values.workdir }} - name: FLUID_WORKDIR diff --git a/charts/fluid/fluid/values.yaml b/charts/fluid/fluid/values.yaml index 3ad619a9dd9..989b439fd05 100644 --- a/charts/fluid/fluid/values.yaml +++ b/charts/fluid/fluid/values.yaml @@ -34,7 +34,7 @@ runtime: replicas: 1 runtimeWorkers: 3 portRange: 20000-26000 - portAllocatePolicy: bitmap + portAllocatePolicy: random enabled: false init: image: fluidcloudnative/init-users:v0.9.0-fcf2004 @@ -48,6 +48,7 @@ runtime: replicas: 1 runtimeWorkers: 3 portRange: 18000-19999 + portAllocatePolicy: random enabled: false engine: jindofsx queryUfsTotal: true @@ -65,6 +66,7 @@ runtime: replicas: 1 runtimeWorkers: 3 portRange: 26000-32000 + portAllocatePolicy: random enabled: false init: image: fluidcloudnative/init-users:v0.9.0-fcf2004