Skip to content

Commit

Permalink
Merge pull request #208 from kube-tarian/template-values-add
Browse files Browse the repository at this point in the history
adding app template values
  • Loading branch information
vramk23 authored Sep 1, 2023
2 parents cb677a9 + 65aa51c commit 2778fe6
Show file tree
Hide file tree
Showing 48 changed files with 2,317 additions and 6,697 deletions.
47 changes: 46 additions & 1 deletion capten/agent/pkg/agent/agent.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package agent

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

"github.com/gogo/status"
"github.com/intelops/go-common/logging"
Expand All @@ -13,9 +14,11 @@ import (
"github.com/kube-tarian/kad/capten/agent/pkg/temporalclient"
"github.com/kube-tarian/kad/capten/agent/pkg/workers"
ory "github.com/ory/client-go"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"gopkg.in/yaml.v2"

"go.temporal.io/sdk/client"
)
Expand Down Expand Up @@ -161,3 +164,45 @@ func verifyToken(log logging.Logger, oryPAT, token string, oryApiClient *ory.API
}
return introspect.Active, nil
}

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

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

transformedData = buf.Bytes()
return
}

func deriveTemplateValuesMapping(overrideValues, templateValues []byte) (map[string]any, error) {
templateValues, err := deriveTemplateValues(overrideValues, templateValues)
if err != nil {
return nil, err
}

templateValuesMapping := map[string]any{}
if err := yaml.Unmarshal(templateValues, &templateValuesMapping); err != nil {
return nil, errors.WithMessagef(err, "failed to Unmarshal template values")
}
return templateValuesMapping, nil
}

func deriveTemplateValues(overrideValues, templateValues []byte) ([]byte, error) {
overrideValuesMapping := map[string]any{}
if err := yaml.Unmarshal(overrideValues, &overrideValuesMapping); err != nil {
return nil, errors.WithMessagef(err, "failed to Unmarshal override values")
}

templateValues, err := executeTemplateValuesTemplate(templateValues, overrideValuesMapping)
if err != nil {
return nil, errors.WithMessagef(err, "failed to exeute template values update")
}
return templateValues, nil
}
93 changes: 45 additions & 48 deletions capten/agent/pkg/agent/agent_app_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,36 @@ import (
"encoding/base64"

"github.com/kube-tarian/kad/capten/agent/pkg/agentpb"
"github.com/kube-tarian/kad/capten/agent/pkg/model"
"github.com/kube-tarian/kad/capten/agent/pkg/workers"
)

func (a *Agent) ClimonAppInstall(ctx context.Context, request *agentpb.ClimonInstallRequest) (*agentpb.JobResponse, error) {
a.log.Infof("Recieved Climon Install event %+v", request)
worker := workers.NewClimon(a.tc, a.log)

if request.ClusterName == "" {
request.ClusterName = "inbuilt"
}
run, err := worker.SendEvent(ctx, "install", request)
if err != nil {
return &agentpb.JobResponse{}, err
}

return prepareJobResponse(run, worker.GetWorkflowName()), err
}

func (a *Agent) ClimonAppDelete(ctx context.Context, request *agentpb.ClimonDeleteRequest) (*agentpb.JobResponse, error) {
a.log.Infof("Recieved Climon delete event %+v", request)
worker := workers.NewClimon(a.tc, a.log)

if request.ClusterName == "" {
request.ClusterName = "inbuilt"
}
run, err := worker.SendDeleteEvent(ctx, "delete", request)
if err != nil {
return &agentpb.JobResponse{}, err
}

return prepareJobResponse(run, worker.GetWorkflowName()), err
}

func (a *Agent) InstallApp(ctx context.Context, request *agentpb.InstallAppRequest) (*agentpb.InstallAppResponse, error) {
a.log.Infof("Recieved App Install request %+v", request)
worker := workers.NewDeployment(a.tc, a.log)

config := &agentpb.ApplicationInstallRequest{
PluginName: "helm",
RepoName: request.AppConfig.RepoName,
RepoUrl: request.AppConfig.RepoURL,
ChartName: request.AppConfig.ChartName,
Namespace: request.AppConfig.Namespace,
ReleaseName: request.AppConfig.ReleaseName,
Version: request.AppConfig.Version,
ClusterName: "capten",
ValuesYaml: base64.StdEncoding.EncodeToString(request.AppValues.OverrideValues),
Timeout: 5,
}

run, err := worker.SendEvent(ctx, "install", config)
templateValues, err := deriveTemplateValues(request.AppValues.OverrideValues, request.AppValues.TemplateValues)
if err != nil {
a.log.Errorf("failed to send store app install event, %v", err)
a.log.Errorf("failed to derive template values, %v", err)
return &agentpb.InstallAppResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "Internall error in create install app job",
StatusMessage: "failed to derive template values",
}, err
}

config := &model.ApplicationInstallRequest{
PluginName: "helm",
RepoName: request.AppConfig.RepoName,
RepoURL: request.AppConfig.RepoURL,
ChartName: request.AppConfig.ChartName,
Namespace: request.AppConfig.Namespace,
ReleaseName: request.AppConfig.ReleaseName,
Version: request.AppConfig.Version,
ClusterName: "capten",
OverrideValues: base64.StdEncoding.EncodeToString(templateValues),
Timeout: 5,
}

syncConfig := &agentpb.SyncAppData{
Config: &agentpb.AppConfig{
ReleaseName: request.AppConfig.ReleaseName,
Expand All @@ -80,7 +51,7 @@ func (a *Agent) InstallApp(ctx context.Context, request *agentpb.InstallAppReque
Icon: request.AppConfig.Icon,
LaunchURL: request.AppConfig.LaunchURL,
LaunchUIDescription: request.AppConfig.LaunchUIDescription,
InstallStatus: "Installed",
InstallStatus: "Installing",
DefualtApp: request.AppConfig.DefualtApp,
},
Values: &agentpb.AppValues{
Expand All @@ -97,11 +68,37 @@ func (a *Agent) InstallApp(ctx context.Context, request *agentpb.InstallAppReque
}, err
}

_, err = worker.SendEvent(ctx, "install", config)
if err != nil {
syncConfig.Config.InstallStatus = "Install Failed"
if err := a.as.UpsertAppConfig(syncConfig); err != nil {
a.log.Errorf("failed to update sync app config, %v", err)
return &agentpb.InstallAppResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to sync app config",
}, err
}

a.log.Errorf("failed to send store app install event, %v", err)
return &agentpb.InstallAppResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "Internall error in create install app job",
}, err
}

syncConfig.Config.InstallStatus = "Installed"
if err := a.as.UpsertAppConfig(syncConfig); err != nil {
a.log.Errorf("failed to update sync app config, %v", err)
return &agentpb.InstallAppResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to sync app config",
}, err
}

a.log.Infof("Sync app [%s] successful", request.AppConfig.ReleaseName)

return &agentpb.InstallAppResponse{
Status: agentpb.StatusCode_OK,
StatusMessage: "success",
JobResponse: &agentpb.JobResponse{Id: run.GetID(), RunID: run.GetRunID(), WorkflowName: worker.GetWorkflowName()},
}, nil
}
75 changes: 44 additions & 31 deletions capten/agent/pkg/agent/agent_conf_sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (

"github.com/kube-tarian/kad/capten/agent/pkg/agentpb"
"github.com/kube-tarian/kad/capten/agent/pkg/credential"
"github.com/kube-tarian/kad/capten/agent/pkg/model"
"github.com/kube-tarian/kad/capten/agent/pkg/workers"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

func (a *Agent) ConfigureAppSSO(
ctx context.Context, req *agentpb.ConfigureAppSSORequest) (*agentpb.ConfigureAppSSOResponse, error) {

if req.ReleaseName == "" {
return &agentpb.ConfigureAppSSOResponse{
Status: agentpb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "release name empty",
}, nil
}
a.log.Infof("Received request for ConfigureAppSSO, release_name: %s\n", req.ReleaseName)
a.log.Infof("Received request for ConfigureAppSSO, app %s", req.ReleaseName)

appConfig, err := a.as.GetAppConfig(req.ReleaseName)
if err != nil {
Expand All @@ -35,7 +35,6 @@ func (a *Agent) ConfigureAppSSO(
}

if err := credential.StoreAppOauthCredential(ctx, req.ReleaseName, req.ClientId, req.ClientSecret); err != nil {
a.log.Audit("security", "storecred", "failed", "system", "failed to intialize credentails for clientId: %s", req.ClientId)
a.log.Errorf("failed to store credential for ClientId: %s, %v", req.ClientId, err)
return &agentpb.ConfigureAppSSOResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
Expand All @@ -49,25 +48,24 @@ func (a *Agent) ConfigureAppSSO(
"OAuthBaseURL": req.OAuthBaseURL,
}

launchUiMapping, overrideValuesMapping := map[string]any{}, map[any]any{}

if err := yaml.Unmarshal(appConfig.Values.LaunchUIValues, &launchUiMapping); err != nil {
a.log.Errorf("failed to Unmarshal LaunchUIValues: %s err: %v", string(appConfig.Values.LaunchUIValues), err)
templateValuesMapping, err := deriveTemplateValuesMapping(appConfig.Values.OverrideValues, appConfig.Values.TemplateValues)
if err != nil {
a.log.Errorf("failed to derivee template values, err: %v", err)
return &agentpb.ConfigureAppSSOResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: errors.WithMessage(err, "err Unmarshalling launchiUiValues").Error(),
StatusMessage: errors.WithMessage(err, "failed to dervice template values").Error(),
}, nil
}

if err := yaml.Unmarshal(appConfig.Values.OverrideValues, &overrideValuesMapping); err != nil {
a.log.Errorf("failed to Unmarshal OverrideValues: %s err: %v", string(appConfig.Values.OverrideValues), err)
launchUiMapping := map[string]any{}
if err := yaml.Unmarshal(appConfig.Values.LaunchUIValues, &launchUiMapping); err != nil {
a.log.Errorf("failed to Unmarshal LaunchUIValues: %s err: %v", string(appConfig.Values.LaunchUIValues), err)
return &agentpb.ConfigureAppSSOResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: errors.WithMessage(err, "err Unmarshalling overrideValues").Error(),
StatusMessage: errors.WithMessage(err, "err Unmarshalling launchiUiValues").Error(),
}, nil
}

// replace values
launchUiMapping, err = replaceTemplateValues(launchUiMapping, ssoOverwriteMapping)
if err != nil {
a.log.Errorf("failed to replaceTemplateValues, err: %v", err)
Expand All @@ -77,33 +75,48 @@ func (a *Agent) ConfigureAppSSO(
}, nil
}

// merge values
overrideValuesMapping = mergeRecursive(overrideValuesMapping, convertKey(launchUiMapping))

// update override values in db
marshaledOverrideValues, err := yaml.Marshal(overrideValuesMapping)
finalOverrideValuesMapping := mergeRecursive(convertKey(templateValuesMapping), convertKey(launchUiMapping))
marshaledOverrideValues, err := yaml.Marshal(finalOverrideValuesMapping)
if err != nil {
a.log.Errorf("failed to Marshal, err: %v", err)
return &agentpb.ConfigureAppSSOResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: errors.WithMessage(err, "err marshalling overrideValues").Error(),
}, nil
}

newAppConfig := *appConfig
newAppConfig.Values.OverrideValues = marshaledOverrideValues
newAppConfig.Config.InstallStatus = "Updating"

if err := a.as.UpsertAppConfig(&newAppConfig); err != nil {
a.log.Errorf("failed to UpsertAppConfig, err: %v", err)
return &agentpb.ConfigureAppSSOResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: errors.WithMessage(err, "err upserting new appConfig").Error(),
}, nil
}

// start temporal workflow to redploy apps
wd := workers.NewDeployment(a.tc, a.log)
run, err := wd.SendEvent(context.TODO(), "update", installRequestFromSyncApp(&newAppConfig))
if err != nil {
newAppConfig.Config.InstallStatus = "Update Failed"
if err := a.as.UpsertAppConfig(&newAppConfig); err != nil {
a.log.Errorf("failed to UpsertAppConfig, err: %v", err)
return &agentpb.ConfigureAppSSOResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: errors.WithMessage(err, "err upserting new appConfig").Error(),
}, nil
}

a.log.Errorf("failed to SendEvent, err: %v", err)
return &agentpb.ConfigureAppSSOResponse{
Status: agentpb.StatusCode_INTERNRAL_ERROR,
StatusMessage: errors.WithMessage(err, "err sending deployment event").Error(),
}, nil
}

// update new values in db
newAppConfig.Config.InstallStatus = "Updated"
if err := a.as.UpsertAppConfig(&newAppConfig); err != nil {
a.log.Errorf("failed to UpsertAppConfig, err: %v", err)
return &agentpb.ConfigureAppSSOResponse{
Expand Down Expand Up @@ -186,19 +199,19 @@ func convertKey(m map[string]any) map[any]any {
return ret
}

func installRequestFromSyncApp(data *agentpb.SyncAppData) *agentpb.ApplicationInstallRequest {
func installRequestFromSyncApp(data *agentpb.SyncAppData) *model.ApplicationInstallRequest {
values := make([]byte, len(data.Values.OverrideValues))
copy(values, data.Values.OverrideValues)
return &agentpb.ApplicationInstallRequest{
PluginName: "helm",
RepoName: data.Config.RepoName,
RepoUrl: data.Config.RepoURL,
ChartName: data.Config.ChartName,
Namespace: data.Config.Namespace,
ReleaseName: data.Config.ReleaseName,
Version: data.Config.Version,
ClusterName: "capten",
ValuesYaml: string(values),
Timeout: 10,
return &model.ApplicationInstallRequest{
PluginName: "helm",
RepoName: data.Config.RepoName,
RepoURL: data.Config.RepoURL,
ChartName: data.Config.ChartName,
Namespace: data.Config.Namespace,
ReleaseName: data.Config.ReleaseName,
Version: data.Config.Version,
ClusterName: "capten",
OverrideValues: string(values),
Timeout: 10,
}
}
38 changes: 0 additions & 38 deletions capten/agent/pkg/agent/agent_deployer.go

This file was deleted.

Loading

0 comments on commit 2778fe6

Please sign in to comment.