From 2488b93821706b3d1163f17396c5eb6f1bd19aaa Mon Sep 17 00:00:00 2001 From: Tharsanan1 Date: Thu, 10 Oct 2024 10:32:46 +0530 Subject: [PATCH] Improve code --- .../internal/k8sClient/k8s_client.go | 20 ++++++++++++------- .../messaging/notification_listener.go | 5 +++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/apim-apk-agent/internal/k8sClient/k8s_client.go b/apim-apk-agent/internal/k8sClient/k8s_client.go index e28d0706..b6937b73 100644 --- a/apim-apk-agent/internal/k8sClient/k8s_client.go +++ b/apim-apk-agent/internal/k8sClient/k8s_client.go @@ -454,10 +454,11 @@ func UpdateRateLimitPolicyCR(policy eventhubTypes.RateLimitPolicy, k8sClient cli func DeploySubscriptionRateLimitPolicyCR(policy eventhubTypes.SubscriptionPolicy, k8sClient client.Client) { conf, _ := config.ReadConfigs() crRateLimitPolicy := dpv1alpha3.RateLimitPolicy{} - if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: conf.DataPlane.Namespace, Name: getSha1Value(fmt.Sprintf("%s-%s",policy.Name, policy.TenantDomain))}, &crRateLimitPolicy); err != nil { + crName := PrepareSubscritionPolicyCRName(policy.Name, policy.TenantDomain) + if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: conf.DataPlane.Namespace, Name: crName}, &crRateLimitPolicy); err != nil { crRateLimitPolicy = dpv1alpha3.RateLimitPolicy{ ObjectMeta: metav1.ObjectMeta{ - Name: getSha1Value(fmt.Sprintf("%s-%s",policy.Name, policy.TenantDomain)), + Name: crName, Namespace: conf.DataPlane.Namespace, }, Spec: dpv1alpha3.RateLimitPolicySpec{ @@ -521,7 +522,7 @@ func DeployAIRateLimitPolicyFromCPPolicy(policy eventhubTypes.SubscriptionPolicy crRateLimitPolicies := dpv1alpha3.AIRateLimitPolicy{ ObjectMeta: metav1.ObjectMeta{ - Name: getSha1Value(fmt.Sprintf("%s-%s",policy.Name, policy.TenantDomain)), + Name: PrepareSubscritionPolicyCRName(policy.Name, policy.TenantDomain), Namespace: conf.DataPlane.Namespace, }, Spec: dpv1alpha3.AIRateLimitPolicySpec{ @@ -555,10 +556,10 @@ func DeployAIRateLimitPolicyFromCPPolicy(policy eventhubTypes.SubscriptionPolicy } // UnDeploySubscriptionRateLimitPolicyCR applies the given RateLimitPolicies struct to the Kubernetes cluster. -func UnDeploySubscriptionRateLimitPolicyCR(policyName string, k8sClient client.Client) { +func UnDeploySubscriptionRateLimitPolicyCR(crName string, k8sClient client.Client) { conf, _ := config.ReadConfigs() crRateLimitPolicies := &dpv1alpha1.RateLimitPolicy{} - if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: conf.DataPlane.Namespace, Name: getSha1Value(policyName)}, crRateLimitPolicies); err != nil { + if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: conf.DataPlane.Namespace, Name: crName}, crRateLimitPolicies); err != nil { loggers.LoggerK8sClient.Error("Unable to get RateLimitPolicies CR: " + err.Error()) } err := k8sClient.Delete(context.Background(), crRateLimitPolicies, &client.DeleteOptions{}) @@ -569,10 +570,10 @@ func UnDeploySubscriptionRateLimitPolicyCR(policyName string, k8sClient client.C } // UndeploySubscriptionAIRateLimitPolicyCR applies the given AIRateLimitPolicies struct to the Kubernetes cluster. -func UndeploySubscriptionAIRateLimitPolicyCR(policyName string, k8sClient client.Client) { +func UndeploySubscriptionAIRateLimitPolicyCR(crName string, k8sClient client.Client) { conf, _ := config.ReadConfigs() crAIRateLimitPolicies := &dpv1alpha3.AIRateLimitPolicy{} - if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: conf.DataPlane.Namespace, Name: getSha1Value(policyName)}, crAIRateLimitPolicies); err != nil { + if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: conf.DataPlane.Namespace, Name: crName}, crAIRateLimitPolicies); err != nil { loggers.LoggerK8sClient.Error("Unable to get AIRateLimitPolicies CR: " + err.Error()) } err := k8sClient.Delete(context.Background(), crAIRateLimitPolicies, &client.DeleteOptions{}) @@ -829,3 +830,8 @@ func RetrieveAllAPISFromK8s(k8sClient client.Client, nextToken string) ([]dpv1al } return resolvedAPIList, apiList.Continue, nil } + +// PrepareSubscritionPolicyCRName prepare the cr name for a given policy name and organization pair +func PrepareSubscritionPolicyCRName(name, org string) string { + return getSha1Value(fmt.Sprintf("%s-%s",name, org)) +} diff --git a/apim-apk-agent/internal/messaging/notification_listener.go b/apim-apk-agent/internal/messaging/notification_listener.go index 82184522..d41ed708 100644 --- a/apim-apk-agent/internal/messaging/notification_listener.go +++ b/apim-apk-agent/internal/messaging/notification_listener.go @@ -490,8 +490,9 @@ func handlePolicyEvents(data []byte, eventType string, c client.Client) { } else if strings.EqualFold(policyEvent.PolicyType, "SUBSCRIPTION") { logger.LoggerMessaging.Infof("Policy: %s for policy type: %s", policyEvent.PolicyName, policyEvent.PolicyType) managementserver.DeleteSubscriptionPolicy(policyEvent.PolicyName, policyEvent.TenantDomain) - k8sclient.UnDeploySubscriptionRateLimitPolicyCR(fmt.Sprintf("%s-%s", policyEvent.PolicyName, policyEvent.TenantDomain), c) - k8sclient.UndeploySubscriptionAIRateLimitPolicyCR(fmt.Sprintf("%s-%s", policyEvent.PolicyName, policyEvent.TenantDomain), c) + crName := k8sclient.PrepareSubscritionPolicyCRName(policyEvent.PolicyName, policyEvent.TenantDomain) + k8sclient.UnDeploySubscriptionRateLimitPolicyCR(crName, c) + k8sclient.UndeploySubscriptionAIRateLimitPolicyCR(crName, c) ratelimitPolicies := managementserver.GetAllRateLimitPolicies() logger.LoggerMessaging.Infof("Rate Limit Policies Internal Map: %v", ratelimitPolicies) }