gateway.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2026 LocoStack.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package reconciler
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/LocoStack/loco-operator/api/v1alpha1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "sigs.k8s.io/controller-runtime/pkg/client"
  21. "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
  22. logf "sigs.k8s.io/controller-runtime/pkg/log"
  23. )
  24. func ReconcileGatewayStatus(ctx context.Context, c client.Client, scheme *runtime.Scheme, stack string, obj client.Object) (*metav1.Condition, error) {
  25. log := logf.FromContext(ctx)
  26. if stack == "" {
  27. return &metav1.Condition{
  28. Type: "GatewayBound",
  29. Status: metav1.ConditionFalse,
  30. Reason: "NoStackRef",
  31. Message: "No stack reference provided",
  32. ObservedGeneration: obj.GetGeneration(),
  33. }, nil
  34. }
  35. gvk, err := apiutil.GVKForObject(obj, scheme)
  36. if err != nil {
  37. log.Error(err, "Failed to get GVK for object", "object", obj)
  38. return nil, err
  39. }
  40. gwList := &v1alpha1.ComponentList{}
  41. err = c.List(ctx, gwList, client.InNamespace(obj.GetNamespace()), client.MatchingLabels{"locostack.com/component": "Gateway"})
  42. if err != nil {
  43. log.Error(err, "Failed to list gateway Components", "namespace", obj.GetNamespace(), "stack", stack)
  44. return nil, err
  45. }
  46. gatewayFound := 0
  47. gatewayBound := 0
  48. for _, gw := range gwList.Items {
  49. if gw.Spec.StackRef.Name != stack {
  50. continue
  51. }
  52. gatewayFound += 1
  53. emStatus, hasKindStatus := gw.Status.Dependencies[gvk.Kind]
  54. if !hasKindStatus {
  55. continue
  56. }
  57. status, hasStatus := emStatus[obj.GetName()]
  58. if !hasStatus {
  59. continue
  60. }
  61. if status == obj.GetGeneration() {
  62. gatewayBound += 1
  63. }
  64. }
  65. if gatewayFound == 0 {
  66. return &metav1.Condition{
  67. Type: "GatewayBound",
  68. Status: metav1.ConditionFalse,
  69. Reason: "GatewayNotFound",
  70. Message: fmt.Sprintf("No gateway Components found that reference this %s in the Stack", gvk.Kind),
  71. ObservedGeneration: obj.GetGeneration(),
  72. }, nil
  73. } else if gatewayBound < gatewayFound {
  74. return &metav1.Condition{
  75. Type: "GatewayBound",
  76. Status: metav1.ConditionFalse,
  77. Reason: "GatewayNotBound",
  78. Message: fmt.Sprintf("%d out of %d gateway Components reference this %s in their status", gatewayBound, gatewayFound, gvk.Kind),
  79. ObservedGeneration: obj.GetGeneration(),
  80. }, nil
  81. } else {
  82. return &metav1.Condition{
  83. Type: "GatewayBound",
  84. Status: metav1.ConditionTrue,
  85. Reason: "GatewayBound",
  86. Message: fmt.Sprintf("All %d gateway Components reference this %s in their status", gatewayFound, gvk.Kind),
  87. ObservedGeneration: obj.GetGeneration(),
  88. }, nil
  89. }
  90. }