forked from temporalio/temporal-worker-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_mapper.go
More file actions
220 lines (179 loc) · 7.38 KB
/
state_mapper.go
File metadata and controls
220 lines (179 loc) · 7.38 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2024 Datadog, Inc.
package controller
import (
"strings"
"github.com/temporalio/temporal-worker-controller/api/v1alpha1"
"github.com/temporalio/temporal-worker-controller/internal/k8s"
"github.com/temporalio/temporal-worker-controller/internal/temporal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// stateMapper maps between Kubernetes and Temporal states
type stateMapper struct {
k8sState *k8s.DeploymentState
temporalState *temporal.TemporalWorkerState
workerDeploymentName string
}
// newStateMapper creates a new state mapper
func newStateMapper(k8sState *k8s.DeploymentState, temporalState *temporal.TemporalWorkerState, workerDeploymentName string) *stateMapper {
return &stateMapper{
k8sState: k8sState,
temporalState: temporalState,
workerDeploymentName: workerDeploymentName,
}
}
// mapToStatus converts the states to a CRD status
func (m *stateMapper) mapToStatus(targetBuildID string) *v1alpha1.TemporalWorkerDeploymentStatus {
status := &v1alpha1.TemporalWorkerDeploymentStatus{
VersionConflictToken: m.temporalState.VersionConflictToken,
}
status.LastModifierIdentity = m.temporalState.LastModifierIdentity
// Compute total replicas from all managed deployments for /scale subresource
// This allows HPA/KEDA to query the current replica count
var totalReplicas int32
for _, deployment := range m.k8sState.Deployments {
if deployment.Status.Replicas > 0 {
totalReplicas += deployment.Status.Replicas
}
}
status.Replicas = totalReplicas
// Set label selector for /scale subresource - allows HPA/KEDA to discover pods
// Uses app.kubernetes.io/name label since all managed pods share this label
// Note: workerDeploymentName is namespace/name, but we only want the name part for labels
name := m.workerDeploymentName
if idx := strings.LastIndex(name, "/"); idx >= 0 {
name = name[idx+1:]
}
status.Selector = "app.kubernetes.io/name=" + name
// Get build IDs directly from temporal state
currentBuildID := m.temporalState.CurrentBuildID
rampingBuildID := m.temporalState.RampingBuildID
// Set current version
status.CurrentVersion = m.mapCurrentWorkerDeploymentVersionByBuildID(currentBuildID)
// Set target version (desired version)
status.TargetVersion = m.mapTargetWorkerDeploymentVersionByBuildID(targetBuildID)
if rampingBuildID == targetBuildID {
status.TargetVersion.RampingSince = m.temporalState.RampingSince
status.TargetVersion.RampLastModifiedAt = m.temporalState.RampLastModifiedAt
rampPercentage := m.temporalState.RampPercentage
status.TargetVersion.RampPercentage = &rampPercentage
}
// Add deprecated versions
var deprecatedVersions []*v1alpha1.DeprecatedWorkerDeploymentVersion
for buildID := range m.k8sState.Deployments {
// Skip current and target versions
if buildID == currentBuildID || buildID == targetBuildID {
continue
}
versionStatus := m.mapDeprecatedWorkerDeploymentVersionByBuildID(buildID)
if versionStatus != nil {
deprecatedVersions = append(deprecatedVersions, versionStatus)
}
}
status.DeprecatedVersions = deprecatedVersions
// Set version count from temporal state (directly from VersionSummaries via Versions map)
status.VersionCount = int32(len(m.temporalState.Versions))
return status
}
// mapCurrentWorkerDeploymentVersionByBuildID creates a current version status from the states using buildID
func (m *stateMapper) mapCurrentWorkerDeploymentVersionByBuildID(buildID string) *v1alpha1.CurrentWorkerDeploymentVersion {
if buildID == "" {
return nil
}
version := &v1alpha1.CurrentWorkerDeploymentVersion{
BaseWorkerDeploymentVersion: v1alpha1.BaseWorkerDeploymentVersion{
BuildID: buildID,
Status: v1alpha1.VersionStatusNotRegistered,
},
}
// Set deployment reference if it exists
if deployment, exists := m.k8sState.Deployments[buildID]; exists {
version.Deployment = m.k8sState.DeploymentRefs[buildID]
// Check deployment health
healthy, healthySince := k8s.IsDeploymentHealthy(deployment)
if healthy {
version.HealthySince = healthySince
}
}
// Set version status from temporal state
if temporalVersion, exists := m.temporalState.Versions[buildID]; exists {
version.Status = temporalVersion.Status
// Set task queues
version.TaskQueues = append(version.TaskQueues, temporalVersion.TaskQueues...)
}
return version
}
// mapTargetWorkerDeploymentVersionByBuildID creates a target version status from the states using buildID
func (m *stateMapper) mapTargetWorkerDeploymentVersionByBuildID(buildID string) v1alpha1.TargetWorkerDeploymentVersion {
version := v1alpha1.TargetWorkerDeploymentVersion{
BaseWorkerDeploymentVersion: v1alpha1.BaseWorkerDeploymentVersion{
BuildID: buildID,
Status: v1alpha1.VersionStatusNotRegistered,
},
}
if buildID == "" {
return version
}
// Set deployment reference if it exists
if deployment, exists := m.k8sState.Deployments[buildID]; exists {
version.Deployment = m.k8sState.DeploymentRefs[buildID]
// Check deployment health
healthy, healthySince := k8s.IsDeploymentHealthy(deployment)
if healthy {
version.HealthySince = healthySince
}
}
// Set version status from temporal state
if temporalVersion, exists := m.temporalState.Versions[buildID]; exists {
version.Status = temporalVersion.Status
// Set ramp percentage if this is a ramping version
if temporalVersion.Status == v1alpha1.VersionStatusRamping && m.temporalState.RampPercentage > 0 {
rampPercentage := m.temporalState.RampPercentage
version.RampPercentage = &rampPercentage
}
// Set task queues
version.TaskQueues = append(version.TaskQueues, temporalVersion.TaskQueues...)
// Set test workflows
version.TestWorkflows = append(version.TestWorkflows, temporalVersion.TestWorkflows...)
}
return version
}
// mapDeprecatedWorkerDeploymentVersionByBuildID creates a deprecated version status from the states using buildID
func (m *stateMapper) mapDeprecatedWorkerDeploymentVersionByBuildID(buildID string) *v1alpha1.DeprecatedWorkerDeploymentVersion {
if buildID == "" {
return nil
}
eligibleForDeletion := false
if vInfo, exists := m.temporalState.Versions[buildID]; exists {
eligibleForDeletion = vInfo.Status == v1alpha1.VersionStatusDrained && vInfo.NoTaskQueuesHaveVersionedPoller
}
version := &v1alpha1.DeprecatedWorkerDeploymentVersion{
BaseWorkerDeploymentVersion: v1alpha1.BaseWorkerDeploymentVersion{
BuildID: buildID,
Status: v1alpha1.VersionStatusNotRegistered,
},
EligibleForDeletion: eligibleForDeletion,
}
// Set deployment reference if it exists
if deployment, exists := m.k8sState.Deployments[buildID]; exists {
version.Deployment = m.k8sState.DeploymentRefs[buildID]
// Check deployment health
healthy, healthySince := k8s.IsDeploymentHealthy(deployment)
if healthy {
version.HealthySince = healthySince
}
}
// Set version status from temporal state
if temporalVersion, exists := m.temporalState.Versions[buildID]; exists {
version.Status = temporalVersion.Status
// Set drained since if available
if temporalVersion.DrainedSince != nil {
drainedSince := metav1.NewTime(*temporalVersion.DrainedSince)
version.DrainedSince = &drainedSince
}
// Set task queues
version.TaskQueues = append(version.TaskQueues, temporalVersion.TaskQueues...)
}
return version
}