Skip to content

Commit

Permalink
add devbox restart pod
Browse files Browse the repository at this point in the history
  • Loading branch information
bearslyricattack committed Aug 29, 2024
1 parent 9b4bd69 commit 8f57807
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
43 changes: 5 additions & 38 deletions controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions controllers/devbox/internal/controller/helper/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"crypto/elliptic"
cryptorand "crypto/rand"
"crypto/x509"
"fmt"
corev1 "k8s.io/api/core/v1"

"encoding/pem"

Expand Down Expand Up @@ -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
}

0 comments on commit 8f57807

Please sign in to comment.