Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharsanan1 committed Oct 10, 2024
1 parent bce5d25 commit 2488b93
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
20 changes: 13 additions & 7 deletions apim-apk-agent/internal/k8sClient/k8s_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{})
Expand All @@ -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{})
Expand Down Expand Up @@ -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))
}
5 changes: 3 additions & 2 deletions apim-apk-agent/internal/messaging/notification_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 2488b93

Please sign in to comment.