Skip to content

Commit

Permalink
adapted to latest refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
share2kanna committed Jun 6, 2024
1 parent 748efa7 commit 90dbd43
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 317 deletions.
27 changes: 26 additions & 1 deletion capten/agent/internal/api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ import (

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

GetClusterPluginData(pluginName string) (*clusterpluginspb.Plugin, error)
DeployClusterPlugin(ctx context.Context, pluginData *clusterpluginspb.Plugin) error
UnDeployClusterPlugin(ctx context.Context, request *clusterpluginspb.UnDeployClusterPluginRequest) error
}

type Agent struct {
agentpb.UnimplementedAgentServer
captenpluginspb.UnimplementedCaptenPluginsServer
Expand All @@ -26,7 +41,7 @@ type Agent struct {
as *captenstore.Store
log logging.Logger
cfg *config.SericeConfig
plugin pluginstore.PluginStoreInterface
plugin pluginStore
createPr bool
}

Expand All @@ -48,6 +63,16 @@ func NewAgent(log logging.Logger, cfg *config.SericeConfig, as *captenstore.Stor
if err != nil {
return nil, err
}

// add default plugins configuration to plugin store
err = agent.plugin.ConfigureStore(&pluginstorepb.PluginStoreConfig{
StoreType: pluginstorepb.StoreType_DEFAULT_STORE,
GitProjectId: "1cf5201d-5f35-4d5b-afe0-4b9d0e0d4cd2",
GitProjectURL: "https://github.com/intelops/capten-plugins",
})
if err != nil {
return nil, err
}
return agent, nil
}

Expand Down
89 changes: 87 additions & 2 deletions capten/agent/internal/api/agent_cluster_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ package api
import (
"context"
"fmt"
"strings"

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

const (
deployedStatus = "deployed"
)

func (a *Agent) SyncApp(ctx context.Context, request *agentpb.SyncAppRequest) (
Expand Down Expand Up @@ -151,10 +157,89 @@ func (a *Agent) GetClusterAppValues(ctx context.Context, request *agentpb.GetClu

func (a *Agent) DeployDefaultApps(ctx context.Context, request *agentpb.DeployDefaultAppsRequest) (
*agentpb.DeployDefaultAppsResponse, error) {
return nil, fmt.Errorf("not implemented")
if err := a.plugin.SyncPlugins(pluginstorepb.StoreType_DEFAULT_STORE); err != nil {
a.log.Errorf("failed to synch providers, %v", err)
}

plugins, err := a.plugin.GetPlugins(pluginstorepb.StoreType_DEFAULT_STORE)
if err != nil {
a.log.Errorf("failed to get plugins, %v", err)
}

failedPlugins := []string{}
for _, plugin := range plugins {
if err := a.plugin.DeployPlugin(pluginstorepb.StoreType_DEFAULT_STORE, plugin.PluginName, plugin.Versions[0], []byte{}); err != nil {
a.log.Errorf("failed to deploy plugin, %v", err)
failedPlugins = append(failedPlugins, plugin.PluginName)
}
}

if len(failedPlugins) == len(plugins) {
return &agentpb.DeployDefaultAppsResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: fmt.Sprintf("failed to deploy all default apps: %s", strings.Join(failedPlugins, ",")),
}, nil
}

statusMessage := agentpb.StatusCode_name[int32(agentpb.StatusCode_OK)]
if len(failedPlugins) != 0 {
statusMessage = fmt.Sprintf("failed to deploy default apps: %s", strings.Join(failedPlugins, ","))
}

return &agentpb.DeployDefaultAppsResponse{
Status: agentpb.StatusCode_OK,
StatusMessage: statusMessage,
}, nil
}

func (a *Agent) GetDefaultAppsStatus(ctx context.Context, request *agentpb.GetDefaultAppsStatusRequest) (
*agentpb.GetDefaultAppsStatusResponse, error) {
return nil, fmt.Errorf("not implemented")
plugins, err := a.plugin.GetPlugins(pluginstorepb.StoreType_DEFAULT_STORE)
if err != nil {
a.log.Errorf("failed to get plugins, %v", err)
}

overallStatus := agentpb.DeploymentStatus_SUCCESS
anyPluginFailed := false
resp := []*agentpb.ApplicationStatus{}
for _, plugin := range plugins {
pluginData, err := a.plugin.GetClusterPluginData(plugin.PluginName)
if err != nil {
a.log.Errorf("failed to fetch plugin status, %v", err)
resp = append(resp, &agentpb.ApplicationStatus{
AppName: plugin.PluginName,
Version: plugin.Versions[0],
Category: plugin.Category,
InstallStatus: "failed to fetch status",
RuntimeStatus: "Unknown",
})
continue
}

if !strings.Contains(pluginData.InstallStatus, "failed") {
anyPluginFailed = true
} else if pluginData.InstallStatus != deployedStatus {
overallStatus = agentpb.DeploymentStatus_ONGOING
}

resp = append(resp, &agentpb.ApplicationStatus{
AppName: pluginData.PluginName,
Version: pluginData.Version,
Category: pluginData.Category,
InstallStatus: pluginData.InstallStatus,
RuntimeStatus: pluginData.InstallStatus,
})
}

// if any plugin failed and other plugins status is success, set overall status will be failed
if anyPluginFailed && overallStatus == agentpb.DeploymentStatus_SUCCESS {
overallStatus = agentpb.DeploymentStatus_FAILED
}

return &agentpb.GetDefaultAppsStatusResponse{
Status: agentpb.StatusCode_OK,
StatusMessage: agentpb.StatusCode_name[int32(agentpb.StatusCode_OK)],
DeploymentStatus: overallStatus,
DefaultAppsStatus: resp,
}, nil
}
10 changes: 4 additions & 6 deletions capten/agent/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"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"
dbinit "github.com/kube-tarian/kad/capten/common-pkg/postgres/db-init"
"github.com/pkg/errors"
"google.golang.org/grpc"
Expand Down Expand Up @@ -132,7 +131,7 @@ func configurePostgresDB() error {
func initializeJobScheduler(
cfg *config.SericeConfig,
as *captenstore.Store,
handler pluginstore.PluginDeployHandler,
handler *agentapi.Agent,
) (*job.Scheduler, error) {
s := job.NewScheduler(log)
if cfg.CrossplaneSyncJobEnabled {
Expand All @@ -147,18 +146,17 @@ func initializeJobScheduler(
}

// Add Default plugin deployer job
addDefualtPluginsDeployerJob(s, as, handler)
addDefualtPluginsDeployerJob(s, handler)

log.Info("successfully initialized job scheduler")
return s, nil
}

func addDefualtPluginsDeployerJob(
s *job.Scheduler,
as *captenstore.Store,
handler pluginstore.PluginDeployHandler,
handler *agentapi.Agent,
) {
dpd, err := defaultplugindeployer.NewDefaultPluginsDeployer(log, "@every 10m", as, handler)
dpd, err := defaultplugindeployer.NewDefaultPluginsDeployer(log, "@every 10m", handler)
if err != nil {
log.Fatal("failed to init default plugins deployer job", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
package defaultplugindeployer

import (
"context"

"github.com/intelops/go-common/logging"
"github.com/kelseyhightower/envconfig"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"
"github.com/kube-tarian/kad/capten/common-pkg/pb/pluginstorepb"
pluginstore "github.com/kube-tarian/kad/capten/common-pkg/plugin-store"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
)

type defaultPluginsDeployer interface {
DeployDefaultApps(ctx context.Context, request *agentpb.DeployDefaultAppsRequest) (*agentpb.DeployDefaultAppsResponse, error)
}

type DefaultPluginsDeployer struct {
pluginStore pluginstore.PluginStoreInterface
log logging.Logger
frequency string
agent defaultPluginsDeployer
log logging.Logger
frequency string
}

func NewDefaultPluginsDeployer(
log logging.Logger,
frequency string,
dbStore *captenstore.Store,
handler pluginstore.PluginDeployHandler,
agent defaultPluginsDeployer,
) (*DefaultPluginsDeployer, error) {
cfg := &pluginstore.Config{}
if err := envconfig.Process("", cfg); err != nil {
return nil, err
}
cfg.PluginFileName = "default-plugin-list.yaml"

pluginStore, err := pluginstore.NewPluginStoreWithConfig(log, cfg, dbStore, handler)
if err != nil {
return nil, err
}

// add default plugins configuration to plugin store
err = pluginStore.ConfigureStore(&pluginstorepb.PluginStoreConfig{
StoreType: pluginstorepb.StoreType_DEFAULT_STORE,
GitProjectId: "1cf5201d-5f35-4d5b-afe0-4b9d0e0d4cd2",
GitProjectURL: "https://github.com/intelops/capten-plugins",
})
if err != nil {
return nil, err
}

return &DefaultPluginsDeployer{
log: log,
frequency: frequency,
pluginStore: pluginStore,
log: log,
frequency: frequency,
agent: agent,
}, nil
}

Expand All @@ -54,24 +35,12 @@ func (p *DefaultPluginsDeployer) CronSpec() string {

func (p *DefaultPluginsDeployer) Run() {
p.log.Debug("started default plugins deployer job")
if err := p.pluginStore.SyncPlugins(pluginstorepb.StoreType_DEFAULT_STORE); err != nil {
p.log.Errorf("failed to synch providers, %v", err)
resp, _ := p.agent.DeployDefaultApps(context.TODO(), &agentpb.DeployDefaultAppsRequest{
Upgrade: false,
})
if resp.Status != agentpb.StatusCode_OK {
p.log.Errorf("failed to deploy default apps, %s", resp.StatusMessage)
return
}

p.deployPlugins()

p.log.Debug("defualt plugins deployer job completed")
}

func (p *DefaultPluginsDeployer) deployPlugins() {
plugins, err := p.pluginStore.GetPlugins(pluginstorepb.StoreType_DEFAULT_STORE)
if err != nil {
p.log.Errorf("failed to get plugins, %v", err)
}

for _, plugin := range plugins {
if err := p.pluginStore.DeployPlugin(pluginstorepb.StoreType_DEFAULT_STORE, plugin.PluginName, plugin.Versions[0], []byte{}); err != nil {
p.log.Errorf("failed to deploy plugin, %v", err)
}
}
}
Loading

0 comments on commit 90dbd43

Please sign in to comment.