forked from nocalhost/nocalhost
/
duplicate_raw_pod_controller.go
112 lines (95 loc) · 2.95 KB
/
duplicate_raw_pod_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* This source code is licensed under the Apache License Version 2.0.
*/
package controller
import (
"context"
"encoding/json"
"fmt"
_const "github.com/lsutils/nocalhost/internal/nhctl/const"
"github.com/lsutils/nocalhost/internal/nhctl/model"
"github.com/lsutils/nocalhost/pkg/nhctl/log"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"time"
)
type DuplicateRawPodController struct {
*Controller
}
func (r *DuplicateRawPodController) ReplaceImage(ctx context.Context, ops *model.DevStartOptions) error {
r.Client.Context(ctx)
originalPod, err := r.Client.GetPod(r.Name)
if err != nil {
return err
}
// Check if pod managed by controller
if len(originalPod.OwnerReferences) > 0 {
return errors.New(fmt.Sprintf("Pod %s is manged by a controller, can not enter DevMode", r.Name))
}
if r.IsInReplaceDevMode() {
if len(originalPod.Annotations) > 0 {
podSpec, ok := originalPod.Annotations[_const.OriginWorkloadDefinition]
if !ok {
return errors.New(
fmt.Sprintf(
"Annotation %s not found, failed to rollback", _const.OriginWorkloadDefinition,
),
)
}
if err = json.Unmarshal([]byte(podSpec), originalPod); err != nil {
return errors.Wrap(err, "")
}
} else {
return errors.New(
fmt.Sprintf(
"Annotation %s not found, failed to rollback", _const.OriginWorkloadDefinition,
),
)
}
} else {
if len(originalPod.Annotations) > 0 {
podSpec, ok := originalPod.Annotations[_const.OriginWorkloadDefinition]
var oPodSpec = corev1.Pod{}
if ok {
if err = json.Unmarshal([]byte(podSpec), &oPodSpec); err == nil {
originalPod = &oPodSpec
}
}
}
}
labelsMap := r.getDuplicateLabelsMap()
originalPod.Name = r.getDuplicateResourceName()
originalPod.Labels = labelsMap
originalPod.Status = corev1.PodStatus{}
originalPod.ResourceVersion = ""
originalPod.Annotations = r.getDevContainerAnnotations(ops.Container, originalPod.Annotations)
devContainer, sideCarContainer, devModeVolumes, err :=
r.genContainersAndVolumes(&originalPod.Spec, ops.Container, ops.DevImage, ops.StorageClass, true)
if err != nil {
return err
}
patchDevContainerToPodSpec(&originalPod.Spec, ops.Container, devContainer, sideCarContainer, devModeVolumes)
log.Info("Create duplicate dev pod...")
if _, err = r.Client.CreatePod(originalPod); err != nil {
return err
}
r.patchAfterDevContainerReplaced(ops.Container, originalPod.Kind, originalPod.Name)
r.waitDevPodToBeReady()
return nil
}
func (r *DuplicateRawPodController) RollBack(reset bool) error {
deploys, err := r.GetPodList()
if err != nil {
return err
}
if len(deploys) != 1 {
if !reset {
return errors.New(fmt.Sprintf("Duplicate pod num is %d (not 1)?", len(deploys)))
} else if len(deploys) == 0 {
log.Warnf("Duplicate pod num is %d (not 1)?", len(deploys))
return nil
}
}
return r.Client.DeletePod(deploys[0].Name, false, 1*time.Second)
}