diff --git a/simulator/oneshotimporter/importer.go b/simulator/oneshotimporter/importer.go index 89b458ca..ab42ea6f 100644 --- a/simulator/oneshotimporter/importer.go +++ b/simulator/oneshotimporter/importer.go @@ -31,6 +31,7 @@ type Service struct { var DefaultGVRs = []schema.GroupVersionResource{ {Group: "", Version: "v1", Resource: "namespaces"}, {Group: "scheduling.k8s.io", Version: "v1", Resource: "priorityclasses"}, + {Group: "", Version: "v1", Resource: "serviceaccounts"}, {Group: "storage.k8s.io", Version: "v1", Resource: "storageclasses"}, {Group: "", Version: "v1", Resource: "persistentvolumeclaims"}, {Group: "", Version: "v1", Resource: "nodes"}, diff --git a/simulator/snapshot/snapshot.go b/simulator/snapshot/snapshot.go index ce2f6fb6..0c3fb444 100644 --- a/simulator/snapshot/snapshot.go +++ b/simulator/snapshot/snapshot.go @@ -30,6 +30,7 @@ type Service struct { // ResourcesForSnap indicates all resources and scheduler configuration to be snapped. type ResourcesForSnap struct { + Sas []corev1.ServiceAccount `json:"sas"` Pods []corev1.Pod `json:"pods"` Nodes []corev1.Node `json:"nodes"` Pvs []corev1.PersistentVolume `json:"pvs"` @@ -42,6 +43,7 @@ type ResourcesForSnap struct { // ResourcesForLoad indicates all resources and scheduler configuration to be loaded. type ResourcesForLoad struct { + Sas []v1.ServiceAccountApplyConfiguration `json:"sas"` Pods []v1.PodApplyConfiguration `json:"pods"` Nodes []v1.NodeApplyConfiguration `json:"nodes"` Pvs []v1.PersistentVolumeApplyConfiguration `json:"pvs"` @@ -103,7 +105,9 @@ func (s *Service) IgnoreSchedulerConfiguration() Option { func (s *Service) get(ctx context.Context, opts options) (*ResourcesForSnap, error) { errgrp := util.NewErrGroupWithSemaphore(ctx) resources := ResourcesForSnap{} - + if err := s.listSas(ctx, &resources, errgrp, opts); err != nil { + return nil, xerrors.Errorf("call listSas: %w", err) + } if err := s.listPods(ctx, &resources, errgrp, opts); err != nil { return nil, xerrors.Errorf("call listPods: %w", err) } @@ -173,6 +177,9 @@ func (s *Service) apply(ctx context.Context, resources *ResourcesForLoad, opts o if err := s.applyNodes(ctx, resources, errgrp, opts); err != nil { return xerrors.Errorf("call applyNodes: %w", err) } + if err := s.applySas(ctx, resources, errgrp, opts); err != nil { + return xerrors.Errorf("call applySas: %w", err) + } if err := s.applyPods(ctx, resources, errgrp, opts); err != nil { return xerrors.Errorf("call applyPods: %w", err) } @@ -214,6 +221,24 @@ func (s *Service) Load(ctx context.Context, resources *ResourcesForLoad, opts .. return nil } +func (s *Service) listSas(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { + if err := eg.Go(func() error { + sass, err := s.client.CoreV1().ServiceAccounts(metav1.NamespaceAll).List(ctx, metav1.ListOptions{}) + if err != nil { + if !opts.ignoreErr { + return xerrors.Errorf("call list Pod: %w", err) + } + klog.Errorf("failed to call list Pod: %v", err) + sass = &corev1.ServiceAccountList{Items: []corev1.ServiceAccount{}} + } + r.Sas = sass.Items + return nil + }); err != nil { + return xerrors.Errorf("start error group: %w", err) + } + return nil +} + func (s *Service) listPods(ctx context.Context, r *ResourcesForSnap, eg *util.SemaphoredErrGroup, opts options) error { if err := eg.Go(func() error { pods, err := s.client.CoreV1().Pods(metav1.NamespaceAll).List(ctx, metav1.ListOptions{}) @@ -490,6 +515,31 @@ func (s *Service) applyNodes(ctx context.Context, r *ResourcesForLoad, eg *util. return nil } +func (s *Service) applySas(ctx context.Context, r *ResourcesForLoad, eg *util.SemaphoredErrGroup, opts options) error { + for i := range r.Sas { + sa := r.Sas[i] + if err := eg.Go(func() error { + sa.ObjectMetaApplyConfiguration.UID = nil + sa.ObjectMetaApplyConfiguration.CreationTimestamp = nil + sa.ObjectMetaApplyConfiguration.ResourceVersion = nil + + sa.WithAPIVersion("v1").WithKind("ServiceAccount") + + _, err := s.client.CoreV1().ServiceAccounts(*sa.Namespace).Apply(ctx, &sa, metav1.ApplyOptions{Force: true, FieldManager: "simulator"}) + if err != nil { + if !opts.ignoreErr { + return xerrors.Errorf("apply Sa: %w", err) + } + klog.Errorf("failed to apply Sa: %v", err) + } + return nil + }); err != nil { + return xerrors.Errorf("start error group: %w", err) + } + } + return nil +} + func (s *Service) applyPods(ctx context.Context, r *ResourcesForLoad, eg *util.SemaphoredErrGroup, opts options) error { for i := range r.Pods { pod := r.Pods[i] diff --git a/simulator/syncer/syncer.go b/simulator/syncer/syncer.go index 36dd2a14..17dc0d76 100644 --- a/simulator/syncer/syncer.go +++ b/simulator/syncer/syncer.go @@ -23,6 +23,7 @@ import ( var DefaultGVRs = []schema.GroupVersionResource{ {Group: "", Version: "v1", Resource: "namespaces"}, {Group: "scheduling.k8s.io", Version: "v1", Resource: "priorityclasses"}, + {Group: "", Version: "v1", Resource: "serviceaccounts"}, {Group: "storage.k8s.io", Version: "v1", Resource: "storageclasses"}, {Group: "", Version: "v1", Resource: "persistentvolumeclaims"}, {Group: "", Version: "v1", Resource: "nodes"},