From c45f97be197ec9aef18aaab247d5bdffa251d7dd Mon Sep 17 00:00:00 2001 From: ONE7live Date: Mon, 13 May 2024 11:29:42 +0800 Subject: [PATCH] pod convert policy code cleanup in main Signed-off-by: ONE7live --- .../kosmos.io_clusterpodconvertpolicies.yaml | 2 +- pkg/kosmosctl/install/install.go | 4 +-- pkg/kosmosctl/join/join.go | 32 ++++++++++++++++--- .../kosmos/kosmos_join_controller.go | 2 +- pkg/utils/podutils/pod_convert.go | 14 ++++---- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/deploy/crds/kosmos.io_clusterpodconvertpolicies.yaml b/deploy/crds/kosmos.io_clusterpodconvertpolicies.yaml index 77e0a52fd..3e213c3ec 100644 --- a/deploy/crds/kosmos.io_clusterpodconvertpolicies.yaml +++ b/deploy/crds/kosmos.io_clusterpodconvertpolicies.yaml @@ -15,7 +15,7 @@ spec: shortNames: - cpcp singular: clusterpodconvertpolicy - scope: Namespaced + scope: Cluster versions: - name: v1alpha1 schema: diff --git a/pkg/kosmosctl/install/install.go b/pkg/kosmosctl/install/install.go index 3e6e44f58..def0394d7 100644 --- a/pkg/kosmosctl/install/install.go +++ b/pkg/kosmosctl/install/install.go @@ -654,8 +654,8 @@ func (o *CommandInstallOptions) createControlCluster() error { return fmt.Errorf("kosmosctl install run error, get control panel cluster failed: %s", err) } } else { - // 'kosmos-control-cluster' has been created, only need to create serviceExport and serviceImport - err = joinOptions.CreateServiceExportAndImport() + // 'kosmos-control-cluster' has been created, only need to create related crds + err = joinOptions.CreateTreeRelatedCRDs() if err != nil { return fmt.Errorf("kosmosctl install run error, join control panel cluster failed: %s", err) } diff --git a/pkg/kosmosctl/join/join.go b/pkg/kosmosctl/join/join.go index 25061000f..f732ef076 100644 --- a/pkg/kosmosctl/join/join.go +++ b/pkg/kosmosctl/join/join.go @@ -246,8 +246,8 @@ func (o *CommandJoinOptions) Run(args []string) error { return nil } -// CreateServiceExportAndImport only enable tree, create serviceExport and serviceImport -func (o *CommandJoinOptions) CreateServiceExportAndImport() error { +// CreateTreeRelatedCRDs only enable tree, create related crds +func (o *CommandJoinOptions) CreateTreeRelatedCRDs() error { serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil) if err != nil { return err @@ -272,6 +272,30 @@ func (o *CommandJoinOptions) CreateServiceExportAndImport() error { } klog.Info("Create CRD " + serviceImport.Name + " successful.") + clusterPodConvert, err := util.GenerateCustomResourceDefinition(manifest.ClusterPodConvert, nil) + if err != nil { + return err + } + _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), clusterPodConvert, metav1.CreateOptions{}) + if err != nil { + if !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err) + } + } + klog.Info("Create CRD " + clusterPodConvert.Name + " successful.") + + podConvert, err := util.GenerateCustomResourceDefinition(manifest.PodConvert, nil) + if err != nil { + return err + } + _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), podConvert, metav1.CreateOptions{}) + if err != nil { + if !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err) + } + } + klog.Info("Create CRD " + podConvert.Name + " successful.") + return nil } @@ -333,8 +357,8 @@ func (o *CommandJoinOptions) runCluster() error { } if o.EnableTree { - // create serviceExport and serviceImport - err := o.CreateServiceExportAndImport() + // create ClusterTree related crds + err := o.CreateTreeRelatedCRDs() if err != nil { return err } diff --git a/pkg/kubenest/controller/kosmos/kosmos_join_controller.go b/pkg/kubenest/controller/kosmos/kosmos_join_controller.go index 2f8ed3368..3d6eadcbb 100644 --- a/pkg/kubenest/controller/kosmos/kosmos_join_controller.go +++ b/pkg/kubenest/controller/kosmos/kosmos_join_controller.go @@ -428,7 +428,7 @@ func (c *KosmosJoinController) CreateCluster(ctx context.Context, request reconc // create crd cluster.kosmos.io klog.Infof("Attempting to create kosmos-clustertree CRDs for virtualcluster %s/%s...", request.Namespace, request.Name) for _, crdToCreate := range []string{file.ServiceImport, file.Cluster, - file.ServiceExport, file.PodConversionCRD, file.PodConvertPolicyCRD} { + file.ServiceExport, file.ClusterPodConvert, file.PodConvert} { crdObject, err := kosmosctl.GenerateCustomResourceDefinition(crdToCreate, nil) if err != nil { return err diff --git a/pkg/utils/podutils/pod_convert.go b/pkg/utils/podutils/pod_convert.go index b16d7eac0..7968e874e 100644 --- a/pkg/utils/podutils/pod_convert.go +++ b/pkg/utils/podutils/pod_convert.go @@ -50,7 +50,7 @@ func ConvertPod(pod *corev1.Pod, cpcp []kosmosv1alpha1.ClusterPodConvertPolicy, convertSchedulerName(pod, converters.SchedulerNameConverter) convertNodeName(pod, converters.NodeNameConverter) convertNodeSelector(pod, converters.NodeSelectorConverter) - converToleration(pod, converters.TolerationConverter) + convertToleration(pod, converters.TolerationConverter) convertAffinity(pod, converters.AffinityConverter) convertTopologySpreadConstraints(pod, converters.TopologySpreadConstraintsConverter) convertHostAliases(pod, converters.HostAliasesConverter) @@ -94,22 +94,22 @@ func convertNodeName(pod *corev1.Pod, converter *kosmosv1alpha1.NodeNameConverte } } -func converToleration(pod *corev1.Pod, conveter *kosmosv1alpha1.TolerationConverter) { - if conveter == nil { +func convertToleration(pod *corev1.Pod, converter *kosmosv1alpha1.TolerationConverter) { + if converter == nil { return } - switch conveter.ConvertType { + switch converter.ConvertType { case kosmosv1alpha1.Add: if pod.Spec.Tolerations == nil { - pod.Spec.Tolerations = conveter.Tolerations + pod.Spec.Tolerations = converter.Tolerations } case kosmosv1alpha1.Remove: pod.Spec.Tolerations = nil case kosmosv1alpha1.Replace: - pod.Spec.Tolerations = conveter.Tolerations + pod.Spec.Tolerations = converter.Tolerations default: - klog.Warningf("Skip other convert type, Tolerations: %s", conveter.ConvertType) + klog.Warningf("Skip other convert type, Tolerations: %s", converter.ConvertType) } }