Skip to content

Commit

Permalink
refactor server api packge
Browse files Browse the repository at this point in the history
  • Loading branch information
vramk23 committed Oct 14, 2023
1 parent 0a2c88f commit a5281c3
Show file tree
Hide file tree
Showing 30 changed files with 1,208 additions and 1,054 deletions.
3 changes: 2 additions & 1 deletion server/data/store-apps/app_list.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
enabledApps:
- argo-cd
- crossplane
- testkube
- testkube-v1.0.0
- testkube-v1.0.1

disabledApps:
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions server/data/store-apps/conf/testkube-v1.0.1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Name: "testkube"
ChartName: "tools/testkube"
Category: "Testing framework"
Description: ""
RepoName: "tools"
RepoURL: "https://kube-tarian.github.io/helmrepo-supporting-tools"
Namespace: "testkube"
ReleaseName: "testkube"
Version: "1.0.1"
CreateNamespace: true
OverrideValues:
DomainName: "{{.DomainName}}"
56 changes: 56 additions & 0 deletions server/pkg/api/add_store_apps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package api

import (
"context"
"encoding/hex"

"github.com/kube-tarian/kad/server/pkg/pb/serverpb"
"github.com/kube-tarian/kad/server/pkg/types"
)

func (s *Server) AddStoreApp(ctx context.Context, request *serverpb.AddStoreAppRequest) (
*serverpb.AddStoreAppResponse, error) {
_, err := validateRequest(ctx, request.AppConfig.AppName, request.AppConfig.Version)
if err != nil {
s.log.Infof("request validation failed", err)
return &serverpb.AddStoreAppResponse{
Status: serverpb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}
s.log.Infof("Add store app [%s:%s] request recieved", request.AppConfig.AppName, request.AppConfig.Version)

config := &types.StoreAppConfig{
ReleaseName: request.AppConfig.ReleaseName,
AppName: request.AppConfig.AppName,
Version: request.AppConfig.Version,
Category: request.AppConfig.Category,
Description: request.AppConfig.Description,
ChartName: request.AppConfig.ChartName,
RepoName: request.AppConfig.RepoName,
RepoURL: request.AppConfig.RepoURL,
Namespace: request.AppConfig.Namespace,
CreateNamespace: request.AppConfig.CreateNamespace,
PrivilegedNamespace: request.AppConfig.PrivilegedNamespace,
Icon: hex.EncodeToString(request.AppConfig.Icon),
LaunchURL: request.AppConfig.LaunchURL,
LaunchUIDescription: request.AppConfig.LaunchUIDescription,
OverrideValues: encodeBase64BytesToString(request.AppValues.OverrideValues),
LaunchUIValues: encodeBase64BytesToString(request.AppValues.LaunchUIValues),
TemplateValues: encodeBase64BytesToString(request.AppValues.TemplateValues),
}

if err := s.serverStore.AddOrUpdateStoreApp(config); err != nil {
s.log.Errorf("failed to add app config to store, %v", err)
return &serverpb.AddStoreAppResponse{
Status: serverpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed add app config to store",
}, nil
}

s.log.Infof("Add store app [%s:%s] request successful", request.AppConfig.AppName, request.AppConfig.Version)
return &serverpb.AddStoreAppResponse{
Status: serverpb.StatusCode_OK,
StatusMessage: "app config is sucessfuly added to store",
}, nil
}
44 changes: 44 additions & 0 deletions server/pkg/api/app_config_mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package api

import (
"github.com/kube-tarian/kad/server/pkg/pb/agentpb"
"github.com/kube-tarian/kad/server/pkg/pb/serverpb"
)

func mapAgentAppsToServerResp(appDataList []*agentpb.AppData) []*serverpb.ClusterAppConfig {
clusterAppConfigs := make([]*serverpb.ClusterAppConfig, len(appDataList))
for index, appConfig := range appDataList {
var clusterAppConfig serverpb.ClusterAppConfig
clusterAppConfig.AppName = appConfig.Config.AppName
clusterAppConfig.Version = appConfig.Config.Version
clusterAppConfig.Category = appConfig.Config.Category
clusterAppConfig.Description = appConfig.Config.Description
clusterAppConfig.ChartName = appConfig.Config.ChartName
clusterAppConfig.RepoName = appConfig.Config.RepoName
clusterAppConfig.RepoURL = appConfig.Config.RepoURL
clusterAppConfig.Namespace = appConfig.Config.Namespace
clusterAppConfig.CreateNamespace = appConfig.Config.CreateNamespace
clusterAppConfig.PrivilegedNamespace = appConfig.Config.PrivilegedNamespace
clusterAppConfig.Icon = appConfig.Config.Icon
clusterAppConfig.LaunchURL = appConfig.Config.LaunchURL
clusterAppConfig.InstallStatus = appConfig.Config.InstallStatus
clusterAppConfig.RuntimeStatus = ""
clusterAppConfig.DefualtApp = appConfig.Config.DefualtApp
clusterAppConfigs[index] = &clusterAppConfig
}
return clusterAppConfigs
}

func mapAgentAppLauncesToServerResp(appLaunchCfgs []*agentpb.AppLaunchConfig) []*serverpb.AppLaunchConfig {
svrAppLaunchCfg := make([]*serverpb.AppLaunchConfig, len(appLaunchCfgs))
for index, cfg := range appLaunchCfgs {
var launchCfg serverpb.AppLaunchConfig
launchCfg.ReleaseName = cfg.ReleaseName
launchCfg.Category = cfg.Category
launchCfg.LaunchUIDescription = cfg.Description
launchCfg.Icon = cfg.Icon
launchCfg.LaunchURL = cfg.LaunchURL
svrAppLaunchCfg[index] = &launchCfg
}
return svrAppLaunchCfg
}
62 changes: 62 additions & 0 deletions server/pkg/api/app_values_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package api

import (
"bytes"
"context"
"fmt"
"text/template"

"github.com/kube-tarian/kad/server/pkg/pb/agentpb"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

func (s *Server) replaceGlobalValues(orgId, clusterID string, overridedValues []byte) ([]byte, error) {
agent, err := s.agentHandeler.GetAgent(orgId, clusterID)
if err != nil {
return nil, errors.WithMessagef(err, "failed to initialize agent for cluster %s", clusterID)
}
resp, err := agent.GetClient().GetClusterGlobalValues(context.TODO(), &agentpb.GetClusterGlobalValuesRequest{})
if err != nil {
return nil, err
}
if resp.Status != agentpb.StatusCode_OK {
return nil, fmt.Errorf("failed to get global values for cluster %s", clusterID)
}

var globalValues map[string]interface{}
err = yaml.Unmarshal(resp.GlobalValues, &globalValues)
if err != nil {
return nil, errors.WithMessagef(err, "failed to unmarshal cluster values")
}

var overrideValues map[string]interface{}
err = yaml.Unmarshal(overridedValues, &overrideValues)
if err != nil {
return nil, errors.WithMessagef(err, "failed to unmarshal override values")
}

return replaceOverrideGlobalValues(overrideValues, globalValues)
}

func replaceOverrideGlobalValues(overrideValues map[string]interface{},
globlaValues map[string]interface{}) (transformedData []byte, err error) {
yamlData, err := yaml.Marshal(overrideValues)
if err != nil {
return
}

tmpl, err := template.New("templateVal").Parse(string(yamlData))
if err != nil {
return
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, globlaValues)
if err != nil {
return
}

transformedData = buf.Bytes()
return
}
156 changes: 0 additions & 156 deletions server/pkg/api/cluster_apps.go

This file was deleted.

Loading

0 comments on commit a5281c3

Please sign in to comment.