From 557ea0f42daf76363e6d5b7ad05e1d0b92efb232 Mon Sep 17 00:00:00 2001 From: YamasouA Date: Sun, 21 Apr 2024 22:42:17 +0900 Subject: [PATCH 1/4] move simulator entrypoint to simulator/cmd/simulator --- simulator/Makefile | 2 +- simulator/{ => cmd/simulator}/config.yaml | 0 simulator/{ => cmd/simulator}/config/config.go | 2 +- simulator/{ => cmd/simulator}/config/config_test.go | 0 simulator/{ => cmd/simulator}/config/v1alpha1/defaults.go | 0 simulator/{ => cmd/simulator}/config/v1alpha1/doc.go | 0 simulator/{ => cmd/simulator}/config/v1alpha1/register.go | 0 simulator/{ => cmd/simulator}/config/v1alpha1/types.go | 0 .../simulator}/config/v1alpha1/zz_generated.conversion.go | 0 .../simulator}/config/v1alpha1/zz_generated.deepcopy.go | 0 .../simulator}/config/v1alpha1/zz_generated.defaults.go | 0 simulator/cmd/simulator/simulator.go | 2 +- simulator/pkg/debuggablescheduler/debuggable_scheduler.go | 2 +- simulator/pkg/externalscheduler/external_scheduler.go | 2 +- simulator/server/server.go | 2 +- 15 files changed, 6 insertions(+), 6 deletions(-) rename simulator/{ => cmd/simulator}/config.yaml (100%) rename simulator/{ => cmd/simulator}/config/config.go (99%) rename simulator/{ => cmd/simulator}/config/config_test.go (100%) rename simulator/{ => cmd/simulator}/config/v1alpha1/defaults.go (100%) rename simulator/{ => cmd/simulator}/config/v1alpha1/doc.go (100%) rename simulator/{ => cmd/simulator}/config/v1alpha1/register.go (100%) rename simulator/{ => cmd/simulator}/config/v1alpha1/types.go (100%) rename simulator/{ => cmd/simulator}/config/v1alpha1/zz_generated.conversion.go (100%) rename simulator/{ => cmd/simulator}/config/v1alpha1/zz_generated.deepcopy.go (100%) rename simulator/{ => cmd/simulator}/config/v1alpha1/zz_generated.defaults.go (100%) diff --git a/simulator/Makefile b/simulator/Makefile index dbdc2872..8a61cee1 100644 --- a/simulator/Makefile +++ b/simulator/Makefile @@ -23,4 +23,4 @@ mod-download: ## Downloads the Go module .PHONY: build build: go build -o ./bin/simulator ./cmd/simulator/simulator.go - go build -o ./bin/scheduler ./cmd/scheduler/scheduler.go + go build -o ./bin/scheduler ./cmd/scheduler/scheduler.go \ No newline at end of file diff --git a/simulator/config.yaml b/simulator/cmd/simulator/config.yaml similarity index 100% rename from simulator/config.yaml rename to simulator/cmd/simulator/config.yaml diff --git a/simulator/config/config.go b/simulator/cmd/simulator/config/config.go similarity index 99% rename from simulator/config/config.go rename to simulator/cmd/simulator/config/config.go index 6a09618c..37ee0138 100644 --- a/simulator/config/config.go +++ b/simulator/cmd/simulator/config/config.go @@ -15,7 +15,7 @@ import ( configv1 "k8s.io/kube-scheduler/config/v1" "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme" - "sigs.k8s.io/kube-scheduler-simulator/simulator/config/v1alpha1" + "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config/v1alpha1" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/config" ) diff --git a/simulator/config/config_test.go b/simulator/cmd/simulator/config/config_test.go similarity index 100% rename from simulator/config/config_test.go rename to simulator/cmd/simulator/config/config_test.go diff --git a/simulator/config/v1alpha1/defaults.go b/simulator/cmd/simulator/config/v1alpha1/defaults.go similarity index 100% rename from simulator/config/v1alpha1/defaults.go rename to simulator/cmd/simulator/config/v1alpha1/defaults.go diff --git a/simulator/config/v1alpha1/doc.go b/simulator/cmd/simulator/config/v1alpha1/doc.go similarity index 100% rename from simulator/config/v1alpha1/doc.go rename to simulator/cmd/simulator/config/v1alpha1/doc.go diff --git a/simulator/config/v1alpha1/register.go b/simulator/cmd/simulator/config/v1alpha1/register.go similarity index 100% rename from simulator/config/v1alpha1/register.go rename to simulator/cmd/simulator/config/v1alpha1/register.go diff --git a/simulator/config/v1alpha1/types.go b/simulator/cmd/simulator/config/v1alpha1/types.go similarity index 100% rename from simulator/config/v1alpha1/types.go rename to simulator/cmd/simulator/config/v1alpha1/types.go diff --git a/simulator/config/v1alpha1/zz_generated.conversion.go b/simulator/cmd/simulator/config/v1alpha1/zz_generated.conversion.go similarity index 100% rename from simulator/config/v1alpha1/zz_generated.conversion.go rename to simulator/cmd/simulator/config/v1alpha1/zz_generated.conversion.go diff --git a/simulator/config/v1alpha1/zz_generated.deepcopy.go b/simulator/cmd/simulator/config/v1alpha1/zz_generated.deepcopy.go similarity index 100% rename from simulator/config/v1alpha1/zz_generated.deepcopy.go rename to simulator/cmd/simulator/config/v1alpha1/zz_generated.deepcopy.go diff --git a/simulator/config/v1alpha1/zz_generated.defaults.go b/simulator/cmd/simulator/config/v1alpha1/zz_generated.defaults.go similarity index 100% rename from simulator/config/v1alpha1/zz_generated.defaults.go rename to simulator/cmd/simulator/config/v1alpha1/zz_generated.defaults.go diff --git a/simulator/cmd/simulator/simulator.go b/simulator/cmd/simulator/simulator.go index 4b2ca159..57fc08ef 100644 --- a/simulator/cmd/simulator/simulator.go +++ b/simulator/cmd/simulator/simulator.go @@ -19,7 +19,7 @@ import ( "k8s.io/client-go/restmapper" "k8s.io/klog/v2" - "sigs.k8s.io/kube-scheduler-simulator/simulator/config" + "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/server" "sigs.k8s.io/kube-scheduler-simulator/simulator/server/di" "sigs.k8s.io/kube-scheduler-simulator/simulator/syncer" diff --git a/simulator/pkg/debuggablescheduler/debuggable_scheduler.go b/simulator/pkg/debuggablescheduler/debuggable_scheduler.go index 57aaf8ef..91e9b36a 100644 --- a/simulator/pkg/debuggablescheduler/debuggable_scheduler.go +++ b/simulator/pkg/debuggablescheduler/debuggable_scheduler.go @@ -20,7 +20,7 @@ import ( configv1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1" "k8s.io/kubernetes/pkg/scheduler/framework/runtime" - simulatorconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/config" + simulatorconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler" simulatorschedulerconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/extender" diff --git a/simulator/pkg/externalscheduler/external_scheduler.go b/simulator/pkg/externalscheduler/external_scheduler.go index fa2e6ed4..e1317a34 100644 --- a/simulator/pkg/externalscheduler/external_scheduler.go +++ b/simulator/pkg/externalscheduler/external_scheduler.go @@ -20,7 +20,7 @@ import ( configv1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1" "k8s.io/kubernetes/pkg/scheduler/framework/runtime" - simulatorconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/config" + simulatorconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler" simulatorschedulerconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/plugin" diff --git a/simulator/server/server.go b/simulator/server/server.go index 01f2d080..4d1d98ec 100644 --- a/simulator/server/server.go +++ b/simulator/server/server.go @@ -11,7 +11,7 @@ import ( "github.com/labstack/echo/v4/middleware" "github.com/labstack/gommon/log" - "sigs.k8s.io/kube-scheduler-simulator/simulator/config" + "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/server/di" "sigs.k8s.io/kube-scheduler-simulator/simulator/server/handler" ) From e12570ed3b54844a39fa0f60cb784eb27153ad62 Mon Sep 17 00:00:00 2001 From: YamasouA Date: Mon, 22 Apr 2024 23:53:23 +0900 Subject: [PATCH 2/4] fix config position --- simulator/cmd/simulator/simulator.go | 2 +- simulator/{cmd/simulator => }/config.yaml | 0 simulator/{cmd/simulator => }/config/config.go | 2 +- simulator/{cmd/simulator => }/config/config_test.go | 0 simulator/{cmd/simulator => }/config/v1alpha1/defaults.go | 0 simulator/{cmd/simulator => }/config/v1alpha1/doc.go | 0 simulator/{cmd/simulator => }/config/v1alpha1/register.go | 0 simulator/{cmd/simulator => }/config/v1alpha1/types.go | 0 .../simulator => }/config/v1alpha1/zz_generated.conversion.go | 0 .../simulator => }/config/v1alpha1/zz_generated.deepcopy.go | 0 .../simulator => }/config/v1alpha1/zz_generated.defaults.go | 0 simulator/pkg/debuggablescheduler/debuggable_scheduler.go | 2 +- simulator/pkg/externalscheduler/external_scheduler.go | 2 +- simulator/server/server.go | 2 +- 14 files changed, 5 insertions(+), 5 deletions(-) rename simulator/{cmd/simulator => }/config.yaml (100%) rename simulator/{cmd/simulator => }/config/config.go (99%) rename simulator/{cmd/simulator => }/config/config_test.go (100%) rename simulator/{cmd/simulator => }/config/v1alpha1/defaults.go (100%) rename simulator/{cmd/simulator => }/config/v1alpha1/doc.go (100%) rename simulator/{cmd/simulator => }/config/v1alpha1/register.go (100%) rename simulator/{cmd/simulator => }/config/v1alpha1/types.go (100%) rename simulator/{cmd/simulator => }/config/v1alpha1/zz_generated.conversion.go (100%) rename simulator/{cmd/simulator => }/config/v1alpha1/zz_generated.deepcopy.go (100%) rename simulator/{cmd/simulator => }/config/v1alpha1/zz_generated.defaults.go (100%) diff --git a/simulator/cmd/simulator/simulator.go b/simulator/cmd/simulator/simulator.go index 57fc08ef..4b2ca159 100644 --- a/simulator/cmd/simulator/simulator.go +++ b/simulator/cmd/simulator/simulator.go @@ -19,7 +19,7 @@ import ( "k8s.io/client-go/restmapper" "k8s.io/klog/v2" - "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config" + "sigs.k8s.io/kube-scheduler-simulator/simulator/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/server" "sigs.k8s.io/kube-scheduler-simulator/simulator/server/di" "sigs.k8s.io/kube-scheduler-simulator/simulator/syncer" diff --git a/simulator/cmd/simulator/config.yaml b/simulator/config.yaml similarity index 100% rename from simulator/cmd/simulator/config.yaml rename to simulator/config.yaml diff --git a/simulator/cmd/simulator/config/config.go b/simulator/config/config.go similarity index 99% rename from simulator/cmd/simulator/config/config.go rename to simulator/config/config.go index 37ee0138..6a09618c 100644 --- a/simulator/cmd/simulator/config/config.go +++ b/simulator/config/config.go @@ -15,7 +15,7 @@ import ( configv1 "k8s.io/kube-scheduler/config/v1" "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme" - "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config/v1alpha1" + "sigs.k8s.io/kube-scheduler-simulator/simulator/config/v1alpha1" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/config" ) diff --git a/simulator/cmd/simulator/config/config_test.go b/simulator/config/config_test.go similarity index 100% rename from simulator/cmd/simulator/config/config_test.go rename to simulator/config/config_test.go diff --git a/simulator/cmd/simulator/config/v1alpha1/defaults.go b/simulator/config/v1alpha1/defaults.go similarity index 100% rename from simulator/cmd/simulator/config/v1alpha1/defaults.go rename to simulator/config/v1alpha1/defaults.go diff --git a/simulator/cmd/simulator/config/v1alpha1/doc.go b/simulator/config/v1alpha1/doc.go similarity index 100% rename from simulator/cmd/simulator/config/v1alpha1/doc.go rename to simulator/config/v1alpha1/doc.go diff --git a/simulator/cmd/simulator/config/v1alpha1/register.go b/simulator/config/v1alpha1/register.go similarity index 100% rename from simulator/cmd/simulator/config/v1alpha1/register.go rename to simulator/config/v1alpha1/register.go diff --git a/simulator/cmd/simulator/config/v1alpha1/types.go b/simulator/config/v1alpha1/types.go similarity index 100% rename from simulator/cmd/simulator/config/v1alpha1/types.go rename to simulator/config/v1alpha1/types.go diff --git a/simulator/cmd/simulator/config/v1alpha1/zz_generated.conversion.go b/simulator/config/v1alpha1/zz_generated.conversion.go similarity index 100% rename from simulator/cmd/simulator/config/v1alpha1/zz_generated.conversion.go rename to simulator/config/v1alpha1/zz_generated.conversion.go diff --git a/simulator/cmd/simulator/config/v1alpha1/zz_generated.deepcopy.go b/simulator/config/v1alpha1/zz_generated.deepcopy.go similarity index 100% rename from simulator/cmd/simulator/config/v1alpha1/zz_generated.deepcopy.go rename to simulator/config/v1alpha1/zz_generated.deepcopy.go diff --git a/simulator/cmd/simulator/config/v1alpha1/zz_generated.defaults.go b/simulator/config/v1alpha1/zz_generated.defaults.go similarity index 100% rename from simulator/cmd/simulator/config/v1alpha1/zz_generated.defaults.go rename to simulator/config/v1alpha1/zz_generated.defaults.go diff --git a/simulator/pkg/debuggablescheduler/debuggable_scheduler.go b/simulator/pkg/debuggablescheduler/debuggable_scheduler.go index 91e9b36a..57aaf8ef 100644 --- a/simulator/pkg/debuggablescheduler/debuggable_scheduler.go +++ b/simulator/pkg/debuggablescheduler/debuggable_scheduler.go @@ -20,7 +20,7 @@ import ( configv1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1" "k8s.io/kubernetes/pkg/scheduler/framework/runtime" - simulatorconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config" + simulatorconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler" simulatorschedulerconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/extender" diff --git a/simulator/pkg/externalscheduler/external_scheduler.go b/simulator/pkg/externalscheduler/external_scheduler.go index e1317a34..fa2e6ed4 100644 --- a/simulator/pkg/externalscheduler/external_scheduler.go +++ b/simulator/pkg/externalscheduler/external_scheduler.go @@ -20,7 +20,7 @@ import ( configv1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1" "k8s.io/kubernetes/pkg/scheduler/framework/runtime" - simulatorconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config" + simulatorconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler" simulatorschedulerconfig "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/plugin" diff --git a/simulator/server/server.go b/simulator/server/server.go index 4d1d98ec..01f2d080 100644 --- a/simulator/server/server.go +++ b/simulator/server/server.go @@ -11,7 +11,7 @@ import ( "github.com/labstack/echo/v4/middleware" "github.com/labstack/gommon/log" - "sigs.k8s.io/kube-scheduler-simulator/simulator/cmd/simulator/config" + "sigs.k8s.io/kube-scheduler-simulator/simulator/config" "sigs.k8s.io/kube-scheduler-simulator/simulator/server/di" "sigs.k8s.io/kube-scheduler-simulator/simulator/server/handler" ) From 82f776361bcb976aa77f55977428fc237cae97e4 Mon Sep 17 00:00:00 2001 From: YamasouA Date: Sun, 15 Sep 2024 21:23:35 +0900 Subject: [PATCH 3/4] can import resource filtered by labelSelector --- simulator/cmd/simulator/simulator.go | 2 +- simulator/config/config.go | 2 + simulator/config/v1alpha1/types.go | 2 + simulator/oneshotimporter/importer.go | 6 +- simulator/server/di/interface.go | 4 +- simulator/server/handler/snapshot.go | 3 +- simulator/snapshot/snapshot.go | 111 ++++++++++++++++++++------ simulator/snapshot/snapshot_test.go | 54 ++++++++++++- 8 files changed, 151 insertions(+), 33 deletions(-) diff --git a/simulator/cmd/simulator/simulator.go b/simulator/cmd/simulator/simulator.go index 4b2ca159..8f905136 100644 --- a/simulator/cmd/simulator/simulator.go +++ b/simulator/cmd/simulator/simulator.go @@ -111,7 +111,7 @@ func startSimulator() error { // This must be called after `StartScheduler` timeoutCtx, timeoutCancel := context.WithTimeout(ctx, importTimeout) defer timeoutCancel() - if err := dic.OneshotClusterResourceImporter().ImportClusterResources(timeoutCtx); err != nil { + if err := dic.OneshotClusterResourceImporter().ImportClusterResources(timeoutCtx, cfg.ImportLabel); err != nil { return xerrors.Errorf("import from the target cluster: %w", err) } } diff --git a/simulator/config/config.go b/simulator/config/config.go index 6a09618c..274e5108 100644 --- a/simulator/config/config.go +++ b/simulator/config/config.go @@ -34,6 +34,7 @@ type Config struct { // ExternalImportEnabled indicates whether the simulator will import resources from a target cluster once // when it's started. ExternalImportEnabled bool + ImportLabel map[string]string // ExternalImportEnabled indicates whether the simulator will keep syncing resources from a target cluster. ResourceSyncEnabled bool // ExternalKubeClientCfg is KubeConfig to get resources from external cluster. @@ -105,6 +106,7 @@ func NewConfig() (*Config, error) { CorsAllowedOriginList: corsAllowedOriginList, InitialSchedulerCfg: initialschedulerCfg, ExternalImportEnabled: externalimportenabled, + ImportLabel: configYaml.ImportLabel, ExternalKubeClientCfg: externalKubeClientCfg, ExternalSchedulerEnabled: externalSchedEnabled, ResourceSyncEnabled: resourceSyncEnabled, diff --git a/simulator/config/v1alpha1/types.go b/simulator/config/v1alpha1/types.go index a44fb703..5911879e 100644 --- a/simulator/config/v1alpha1/types.go +++ b/simulator/config/v1alpha1/types.go @@ -61,6 +61,8 @@ type SimulatorConfiguration struct { // Note, this is still a beta feature. ExternalImportEnabled bool `json:"externalImportEnabled,omitempty"` + ImportLabel map[string]string `json:"importLabel,omitempty"` + // This variable indicates whether the simulator will // sync resources from an user cluster's or not. ResourceSyncEnabled bool `json:"resourceSyncEnabled,omitempty"` diff --git a/simulator/oneshotimporter/importer.go b/simulator/oneshotimporter/importer.go index f939e960..5af97e91 100644 --- a/simulator/oneshotimporter/importer.go +++ b/simulator/oneshotimporter/importer.go @@ -20,7 +20,7 @@ type Service struct { type ReplicateService interface { // Snap will be used to export resources from target cluster. - Snap(ctx context.Context, opts ...snapshot.Option) (*snapshot.ResourcesForSnap, error) + Snap(ctx context.Context, importLabel map[string]string, opts ...snapshot.Option) (*snapshot.ResourcesForSnap, error) // Load will be used to import resources the from data which was exported. Load(ctx context.Context, resources *snapshot.ResourcesForLoad, opts ...snapshot.Option) error IgnoreErr() snapshot.Option @@ -40,8 +40,8 @@ func NewService(e ReplicateService, i ReplicateService) *Service { // Note: this method doesn't handle scheduler configuration. // If you want to use the scheduler configuration along with the imported resources on the simulator, // you need to set the path of the scheduler configuration file to `kubeSchedulerConfigPath` value in the Simulator Server Configuration. -func (s *Service) ImportClusterResources(ctx context.Context) error { - expRes, err := s.exportService.Snap(ctx) +func (s *Service) ImportClusterResources(ctx context.Context, importLabel map[string]string) error { + expRes, err := s.exportService.Snap(ctx, importLabel) if err != nil { return xerrors.Errorf("call Snap of the exportService: %w", err) } diff --git a/simulator/server/di/interface.go b/simulator/server/di/interface.go index 7bd806ce..cf61acd5 100644 --- a/simulator/server/di/interface.go +++ b/simulator/server/di/interface.go @@ -24,7 +24,7 @@ type SchedulerService interface { // SnapshotService represents a service for exporting/importing resources on the simulator. type SnapshotService interface { - Snap(ctx context.Context, opts ...snapshot.Option) (*snapshot.ResourcesForSnap, error) + Snap(ctx context.Context, importLabel map[string]string, opts ...snapshot.Option) (*snapshot.ResourcesForSnap, error) Load(ctx context.Context, resources *snapshot.ResourcesForLoad, opts ...snapshot.Option) error IgnoreErr() snapshot.Option } @@ -35,7 +35,7 @@ type ResetService interface { // OneShotClusterResourceImporter represents a service to import resources from an target cluster when starting the simulator. type OneShotClusterResourceImporter interface { - ImportClusterResources(ctx context.Context) error + ImportClusterResources(ctx context.Context, importLabel map[string]string) error } // ResourceSyncer represents a service to constantly sync resources from an target cluster. diff --git a/simulator/server/handler/snapshot.go b/simulator/server/handler/snapshot.go index 5141d224..4d435b6d 100644 --- a/simulator/server/handler/snapshot.go +++ b/simulator/server/handler/snapshot.go @@ -36,7 +36,8 @@ func NewSnapshotHandler(s di.SnapshotService) *SnapshotHandler { func (h *SnapshotHandler) Snap(c echo.Context) error { ctx := c.Request().Context() - rs, err := h.service.Snap(ctx) + var label map[string]string + rs, err := h.service.Snap(ctx, label) if err != nil { klog.Errorf("failed to save all resources: %+v", err) return echo.NewHTTPError(http.StatusInternalServerError) diff --git a/simulator/snapshot/snapshot.go b/simulator/snapshot/snapshot.go index ce2f6fb6..9dbb7c50 100644 --- a/simulator/snapshot/snapshot.go +++ b/simulator/snapshot/snapshot.go @@ -100,29 +100,29 @@ func (s *Service) IgnoreSchedulerConfiguration() Option { } // get gets all resources from each service. -func (s *Service) get(ctx context.Context, opts options) (*ResourcesForSnap, error) { +func (s *Service) get(ctx context.Context, importLabel map[string]string, opts options) (*ResourcesForSnap, error) { errgrp := util.NewErrGroupWithSemaphore(ctx) resources := ResourcesForSnap{} - if err := s.listPods(ctx, &resources, errgrp, opts); err != nil { + if err := s.listPods(ctx, &resources, errgrp, importLabel, opts); err != nil { return nil, xerrors.Errorf("call listPods: %w", err) } - if err := s.listNodes(ctx, &resources, errgrp, opts); err != nil { + if err := s.listNodes(ctx, &resources, errgrp, importLabel, opts); err != nil { return nil, xerrors.Errorf("call listNodes: %w", err) } - if err := s.listPvs(ctx, &resources, errgrp, opts); err != nil { + if err := s.listPvs(ctx, &resources, errgrp, importLabel, opts); err != nil { return nil, xerrors.Errorf("call listPvs: %w", err) } - if err := s.listPvcs(ctx, &resources, errgrp, opts); err != nil { + if err := s.listPvcs(ctx, &resources, errgrp, importLabel, opts); err != nil { return nil, xerrors.Errorf("call listPvcs: %w", err) } - if err := s.listStorageClasses(ctx, &resources, errgrp, opts); err != nil { + if err := s.listStorageClasses(ctx, &resources, errgrp, importLabel, opts); err != nil { return nil, xerrors.Errorf("call listStorageClasses: %w", err) } - if err := s.listPcs(ctx, &resources, errgrp, opts); err != nil { + if err := s.listPcs(ctx, &resources, errgrp, importLabel, opts); err != nil { return nil, xerrors.Errorf("call listPcs: %w", err) } - if err := s.listNamespaces(ctx, &resources, errgrp, opts); err != nil { + if err := s.listNamespaces(ctx, &resources, errgrp, importLabel, opts); err != nil { return nil, xerrors.Errorf("call listNamespaces: %w", err) } if err := s.getSchedulerConfig(&resources, errgrp, opts); err != nil { @@ -136,12 +136,12 @@ func (s *Service) get(ctx context.Context, opts options) (*ResourcesForSnap, err } // Snap exports all resources as one data. -func (s *Service) Snap(ctx context.Context, opts ...Option) (*ResourcesForSnap, error) { +func (s *Service) Snap(ctx context.Context, importLabel map[string]string, opts ...Option) (*ResourcesForSnap, error) { options := options{} for _, o := range opts { o.apply(&options) } - resources, err := s.get(ctx, options) + resources, err := s.get(ctx, importLabel, options) if err != nil { return nil, xerrors.Errorf("failed to get(): %w", err) } @@ -214,9 +214,18 @@ func (s *Service) Load(ctx context.Context, resources *ResourcesForLoad, opts .. return nil } -func (s *Service) listPods(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { +func (s *Service) listPods(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, importLabel map[string]string, opts options) error { if err := eg.Go(func() error { - pods, err := s.client.CoreV1().Pods(metav1.NamespaceAll).List(ctx, metav1.ListOptions{}) + labelSelector := metav1.LabelSelector{ + MatchLabels: importLabel, + } + selector, err := metav1.LabelSelectorAsSelector(&labelSelector) + if err != nil { + return xerrors.Errorf("failed to create label selector: %w", err) + } + pods, err := s.client.CoreV1().Pods(metav1.NamespaceAll).List(ctx, metav1.ListOptions{ + LabelSelector: selector.String(), + }) if err != nil { if !opts.ignoreErr { return xerrors.Errorf("call list Pod: %w", err) @@ -232,9 +241,18 @@ func (s *Service) listPods(ctx context.Context, r *ResourcesForSnap, eg *util.Se return nil } -func (s *Service) listNodes(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { +func (s *Service) listNodes(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, importLabel map[string]string, opts options) error { if err := eg.Go(func() error { - nodes, err := s.client.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) + labelSelector := metav1.LabelSelector{ + MatchLabels: importLabel, + } + selector, err := metav1.LabelSelectorAsSelector(&labelSelector) + if err != nil { + return xerrors.Errorf("failed to create label selector: %w", err) + } + nodes, err := s.client.CoreV1().Nodes().List(ctx, metav1.ListOptions{ + LabelSelector: selector.String(), + }) if err != nil { if !opts.ignoreErr { return xerrors.Errorf("call list Node: %w", err) @@ -250,9 +268,18 @@ func (s *Service) listNodes(ctx context.Context, r *ResourcesForSnap, eg *util.S return nil } -func (s *Service) listPvs(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { +func (s *Service) listPvs(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, importLabel map[string]string, opts options) error { if err := eg.Go(func() error { - pvs, err := s.client.CoreV1().PersistentVolumes().List(ctx, metav1.ListOptions{}) + labelSelector := metav1.LabelSelector{ + MatchLabels: importLabel, + } + selector, err := metav1.LabelSelectorAsSelector(&labelSelector) + if err != nil { + return xerrors.Errorf("failed to create label selector: %w", err) + } + pvs, err := s.client.CoreV1().PersistentVolumes().List(ctx, metav1.ListOptions{ + LabelSelector: selector.String(), + }) if err != nil { if !opts.ignoreErr { return xerrors.Errorf("call list PersistentVolume: %w", err) @@ -268,9 +295,18 @@ func (s *Service) listPvs(ctx context.Context, r *ResourcesForSnap, eg *util.Sem return nil } -func (s *Service) listPvcs(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { +func (s *Service) listPvcs(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, importLabel map[string]string, opts options) error { if err := eg.Go(func() error { - pvcs, err := s.client.CoreV1().PersistentVolumeClaims(metav1.NamespaceAll).List(ctx, metav1.ListOptions{}) + labelSelector := metav1.LabelSelector{ + MatchLabels: importLabel, + } + selector, err := metav1.LabelSelectorAsSelector(&labelSelector) + if err != nil { + return xerrors.Errorf("failed to create label selector: %w", err) + } + pvcs, err := s.client.CoreV1().PersistentVolumeClaims(metav1.NamespaceAll).List(ctx, metav1.ListOptions{ + LabelSelector: selector.String(), + }) if err != nil { if !opts.ignoreErr { return xerrors.Errorf("call list PersistentVolumeClaim: %w", err) @@ -286,9 +322,18 @@ func (s *Service) listPvcs(ctx context.Context, r *ResourcesForSnap, eg *util.Se return nil } -func (s *Service) listStorageClasses(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { +func (s *Service) listStorageClasses(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, importLabel map[string]string, opts options) error { if err := eg.Go(func() error { - scs, err := s.client.StorageV1().StorageClasses().List(ctx, metav1.ListOptions{}) + labelSelector := metav1.LabelSelector{ + MatchLabels: importLabel, + } + selector, err := metav1.LabelSelectorAsSelector(&labelSelector) + if err != nil { + return xerrors.Errorf("failed to create label selector: %w", err) + } + scs, err := s.client.StorageV1().StorageClasses().List(ctx, metav1.ListOptions{ + LabelSelector: selector.String(), + }) if err != nil { if !opts.ignoreErr { return xerrors.Errorf("call list StorageClass: %w", err) @@ -304,9 +349,18 @@ func (s *Service) listStorageClasses(ctx context.Context, r *ResourcesForSnap, e return nil } -func (s *Service) listPcs(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { +func (s *Service) listPcs(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, importLabel map[string]string, opts options) error { if err := eg.Go(func() error { - pcs, err := s.client.SchedulingV1().PriorityClasses().List(ctx, metav1.ListOptions{}) + labelSelector := metav1.LabelSelector{ + MatchLabels: importLabel, + } + selector, err := metav1.LabelSelectorAsSelector(&labelSelector) + if err != nil { + return xerrors.Errorf("failed to create label selector: %w", err) + } + pcs, err := s.client.SchedulingV1().PriorityClasses().List(ctx, metav1.ListOptions{ + LabelSelector: selector.String(), + }) if err != nil { if !opts.ignoreErr { return xerrors.Errorf("call list PriorityClass: %w", err) @@ -328,9 +382,18 @@ func (s *Service) listPcs(ctx context.Context, r *ResourcesForSnap, eg *util.Sem return nil } -func (s *Service) listNamespaces(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { +func (s *Service) listNamespaces(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, importLabel map[string]string, opts options) error { if err := eg.Go(func() error { - nss, err := s.client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + labelSelector := metav1.LabelSelector{ + MatchLabels: importLabel, + } + selector, err := metav1.LabelSelectorAsSelector(&labelSelector) + if err != nil { + return xerrors.Errorf("failed to create label selector: %w", err) + } + nss, err := s.client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{ + LabelSelector: selector.String(), + }) if err != nil { if !opts.ignoreErr { return xerrors.Errorf("call list Namespace: %w", err) diff --git a/simulator/snapshot/snapshot_test.go b/simulator/snapshot/snapshot_test.go index de6c3c5d..51430179 100644 --- a/simulator/snapshot/snapshot_test.go +++ b/simulator/snapshot/snapshot_test.go @@ -229,6 +229,7 @@ func TestService_Snap(t *testing.T) { name string prepareEachServiceMockFn func(*mock_snapshot.MockSchedulerService) prepareFakeClientSetFn func() *fake.Clientset + labels map[string]string wantReturn func() *ResourcesForSnap wantErr error }{ @@ -520,6 +521,54 @@ func TestService_Snap(t *testing.T) { return r }, }, + { + name: "Snap all Pods with different namespaces use labelSelector", + prepareEachServiceMockFn: func(ss *mock_snapshot.MockSchedulerService) { + ss.EXPECT().GetSchedulerConfig().Return(&configv1.KubeSchedulerConfiguration{}, nil) + }, + prepareFakeClientSetFn: func() *fake.Clientset { + c := fake.NewSimpleClientset() + ctx := context.Background() + // add test data. + invokeResourcesFn(ctx, c, SettingClientFuncMap{ + pod: func(ctx context.Context, c *fake.Clientset) { + c.CoreV1().Pods(testNamespace1).Create(ctx, &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pod1", + Labels: map[string]string{ + "test": "test1", + }, + }, + Spec: corev1.PodSpec{ + NodeName: "node1", + }, + }, metav1.CreateOptions{}) + c.CoreV1().Pods(testNamespace2).Create(ctx, &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pod2", + }, + Spec: corev1.PodSpec{ + NodeName: "node1", + }, + }, metav1.CreateOptions{}) + }, + }, defaultFuncs) + return c + }, + labels: map[string]string{ + "test": "test1", + }, + wantReturn: func() *ResourcesForSnap { + r := defaultResForSnapFn() + r.Pods = []corev1.Pod{ + { + ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: testNamespace1}, + Spec: corev1.PodSpec{NodeName: "node1"}, + }, + } + return r + }, + }, } for _, tt := range tests { tt := tt @@ -531,7 +580,7 @@ func TestService_Snap(t *testing.T) { s := NewService(fakeClientset, mockSchedulerSvc) tt.prepareEachServiceMockFn(mockSchedulerSvc) - r, err := s.Snap(context.Background()) + r, err := s.Snap(context.Background(), tt.labels) var diffResponse string if tt.wantReturn != nil { @@ -750,7 +799,8 @@ func TestService_Snap_IgnoreErrOption(t *testing.T) { mockSchedulerSvc := mock_snapshot.NewMockSchedulerService(ctrl) s := NewService(fakeClientset, mockSchedulerSvc) tt.prepareEachServiceMockFn(mockSchedulerSvc) - r, err := s.Snap(context.Background(), s.IgnoreErr()) + var labels map[string]string + r, err := s.Snap(context.Background(), labels, s.IgnoreErr()) var diffResponse string if tt.wantReturn != nil { From 7a7b7a194fdf59a149d58ad11eb6fec7bf2bef01 Mon Sep 17 00:00:00 2001 From: YamasouA Date: Sat, 28 Sep 2024 14:49:23 +0900 Subject: [PATCH 4/4] tweak --- simulator/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulator/Makefile b/simulator/Makefile index 8a61cee1..dbdc2872 100644 --- a/simulator/Makefile +++ b/simulator/Makefile @@ -23,4 +23,4 @@ mod-download: ## Downloads the Go module .PHONY: build build: go build -o ./bin/simulator ./cmd/simulator/simulator.go - go build -o ./bin/scheduler ./cmd/scheduler/scheduler.go \ No newline at end of file + go build -o ./bin/scheduler ./cmd/scheduler/scheduler.go