From af9e9ba62771486224e64a2d9f88a9b27ad5e828 Mon Sep 17 00:00:00 2001
From: lijianguo <lijianguo1991@outlook.com>
Date: Fri, 24 Nov 2023 14:32:39 +0800
Subject: [PATCH] feat(gateways): add addresses field

---
 rootfs/api/models/gateway.py     |  8 ++++++++
 rootfs/api/serializers.py        |  6 ++++++
 rootfs/api/tests/test_gateway.py | 10 +++++++++-
 rootfs/scheduler/mock.py         | 12 +++++++++++-
 4 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/rootfs/api/models/gateway.py b/rootfs/api/models/gateway.py
index 55a9b9d7f..62de210a9 100644
--- a/rootfs/api/models/gateway.py
+++ b/rootfs/api/models/gateway.py
@@ -88,6 +88,14 @@ def listeners(self):
                 })
         return listeners
 
+    @property
+    def addresses(self):
+        data = self._scheduler.gateways.get(self.app.id, self.name, ignore_exception=True)
+        if data.status_code != 200:
+            return []
+        addresses = data.json()["status"].get("addresses", [])
+        return addresses
+
     def refresh_to_k8s(self):
         try:
             try:
diff --git a/rootfs/api/serializers.py b/rootfs/api/serializers.py
index 819fdad6b..0fd501c2d 100644
--- a/rootfs/api/serializers.py
+++ b/rootfs/api/serializers.py
@@ -658,6 +658,12 @@ class GatewaySerializer(serializers.Serializer):
     owner = serializers.ReadOnlyField(source='owner.username')
     name = serializers.CharField(max_length=63, required=True)
     listeners = serializers.JSONField(required=False)
+    addresses = serializers.JSONField(read_only=True)
+
+    def to_representation(self, instance):
+        representation = super().to_representation(instance)
+        representation['addresses'] = instance.addresses
+        return representation
 
     @staticmethod
     def validate_port(value):
diff --git a/rootfs/api/tests/test_gateway.py b/rootfs/api/tests/test_gateway.py
index 344dbd955..415b676e4 100644
--- a/rootfs/api/tests/test_gateway.py
+++ b/rootfs/api/tests/test_gateway.py
@@ -102,7 +102,11 @@ def test_add_listener(self):
                     "port": 443,
                     "protocol": "HTTP",
                     "allowedRoutes": {"namespaces": {"from": "All"}}
-                }]
+                }],
+            "addresses": [{
+                "type": "IPAddress",
+                "value": "172.22.108.207"
+            }]
         }]
         self.assertEqual(results, json.loads(json.dumps(response.data["results"])))
 
@@ -139,6 +143,10 @@ def add_tls_listener(self, name, protocol):
                         "from": "All"
                     }
                 }
+            }],
+            "addresses": [{
+                "type": "IPAddress",
+                "value": "172.22.108.207"
             }]
         }]
         self.assertEqual(results, json.loads(json.dumps(response.data["results"])))
diff --git a/rootfs/scheduler/mock.py b/rootfs/scheduler/mock.py
index f6e52fdc6..fd9c2b837 100644
--- a/rootfs/scheduler/mock.py
+++ b/rootfs/scheduler/mock.py
@@ -712,6 +712,13 @@ def post(request, context):
                 ]
             }
         }
+    if resource_type in ["gateways"]:
+        data['status'] = {
+            "addresses": [{
+                "type": "IPAddress",
+                "value": "172.22.108.207"
+            }],
+        }
     # Handle RC / RS / Deployments
     if resource_type in ['replicationcontrollers', 'replicasets', 'deployments']:
         data['status'] = {
@@ -806,7 +813,7 @@ def put(request, context):
     return request.json()
 
 
-def patch(request, context):
+def patch(request, context):  # noqa: C901
     """Process a PATCH request to the kubernetes API"""
     url = cache_key(request.url)
     # type is the second last element
@@ -869,6 +876,9 @@ def patch(request, context):
             upsert_pods(data, url)
         elif resource_type == 'deployments':
             manage_replicasets(data, url)
+    elif resource_type in ['gateways']:
+        data['status'] = item['status']
+        cache.set(url, data, None)
     else:
         # Update the individual resource
         cache.set(url, data, None)
