Skip to content

Commit

Permalink
refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
tharindu1st committed Nov 23, 2023
1 parent 5b0a4c7 commit 36c06a2
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,70 +102,64 @@ func (applicationReconciler *ApplicationReconciler) Reconcile(ctx context.Contex
if found {
utils.SendAppDeletionEvent(applicationKey.Name, applicationSpec)
applicationReconciler.ods.DeleteApplicationFromStore(applicationKey)
server.DeleteApplication(applicationKey.Name)
} else {
loggers.LoggerAPKOperator.Infof("Application %s/%s does not exist in k8s", applicationKey.Namespace, applicationKey.Name)
loggers.LoggerAPKOperator.Debugf("Application %s/%s does not exist in k8s", applicationKey.Namespace, applicationKey.Name)
}
}
} else {
loggers.LoggerAPKOperator.Infof("Application cr available in k8s")
loggers.LoggerAPKOperator.Debugf("Application cr available in k8s")
applicationSpec, found := applicationReconciler.ods.GetApplicationFromStore(applicationKey)
if found {
// update
loggers.LoggerAPKOperator.Infof("Application in ods")
loggers.LoggerAPKOperator.Debugf("Application in ods")
utils.SendAppUpdateEvent(applicationKey.Name, applicationSpec, application.Spec)

} else {
loggers.LoggerAPKOperator.Infof("Application in ods consider as update")
loggers.LoggerAPKOperator.Debugf("Application in ods consider as update")
utils.SendAddApplicationEvent(application)
}
applicationReconciler.ods.AddorUpdateApplicationToStore(applicationKey, application.Spec)
applicationReconciler.sendAppUpdates(application, found)
}
sendAppUpdates(applicationList)
return ctrl.Result{}, nil
}

func sendAppUpdates(applicationList *cpv1alpha2.ApplicationList) {
appList := marshalApplicationList(applicationList.Items)
server.AddApplication(appList)
appKeyMappingList := marshalApplicationKeyMapping(applicationList.Items)
server.AddApplicationKeyMapping(appKeyMappingList)
func (applicationReconciler *ApplicationReconciler) sendAppUpdates(application cpv1alpha2.Application, update bool) {
resolvedApplication := marshalApplication(application)
if update {
server.DeleteApplication(application.Name)
}
server.AddApplication(resolvedApplication)
appKeyMappingList := marshalApplicationKeyMapping(application)
for _, applicationKeyMapping := range appKeyMappingList {
server.AddApplicationKeyMapping(applicationKeyMapping)
}
}

func marshalApplicationList(applicationList []cpv1alpha2.Application) server.ApplicationList {
applications := []server.Application{}
for _, appInternal := range applicationList {
app := server.Application{
UUID: appInternal.Name,
Name: appInternal.Spec.Name,
Owner: appInternal.Spec.Owner,
OrganizationID: appInternal.Spec.Organization,
Attributes: appInternal.Spec.Attributes,
}
applications = append(applications, app)
}
return server.ApplicationList{
List: applications,
func marshalApplication(application cpv1alpha2.Application) server.Application {
return server.Application{
UUID: application.Name,
Name: application.Spec.Name,
Owner: application.Spec.Owner,
OrganizationID: application.Spec.Organization,
Attributes: application.Spec.Attributes,
}
}

func marshalApplicationKeyMapping(applicationList []cpv1alpha2.Application) server.ApplicationKeyMappingList {
func marshalApplicationKeyMapping(appInternal cpv1alpha2.Application) []server.ApplicationKeyMapping {
applicationKeyMappings := []server.ApplicationKeyMapping{}
for _, appInternal := range applicationList {
var oauth2SecurityScheme = appInternal.Spec.SecuritySchemes.OAuth2
if oauth2SecurityScheme != nil {
for _, env := range oauth2SecurityScheme.Environments {
appIdentifier := server.ApplicationKeyMapping{
ApplicationUUID: appInternal.Name,
SecurityScheme: constants.OAuth2,
ApplicationIdentifier: env.AppID,
KeyType: env.KeyType,
EnvID: env.EnvID,
}
applicationKeyMappings = append(applicationKeyMappings, appIdentifier)
var oauth2SecurityScheme = appInternal.Spec.SecuritySchemes.OAuth2
if oauth2SecurityScheme != nil {
for _, env := range oauth2SecurityScheme.Environments {
appIdentifier := server.ApplicationKeyMapping{
ApplicationUUID: appInternal.Name,
SecurityScheme: constants.OAuth2,
ApplicationIdentifier: env.AppID,
KeyType: env.KeyType,
EnvID: env.EnvID,
}
applicationKeyMappings = append(applicationKeyMappings, appIdentifier)
}
}
return server.ApplicationKeyMappingList{
List: applicationKeyMappings,
}
return applicationKeyMappings
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cp

import (
"context"
"fmt"

"github.com/wso2/apk/adapter/pkg/logging"
"github.com/wso2/apk/common-controller/internal/cache"
Expand All @@ -34,7 +33,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

cpv1alpha2 "github.com/wso2/apk/common-controller/internal/operator/apis/cp/v1alpha2"
Expand Down Expand Up @@ -87,48 +85,37 @@ func NewApplicationMappingController(mgr manager.Manager, subscriptionStore *cac
func (r *ApplicationMappingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
applicationMappingKey := req.NamespacedName
var applicationMappingList = new(cpv1alpha2.ApplicationMappingList)

loggers.LoggerAPKOperator.Debugf("Reconciling application mapping: %v", applicationMappingKey.String())
if err := r.client.List(ctx, applicationMappingList); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get application mappings %s/%s",
applicationMappingKey.Namespace, applicationMappingKey.Name)
}
sendUpdates(applicationMappingList)
var applicationMapping cpv1alpha2.ApplicationMapping
if err := r.client.Get(ctx, req.NamespacedName, &applicationMapping); err != nil {
if k8error.IsNotFound(err) {
applicationMapping, found := r.ods.GetApplicationMappingFromStore(applicationMappingKey)
if found {
utils.SendDeleteApplicationMappingEvent(applicationMappingKey.Name, applicationMapping)
r.ods.DeleteApplicationMappingFromStore(applicationMappingKey)
server.DeleteApplicationMapping(applicationMappingKey.Name)
} else {
loggers.LoggerAPKOperator.Debugf("Application mapping %s/%s not found. Ignoring since object must be deleted", applicationMappingKey.Namespace, applicationMappingKey.Name)
}
}
} else {
sendUpdates(&applicationMapping)
utils.SendCreateApplicationMappingEvent(applicationMapping)
r.ods.AddorUpdateApplicationMappingToStore(applicationMappingKey, applicationMapping.Spec)
}
return ctrl.Result{}, nil
}

func sendUpdates(applicationMappingList *cpv1alpha2.ApplicationMappingList) {
appMappingList := marshalApplicationMappingList(applicationMappingList.Items)
func sendUpdates(applicationMapping *cpv1alpha2.ApplicationMapping) {
appMappingList := marshalApplicationMapping(applicationMapping)
server.AddApplicationMapping(appMappingList)
}

func marshalApplicationMappingList(applicationMappingList []cpv1alpha2.ApplicationMapping) server.ApplicationMappingList {
applicationMappings := []server.ApplicationMapping{}
for _, appMappingInternal := range applicationMappingList {
appMapping := server.ApplicationMapping{
UUID: appMappingInternal.Name,
ApplicationRef: appMappingInternal.Spec.ApplicationRef,
SubscriptionRef: appMappingInternal.Spec.SubscriptionRef,
}
applicationMappings = append(applicationMappings, appMapping)
}
return server.ApplicationMappingList{
List: applicationMappings,
func marshalApplicationMapping(applicationMapping *cpv1alpha2.ApplicationMapping) server.ApplicationMapping {
return server.ApplicationMapping{
UUID: applicationMapping.Name,
ApplicationRef: applicationMapping.Spec.ApplicationRef,
SubscriptionRef: applicationMapping.Spec.SubscriptionRef,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cp

import (
"context"
"fmt"

"github.com/wso2/apk/adapter/pkg/logging"
"github.com/wso2/apk/common-controller/internal/cache"
Expand All @@ -36,7 +35,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

cpv1alpha2 "github.com/wso2/apk/common-controller/internal/operator/apis/cp/v1alpha2"
Expand Down Expand Up @@ -90,12 +88,6 @@ func (subscriptionReconciler *SubscriptionReconciler) Reconcile(ctx context.Cont
loggers.LoggerAPKOperator.Debugf("Reconciling subscription: %v", req.NamespacedName.String())

subscriptionKey := req.NamespacedName
var subscriptionList = new(cpv1alpha2.SubscriptionList)
if err := subscriptionReconciler.client.List(ctx, subscriptionList); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get subscriptions %s/%s",
subscriptionKey.Namespace, subscriptionKey.Name)
}
sendSubUpdates(subscriptionList)
var subscription cpv1alpha2.Subscription
if err := subscriptionReconciler.client.Get(ctx, req.NamespacedName, &subscription); err != nil {
if k8error.IsNotFound(err) {
Expand All @@ -105,36 +97,34 @@ func (subscriptionReconciler *SubscriptionReconciler) Reconcile(ctx context.Cont
loggers.LoggerAPKOperator.Debugf("Subscription %s/%s not found. Ignoring since object must be deleted", subscriptionKey.Namespace, subscriptionKey.Name)
utils.SendDeleteSubscriptionEvent(subscriptionKey.Name, subscriptionSpec)
subscriptionReconciler.ods.DeleteSubscriptionFromStore(subscriptionKey)
server.DeleteSubscription(subscriptionKey.Name)
return ctrl.Result{}, nil
}
}
} else {
sendSubUpdates(subscription)
utils.SendAddSubscriptionEvent(subscription)
subscriptionReconciler.ods.AddorUpdateSubscriptionToStore(subscriptionKey, subscription.Spec)
}
return ctrl.Result{}, nil
}

func sendSubUpdates(subscriptionsList *cpv1alpha2.SubscriptionList) {
subList := marshalSubscriptionList(subscriptionsList.Items)
func sendSubUpdates(subscription cpv1alpha2.Subscription) {
subList := marshalSubscription(subscription)
server.AddSubscription(subList)
}

func marshalSubscriptionList(subscriptionList []cpv1alpha2.Subscription) server.SubscriptionList {
subscriptions := []server.Subscription{}
for _, subInternal := range subscriptionList {
subscribedAPI := &server.SubscribedAPI{}
sub := server.Subscription{
UUID: subInternal.Name,
SubStatus: subInternal.Spec.SubscriptionStatus,
Organization: subInternal.Spec.Organization,
}
if subInternal.Spec.API.Name != "" && subInternal.Spec.API.Version != "" {
subscribedAPI.Name = subInternal.Spec.API.Name
subscribedAPI.Version = subInternal.Spec.API.Version
}
sub.SubscribedAPI = subscribedAPI
subscriptions = append(subscriptions, sub)
func marshalSubscription(subscription cpv1alpha2.Subscription) server.Subscription {
subscribedAPI := &server.SubscribedAPI{}
sub := server.Subscription{
UUID: subscription.Name,
SubStatus: subscription.Spec.SubscriptionStatus,
Organization: subscription.Spec.Organization,
}
if subscription.Spec.API.Name != "" && subscription.Spec.API.Version != "" {
subscribedAPI.Name = subscription.Spec.API.Name
subscribedAPI.Version = subscription.Spec.API.Version
}
return server.SubscriptionList{List: subscriptions}
sub.SubscribedAPI = subscribedAPI
return sub
}
70 changes: 54 additions & 16 deletions common-controller/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,48 @@ package server
import (
"fmt"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/wso2/apk/common-controller/internal/config"
)

var applicationList = ApplicationList{List: []Application{}}
var subscriptionList = SubscriptionList{List: []Subscription{}}
var applicationMappingList = ApplicationMappingList{List: []ApplicationMapping{}}
var applicationKeyMappingList = ApplicationKeyMappingList{List: []ApplicationKeyMapping{}}
var applicationMap = make(map[string]Application)
var subscriptionMap = make(map[string]Subscription)
var applicationMappingMap = make(map[string]ApplicationMapping)
var applicationKeyMappingMap = make(map[string]ApplicationKeyMapping)

// StartInternalServer starts the internal server
func StartInternalServer() {
r := gin.Default()

r.GET("/applications", func(c *gin.Context) {
c.JSON(http.StatusOK, applicationList)
applicationList := []Application{}
for _, application := range applicationMap {
applicationList = append(applicationList, application)
}
c.JSON(http.StatusOK, ApplicationList{List: applicationList})
})
r.GET("/subscriptions", func(c *gin.Context) {
c.JSON(http.StatusOK, subscriptionList)
subscriptionList := []Subscription{}
for _, subscription := range subscriptionMap {
subscriptionList = append(subscriptionList, subscription)
}
c.JSON(http.StatusOK, SubscriptionList{List: subscriptionList})
})
r.GET("/applicationmappings", func(c *gin.Context) {
c.JSON(http.StatusOK, applicationMappingList)
applicationMappingList := []ApplicationMapping{}
for _, applicationMapping := range applicationMappingMap {
applicationMappingList = append(applicationMappingList, applicationMapping)
}
c.JSON(http.StatusOK, ApplicationMappingList{List: applicationMappingList})
})
r.GET("/applicationkeymappings", func(c *gin.Context) {
c.JSON(http.StatusOK, applicationKeyMappingList)
applicationKeyMappingList := []ApplicationKeyMapping{}
for _, applicationKeyMapping := range applicationKeyMappingMap {
applicationKeyMappingList = append(applicationKeyMappingList, applicationKeyMapping)
}
c.JSON(http.StatusOK, ApplicationKeyMappingList{List: applicationKeyMappingList})
})
gin.SetMode(gin.ReleaseMode)
conf := config.ReadConfigs()
Expand All @@ -38,21 +55,42 @@ func StartInternalServer() {
}

// AddApplication adds an application to the application list
func AddApplication(appList ApplicationList) {
applicationList = appList
func AddApplication(application Application) {
applicationMap[application.UUID] = application
}

// AddSubscription adds a subscription to the subscription list
func AddSubscription(subList SubscriptionList) {
subscriptionList = subList
func AddSubscription(subscription Subscription) {
subscriptionMap[subscription.UUID] = subscription
}

// AddApplicationMapping adds an application mapping to the application mapping list
func AddApplicationMapping(appMappingList ApplicationMappingList) {
applicationMappingList = appMappingList
func AddApplicationMapping(applicationMapping ApplicationMapping) {
applicationMappingMap[applicationMapping.UUID] = applicationMapping
}

// AddApplicationKeyMapping adds an application key mapping to the application key mapping list
func AddApplicationKeyMapping(appKeyMappingList ApplicationKeyMappingList) {
applicationKeyMappingList = appKeyMappingList
func AddApplicationKeyMapping(applicationKeyMapping ApplicationKeyMapping) {
applicationMappingKey := strings.Join([]string{applicationKeyMapping.ApplicationUUID, applicationKeyMapping.EnvID, applicationKeyMapping.SecurityScheme, applicationKeyMapping.KeyType}, ":")
applicationKeyMappingMap[applicationMappingKey] = applicationKeyMapping
}

// DeleteApplication deletes an application from the application list
func DeleteApplication(applicationUUID string) {
delete(applicationMap, applicationUUID)
for key := range applicationKeyMappingMap {
if strings.HasPrefix(key, applicationUUID) {
delete(applicationKeyMappingMap, key)
}
}
}

// DeleteSubscription deletes a subscription from the subscription list
func DeleteSubscription(subscriptionUUID string) {
delete(subscriptionMap, subscriptionUUID)
}

// DeleteApplicationMapping deletes an application mapping from the application mapping list
func DeleteApplicationMapping(applicationMappingUUID string) {
delete(applicationMappingMap, applicationMappingUUID)
}
Loading

0 comments on commit 36c06a2

Please sign in to comment.