package base import ( "bytes" "context" "go.uber.org/zap" "io" "jcc-schedule/global" "jcc-schedule/pkg/apiserver" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/client-go/kubernetes" "k8s.io/client-go/restmapper" sigyaml "sigs.k8s.io/yaml" ) type OperateStruct struct { ClusterName string `json:"clusterName"` YamlString string `json:"yamlString"` } func (p *OperateStruct) Apply() error { d := yaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(p.YamlString), 4096) var err error for { var rawObj runtime.RawExtension err = d.Decode(&rawObj) if err == io.EOF { break } if err != nil { global.PCM_LOG.Error("Decode err", zap.Error(err)) return err } obj := &unstructured.Unstructured{} syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj) unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj) if err != nil { global.PCM_LOG.Error("DefaultUnstructuredConverter err", zap.Error(err)) return err } unStructureObj := &unstructured.Unstructured{Object: unstructuredMap} gvr, err := GetGVR(apiserver.ApiServer.ClientSetMap[p.ClusterName], unStructureObj.GroupVersionKind()) if err != nil { global.PCM_LOG.Error("GetGVR err", zap.Error(err)) return err } unstructuredYaml, err := sigyaml.Marshal(unStructureObj) if err != nil { global.PCM_LOG.Error("Marshal err", zap.Error(err)) return err } if unStructureObj.GetNamespace() == "" { unStructureObj.SetNamespace("default") } _, getErr := apiserver.ApiServer.DynamicClientMap[p.ClusterName].Resource(gvr).Namespace(unStructureObj.GetNamespace()).Get(context.Background(), unStructureObj.GetName(), metav1.GetOptions{}) if getErr != nil { _, createErr := apiserver.ApiServer.DynamicClientMap[p.ClusterName].Resource(gvr).Namespace(unStructureObj.GetNamespace()).Create(context.Background(), unStructureObj, metav1.CreateOptions{}) if createErr != nil { global.PCM_LOG.Error("createErr err", zap.Error(createErr)) return createErr } } force := true _, err = apiserver.ApiServer.DynamicClientMap[p.ClusterName].Resource(gvr). Namespace(unStructureObj.GetNamespace()). Patch(context.Background(), unStructureObj.GetName(), types.ApplyPatchType, unstructuredYaml, metav1.PatchOptions{ FieldManager: unStructureObj.GetName(), Force: &force, }) if err != nil { global.PCM_LOG.Error("err", zap.Error(err)) return err } } return nil } // GetGVR 获取GVR func GetGVR(clientSet *kubernetes.Clientset, gvk schema.GroupVersionKind) (schema.GroupVersionResource, error) { gr, err := restmapper.GetAPIGroupResources(clientSet) if err != nil { return schema.GroupVersionResource{}, err } mapper := restmapper.NewDiscoveryRESTMapper(gr) mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version) if err != nil { global.PCM_LOG.Error("RESTMapping", zap.Error(err)) return schema.GroupVersionResource{}, err } return mapping.Resource, nil }