Skip to content

Commit

Permalink
Merge pull request #5094 from zhy76/feat/sts
Browse files Browse the repository at this point in the history
Align federated StatefulSet's observedGeneration semantics with its native
  • Loading branch information
karmada-bot authored Jun 29, 2024
2 parents 2271a41 + d7d28ea commit c0c5569
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,46 @@ spec:
statusAggregation:
luaScript: >
function AggregateStatus(desiredObj, statusItems)
if statusItems == nil then
return desiredObj
end
if desiredObj.status == nil then
desiredObj.status = {}
end
if desiredObj.metadata.generation == nil then
desiredObj.metadata.generation = 0
end
generation = desiredObj.metadata.generation
replicas = 0
readyReplicas = 0
currentReplicas = 0
updatedReplicas = 0
availableReplicas = 0
updatedReadyReplicas = 0
updateRevision = ''
currentRevision = ''
if desiredObj.status.observedGeneration == nil then
desiredObj.status.observedGeneration = 0
end
-- Initialize status fields if status doest not exist
-- If the StatefulSet is not spread to any cluster, its status also should be aggregated
if statusItems == nil then
desiredObj.status.observedGeneration = desiredObj.metadata.generation
desiredObj.status.replicas = 0
desiredObj.status.readyReplicas = 0
desiredObj.status.currentReplicas = 0
desiredObj.status.updatedReplicas = 0
desiredObj.status.availableReplicas = 0
desiredObj.status.updatedReadyReplicas = 0
desiredObj.status.updateRevision = ''
desiredObj.status.currentRevision = ''
return desiredObj
end
local generation = desiredObj.metadata.generation
local observedGeneration = desiredObj.status.observedGeneration
local replicas = 0
local readyReplicas = 0
local currentReplicas = 0
local updatedReplicas = 0
local availableReplicas = 0
local updatedReadyReplicas = 0
local updateRevision = ''
local currentRevision = ''
local labelSelector = ''
-- Count all members that their status is updated to the latest generation
local observedResourceTemplateGenerationCount = 0
for i = 1, #statusItems do
if statusItems[i].status ~= nil and statusItems[i].status.replicas ~= nil then
replicas = replicas + statusItems[i].status.replicas
Expand All @@ -67,11 +89,32 @@ spec:
if statusItems[i].status ~= nil and statusItems[i].status.currentRevision ~= nil and statusItems[i].status.currentRevision ~= '' then
currentRevision = statusItems[i].status.currentRevision
end
if statusItems[i].status ~= nil and statusItems[i].status.observedGeneration ~= nil and statusItems[i].status.observedGeneration ~= '' then
generation = statusItems[i].status.observedGeneration
end
-- Check if the member's status is updated to the latest generation
local resourceTemplateGeneration = 0
if statusItems[i].status ~= nil and statusItems[i].status.resourceTemplateGeneration ~= nil then
resourceTemplateGeneration = statusItems[i].status.resourceTemplateGeneration
end
local memberGeneration = 0
if statusItems[i].status ~= nil and statusItems[i].status.generation ~= nil then
memberGeneration = statusItems[i].status.generation
end
local memberObservedGeneration = 0
if statusItems[i].status ~= nil and statusItems[i].status.observedGeneration ~= nil then
memberObservedGeneration = statusItems[i].status.observedGeneration
end
if resourceTemplateGeneration == generation and memberGeneration == memberObservedGeneration then
observedResourceTemplateGenerationCount = observedResourceTemplateGenerationCount + 1
end
end
desiredObj.status.observedGeneration = generation
-- Update the observed generation based on the observedResourceTemplateGenerationCount
if observedResourceTemplateGenerationCount == #statusItems then
desiredObj.status.observedGeneration = generation
else
desiredObj.status.observedGeneration = observedGeneration
end
desiredObj.status.replicas = replicas
desiredObj.status.readyReplicas = readyReplicas
desiredObj.status.currentReplicas = currentReplicas
Expand All @@ -84,8 +127,8 @@ spec:
end
statusReflection:
luaScript: >
function ReflectStatus (observedObj)
status = {}
function ReflectStatus(observedObj)
local status = {}
if observedObj == nil or observedObj.status == nil then
return status
end
Expand All @@ -98,6 +141,20 @@ spec:
status.currentRevision = observedObj.status.currentRevision
status.updatedReadyReplicas = observedObj.status.updatedReadyReplicas
status.observedGeneration = observedObj.status.observedGeneration
-- handle resource generation report
if observedObj.metadata == nil then
return status
end
status.generation = observedObj.metadata.generation
if observedObj.metadata.annotations == nil then
return status
end
local resourceTemplateGeneration = tonumber(observedObj.metadata.annotations["resourcetemplate.karmada.io/generation"])
if resourceTemplateGeneration ~= nil then
status.resourceTemplateGeneration = resourceTemplateGeneration
end
return status
end
healthInterpretation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: StatefulSet
metadata:
name: sample
namespace: test-statefulset
generation: 1
spec:
replicas: 2
serviceName: sample-statefulset-headless-service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
apiVersion: apps.kruise.io/v1beta1
kind: StatefulSet
metadata:
annotations:
resourcetemplate.karmada.io/generation: "1"
name: sample
namespace: test-statefulset
generation: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ status:
availableReplicas: 2
currentReplicas: 2
currentRevision: sample-5675547df7
generation: 1
resourceTemplateGeneration: 1
observedGeneration: 1
readyReplicas: 2
replicas: 2
Expand All @@ -19,7 +21,9 @@ status:
availableReplicas: 2
currentReplicas: 2
currentRevision: sample-5675547df7
generation: 1
observedGeneration: 1
resourceTemplateGeneration: 1
readyReplicas: 2
replicas: 2
updateRevision: sample-5675547df7
Expand Down

0 comments on commit c0c5569

Please sign in to comment.