This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
/
task.go
157 lines (126 loc) · 3.87 KB
/
task.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package resources
import (
"fmt"
"time"
"github.com/cosi-project/runtime/pkg/resource"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/siderolabs/theila/api/rpc"
)
const (
// TaskStatusType is the type of TaskStatus resource.
TaskStatusType = resource.Type("TaskStatuses.theila.sidero.dev")
// TaskStateType is the type of TaskState resource.
TaskStateType = resource.Type("TaskState.theila.sidero.dev")
)
// TaskStatus represents the ongoing K8s upgrade task status.
type TaskStatus struct {
spec *rpc.TaskStatusSpec
md resource.Metadata
}
// NewTaskStatus creates new TaskStatus resource.
func NewTaskStatus(ns resource.Namespace, id resource.ID) *TaskStatus {
r := &TaskStatus{
md: resource.NewMetadata(ns, TaskStatusType, id, resource.VersionUndefined),
spec: &rpc.TaskStatusSpec{
Phase: rpc.TaskStatusSpec_RUNNING,
},
}
r.md.BumpVersion()
return r
}
// Metadata implements resource.Resource.
func (r *TaskStatus) Metadata() *resource.Metadata {
return &r.md
}
// Spec implements resource.Resource.
func (r *TaskStatus) Spec() interface{} {
return r.spec
}
func (r *TaskStatus) String() string {
return fmt.Sprintf("TaskStatus(%s): owner %q", r.md.ID(), r.md.Owner())
}
// DeepCopy implements resource.Resource.
func (r *TaskStatus) DeepCopy() resource.Resource {
return &TaskStatus{
md: r.md,
spec: proto.Clone(r.spec).(*rpc.TaskStatusSpec),
}
}
// UnmarshalProto implements protobuf.ResourceUnmarshaler.
func (r *TaskStatus) UnmarshalProto(md *resource.Metadata, protoSpec []byte) error {
r.md = *md
r.spec = &rpc.TaskStatusSpec{}
return proto.Unmarshal(protoSpec, r.spec)
}
// SetPhase updates status phase.
func (r *TaskStatus) SetPhase(phase rpc.TaskStatusSpec_Phase) {
r.spec.Phase = phase
if phase != rpc.TaskStatusSpec_RUNNING {
r.spec.FinishedAt = timestamppb.New(time.Now())
}
}
// TypedSpec returns the exact type of the spec of this resource.
func (r *TaskStatus) TypedSpec() *rpc.TaskStatusSpec {
return r.spec
}
// SetProgress updates status progress.
func (r *TaskStatus) SetProgress(progress float32) {
r.spec.Progress = progress
}
// SetError sets status error.
func (r *TaskStatus) SetError(err error) {
r.spec.Error = err.Error()
}
// SetVersions updates the versions of the upgrade task.
func (r *TaskStatus) SetVersions(from, to string) {
r.spec.FromVersion = from
r.spec.ToVersion = to
}
// TaskState represents the ongoing K8s upgrade task status.
type TaskState struct {
spec *rpc.TaskStateSpec
md resource.Metadata
}
// NewTaskState creates new TaskState resource.
func NewTaskState(ns resource.Namespace, id, statusID resource.ID) *TaskState {
r := &TaskState{
md: resource.NewMetadata(ns, TaskStateType, id, resource.VersionUndefined),
spec: &rpc.TaskStateSpec{
StatusId: statusID,
},
}
r.md.BumpVersion()
return r
}
// Metadata implements resource.Resource.
func (r *TaskState) Metadata() *resource.Metadata {
return &r.md
}
// Spec implements resource.Resource.
func (r *TaskState) Spec() interface{} {
return r.spec
}
// TypedSpec returns typed representation for the spec.
func (r *TaskState) TypedSpec() *rpc.TaskStateSpec {
return r.spec
}
func (r *TaskState) String() string {
return fmt.Sprintf("TaskState(%s): owner %q", r.md.ID(), r.md.Owner())
}
// DeepCopy implements resource.Resource.
func (r *TaskState) DeepCopy() resource.Resource {
return &TaskState{
md: r.md,
spec: proto.Clone(r.spec).(*rpc.TaskStateSpec),
}
}
// UnmarshalProto implements protobuf.ResourceUnmarshaler.
func (r *TaskState) UnmarshalProto(md *resource.Metadata, protoSpec []byte) error {
r.md = *md
r.spec = &rpc.TaskStateSpec{}
return proto.Unmarshal(protoSpec, r.spec)
}