From 0e39b9223c440850a6af8d3b166f8e63f647d154 Mon Sep 17 00:00:00 2001 From: peng9808 Date: Tue, 30 Jul 2024 10:57:41 +0800 Subject: [PATCH] Fix the bug of reinstallation Signed-off-by: peng9808 --- controllers/cluster_controller.go | 7 +++-- main.go | 2 ++ .../admission_controller.go | 31 +++++++++++-------- pkg/install/admissioncontroller/service.go | 3 +- pkg/install/apiserver/apiserver.go | 25 ++++++++------- pkg/install/apiserver/apiserver_service.go | 3 +- pkg/install/auditor/auditor.go | 3 +- .../dataloadmanager/dataloadmanager.go | 3 +- pkg/install/datasetmanager/datasetmanager.go | 3 +- pkg/install/evictor/evictor.go | 6 ++-- pkg/install/exporter/exporter.go | 6 ++-- pkg/install/exporter/exporter_service.go | 3 +- .../failoverassistant/failover_assistant.go | 3 +- .../localdiskmanager_csi_controller.go | 3 +- .../localdiskactioncontroller.go | 3 +- .../localdiskmanager/localdiskmanager.go | 3 +- pkg/install/localstorage/localstorage.go | 3 +- .../localstorage_csi_controller.go | 3 +- pkg/install/pvcautoresizer/pvc_autoresizer.go | 3 +- pkg/install/scheduler/scheduler.go | 6 ++-- pkg/install/ui/ui.go | 3 +- pkg/install/ui/ui_service.go | 3 +- 22 files changed, 80 insertions(+), 48 deletions(-) diff --git a/controllers/cluster_controller.go b/controllers/cluster_controller.go index 1176311c..152942da 100644 --- a/controllers/cluster_controller.go +++ b/controllers/cluster_controller.go @@ -115,12 +115,13 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct // status.installedCRDs true bool value will cause hwameistor crds not updated when upgrade, // so we turn status.installedCRDS to false bool value here once spec generation changed. // That will ensure hwameistor crds updating not missed when upgrading. - if r.ClusterSpecGeneration != newInstance.Generation && newInstance.Status.InstalledCRDS{ + // Upgrade crd when operator restarts + if r.ClusterSpecGeneration == 0 && newInstance.Status.InstalledCRDS { log.Infof("cached cluster spec generation:%v, gotten cluster generation: %v", r.ClusterSpecGeneration, newInstance.Generation) log.Infof("going to set status.installedCRDS to false bool value") newInstance.Status.InstalledCRDS = false if err := r.Client.Status().Update(ctx, newInstance); err != nil { - log.Errorf("Update status err: %v", err) + log.Errorf("Update InstalledCRDS=false status err: %v", err) return ctrl.Result{}, err } r.ClusterSpecGeneration = newInstance.Generation @@ -134,7 +135,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct } newInstance.Status.InstalledCRDS = true if err := r.Client.Status().Update(ctx, newInstance); err != nil { - log.Errorf("Update status err: %v", err) + log.Errorf("Update InstalledCRDS=true status err: %v", err) return ctrl.Result{}, err } return ctrl.Result{}, nil diff --git a/main.go b/main.go index 7cfbf66f..9add5c1c 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( "os" "path" "runtime" + "sigs.k8s.io/controller-runtime/pkg/client" "strings" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) @@ -76,6 +77,7 @@ func main() { kubeconfig.Set(ctrl.GetConfigOrDie()) mgr, err := ctrl.NewManager(kubeconfig.Get(), ctrl.Options{ + ClientDisableCacheFor: []client.Object{&hwameistoroperatorv1alpha1.Cluster{}}, Scheme: scheme, MetricsBindAddress: metricsAddr, Port: 9443, diff --git a/pkg/install/admissioncontroller/admission_controller.go b/pkg/install/admissioncontroller/admission_controller.go index 748ff83d..169a812e 100644 --- a/pkg/install/admissioncontroller/admission_controller.go +++ b/pkg/install/admissioncontroller/admission_controller.go @@ -3,6 +3,7 @@ package admissioncontroller import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" "time" @@ -115,17 +116,19 @@ var admissionController = appsv1.Deployment{ }, } -func SetAdmissionController(clusterInstance *hwameistoriov1alpha1.Cluster) { - admissionController.Namespace = clusterInstance.Spec.TargetNamespace - admissionController.OwnerReferences = append(admissionController.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) +func SetAdmissionController(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.Deployment { + + resourceCreate := admissionController.DeepCopy() + resourceCreate.Namespace = clusterInstance.Spec.TargetNamespace + resourceCreate.OwnerReferences = append(resourceCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) replicas := getAdmissionControllerReplicasFromClusterInstance(clusterInstance) - admissionController.Spec.Replicas = &replicas - admissionController.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName - setAdmissionControllerContainers(clusterInstance) + resourceCreate.Spec.Replicas = &replicas + resourceCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName + return setAdmissionControllerContainers(clusterInstance, resourceCreate) } -func setAdmissionControllerContainers(clusterInstance *hwameistoriov1alpha1.Cluster) { - for i, container := range admissionController.Spec.Template.Spec.Containers { +func setAdmissionControllerContainers(clusterInstance *hwameistoriov1alpha1.Cluster, resourceCreate *appsv1.Deployment) *appsv1.Deployment { + for i, container := range resourceCreate.Spec.Template.Spec.Containers { if container.Name == admissionControllerContainerName { // container.Resources = *clusterInstance.Spec.AdmissionController.Controller.Resources if resources := clusterInstance.Spec.AdmissionController.Controller.Resources; resources != nil { @@ -152,8 +155,10 @@ func setAdmissionControllerContainers(clusterInstance *hwameistoriov1alpha1.Clus }, }...) } - admissionController.Spec.Template.Spec.Containers[i] = container + resourceCreate.Spec.Template.Spec.Containers[i] = container } + + return resourceCreate } func getAdmissionControllerContainerImageStringFromClusterInstance(clusterInstance *hwameistoriov1alpha1.Cluster) string { @@ -207,7 +212,7 @@ func needOrNotToUpdateAdmissionController(cluster *hwameistoriov1alpha1.Cluster, func (m *AdmissionControllerMaintainer) Ensure() (*hwameistoriov1alpha1.Cluster, error) { newClusterInstance := m.ClusterInstance.DeepCopy() - SetAdmissionController(newClusterInstance) + resourceCreate := SetAdmissionController(newClusterInstance) // ensure admission controller CA is generated if err := m.ensureAdmissionCA(); err != nil { @@ -215,13 +220,13 @@ func (m *AdmissionControllerMaintainer) Ensure() (*hwameistoriov1alpha1.Cluster, return newClusterInstance, err } key := types.NamespacedName{ - Namespace: admissionController.Namespace, - Name: admissionController.Name, + Namespace: resourceCreate.Namespace, + Name: resourceCreate.Name, } var gottenAdmissionController appsv1.Deployment if err := m.Client.Get(context.TODO(), key, &gottenAdmissionController); err != nil { if apierrors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &admissionController); errCreate != nil { + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create AdmissionController err: %v", errCreate) return newClusterInstance, errCreate } diff --git a/pkg/install/admissioncontroller/service.go b/pkg/install/admissioncontroller/service.go index ebbd2271..49ad505e 100644 --- a/pkg/install/admissioncontroller/service.go +++ b/pkg/install/admissioncontroller/service.go @@ -57,7 +57,8 @@ func (m *AdmissionControllerServiceMaintainer) Ensure() error { var gottenService corev1.Service if err := m.Client.Get(context.TODO(), key, &gottenService); err != nil { if errors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &admissionControllerService); errCreate != nil { + resourceCreate := admissionControllerService.DeepCopy() + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create AdmissionController Service err: %v", err) return errCreate } diff --git a/pkg/install/apiserver/apiserver.go b/pkg/install/apiserver/apiserver.go index 310fc3a8..22f2e015 100644 --- a/pkg/install/apiserver/apiserver.go +++ b/pkg/install/apiserver/apiserver.go @@ -3,6 +3,7 @@ package apiserver import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" "strconv" @@ -95,13 +96,14 @@ var apiServer = appsv1.Deployment{ }, } -func SetApiServer(clusterInstance *hwameistoriov1alpha1.Cluster) { - apiServer.Namespace = clusterInstance.Spec.TargetNamespace - apiServer.OwnerReferences = append(apiServer.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) +func SetApiServer(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.Deployment { + resourceCreate := apiServer.DeepCopy() + resourceCreate.Namespace = clusterInstance.Spec.TargetNamespace + resourceCreate.OwnerReferences = append(resourceCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) replicas := getApiserverReplicasFromClusterInstance(clusterInstance) - apiServer.Spec.Replicas = &replicas - apiServer.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName - for i, container := range apiServer.Spec.Template.Spec.Containers { + resourceCreate.Spec.Replicas = &replicas + resourceCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName + for i, container := range resourceCreate.Spec.Template.Spec.Containers { if container.Name == apiserverContainerName { container.Image = getApiserverContainerImageStringFromClusterInstance(clusterInstance) if resources := clusterInstance.Spec.ApiServer.Server.Resources; resources != nil { @@ -122,8 +124,9 @@ func SetApiServer(clusterInstance *hwameistoriov1alpha1.Cluster) { }, }...) } - apiServer.Spec.Template.Spec.Containers[i] = container + resourceCreate.Spec.Template.Spec.Containers[i] = container } + return resourceCreate } func getApiserverContainerImageStringFromClusterInstance(clusterInstance *hwameistoriov1alpha1.Cluster) string { @@ -161,15 +164,15 @@ func needOrNotToUpdateApiserver(cluster *hwameistoriov1alpha1.Cluster, gottenApi func (m *ApiServerMaintainer) Ensure() (*hwameistoriov1alpha1.Cluster, error) { newClusterInstance := m.ClusterInstance.DeepCopy() - SetApiServer(newClusterInstance) + resourceCreate := SetApiServer(newClusterInstance) key := types.NamespacedName{ - Namespace: apiServer.Namespace, - Name: apiServer.Name, + Namespace: resourceCreate.Namespace, + Name: resourceCreate.Name, } var gotten appsv1.Deployment if err := m.Client.Get(context.TODO(), key, &gotten); err != nil { if apierrors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &apiServer); errCreate != nil { + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create ApiServer err: %v", errCreate) return newClusterInstance, errCreate } diff --git a/pkg/install/apiserver/apiserver_service.go b/pkg/install/apiserver/apiserver_service.go index 332b592a..5313a37f 100644 --- a/pkg/install/apiserver/apiserver_service.go +++ b/pkg/install/apiserver/apiserver_service.go @@ -60,7 +60,8 @@ func (m *ApiServerServiceMaintainer) Ensure() error { var gottenService corev1.Service if err := m.Client.Get(context.TODO(), key, &gottenService); err != nil { if errors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &apiServerService); errCreate != nil { + resourceCreate := apiServerService.DeepCopy() + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create ApiServer Service err: %v", err) return errCreate } diff --git a/pkg/install/auditor/auditor.go b/pkg/install/auditor/auditor.go index f42bfd35..edd54707 100644 --- a/pkg/install/auditor/auditor.go +++ b/pkg/install/auditor/auditor.go @@ -3,6 +3,7 @@ package auditor import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -102,7 +103,7 @@ func SetAuditor(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.Deploymen auditorToCreate := auditorTemplate.DeepCopy() auditorToCreate.Namespace = clusterInstance.Spec.TargetNamespace - auditorToCreate.OwnerReferences = append(auditorToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + auditorToCreate.OwnerReferences = append(auditorToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) replicas := getAuditorReplicasFromClusterInstance(clusterInstance) auditorToCreate.Spec.Replicas = &replicas auditorToCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName diff --git a/pkg/install/dataloadmanager/dataloadmanager.go b/pkg/install/dataloadmanager/dataloadmanager.go index 64d19534..858a35fd 100644 --- a/pkg/install/dataloadmanager/dataloadmanager.go +++ b/pkg/install/dataloadmanager/dataloadmanager.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" "reflect" @@ -137,7 +138,7 @@ var dlmDaemonSetTemplate = appsv1.DaemonSet{ func SetDLMDaemonSet(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.DaemonSet { dlmDaemonSetToCreate := dlmDaemonSetTemplate.DeepCopy() - dlmDaemonSetToCreate.OwnerReferences = append(dlmDaemonSetToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + dlmDaemonSetToCreate.OwnerReferences = append(dlmDaemonSetToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) dlmDaemonSetToCreate.Namespace = clusterInstance.Spec.TargetNamespace dlmDaemonSetToCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName dlmDaemonSetToCreate = setDLMDaemonSetContainers(clusterInstance, dlmDaemonSetToCreate) diff --git a/pkg/install/datasetmanager/datasetmanager.go b/pkg/install/datasetmanager/datasetmanager.go index e4898c39..0513e637 100644 --- a/pkg/install/datasetmanager/datasetmanager.go +++ b/pkg/install/datasetmanager/datasetmanager.go @@ -9,6 +9,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -80,7 +81,7 @@ func int64Ptr(i int64) *int64 { return &i } func SetDSMDeployment(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.Deployment { dsmDeploymentToCreate := dsmDeploymentTemplate.DeepCopy() - dsmDeploymentToCreate.OwnerReferences = append(dsmDeploymentToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + dsmDeploymentToCreate.OwnerReferences = append(dsmDeploymentToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) dsmDeploymentToCreate.Namespace = clusterInstance.Spec.TargetNamespace dsmDeploymentToCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName dsmDeploymentToCreate = setdsmDeploymentContainers(clusterInstance, dsmDeploymentToCreate) diff --git a/pkg/install/evictor/evictor.go b/pkg/install/evictor/evictor.go index 95710b27..1c21ee6f 100644 --- a/pkg/install/evictor/evictor.go +++ b/pkg/install/evictor/evictor.go @@ -3,6 +3,7 @@ package evictor import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -73,7 +74,7 @@ var evictorDeployment = appsv1.Deployment{ func SetEvictor(clusterInstance *hwameistoriov1alpha1.Cluster) { evictorDeployment.Namespace = clusterInstance.Spec.TargetNamespace - evictorDeployment.OwnerReferences = append(evictorDeployment.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + evictorDeployment.OwnerReferences = append(evictorDeployment.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) replicas := getEvictorReplicasFromClusterInstance(clusterInstance) evictorDeployment.Spec.Replicas = &replicas evictorDeployment.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName @@ -152,7 +153,8 @@ func (m *EvictorMaintainer) Ensure() (*hwameistoriov1alpha1.Cluster, error) { var gotten appsv1.Deployment if err := m.Client.Get(context.TODO(), key, &gotten); err != nil { if apierrors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &evictorDeployment); errCreate != nil { + resourceCreate := evictorDeployment.DeepCopy() + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create Evictor err: %v", errCreate) return newClusterInstance, errCreate } diff --git a/pkg/install/exporter/exporter.go b/pkg/install/exporter/exporter.go index c5fd973b..cde83c53 100644 --- a/pkg/install/exporter/exporter.go +++ b/pkg/install/exporter/exporter.go @@ -3,6 +3,7 @@ package exporter import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -88,7 +89,7 @@ var exporter = appsv1.Deployment{ func SetExporter(clusterInstance *hwameistoriov1alpha1.Cluster) { exporter.Namespace = clusterInstance.Spec.TargetNamespace - exporter.OwnerReferences = append(exporter.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + exporter.OwnerReferences = append(exporter.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) replicas := getExporterReplicasFromClusterInstance(clusterInstance) exporter.Spec.Replicas = &replicas exporter.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName @@ -146,7 +147,8 @@ func (m *ExporterMaintainer) Ensure() (*hwameistoriov1alpha1.Cluster, error) { var gotten appsv1.Deployment if err := m.Client.Get(context.TODO(), key, &gotten); err != nil { if apierrors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &exporter); errCreate != nil { + resourceCreate := exporter.DeepCopy() + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create Exporter err: %v", errCreate) return newClusterInstance, errCreate } diff --git a/pkg/install/exporter/exporter_service.go b/pkg/install/exporter/exporter_service.go index fbe3656e..064c11b7 100644 --- a/pkg/install/exporter/exporter_service.go +++ b/pkg/install/exporter/exporter_service.go @@ -61,7 +61,8 @@ func (m *ExporterServiceMaintainer) Ensure() error { var gottenService corev1.Service if err := m.Client.Get(context.TODO(), key, &gottenService); err != nil { if errors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &exporterService); errCreate != nil { + resourceCreate := exporterService.DeepCopy() + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create Exporter Service err: %v", err) return errCreate } diff --git a/pkg/install/failoverassistant/failover_assistant.go b/pkg/install/failoverassistant/failover_assistant.go index 16930a78..cd78b8d9 100644 --- a/pkg/install/failoverassistant/failover_assistant.go +++ b/pkg/install/failoverassistant/failover_assistant.go @@ -3,6 +3,7 @@ package failoverassistant import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -102,7 +103,7 @@ func SetFailoverAssistant(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1 deployToCreate := failoverAssistantTemplate.DeepCopy() deployToCreate.Namespace = clusterInstance.Spec.TargetNamespace - deployToCreate.OwnerReferences = append(deployToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + deployToCreate.OwnerReferences = append(deployToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) replicas := getFailoverAssistantReplicasFromClusterInstance(clusterInstance) deployToCreate.Spec.Replicas = &replicas deployToCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName diff --git a/pkg/install/ldmcsicontroller/localdiskmanager_csi_controller.go b/pkg/install/ldmcsicontroller/localdiskmanager_csi_controller.go index faa3ae22..c27913f6 100644 --- a/pkg/install/ldmcsicontroller/localdiskmanager_csi_controller.go +++ b/pkg/install/ldmcsicontroller/localdiskmanager_csi_controller.go @@ -3,6 +3,7 @@ package ldmcsicontroller import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -133,7 +134,7 @@ var ldmCSIController = appsv1.Deployment{ func SetLDMCSIController(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.Deployment { ldmCSIControllerToCreate := ldmCSIController.DeepCopy() ldmCSIControllerToCreate.Namespace = clusterInstance.Spec.TargetNamespace - ldmCSIControllerToCreate.OwnerReferences = append(ldmCSIControllerToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + ldmCSIControllerToCreate.OwnerReferences = append(ldmCSIControllerToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) // ldmCSIController.Spec.Template.Spec.PriorityClassName = clusterInstance.Spec.LocalDiskManager.CSI.Controller.Common.PriorityClassName replicas := getReplicasFromClusterInstance(clusterInstance) ldmCSIControllerToCreate.Spec.Replicas = &replicas diff --git a/pkg/install/localdiskactioncontroller/localdiskactioncontroller.go b/pkg/install/localdiskactioncontroller/localdiskactioncontroller.go index 20946c47..f78461eb 100644 --- a/pkg/install/localdiskactioncontroller/localdiskactioncontroller.go +++ b/pkg/install/localdiskactioncontroller/localdiskactioncontroller.go @@ -3,6 +3,7 @@ package localdiskactioncontroller import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -101,7 +102,7 @@ func SetActionController(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1. deployToCreate := deployTemplate.DeepCopy() deployToCreate.Namespace = clusterInstance.Spec.TargetNamespace - deployToCreate.OwnerReferences = append(deployToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + deployToCreate.OwnerReferences = append(deployToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) deployToCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName for i, container := range deployToCreate.Spec.Template.Spec.Containers { if container.Name == ldaContainerName { diff --git a/pkg/install/localdiskmanager/localdiskmanager.go b/pkg/install/localdiskmanager/localdiskmanager.go index 0c74db0e..469ff844 100644 --- a/pkg/install/localdiskmanager/localdiskmanager.go +++ b/pkg/install/localdiskmanager/localdiskmanager.go @@ -2,6 +2,7 @@ package localdiskmanager import ( "context" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -223,7 +224,7 @@ var ldmDaemonSet = appsv1.DaemonSet{ func SetLDMDaemonSet(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.DaemonSet { ldmDaemonSetToCreate := ldmDaemonSet.DeepCopy() - ldmDaemonSetToCreate.OwnerReferences = append(ldmDaemonSetToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + ldmDaemonSetToCreate.OwnerReferences = append(ldmDaemonSetToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) ldmDaemonSetToCreate.Namespace = clusterInstance.Spec.TargetNamespace newClusterInstance := clusterInstance.DeepCopy() diff --git a/pkg/install/localstorage/localstorage.go b/pkg/install/localstorage/localstorage.go index 232b498c..429d03fa 100644 --- a/pkg/install/localstorage/localstorage.go +++ b/pkg/install/localstorage/localstorage.go @@ -2,6 +2,7 @@ package localstorage import ( "context" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -266,7 +267,7 @@ var lsDaemonSetTemplate = appsv1.DaemonSet{ func SetLSDaemonSet(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.DaemonSet { lsDaemonSetToCreate := lsDaemonSetTemplate.DeepCopy() - lsDaemonSetToCreate.OwnerReferences = append(lsDaemonSetToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + lsDaemonSetToCreate.OwnerReferences = append(lsDaemonSetToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) lsDaemonSetToCreate.Namespace = clusterInstance.Spec.TargetNamespace // lsDaemonSet.Spec.Template.Spec.PriorityClassName = clusterInstance.Spec.LocalStorage.Common.PriorityClassName lsDaemonSetToCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName diff --git a/pkg/install/lscsicontroller/localstorage_csi_controller.go b/pkg/install/lscsicontroller/localstorage_csi_controller.go index 48f62724..8547bd7c 100644 --- a/pkg/install/lscsicontroller/localstorage_csi_controller.go +++ b/pkg/install/lscsicontroller/localstorage_csi_controller.go @@ -3,6 +3,7 @@ package lscsicontroller import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -281,7 +282,7 @@ func SetLSCSIController(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.D lsCSIControllerToCreate := lsCSIController.DeepCopy() lsCSIControllerToCreate.Namespace = clusterInstance.Spec.TargetNamespace - lsCSIControllerToCreate.OwnerReferences = append(lsCSIControllerToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + lsCSIControllerToCreate.OwnerReferences = append(lsCSIControllerToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) replicas := getReplicasFromClusterInstance(clusterInstance) lsCSIControllerToCreate.Spec.Replicas = &replicas lsCSIControllerToCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName diff --git a/pkg/install/pvcautoresizer/pvc_autoresizer.go b/pkg/install/pvcautoresizer/pvc_autoresizer.go index d5746107..18a0505c 100644 --- a/pkg/install/pvcautoresizer/pvc_autoresizer.go +++ b/pkg/install/pvcautoresizer/pvc_autoresizer.go @@ -3,6 +3,7 @@ package pvcautoresizer import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -102,7 +103,7 @@ func SetPVCAutoResizer(clusterInstance *hwameistoriov1alpha1.Cluster) *appsv1.De deployToCreate := deployTemplate.DeepCopy() deployToCreate.Namespace = clusterInstance.Spec.TargetNamespace - deployToCreate.OwnerReferences = append(deployToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + deployToCreate.OwnerReferences = append(deployToCreate.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) replicas := getPVCAutoResizerReplicasFromClusterInstance(clusterInstance) deployToCreate.Spec.Replicas = &replicas deployToCreate.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName diff --git a/pkg/install/scheduler/scheduler.go b/pkg/install/scheduler/scheduler.go index df626af4..ef32a4db 100644 --- a/pkg/install/scheduler/scheduler.go +++ b/pkg/install/scheduler/scheduler.go @@ -3,6 +3,7 @@ package scheduler import ( "context" "errors" + "k8s.io/apimachinery/pkg/runtime/schema" "reflect" hwameistoriov1alpha1 "github.com/hwameistor/hwameistor-operator/api/v1alpha1" @@ -150,7 +151,7 @@ var schedulerDeploy = appsv1.Deployment{ func SetScheduler(clusterInstance *hwameistoriov1alpha1.Cluster) { schedulerDeploy.Namespace = clusterInstance.Spec.TargetNamespace - schedulerDeploy.OwnerReferences = append(schedulerDeploy.OwnerReferences, *metav1.NewControllerRef(clusterInstance, clusterInstance.GroupVersionKind())) + schedulerDeploy.OwnerReferences = append(schedulerDeploy.OwnerReferences, *metav1.NewControllerRef(clusterInstance, schema.FromAPIVersionAndKind("hwameistor.io/v1alpha1", "Cluster"))) schedulerDeploy.Spec.Template.Spec.ServiceAccountName = clusterInstance.Spec.RBAC.ServiceAccountName replicas := getSchedulerReplicasFromClusterInstance(clusterInstance) schedulerDeploy.Spec.Replicas = &replicas @@ -213,7 +214,8 @@ func (m *SchedulerMaintainer) Ensure() (*hwameistoriov1alpha1.Cluster, error) { var gotten appsv1.Deployment if err := m.Client.Get(context.TODO(), key, &gotten); err != nil { if apierrors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &schedulerDeploy); errCreate != nil { + resourceCreate := schedulerDeploy.DeepCopy() + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create Scheduler err: %v", errCreate) return newClusterInstance, errCreate } diff --git a/pkg/install/ui/ui.go b/pkg/install/ui/ui.go index a548b859..1e4f9df2 100644 --- a/pkg/install/ui/ui.go +++ b/pkg/install/ui/ui.go @@ -131,7 +131,8 @@ func (m *UIMaintainer) Ensure() (*operatorv1alpha1.Cluster, error) { var gotten appsv1.Deployment if err := m.Client.Get(context.TODO(), key, &gotten); err != nil { if apierrors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &ui); errCreate != nil { + resourceCreate := ui.DeepCopy() + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create UI err: %v", errCreate) return newClusterInstance, errCreate } diff --git a/pkg/install/ui/ui_service.go b/pkg/install/ui/ui_service.go index 1706033b..b6798f2d 100644 --- a/pkg/install/ui/ui_service.go +++ b/pkg/install/ui/ui_service.go @@ -61,7 +61,8 @@ func (m *UIServiceMaintainer) Ensure() error { var gotten corev1.Service if err := m.Client.Get(context.TODO(), key, &gotten); err != nil { if errors.IsNotFound(err) { - if errCreate := m.Client.Create(context.TODO(), &uiService); errCreate != nil { + resourceCreate := uiService.DeepCopy() + if errCreate := m.Client.Create(context.TODO(), resourceCreate); errCreate != nil { log.Errorf("Create UI Service err: %v", err) return errCreate }