Skip to content

Commit

Permalink
fix: 修正patch操作
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Apr 25, 2023
1 parent 8cf2196 commit 997b04a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
18 changes: 5 additions & 13 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
podrestarterv1alpha1 "github.com/myoperator/poddeployer/pkg/apis/podDeployer/v1alpha1"
"github.com/myoperator/poddeployer/pkg/util"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
Expand Down Expand Up @@ -52,25 +51,18 @@ func (r *PodDeployerController) Reconcile(ctx context.Context, req reconcile.Req
}

if len(podDeployer.Spec.DeploymentSpec.Template.Spec.Containers) != 1 || len(podDeployer.Spec.PriorityImages) != 0 {
klog.Info("do patch image to pods...")
time.Sleep(time.Second * 30)
// 由deployment找到pods
pods := util.GetPodsByDeployment(podDeployer.Name, podDeployer.Namespace)
klog.Info(len(pods))
klog.Info("do patch image to deployment...")
klog.Info(SetOtherContainers)
// 执行patch操作
for _, container := range SetOtherContainers {
for _, pod := range pods {
wg.Add(1)
go patchContainer(&pod, &container)
}
time.Sleep(time.Second * 15)
patchDeployment(podDeployer.Name, podDeployer.Namespace, &container)
}
wg.Wait()
}


}

return reconcile.Result{}, nil

}

// InjectClient 使用controller-runtime 需要注入的client
Expand Down
26 changes: 10 additions & 16 deletions pkg/controller/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ func mutateDeployment(podDeployer *podrestarterv1alpha1.Poddeployer, deployment
deployment.Spec.Template.Labels["podDeployer"] = podDeployer.Name
}




// 为image排序
func calculatePriorityImages(podDeployer *podrestarterv1alpha1.Poddeployer) []podrestarterv1alpha1.PriorityImage {
// 找出对应的
imageList := podDeployer.Spec.PriorityImages
Expand All @@ -95,6 +93,7 @@ func calculatePriorityImages(podDeployer *podrestarterv1alpha1.Poddeployer) []po
return imageList
}

// 替换image,并返回剩下排序后的images
func handleDeploymentImageSort(priorityImages []podrestarterv1alpha1.PriorityImage, podDeployer *podrestarterv1alpha1.Poddeployer) ([]v1.Container, error){
if len(priorityImages) != len(podDeployer.Spec.DeploymentSpec.Template.Spec.Containers) {
return nil, errors.New("priorityImage len error")
Expand Down Expand Up @@ -122,15 +121,14 @@ type patchOperation struct {
Value interface{} `json:"value,omitempty"`
}

// patchDeployment 使用deployment patch的方式顺序执行pod
func patchDeployment(deploymentName, namespace string, container *v1.Container) {
klog.Info("do deployment patch....")

func patchContainer(pod *v1.Pod, container *v1.Container) {
klog.Info("do patch....")
defer wg.Done()
// FIXME: 这里有很大的问题,不能使用add 来原地升级pod。。。
pa := []patchOperation{}
pa := make([]patchOperation, 0)
p := patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/containers/-"),
Path: fmt.Sprintf("/spec/template/spec/containers/-"),
Value: container,
}
pa = append(pa, p)
Expand All @@ -140,9 +138,6 @@ func patchContainer(pod *v1.Pod, container *v1.Container) {
return
}

//patch := fmt.Sprintf(`[{"op": "add", "path": "/spec/containers/-", "value": "%v"}]`, *container)
//patchBytes := []byte(patch)


jsonPatch, err := jsonpatch.DecodePatch(patchBytes)
if err != nil {
Expand All @@ -154,13 +149,12 @@ func patchContainer(pod *v1.Pod, container *v1.Container) {
klog.Error("json Marshal error: ", err)
return
}
fmt.Println(string(jsonPatchBytes))
_, err = k8sconfig.ClientSet.CoreV1().Pods(pod.Namespace).
Patch(context.TODO(), pod.Name, types.JSONPatchType,
klog.Info(string(jsonPatchBytes))
_, err = k8sconfig.ClientSet.AppsV1().Deployments(namespace).
Patch(context.TODO(), deploymentName, types.JSONPatchType,
jsonPatchBytes, metav1.PatchOptions{})
if err != nil {
klog.Error("patch error: ", err)
return
}

}
14 changes: 13 additions & 1 deletion yaml/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ spec:
image: nginx:1.14.2
ports:
- containerPort: 80
priority_images:
- name: example3
image: nginx:1.14.2
ports:
- containerPort: 81
- name: example4
image: nginx:1.18-alpine
ports:
- containerPort: 82
priority_images: # image的权重排序
- image: example1
value: 200
- image: example2
value: 50
- image: example3
value: 100
- image: example4
value: 1000



Expand Down

0 comments on commit 997b04a

Please sign in to comment.