1
1
package org .hango .cloud .core .gateway ;
2
2
3
+ import org .apache .commons .lang3 .math .NumberUtils ;
3
4
import org .hango .cloud .core .GlobalConfig ;
4
5
import org .hango .cloud .core .IstioModelEngine ;
5
6
import org .hango .cloud .core .editor .EditorContext ;
7
+ import org .hango .cloud .core .gateway .handler .*;
6
8
import org .hango .cloud .core .gateway .processor .DefaultModelProcessor ;
7
9
import org .hango .cloud .core .gateway .processor .NeverReturnNullModelProcessor ;
8
10
import org .hango .cloud .core .gateway .processor .RenderTwiceModelProcessor ;
21
23
import org .hango .cloud .service .PluginService ;
22
24
import org .hango .cloud .util .Const ;
23
25
import org .hango .cloud .util .constant .LogConstant ;
24
- import org .hango .cloud .util .constant .PluginConstant ;
25
26
import io .fabric8 .kubernetes .api .model .HasMetadata ;
26
27
import istio .networking .v1alpha3 .VirtualServiceOuterClass ;
27
- import org .hango .cloud .core .gateway .handler .GatewayPluginConfigMapDataHandler ;
28
- import org .hango .cloud .core .gateway .handler .GatewayPluginDataHandler ;
29
- import org .hango .cloud .core .gateway .handler .PluginOrderDataHandler ;
30
- import org .hango .cloud .core .gateway .handler .PortalDestinationRuleServiceDataHandler ;
31
- import org .hango .cloud .core .gateway .handler .PortalGatewayDataHandler ;
32
- import org .hango .cloud .core .gateway .handler .PortalServiceEntryServiceDataHandler ;
33
- import org .hango .cloud .core .gateway .handler .PortalVirtualServiceAPIDataHandler ;
34
28
import org .hango .cloud .meta .API ;
35
29
import org .hango .cloud .meta .GatewayPlugin ;
36
30
import org .hango .cloud .meta .IstioGateway ;
46
40
import org .springframework .util .CollectionUtils ;
47
41
import org .springframework .util .StringUtils ;
48
42
49
- import java .util .ArrayList ;
50
- import java .util .Collections ;
51
- import java .util .List ;
52
- import java .util .Optional ;
43
+ import java .util .*;
53
44
import java .util .stream .Collectors ;
45
+ import java .util .stream .Stream ;
54
46
55
47
@ Component
56
48
public class GatewayIstioModelEngine extends IstioModelEngine {
@@ -102,6 +94,9 @@ public GatewayIstioModelEngine(IntegratedResourceOperator operator, TemplateTran
102
94
private static final String pluginManager = "gateway/pluginManager" ;
103
95
private static final String serviceServiceEntry = "gateway/service/serviceEntry" ;
104
96
private static final String gatewayPlugin = "gateway/gatewayPlugin" ;
97
+ private static final String smartLimiter = "gateway/smartLimiter" ;
98
+ private static final String envoyFilter = "gateway/envoyFilter" ;
99
+ private static final String grpcConfigPatch = "gateway/grpcConfigPatch" ;
105
100
private static final String VIRTUAL_SERVICE = "VirtualService" ;
106
101
107
102
public List <K8sResourcePack > translate (API api ) {
@@ -126,6 +121,9 @@ public List<K8sResourcePack> translate(API api, boolean simple) {
126
121
.map (FragmentHolder ::getVirtualServiceFragment )
127
122
.collect (Collectors .toList ());
128
123
}
124
+ if (NumberUtils .INTEGER_ZERO .equals (api .getCustomDefaultRespCode ())){
125
+ api .setCustomDefaultRespCode (globalConfig .getCustomDefaultRespCode ());
126
+ }
129
127
List <String > rawVirtualServices = renderTwiceModelProcessor
130
128
.process (apiVirtualService , api ,
131
129
new PortalVirtualServiceAPIDataHandler (
@@ -170,34 +168,39 @@ public List<K8sResourcePack> translate(GatewayPlugin plugin) {
170
168
*/
171
169
private List <K8sResourcePack > generateAndAddK8sResource (RawResourceContainer rawResourceContainer ,
172
170
GatewayPlugin plugin ) {
173
- // 插件渲染GatewayPlugin资源
174
- logger .info ("{}{} start render raw GatewayPlugin CRDs " ,
171
+ // 插件渲染网关插件CR资源
172
+ logger .info ("{}{} start render raw Plugin CRs " ,
175
173
LogConstant .TRANSLATE_LOG_NOTE , LogConstant .PLUGIN_LOG_NOTE );
176
- List <K8sResourcePack > resourcePacks = configureGatewayPlugin (rawResourceContainer , plugin );
177
174
178
- // 路由级别的限流插件要渲染ConfigMap资源
179
- if (isNeedToRenderConfigMap (plugin )) {
180
- logger .info ("{}{} start render raw ConfigMap CRDs" ,
181
- LogConstant .TRANSLATE_LOG_NOTE , LogConstant .PLUGIN_LOG_NOTE );
182
- configureRateLimitConfigMap (rawResourceContainer , resourcePacks , plugin );
183
- }
175
+ // 渲染EnvoyPlugin和SmartPlugin资源
176
+ List <K8sResourcePack > envoyPlugins = configureEnvoyPlugin (rawResourceContainer , plugin );
177
+ List <K8sResourcePack > smartLimiters = configureSmartLimiter (rawResourceContainer , plugin );
184
178
185
- return resourcePacks ;
179
+ // 聚合插件CR资源
180
+ return Stream .of (envoyPlugins , smartLimiters )
181
+ .flatMap (Collection ::stream )
182
+ .collect (Collectors .toList ());
186
183
}
187
184
188
185
/**
189
- * 插件流程只有两个场景需要渲染ConfigMap资源
190
- * 1.路由级别的限流插件
191
- * 2.没有传插件类型,即批量操作的场景,此时可能会有限流插件,因此需要无差别渲染(匹配路由启用、禁用、下线场景)
192
- * 注:限流插件只有路由级别,因此路由插件是先决条件
186
+ * 插件转换为SmartLimiter CRD资源
193
187
*
194
- * @param plugin 网关插件实例(内含插件配置)
195
- * @return 是否需要渲染ConfigMap资源
188
+ * @param rawResourceContainer 存放资源的容器对象
189
+ * @param plugin 插件对象
190
+ * @return k8s资源集合
196
191
*/
197
- private boolean isNeedToRenderConfigMap (GatewayPlugin plugin ) {
198
- return plugin .isRoutePlugin () &&
199
- (StringUtils .isEmpty (plugin .getPluginType ()) || plugin .getPluginType ().equals (
200
- PluginConstant .RATE_LIMIT_PLUGIN_TYPE ));
192
+ private List <K8sResourcePack > configureSmartLimiter (RawResourceContainer rawResourceContainer ,
193
+ GatewayPlugin plugin ) {
194
+ List <K8sResourcePack > resourcePacks = new ArrayList <>();
195
+ // 将插件配置转换为SmartLimiters
196
+ List <String > rawSmartLimiters = defaultModelProcessor .process (smartLimiter , plugin ,
197
+ new SmartLimiterDataHandler (rawResourceContainer .getSmartLimiters (), globalConfig .getResourceNamespace ()));
198
+
199
+ resourcePacks .addAll (generateK8sPack (rawSmartLimiters ));
200
+ logger .info ("{}{} raw SmartLimiter CRs added ok" ,
201
+ LogConstant .TRANSLATE_LOG_NOTE , LogConstant .PLUGIN_LOG_NOTE );
202
+
203
+ return resourcePacks ;
201
204
}
202
205
203
206
/**
@@ -207,8 +210,8 @@ private boolean isNeedToRenderConfigMap(GatewayPlugin plugin) {
207
210
* @param plugin 插件对象
208
211
* @return k8s资源集合
209
212
*/
210
- private List <K8sResourcePack > configureGatewayPlugin (RawResourceContainer rawResourceContainer ,
211
- GatewayPlugin plugin ) {
213
+ private List <K8sResourcePack > configureEnvoyPlugin (RawResourceContainer rawResourceContainer ,
214
+ GatewayPlugin plugin ) {
212
215
List <K8sResourcePack > resourcePacks = new ArrayList <>();
213
216
// 插件配置放在GatewayPlugin的CRD上
214
217
List <String > rawGatewayPlugins = renderTwiceModelProcessor .process (gatewayPlugin , plugin ,
@@ -229,28 +232,6 @@ private List<K8sResourcePack> configureGatewayPlugin(RawResourceContainer rawRes
229
232
return resourcePacks ;
230
233
}
231
234
232
- /**
233
- * 限流插件转换为ConfigMap CRD资源
234
- *
235
- * @param rawResourceContainer 存放资源的容器对象
236
- * @param resourcePacks k8s资源集合
237
- * @param plugin 插件对象
238
- */
239
- private void configureRateLimitConfigMap (RawResourceContainer rawResourceContainer ,
240
- List <K8sResourcePack > resourcePacks ,
241
- GatewayPlugin plugin ) {
242
- // 限流插件需要额外的configMap配置
243
- List <String > rawConfigMaps = neverNullRenderTwiceProcessor .process (rateLimitConfigMap , plugin ,
244
- new GatewayPluginConfigMapDataHandler (
245
- rawResourceContainer .getSharedConfigs (), rateLimitConfigMapName , rateLimitNamespace ));
246
- // 加入限流插件configMap配置
247
- resourcePacks .addAll (generateK8sPack (rawConfigMaps ,
248
- new GatewayRateLimitConfigMapMerger (),
249
- new GatewayRateLimitConfigMapSubtracter (plugin .getGateway (), plugin .getRouteId ()),
250
- new EmptyResourceGenerator (new EmptyConfigMap (rateLimitConfigMapName , rateLimitNamespace ))));
251
- logger .info ("{}{} raw ConfigMap CRDs added ok" , LogConstant .TRANSLATE_LOG_NOTE , LogConstant .PLUGIN_LOG_NOTE );
252
- }
253
-
254
235
public List <K8sResourcePack > translate (Service service ) {
255
236
List <K8sResourcePack > resources = new ArrayList <>();
256
237
List <String > destinations = defaultModelProcessor .process (serviceDestinationRule , service , new PortalDestinationRuleServiceDataHandler ());
0 commit comments