diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index 8f8f90d0542..b5fda0560b1 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -218,11 +218,14 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D if removeFlag { return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) } + tag := helper.CheckPodConsistency(devbox, &podList.Items[0]) + if !tag { + _ = r.Delete(ctx, &podList.Items[0]) + } case corev1.PodRunning: //if pod is running,check pod need restart - tag := r.CheckPodConsistency(ctx, devbox, podList.Items[0]) + tag := helper.CheckPodConsistency(devbox, &podList.Items[0]) if !tag { - fmt.Println("进行重启!") _ = r.Delete(ctx, &podList.Items[0]) } return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) @@ -274,42 +277,6 @@ func commitSuccess(podStatus corev1.PodPhase) bool { return false } -func (r *DevboxReconciler) CheckPodConsistency(ctx context.Context, devbox *devboxv1alpha1.Devbox, pod corev1.Pod) bool { - container := pod.Spec.Containers[0] - //check cpu and memory - if !container.Resources.Limits.Cpu().Equal(devbox.Spec.Resource["cpu"]) { - return false - } - if !container.Resources.Limits.Memory().Equal(devbox.Spec.Resource["memory"]) { - return false - } - //check ports - if len(container.Ports) != len(devbox.Spec.NetworkSpec.ExtraPorts)+1 { - fmt.Println("1111111") - return false - } - portMap := make(map[string]int) - for _, podPort := range container.Ports { - key := fmt.Sprintf("%d-%s", podPort.ContainerPort, podPort.Protocol) - portMap[key]++ - } - for _, devboxPort := range devbox.Spec.NetworkSpec.ExtraPorts { - key := fmt.Sprintf("%d-%s", devboxPort.ContainerPort, devboxPort.Protocol) - if _, found := portMap[key]; !found { - return false - } - portMap[key]-- - if portMap[key] == 0 { - delete(portMap, key) - } - } - if len(portMap) != 1 { - fmt.Println("2222222") - return false - } - return true -} - func (r *DevboxReconciler) updateDevboxCommitHistory(ctx context.Context, devbox *devboxv1alpha1.Devbox, pod *corev1.Pod) error { for i := len(devbox.Status.CommitHistory) - 1; i >= 0; i-- { if devbox.Status.CommitHistory[i].Pod == pod.Name { diff --git a/controllers/devbox/internal/controller/helper/devbox.go b/controllers/devbox/internal/controller/helper/devbox.go index 6c00cf2da36..e7e89a45b1b 100644 --- a/controllers/devbox/internal/controller/helper/devbox.go +++ b/controllers/devbox/internal/controller/helper/devbox.go @@ -19,6 +19,8 @@ import ( "crypto/elliptic" cryptorand "crypto/rand" "crypto/x509" + "fmt" + corev1 "k8s.io/api/core/v1" "encoding/pem" @@ -60,3 +62,37 @@ func GenerateSSHKeyPair() ([]byte, []byte, error) { sshPublicKey := ssh.MarshalAuthorizedKey(publicKey) return sshPublicKey, privateKeyPem, nil } + +func CheckPodConsistency(devbox *devboxv1alpha1.Devbox, pod *corev1.Pod) bool { + container := pod.Spec.Containers[0] + //check cpu and memory + if !container.Resources.Limits.Cpu().Equal(devbox.Spec.Resource["cpu"]) { + return false + } + if !container.Resources.Limits.Memory().Equal(devbox.Spec.Resource["memory"]) { + return false + } + //check ports + if len(container.Ports) != len(devbox.Spec.NetworkSpec.ExtraPorts)+1 { + return false + } + portMap := make(map[string]int) + for _, podPort := range container.Ports { + key := fmt.Sprintf("%d-%s", podPort.ContainerPort, podPort.Protocol) + portMap[key]++ + } + for _, devboxPort := range devbox.Spec.NetworkSpec.ExtraPorts { + key := fmt.Sprintf("%d-%s", devboxPort.ContainerPort, devboxPort.Protocol) + if _, found := portMap[key]; !found { + return false + } + portMap[key]-- + if portMap[key] == 0 { + delete(portMap, key) + } + } + if len(portMap) != 1 { + return false + } + return true +}