refactor: Re-use a lot of code and move to specific packages

This commit is contained in:
TheiLLeniumStudios
2025-12-28 08:47:55 +01:00
parent 3defc8bb29
commit c19058a66e
16 changed files with 251 additions and 717 deletions
+37 -34
View File
@@ -28,18 +28,45 @@ func NewService(cfg *config.Config) *Service {
}
}
// ResourceChange represents a change event for a ConfigMap or Secret.
type ResourceChange interface {
IsNil() bool
GetEventType() EventType
GetName() string
GetNamespace() string
GetAnnotations() map[string]string
GetResourceType() ResourceType
ComputeHash(hasher *Hasher) string
}
// ConfigMapChange represents a change event for a ConfigMap.
type ConfigMapChange struct {
ConfigMap *corev1.ConfigMap
EventType EventType
}
func (c ConfigMapChange) IsNil() bool { return c.ConfigMap == nil }
func (c ConfigMapChange) GetEventType() EventType { return c.EventType }
func (c ConfigMapChange) GetName() string { return c.ConfigMap.Name }
func (c ConfigMapChange) GetNamespace() string { return c.ConfigMap.Namespace }
func (c ConfigMapChange) GetAnnotations() map[string]string { return c.ConfigMap.Annotations }
func (c ConfigMapChange) GetResourceType() ResourceType { return ResourceTypeConfigMap }
func (c ConfigMapChange) ComputeHash(h *Hasher) string { return h.HashConfigMap(c.ConfigMap) }
// SecretChange represents a change event for a Secret.
type SecretChange struct {
Secret *corev1.Secret
EventType EventType
}
func (c SecretChange) IsNil() bool { return c.Secret == nil }
func (c SecretChange) GetEventType() EventType { return c.EventType }
func (c SecretChange) GetName() string { return c.Secret.Name }
func (c SecretChange) GetNamespace() string { return c.Secret.Namespace }
func (c SecretChange) GetAnnotations() map[string]string { return c.Secret.Annotations }
func (c SecretChange) GetResourceType() ResourceType { return ResourceTypeSecret }
func (c SecretChange) ComputeHash(h *Hasher) string { return h.HashSecret(c.Secret) }
// EventType represents the type of change event.
type EventType string
@@ -77,55 +104,31 @@ func FilterDecisions(decisions []ReloadDecision) []ReloadDecision {
return result
}
// ProcessConfigMap evaluates all workloads to determine which should be reloaded.
func (s *Service) ProcessConfigMap(change ConfigMapChange, workloads []workload.WorkloadAccessor) []ReloadDecision {
if change.ConfigMap == nil {
// Process evaluates all workloads to determine which should be reloaded.
func (s *Service) Process(change ResourceChange, workloads []workload.WorkloadAccessor) []ReloadDecision {
if change.IsNil() {
return nil
}
if !s.shouldProcessEvent(change.EventType) {
if !s.shouldProcessEvent(change.GetEventType()) {
return nil
}
hash := s.hasher.HashConfigMap(change.ConfigMap)
if change.EventType == EventTypeDelete {
hash := change.ComputeHash(s.hasher)
if change.GetEventType() == EventTypeDelete {
hash = s.hasher.EmptyHash()
}
return s.processResource(
change.ConfigMap.Name,
change.ConfigMap.Namespace,
change.ConfigMap.Annotations,
ResourceTypeConfigMap,
change.GetName(),
change.GetNamespace(),
change.GetAnnotations(),
change.GetResourceType(),
hash,
workloads,
)
}
// ProcessSecret evaluates all workloads to determine which should be reloaded.
func (s *Service) ProcessSecret(change SecretChange, workloads []workload.WorkloadAccessor) []ReloadDecision {
if change.Secret == nil {
return nil
}
if !s.shouldProcessEvent(change.EventType) {
return nil
}
hash := s.hasher.HashSecret(change.Secret)
if change.EventType == EventTypeDelete {
hash = s.hasher.EmptyHash()
}
return s.processResource(
change.Secret.Name,
change.Secret.Namespace,
change.Secret.Annotations,
ResourceTypeSecret,
hash,
workloads,
)
}
func (s *Service) processResource(
resourceName string,