From d059156f2f68b29d8f8201bfe069107e9b54913d Mon Sep 17 00:00:00 2001 From: Philipp Plotnikov Date: Mon, 19 Feb 2024 16:26:13 +0500 Subject: [PATCH] fix: handle the signal removeManagedRoutes before creating config map (#37) * fix: handle the signal removeManagedRoutes before creating config map Signed-off-by: Philipp Plotnikov * refactor: rename CreateConfigMap func to GetOrCreateConfigMap Signed-off-by: Philipp Plotnikov * refactor: update config on removeHTTPManagedRoutes only it needs Signed-off-by: Philipp Plotnikov --------- Signed-off-by: Philipp Plotnikov --- internal/utils/common.go | 2 +- pkg/plugin/httproute.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/utils/common.go b/internal/utils/common.go index 3c1a56d..f13d3ff 100644 --- a/internal/utils/common.go +++ b/internal/utils/common.go @@ -52,7 +52,7 @@ func CreateFormatter(logFormat string) log.Formatter { return formatType } -func CreateConfigMap(name string, options CreateConfigMapOptions) (*v1.ConfigMap, error) { +func GetOrCreateConfigMap(name string, options CreateConfigMapOptions) (*v1.ConfigMap, error) { clientset := options.Clientset ctx := options.Ctx configMap, err := clientset.Get(ctx, name, metav1.GetOptions{}) diff --git a/pkg/plugin/httproute.go b/pkg/plugin/httproute.go index 0145de4..aa8fb65 100644 --- a/pkg/plugin/httproute.go +++ b/pkg/plugin/httproute.go @@ -84,7 +84,7 @@ func (r *RpcPlugin) setHTTPHeaderRoute(rollout *v1alpha1.Rollout, headerRouting httpRouteClient = gatewayV1beta1.HTTPRoutes(gatewayAPIConfig.Namespace) clientset = r.Clientset.CoreV1().ConfigMaps(gatewayAPIConfig.Namespace) } - configMap, err := utils.CreateConfigMap(gatewayAPIConfig.ConfigMap, utils.CreateConfigMapOptions{ + configMap, err := utils.GetOrCreateConfigMap(gatewayAPIConfig.ConfigMap, utils.CreateConfigMapOptions{ Clientset: clientset, Ctx: ctx, }) @@ -259,7 +259,10 @@ func (r *RpcPlugin) removeHTTPManagedRoutes(managedRouteNameList []v1alpha1.Mang httpRouteClient = gatewayV1beta1.HTTPRoutes(gatewayAPIConfig.Namespace) clientset = r.Clientset.CoreV1().ConfigMaps(gatewayAPIConfig.Namespace) } - configMap, err := clientset.Get(ctx, gatewayAPIConfig.ConfigMap, metav1.GetOptions{}) + configMap, err := utils.GetOrCreateConfigMap(gatewayAPIConfig.ConfigMap, utils.CreateConfigMapOptions{ + Clientset: clientset, + Ctx: ctx, + }) if err != nil { return pluginTypes.RpcError{ ErrorString: err.Error(), @@ -278,6 +281,7 @@ func (r *RpcPlugin) removeHTTPManagedRoutes(managedRouteNameList []v1alpha1.Mang } } httpRouteRuleList := HTTPRouteRuleList(httpRoute.Spec.Rules) + isHTTPRouteRuleListChanged := false for _, managedRoute := range managedRouteNameList { managedRouteName := managedRoute.Name _, isOk := managedRouteMap[managedRouteName] @@ -285,6 +289,7 @@ func (r *RpcPlugin) removeHTTPManagedRoutes(managedRouteNameList []v1alpha1.Mang r.LogCtx.Logger.Info(fmt.Sprintf("%s is not in httpHeaderManagedRouteMap", managedRouteName)) continue } + isHTTPRouteRuleListChanged = true httpRouteRuleList, err = removeManagedRouteEntry(managedRouteMap, httpRouteRuleList, managedRouteName) if err != nil { return pluginTypes.RpcError{ @@ -292,6 +297,9 @@ func (r *RpcPlugin) removeHTTPManagedRoutes(managedRouteNameList []v1alpha1.Mang } } } + if !isHTTPRouteRuleListChanged { + return pluginTypes.RpcError{} + } oldHTTPRuleList := httpRoute.Spec.Rules httpRoute.Spec.Rules = httpRouteRuleList oldConfigMapData := make(map[string]int)