Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tenant flow for ai subscription ratelimit policy #1212

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions apim-apk-agent/internal/k8sClient/k8s_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"strings"

dpv1alpha1 "github.com/wso2/apk/common-go-libs/apis/dp/v1alpha1"
Expand Down Expand Up @@ -453,9 +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: policy.Name}, &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(policy.Name),
ObjectMeta: metav1.ObjectMeta{
Name: crName,
Namespace: conf.DataPlane.Namespace,
},
Spec: dpv1alpha3.RateLimitPolicySpec{
Expand Down Expand Up @@ -519,7 +522,7 @@ func DeployAIRateLimitPolicyFromCPPolicy(policy eventhubTypes.SubscriptionPolicy

crRateLimitPolicies := dpv1alpha3.AIRateLimitPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: getSha1Value(policy.Name),
Name: PrepareSubscritionPolicyCRName(policy.Name, policy.TenantDomain),
Namespace: conf.DataPlane.Namespace,
},
Spec: dpv1alpha3.AIRateLimitPolicySpec{
Expand Down Expand Up @@ -553,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 @@ -567,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 @@ -827,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(policyEvent.PolicyName, c)
k8sclient.UndeploySubscriptionAIRateLimitPolicyCR(policyEvent.PolicyName, 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
Loading