Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add devbox restart pod #5010

Merged
merged 26 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bdb9cac
add generate public and private key
bearslyricattack Aug 28, 2024
09064d8
add generate public and private key
bearslyricattack Aug 28, 2024
90cbb69
add generate public and private key
bearslyricattack Aug 28, 2024
ea4a520
add generate public and private key
bearslyricattack Aug 29, 2024
2be964e
add generate public and private key
bearslyricattack Aug 29, 2024
67b1f9b
add generate public and private key
bearslyricattack Aug 29, 2024
541b1c4
fix bug
bearslyricattack Aug 29, 2024
1f67d37
Changes
bearslyricattack Aug 29, 2024
859e390
Changes
bearslyricattack Aug 29, 2024
4cd35e2
Changes
bearslyricattack Aug 29, 2024
b504a5a
Changes
bearslyricattack Aug 29, 2024
470875d
Changes
bearslyricattack Aug 29, 2024
b3464fb
add devbox restart pod
bearslyricattack Aug 29, 2024
11c78a7
Merge branch 'labring:main' into wpy-test
bearslyricattack Aug 29, 2024
00158c1
add devbox restart pod
bearslyricattack Aug 29, 2024
4321003
Merge remote-tracking branch 'origin/wpy-test' into wpy-test
bearslyricattack Aug 29, 2024
31a2c17
add devbox restart pod
bearslyricattack Aug 29, 2024
49a0ef4
add devbox restart pod
bearslyricattack Aug 29, 2024
9b4bd69
add devbox restart pod
bearslyricattack Aug 29, 2024
8f57807
add devbox restart pod
bearslyricattack Aug 29, 2024
eb45850
add devbox restart pod
bearslyricattack Aug 29, 2024
d9050e2
add devbox restart pod
bearslyricattack Aug 29, 2024
a1e9c76
add devbox restart pod
bearslyricattack Aug 29, 2024
4eca821
add devbox restart pod
bearslyricattack Aug 30, 2024
73b2fa8
add devbox restart pod
bearslyricattack Aug 30, 2024
4dde305
add devbox restart pod
bearslyricattack Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,14 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
if removeFlag {
return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0])
}
if !helper.CheckPodConsistency(devbox, &podList.Items[0]) {
_ = r.Delete(ctx, &podList.Items[0])
}
case corev1.PodRunning:
// we do not recreate pod if it is running, even if pod does not have expected values
// update commit history status to success by pod name
//if pod is running,check pod need restart
if !helper.CheckPodConsistency(devbox, &podList.Items[0]) {
_ = r.Delete(ctx, &podList.Items[0])
}
return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0])
case corev1.PodSucceeded:
if controllerutil.RemoveFinalizer(&podList.Items[0], FinalizerName) {
Expand Down Expand Up @@ -313,6 +318,10 @@ func (r *DevboxReconciler) generateDevboxPod(ctx context.Context, devbox *devbox
Name: "SEALOS_COMMIT_IMAGE_SQUASH",
Value: fmt.Sprintf("%v", devbox.Spec.Squash),
},
{
Name: "SEALOS_DEVBOX_NAME",
Value: devbox.ObjectMeta.Namespace + devbox.ObjectMeta.Name,
},
{
Name: "SEALOS_DEVBOX_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
Expand Down
60 changes: 44 additions & 16 deletions controllers/devbox/internal/controller/helper/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
package helper

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/ed25519"
cryptorand "crypto/rand"
"crypto/x509"

"golang.org/x/crypto/ssh"

"encoding/pem"
"fmt"

"golang.org/x/crypto/ssh"
corev1 "k8s.io/api/core/v1"

devboxv1alpha1 "github.com/labring/sealos/controllers/devbox/api/v1alpha1"
)
Expand All @@ -40,23 +41,50 @@ func GetLastSuccessCommitHistory(devbox *devboxv1alpha1.Devbox) *devboxv1alpha1.
}

func GenerateSSHKeyPair() ([]byte, []byte, error) {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), cryptorand.Reader)
pubKey, privKey, err := ed25519.GenerateKey(cryptorand.Reader)
if err != nil {
return []byte(""), []byte(""), err
return nil, nil, err
}
public := &privateKey.PublicKey
derPrivateKey, err := x509.MarshalECPrivateKey(privateKey)
pemKey, err := ssh.MarshalPrivateKey(privKey, "")
if err != nil {
return []byte(""), []byte(""), err
return nil, nil, err
}
privateKeyPem := pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: derPrivateKey,
})
publicKey, err := ssh.NewPublicKey(public)
privateKey := pem.EncodeToMemory(pemKey)
publicKey, err := ssh.NewPublicKey(pubKey)
if err != nil {
return []byte(""), []byte(""), err
return nil, nil, err
}
sshPublicKey := ssh.MarshalAuthorizedKey(publicKey)
return sshPublicKey, privateKeyPem, nil
return sshPublicKey, privateKey, 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)
}
}
return len(portMap) == 1
}
Loading