k8s之patch

1、JsonPatch创建数组

 imagePullPolicy4 := "44"
   patch := []byte(fmt.Sprintf(`
[
  {
    "op": "add",
    "path": "/spec/sysctlInit/command",
    "value": ["%s"]
  }
]
   `, imagePullPolicy4))

   if err := kubeCli.Patch(context.Background(), &v1alpha1.MyReconciler{
      ObjectMeta: metav1.ObjectMeta{
         Namespace: namespace,
         Name:      name,
      }}, client.RawPatch(ctypes.JSONPatchType, patch)); err != nil {
      return err
   }

2、JsonPatch为数组添加元素

imagePullPolicy5 := "55"
   patch := []byte(fmt.Sprintf(`
[
  {
    "op": "add",
    "path": "/spec/sysctlInit/command/-",
    "value": "%s"
  }
]
   `, imagePullPolicy5))
   if err := kubeCli.Patch(context.Background(), &v1alpha1.MyReconciler{
      ObjectMeta: metav1.ObjectMeta{
         Namespace: namespace,
         Name:      name,
      }}, client.RawPatch(ctypes.JSONPatchType, patch)); err != nil {
      return err
   }

3、JsonPacth创建对象

imagePullPolicy := "44"
patch := []byte(fmt.Sprintf(`
   [
      {    
         "op": "add",
         "path": "/spec/sysctlInit",
         "value": {
             "imagePullPolicy": "%s",
             "enabled": true
         }
      }
   ]
`, imagePullPolicy))
if err := kubeCli.Patch(context.Background(), &v1alpha1.MyReconciler{
   ObjectMeta: metav1.ObjectMeta{
      Namespace: namespace,
      Name:      name,
   }}, client.RawPatch(ctypes.JSONPatchType, patch)); err != nil {
   return err
}

4、StrategicPatch

notCrpatch := []byte(`{"metadata":{"annotations":{"version": "v2"}}}`)
if err := kubeCli.Patch(context.Background(), &v1alpha1.MyReconciler{
   ObjectMeta: metav1.ObjectMeta{
      Namespace: namespace,
      Name:      name,
   }}, client.RawPatch(ctypes.StrategicMergePatchType, notCrpatch)); err != nil {
   return err
}

你可能感兴趣的:(K8S,golang)