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

refactor capten deploy methods to common package #494

Merged
merged 1 commit into from
May 23, 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
7 changes: 5 additions & 2 deletions capten/agent/internal/api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

"github.com/intelops/go-common/logging"
"github.com/kube-tarian/kad/capten/agent/internal/config"
"github.com/kube-tarian/kad/capten/agent/internal/temporalclient"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"
"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"
"github.com/kube-tarian/kad/capten/common-pkg/temporalclient"
)

var _ agentpb.AgentServer = &Agent{}
Expand All @@ -26,6 +26,9 @@ type pluginStore interface {
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

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

type Agent struct {
Expand Down Expand Up @@ -58,7 +61,7 @@ func NewAgent(log logging.Logger, cfg *config.SericeConfig,
log: log,
}

agent.plugin, err = pluginstore.NewPluginStore(log, as, agent)
agent.plugin, err = pluginstore.NewPluginStore(log, as, tc)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions capten/agent/internal/api/agent_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (a *Agent) AuthInterceptor(ctx context.Context, req interface{}, info *grpc
a.log.Errorf("error occured while extracting oauth token, oryurl, and ory pat token error: %v", err.Error())
return nil, status.Error(codes.Unauthenticated, "invalid or missing token")
}
oryApiClient := newOryAPIClient(a.log, oryUrl)
oryApiClient := newOryAPIClient(oryUrl)
isValid, err := verifyToken(a.log, oryPat, tk, oryApiClient)
if err != nil || !isValid {
return nil, status.Error(codes.Unauthenticated, "invalid or missing token")
Expand All @@ -27,7 +27,7 @@ func (a *Agent) AuthInterceptor(ctx context.Context, req interface{}, info *grpc
return handler(ctx, req)
}

func newOryAPIClient(log logging.Logger, oryURL string) *ory.APIClient {
func newOryAPIClient(oryURL string) *ory.APIClient {
config := ory.NewConfiguration()
config.Servers = ory.ServerConfigurations{{
URL: oryURL,
Expand Down
2 changes: 1 addition & 1 deletion capten/agent/internal/api/app_config_sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package api
import (
"context"

"github.com/kube-tarian/kad/capten/agent/internal/workers"
"github.com/kube-tarian/kad/capten/common-pkg/credential"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/workers"
"github.com/kube-tarian/kad/capten/model"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
Expand Down
104 changes: 8 additions & 96 deletions capten/agent/internal/api/cluster_plugin_apis.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package api

import (
"bytes"
"context"
"html/template"

"github.com/kube-tarian/kad/capten/agent/internal/workers"
"github.com/kube-tarian/kad/capten/common-pkg/pb/clusterpluginspb"
"github.com/kube-tarian/kad/capten/model"
"gopkg.in/yaml.v2"
)

func (a *Agent) GetClusterPlugins(ctx context.Context, request *clusterpluginspb.GetClusterPluginsRequest) (
Expand Down Expand Up @@ -43,27 +38,15 @@ func (a *Agent) DeployClusterPlugin(ctx context.Context, request *clusterplugins
*clusterpluginspb.DeployClusterPluginResponse, error) {
a.log.Infof("Recieved Plugin Deploy request for plugin %s, version %+v", request.Plugin.PluginName, request.Plugin.Version)

values, err := replaceTemplateValuesInByteData(request.Plugin.Values, request.Plugin.OverrideValues)
err := a.plugin.DeployClusterPlugin(ctx, request.Plugin)
if err != nil {
a.log.Errorf("failed to derive template values for plugin %s, %v", request.Plugin.PluginName, err)
a.log.Errorf("failed to deploy plugin [%s], %v", request.Plugin.PluginName, err)
return &clusterpluginspb.DeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to prepare plugin values",
}, nil
}

request.Plugin.Values = values
request.Plugin.InstallStatus = string(model.AppIntallingStatus)
if err := a.as.UpsertClusterPluginConfig(request.Plugin); err != nil {
a.log.Errorf("failed to update plugin config data for plugin %s, %v", request.Plugin.PluginName, err)
return &clusterpluginspb.DeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to update plugin config data",
}, nil
StatusMessage: "failed to deploy plugin",
}, err
}

go a.deployPluginWithWorkflow(request.Plugin)

a.log.Infof("Triggerred plugin [%s] install", request.Plugin.PluginName)
return &clusterpluginspb.DeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_OK,
Expand All @@ -75,89 +58,18 @@ func (a *Agent) UnDeployClusterPlugin(ctx context.Context, request *clusterplugi
*clusterpluginspb.UnDeployClusterPluginResponse, error) {
a.log.Infof("Recieved Plugin UnInstall request %+v", request)

if request.PluginName == "" {
a.log.Errorf("release name is empty")
return &clusterpluginspb.UnDeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "release name is missing in request",
}, nil
}

pluginConfigdata, err := a.as.GetClusterPluginConfig(request.PluginName)
err := a.plugin.UnDeployClusterPlugin(ctx, request)
if err != nil {
a.log.Errorf("failed to fetch plugin config record %s, %v", request.PluginName, err)
return &clusterpluginspb.UnDeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to fetch plugin config",
}, nil
}

pluginConfigdata.InstallStatus = string(model.AppUnInstallingStatus)
if err := a.as.UpsertClusterPluginConfig(pluginConfigdata); err != nil {
a.log.Errorf("failed to update plugin config status with UnInstalling for plugin %s, %v", pluginConfigdata.PluginName, err)
a.log.Errorf("failed to undeploy plugin [%s], %v", request.PluginName, err)
return &clusterpluginspb.UnDeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to undeploy the plugin",
}, nil
StatusMessage: "failed to undeploy plugin",
}, err
}

go a.unInstallPluginWithWorkflow(request, pluginConfigdata)

a.log.Infof("Triggerred plugin [%s] un install", request.PluginName)
return &clusterpluginspb.UnDeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_OK,
StatusMessage: "plugin is successfully undeployed",
}, nil
}

func (a *Agent) deployPluginWithWorkflow(plugin *clusterpluginspb.Plugin) {
wd := workers.NewDeployment(a.tc, a.log)
_, err := wd.SendEventV2(context.TODO(), wd.GetPluginWorkflowName(), string(model.AppInstallAction), plugin)
if err != nil {
// pluginConfig.InstallStatus = string(model.AppIntallFailedStatus)
// if err := a.pas.UpsertPluginConfig(pluginConfig); err != nil {
// a.log.Errorf("failed to update plugin config for plugin %s, %v", pluginConfig.PluginName, err)
// return
// }
a.log.Errorf("sendEventV2 failed, plugin: %s, reason: %v", plugin.PluginName, err)
return
}
// TODO: workflow will update the final status
// Write a periodic scheduler which will go through all apps not in installed status and check the status till either success or failed.
// Make SendEventV2 asynchrounous so that periodic scheduler will take care of monitoring.
}

func (a *Agent) unInstallPluginWithWorkflow(request *clusterpluginspb.UnDeployClusterPluginRequest, plugin *clusterpluginspb.Plugin) {
wd := workers.NewDeployment(a.tc, a.log)
_, err := wd.SendDeleteEvent(context.TODO(), wd.GetPluginWorkflowName(), string(model.AppUnInstallAction), request)
if err != nil {
a.log.Errorf("failed to send delete event to workflow for plugin %s, %v", request.PluginName, err)

plugin.InstallStatus = string(model.AppUnUninstallFailedStatus)
if err := a.as.UpsertClusterPluginConfig(plugin); err != nil {
a.log.Errorf("failed to update plugin config status with Installed for plugin %s, %v", request.PluginName, err)
}
}
}

func replaceTemplateValuesInByteData(data []byte,
values []byte) (transformedData []byte, err error) {
tmpl, err := template.New("templateVal").Parse(string(data))
if err != nil {
return
}

mapValues := map[string]any{}
if err = yaml.Unmarshal(values, &mapValues); err != nil {
return
}

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

transformedData = buf.Bytes()
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

"github.com/kube-tarian/kad/capten/agent/internal/workers"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/workers"
"github.com/kube-tarian/kad/capten/model"
)

Expand Down
2 changes: 1 addition & 1 deletion capten/agent/internal/api/plugin_tekton_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package api
import (
"context"

"github.com/kube-tarian/kad/capten/agent/internal/workers"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/workers"
"github.com/kube-tarian/kad/capten/model"
)

Expand Down
2 changes: 1 addition & 1 deletion capten/agent/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
ginapiserver "github.com/kube-tarian/kad/capten/agent/gin-api-server"
agentapi "github.com/kube-tarian/kad/capten/agent/internal/api"
"github.com/kube-tarian/kad/capten/agent/internal/config"
"github.com/kube-tarian/kad/capten/agent/internal/crossplane"
"github.com/kube-tarian/kad/capten/agent/internal/job"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"
"github.com/kube-tarian/kad/capten/common-pkg/crossplane"
"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"
Expand Down
2 changes: 1 addition & 1 deletion capten/agent/internal/job/crossplane_resources_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package job

import (
"github.com/intelops/go-common/logging"
"github.com/kube-tarian/kad/capten/agent/internal/crossplane"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"
"github.com/kube-tarian/kad/capten/common-pkg/crossplane"
)

type CrossplaneResourcesSync struct {
Expand Down
118 changes: 0 additions & 118 deletions capten/agent/internal/temporalclient/client.go

This file was deleted.

Loading
Loading