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
113
114
115
116
117
118
119
120
121
|
/*
Copyright 2023 The KubeEdge Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nodeupgradecontroller
import (
"fmt"
"time"
fsmapi "github.com/kubeedge/api/apis/fsm/v1alpha1"
v1alpha12 "github.com/kubeedge/api/apis/fsm/v1alpha1"
"github.com/kubeedge/api/apis/operations/v1alpha1"
"github.com/kubeedge/kubeedge/cloud/pkg/common/client"
"github.com/kubeedge/kubeedge/pkg/util/fsm"
)
func currentUpgradeNodeState(id, nodeName string) (v1alpha12.State, error) {
v, ok := cache.CacheMap.Load(id)
if !ok {
return "", fmt.Errorf("can not find task %s", id)
}
task := v.(*v1alpha1.NodeUpgradeJob)
var state v1alpha12.State
for _, status := range task.Status.Status {
if status.NodeName == nodeName {
state = status.State
break
}
}
if state == "" {
state = v1alpha12.TaskInit
}
return state, nil
}
func updateUpgradeNodeState(id, nodeName string, state v1alpha12.State, event fsm.Event) error {
v, ok := cache.CacheMap.Load(id)
if !ok {
return fmt.Errorf("can not find task %s", id)
}
task := v.(*v1alpha1.NodeUpgradeJob)
newTask := task.DeepCopy()
status := newTask.Status.DeepCopy()
for i, nodeStatus := range status.Status {
if nodeStatus.NodeName == nodeName {
status.Status[i] = v1alpha1.TaskStatus{
NodeName: nodeName,
State: state,
Event: event.Type,
Action: event.Action,
Time: time.Now().UTC().Format(time.RFC3339),
Reason: event.Msg,
}
break
}
}
err := patchStatus(newTask, *status, client.GetCRDClient())
if err != nil {
return err
}
return nil
}
func NewUpgradeNodeFSM(taskName, nodeName string) *fsm.FSM {
fsm := &fsm.FSM{}
return fsm.NodeName(nodeName).ID(taskName).Guard(fsmapi.UpgradeRule).StageSequence(fsmapi.UpdateStageSequence).CurrentFunc(currentUpgradeNodeState).UpdateFunc(updateUpgradeNodeState)
}
func NewUpgradeTaskFSM(taskName string) *fsm.FSM {
fsm := &fsm.FSM{}
return fsm.ID(taskName).Guard(fsmapi.UpgradeRule).StageSequence(fsmapi.UpdateStageSequence).CurrentFunc(currentUpgradeTaskState).UpdateFunc(updateUpgradeTaskState)
}
func currentUpgradeTaskState(id, _ string) (v1alpha12.State, error) {
v, ok := cache.CacheMap.Load(id)
if !ok {
return "", fmt.Errorf("can not find task %s", id)
}
task := v.(*v1alpha1.NodeUpgradeJob)
state := task.Status.State
if state == "" {
state = v1alpha12.TaskInit
}
return state, nil
}
func updateUpgradeTaskState(id, _ string, state v1alpha12.State, event fsm.Event) error {
v, ok := cache.CacheMap.Load(id)
if !ok {
return fmt.Errorf("can not find task %s", id)
}
task := v.(*v1alpha1.NodeUpgradeJob)
newTask := task.DeepCopy()
status := newTask.Status.DeepCopy()
status.Event = event.Type
status.Action = event.Action
status.Reason = event.Msg
status.State = state
status.Time = time.Now().UTC().Format(time.RFC3339)
err := patchStatus(newTask, *status, client.GetCRDClient())
if err != nil {
return err
}
return nil
}
|