Skip to content

Commit

Permalink
Merge pull request #490 from kube-tarian/plugin-store
Browse files Browse the repository at this point in the history
handling plugin store mgmt apis on capten agent
  • Loading branch information
vramk23 authored May 19, 2024
2 parents ea6b057 + f7cb3a8 commit ee19af2
Show file tree
Hide file tree
Showing 46 changed files with 3,717 additions and 169 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ vendor
.idea
info.txt
Dockerfile
.vscode
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gen-protoc:
mkdir -p capten/common-pkg/pb/agentpb
mkdir -p capten/common-pkg/pb/clusterpluginspb
mkdir -p server/pkg/pb/clusterpluginspb
mkdir -p capten/common-pkg/pb/pluginstorepb

cd proto && protoc --go_out=../server/pkg/pb/serverpb/ --go_opt=paths=source_relative \
--go-grpc_out=../server/pkg/pb/serverpb --go-grpc_opt=paths=source_relative \
Expand Down Expand Up @@ -45,6 +46,9 @@ gen-protoc:
--go-grpc_out=../server/pkg/pb/pluginstorepb --go-grpc_opt=paths=source_relative \
./plugin_store.proto

cd proto && protoc --go_out=../capten/common-pkg/pb/pluginstorepb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/pb/pluginstorepb --go-grpc_opt=paths=source_relative \
./plugin_store.proto
cd proto && protoc --go_out=../capten/common-pkg/pb/clusterpluginspb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/pb/clusterpluginspb --go-grpc_opt=paths=source_relative \
./cluster_plugins.proto
Expand Down
23 changes: 22 additions & 1 deletion capten/agent/internal/api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,38 @@ import (
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/clusterpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/pluginstorepb"
pluginstore "github.com/kube-tarian/kad/capten/common-pkg/plugin-store"
)

var _ agentpb.AgentServer = &Agent{}

type pluginStore interface {
ConfigureStore(config *pluginstorepb.PluginStoreConfig) error
GetStoreConfig(storeType pluginstorepb.StoreType) (*pluginstorepb.PluginStoreConfig, error)
SyncPlugins(storeType pluginstorepb.StoreType) error
GetPlugins(storeType pluginstorepb.StoreType) ([]*pluginstorepb.Plugin, error)
GetPluginData(storeType pluginstorepb.StoreType, pluginName string) (*pluginstorepb.PluginData, error)
GetPluginValues(storeType pluginstorepb.StoreType, pluginName, version string) ([]byte, error)
DeployPlugin(storeType pluginstorepb.StoreType, pluginName, version string, values []byte) error
UnDeployPlugin(storeType pluginstorepb.StoreType, pluginName string) error
}

type Agent struct {
agentpb.UnimplementedAgentServer
captenpluginspb.UnimplementedCaptenPluginsServer
clusterpluginspb.UnimplementedClusterPluginsServer
pluginstorepb.UnimplementedPluginStoreServer
tc *temporalclient.Client
as *captenstore.Store
log logging.Logger
cfg *config.SericeConfig
plugin pluginStore
createPr bool
}

func NewAgent(log logging.Logger, cfg *config.SericeConfig, as *captenstore.Store) (*Agent, error) {
func NewAgent(log logging.Logger, cfg *config.SericeConfig,
as *captenstore.Store) (*Agent, error) {
var tc *temporalclient.Client
var err error

Expand All @@ -41,6 +57,11 @@ func NewAgent(log logging.Logger, cfg *config.SericeConfig, as *captenstore.Stor
cfg: cfg,
log: log,
}

agent.plugin, err = pluginstore.NewPluginStore(log, as, agent)
if err != nil {
return nil, err
}
return agent, nil
}

Expand Down
1 change: 0 additions & 1 deletion capten/agent/internal/api/app_values_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func populateTemplateValues(appConfig *agentpb.SyncAppData, newOverrideValues, l
newAppConfig.Values.OverrideValues = finalOverrideMappingBytes

// replace template with new override values from the request
//fmt.Printf("template: %s\n", string(appConfig.Values.TemplateValues))
log.Debugf("finalOverrideMapping: %s\n", string(finalOverrideMappingBytes))
populatedTemplateValuesMapping, err := deriveTemplateValuesMapping(finalOverrideMappingBytes, appConfig.Values.TemplateValues)
if err != nil {
Expand Down
203 changes: 203 additions & 0 deletions capten/agent/internal/api/plugin_store_apis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package api

import (
"context"

"github.com/kube-tarian/kad/capten/common-pkg/pb/pluginstorepb"
)

func (a *Agent) ConfigurePluginStore(ctx context.Context, request *pluginstorepb.ConfigurePluginStoreRequest) (
*pluginstorepb.ConfigurePluginStoreResponse, error) {
if err := validateArgs(request.Config.GitProjectId, request.Config.GitProjectURL); err != nil {
a.log.Infof("request validation failed", err)
return &pluginstorepb.ConfigurePluginStoreResponse{
Status: pluginstorepb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, err
}
a.log.Infof("Configure plugin store request recieved for store type %d", request.Config.StoreType)
err := a.plugin.ConfigureStore(request.Config)
if err != nil {
a.log.Errorf("Configure plugin store request failed, %w", err)
return &pluginstorepb.ConfigurePluginStoreResponse{
Status: pluginstorepb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to configuere plugin store",
}, err
}

a.log.Infof("Plugin store request processed")
return &pluginstorepb.ConfigurePluginStoreResponse{
Status: pluginstorepb.StatusCode_OK,
}, nil
}

func (a *Agent) GetPluginStoreConfig(ctx context.Context, request *pluginstorepb.GetPluginStoreConfigRequest) (
*pluginstorepb.GetPluginStoreConfigResponse, error) {
a.log.Infof("Get plugin store config request recieved for store type %d", request.StoreType)

config, err := a.plugin.GetStoreConfig(request.StoreType)
if err != nil {
a.log.Errorf("Get plugin store config request failed, %w", err)
return &pluginstorepb.GetPluginStoreConfigResponse{
Status: pluginstorepb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to get plugin store config",
}, err
}

a.log.Infof("Get plugin store config request processed")
return &pluginstorepb.GetPluginStoreConfigResponse{
Status: pluginstorepb.StatusCode_OK,
Config: config,
}, nil
}

func (a *Agent) SyncPluginStore(ctx context.Context, request *pluginstorepb.SyncPluginStoreRequest) (
*pluginstorepb.SyncPluginStoreResponse, error) {
a.log.Infof("Sync plugin store request recieved for store type %d", request.StoreType)

err := a.plugin.SyncPlugins(request.StoreType)
if err != nil {
a.log.Errorf("Sync plugin store request failed, %w", err)
return &pluginstorepb.SyncPluginStoreResponse{
Status: pluginstorepb.StatusCode_INTERNRAL_ERROR,
StatusMessage: err.Error(),
}, err
}

a.log.Infof("Sync plugin store request processed")
return &pluginstorepb.SyncPluginStoreResponse{
Status: pluginstorepb.StatusCode_OK,
}, nil
}

func (a *Agent) GetPlugins(ctx context.Context, request *pluginstorepb.GetPluginsRequest) (
*pluginstorepb.GetPluginsResponse, error) {
a.log.Infof("Get plugins request recieved")

plugins, err := a.plugin.GetPlugins(request.StoreType)
if err != nil {
a.log.Errorf("Get plugins request failed, %w", err)
return &pluginstorepb.GetPluginsResponse{
Status: pluginstorepb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to get plugins",
}, err
}

a.log.Infof("Get plugins request processed")
return &pluginstorepb.GetPluginsResponse{
Status: pluginstorepb.StatusCode_OK,
Plugins: plugins,
}, nil
}

func (a *Agent) GetPluginValues(ctx context.Context, request *pluginstorepb.GetPluginValuesRequest) (
*pluginstorepb.GetPluginValuesResponse, error) {
err := validateArgs(request.PluginName, request.Version)
if err != nil {
a.log.Infof("request validation failed", err)
return &pluginstorepb.GetPluginValuesResponse{
Status: pluginstorepb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, err
}
a.log.Infof("Get plugin values request recieved for plugin %s-%s", request.PluginName, request.Version)

values, err := a.plugin.GetPluginValues(request.StoreType, request.PluginName, request.Version)
if err != nil {
a.log.Errorf("Get plugin values request failed for plugin %s-%s, %w",
request.PluginName, request.Version, err)
return &pluginstorepb.GetPluginValuesResponse{
Status: pluginstorepb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to get plugins",
}, err
}

a.log.Infof("Get plugin values request processed for plugin %s-%s",
request.PluginName, request.Version)
return &pluginstorepb.GetPluginValuesResponse{
Status: pluginstorepb.StatusCode_OK,
Values: values,
}, nil
}

func (a *Agent) GetPluginData(ctx context.Context, request *pluginstorepb.GetPluginDataRequest) (
*pluginstorepb.GetPluginDataResponse, error) {
err := validateArgs(request.PluginName)
if err != nil {
a.log.Infof("request validation failed", err)
return &pluginstorepb.GetPluginDataResponse{
Status: pluginstorepb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, err
}
a.log.Infof("Get plugin data request recieved for plugin %s", request.PluginName)

pluginData, err := a.plugin.GetPluginData(request.StoreType, request.PluginName)
if err != nil {
a.log.Errorf("Get plugin data request failed for plugin %s, %w", request.PluginName, err)
return &pluginstorepb.GetPluginDataResponse{
Status: pluginstorepb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to get plugins",
}, err
}

a.log.Infof("Get plugin data request processed for plugin %s", request.PluginName)
return &pluginstorepb.GetPluginDataResponse{
Status: pluginstorepb.StatusCode_OK,
PluginData: pluginData,
}, nil
}

func (a *Agent) DeployPlugin(ctx context.Context, request *pluginstorepb.DeployPluginRequest) (
*pluginstorepb.DeployPluginResponse, error) {
err := validateArgs(request.PluginName)
if err != nil {
a.log.Infof("request validation failed", err)
return &pluginstorepb.DeployPluginResponse{
Status: pluginstorepb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, err
}
a.log.Infof("Deploy plugin request recieved for plugin %s-%s", request.PluginName, request.Version)

err = a.plugin.DeployPlugin(request.StoreType, request.PluginName, request.Version, request.Values)
if err != nil {
a.log.Errorf("Deploy plugin request failed for plugin %s-%s, %v", request.PluginName, request.Version, err)
return &pluginstorepb.DeployPluginResponse{
Status: pluginstorepb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to deploy plugin",
}, err
}

a.log.Infof("Deploy plugin request processed for plugin %s-%s", request.PluginName, request.Version)
return &pluginstorepb.DeployPluginResponse{
Status: pluginstorepb.StatusCode_OK,
}, nil
}

func (a *Agent) UnDeployPlugin(ctx context.Context, request *pluginstorepb.UnDeployPluginRequest) (
*pluginstorepb.UnDeployPluginResponse, error) {
err := validateArgs(request.PluginName)
if err != nil {
a.log.Infof("request validation failed", err)
return &pluginstorepb.UnDeployPluginResponse{
Status: pluginstorepb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, err
}
a.log.Infof("UnDeploy plugin request recieved for plugin %s", request.PluginName)

err = a.plugin.UnDeployPlugin(request.StoreType, request.PluginName)
if err != nil {
a.log.Errorf("UnDeploy plugin request failed for plugin %s, %v", request.PluginName, err)
return &pluginstorepb.UnDeployPluginResponse{
Status: pluginstorepb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to undeploy plugin",
}, err
}

a.log.Infof("UnDeploy plugin request processed for plugin %s", request.PluginName)
return &pluginstorepb.UnDeployPluginResponse{
Status: pluginstorepb.StatusCode_OK,
}, nil
}
2 changes: 2 additions & 0 deletions capten/agent/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/clusterpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/pluginstorepb"
dbinit "github.com/kube-tarian/kad/capten/common-pkg/postgres/db-init"
"github.com/pkg/errors"
"google.golang.org/grpc"
Expand Down Expand Up @@ -69,6 +70,7 @@ func Start() {
agentpb.RegisterAgentServer(grpcServer, rpcapi)
captenpluginspb.RegisterCaptenPluginsServer(grpcServer, rpcapi)
clusterpluginspb.RegisterClusterPluginsServer(grpcServer, rpcapi)
pluginstorepb.RegisterPluginStoreServer(grpcServer, rpcapi)

log.Infof("Agent listening at %v", listener.Addr())
reflection.Register(grpcServer)
Expand Down
3 changes: 0 additions & 3 deletions capten/common-pkg/capten-store/cluster_plugin_config_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func (a *Store) UpsertClusterPluginConfig(pluginConfig *clusterpluginspb.Plugin)
}
err = nil
recordFound = false
fmt.Println("not found")
} else if plugin.PluginName == "" {
recordFound = false
}
Expand All @@ -52,10 +51,8 @@ func (a *Store) UpsertClusterPluginConfig(pluginConfig *clusterpluginspb.Plugin)

if !recordFound {
err = a.dbClient.Create(plugin)
fmt.Println("created")
} else {
err = a.dbClient.Update(plugin, ClusterPluginConfig{PluginName: pluginConfig.PluginName})
fmt.Println("updated")
}
return err
}
Expand Down
1 change: 0 additions & 1 deletion capten/common-pkg/capten-store/crossplane_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func (a *Store) updateCrossplaneProject() (*model.CrossplaneProject, error) {
GitProjectURL: crosplaneGitProject.ProjectUrl,
Status: crossplaneProject.Status,
LastUpdateTime: time.Now()}

err = a.dbClient.Update(&project, CrossplaneProject{ID: 1})
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit ee19af2

Please sign in to comment.