Skip to content

Commit

Permalink
Add plural cd services template (#509)
Browse files Browse the repository at this point in the history
This will make it a much smoother experience to test .liquid or .tpl files as they are being developed.  Should be functionally equivalent to the process done in-agent.
  • Loading branch information
michaeljguarino committed May 6, 2024
1 parent 97979db commit 9b031ce
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 30 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/add-asana-comment.yaml

This file was deleted.

63 changes: 63 additions & 0 deletions cmd/plural/cd_services.go
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

gqlclient "github.com/pluralsh/console-client-go"
"github.com/pluralsh/plural-cli/pkg/cd/template"
"github.com/pluralsh/plural-cli/pkg/console"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/pluralsh/polly/containers"
Expand Down Expand Up @@ -95,6 +96,24 @@ func (p *Plural) cdServiceCommands() []cli.Command {
},
Usage: "describe cluster service",
},
{
Name: "template",
Action: p.handleTemplateService,
Usage: "Dry-runs templating a .liquid or .tpl file with either a full service as params or custom config",
Flags: []cli.Flag{
cli.StringFlag{
Name: "service",
Usage: "specify the service you want to use as context while templating"},
cli.StringFlag{
Name: "configuration",
Usage: "hand-coded configuration for templating (useful if you want to test before creating a service)",
},
cli.StringFlag{
Name: "file",
Usage: "The .liquid or .tpl file you want to attempt to template.",
},
},
},
{
Name: "delete",
ArgsUsage: "SERVICE_ID",
Expand Down Expand Up @@ -213,6 +232,50 @@ func (p *Plural) handleCreateClusterService(c *cli.Context) error {
})
}

func (p *Plural) handleTemplateService(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}

printResult := func(out []byte) error {
fmt.Println()
fmt.Println(string(out))
return nil
}

if identifier := c.String("service"); identifier != "" {
serviceId, clusterName, serviceName, err := getServiceIdClusterNameServiceName(identifier)
if err != nil {
return err
}

existing, err := p.ConsoleClient.GetClusterService(serviceId, serviceName, clusterName)
if err != nil {
return err
}
if existing == nil {
return fmt.Errorf("Service %s does not exist", identifier)
}

res, err := template.RenderService(c.String("file"), existing)
if err != nil {
return err
}
return printResult(res)
}

bindings := map[string]interface{}{}
if err := utils.YamlFile(c.String("configuration"), &bindings); err != nil {
return err
}

res, err := template.RenderYaml(c.String("file"), bindings)
if err != nil {
return err
}
return printResult(res)
}

func (p *Plural) handleCloneClusterService(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Expand Up @@ -51,14 +51,14 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/norwoodj/helm-docs v1.11.2
github.com/olekukonko/tablewriter v0.0.5
github.com/osteele/liquid v1.3.2
github.com/osteele/liquid v1.4.0
github.com/packethost/packngo v0.29.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pluralsh/cluster-api-migration v0.2.15
github.com/pluralsh/console-client-go v0.5.3
github.com/pluralsh/console-client-go v0.5.5
github.com/pluralsh/gqlclient v1.11.0
github.com/pluralsh/plural-operator v0.5.5
github.com/pluralsh/polly v0.1.7
github.com/pluralsh/polly v0.1.8
github.com/pluralsh/terraform-delinker v0.0.2
github.com/posthog/posthog-go v0.0.0-20230801140217-d607812dee69
github.com/rivo/tview v0.0.0-20230615085408-bb9595ee0f4d
Expand Down
14 changes: 6 additions & 8 deletions go.sum
Expand Up @@ -1382,8 +1382,8 @@ github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/orcaman/concurrent-map v1.0.0 h1:I/2A2XPCb4IuQWcQhBhSwGfiuybl/J0ev9HDbW65HOY=
github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
github.com/osteele/liquid v1.3.2 h1:G+MvVYt1HX2xuv99JgdrhV7zRVdlvFnNi8M5rN8gQmI=
github.com/osteele/liquid v1.3.2/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
github.com/osteele/liquid v1.4.0 h1:WS6lT3MFWUAxNbveF22tMLluOWNghGnKCZHLn7NbJGs=
github.com/osteele/liquid v1.4.0/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
github.com/osteele/tuesday v1.0.3 h1:SrCmo6sWwSgnvs1bivmXLvD7Ko9+aJvvkmDjB5G4FTU=
github.com/osteele/tuesday v1.0.3/go.mod h1:pREKpE+L03UFuR+hiznj3q7j3qB1rUZ4XfKejwWFF2M=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
Expand Down Expand Up @@ -1424,10 +1424,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pluralsh/cluster-api-migration v0.2.15 h1:TIfusD+wnhZTGmwNfIlKlKJOT2dE3rUaZawDJw98GjY=
github.com/pluralsh/cluster-api-migration v0.2.15/go.mod h1:J6lEvC/70KouikX16mE331cxc3y3sBwtmfHGwZqu06w=
github.com/pluralsh/console-client-go v0.1.17 h1:QMtnWdRvV13/sND/CFjFBUR8nyg3JJgwXReSyM6bK7A=
github.com/pluralsh/console-client-go v0.1.17/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/console-client-go v0.5.3 h1:RB4XtKlvh8+BM5o1o0h+W6zHculBGbL6q5lI/yRnqJE=
github.com/pluralsh/console-client-go v0.5.3/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/console-client-go v0.5.5 h1:SvpI1bCQTUMA1VAdSHH5ESxr6r9xLxkbySlzxOTBO40=
github.com/pluralsh/console-client-go v0.5.5/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
github.com/pluralsh/gqlclient v1.11.0 h1:FfXW7FiEJLHOfTAa7NxDb8jb3aMZNIpCAcG+bg8uHYA=
Expand All @@ -1438,8 +1436,8 @@ github.com/pluralsh/oauth v0.9.2 h1:tM9hBK4tCnJUeCOgX0ctxBBCS3hiCDPoxkJLODtedmQ=
github.com/pluralsh/oauth v0.9.2/go.mod h1:aTUw/75rzcsbvW+/TLvWtHVDXFIdtFrDtUncOq9vHyM=
github.com/pluralsh/plural-operator v0.5.5 h1:57GxniNjUa3hpHgvFr9oDonFgvDUC8XDD5B0e7Xduzk=
github.com/pluralsh/plural-operator v0.5.5/go.mod h1:WIXiz26/WDcUn0FA7Q1jPxmfsm98U1/JL8YpIdKVLX0=
github.com/pluralsh/polly v0.1.7 h1:MUuTb6rCUV1doisaFGC+iz+33ZPU4FZHOb/kFwSDkjw=
github.com/pluralsh/polly v0.1.7/go.mod h1:Yo1/jcW+4xwhWG+ZJikZy4J4HJkMNPZ7sq5auL2c/tY=
github.com/pluralsh/polly v0.1.8 h1:fkF5fLNofN4CyOs89lQfKeZaSXgRe8MnXz9VK5MzvRU=
github.com/pluralsh/polly v0.1.8/go.mod h1:W9IBX3e3xEjJuRjAQRfFJpH+UkNjddVY5YjMhyisQqQ=
github.com/pluralsh/terraform-delinker v0.0.2 h1:8SbUVxQa5To13ZZV2H64JLDLMEKolZWsqC+osyaTAu0=
github.com/pluralsh/terraform-delinker v0.0.2/go.mod h1:mU4F5OtfAIG5Xobbk+3uiU++AWKyfZPyyMmVZd23bV4=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
41 changes: 41 additions & 0 deletions pkg/cd/template/render.go
@@ -0,0 +1,41 @@
package template

import (
"fmt"
"os"
"strings"

console "github.com/pluralsh/console-client-go"
"github.com/pluralsh/polly/template"
)

func RenderYaml(path string, bindings map[string]interface{}) ([]byte, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}

if strings.HasSuffix(path, ".tpl") {
return template.RenderTpl(content, bindings)
}

if strings.HasSuffix(path, ".liquid") {
return template.RenderLiquid(content, bindings)
}

return content, fmt.Errorf("Not a .liquid or .tpl file")
}

func RenderService(path string, svc *console.ServiceDeploymentExtended) ([]byte, error) {
bindings := map[string]interface{}{
"Configuration": configMap(svc),
"Cluster": clusterConfiguration(svc.Cluster),
"Contexts": contexts(svc),
}

for k, v := range bindings {
bindings[strings.ToLower(k)] = v
}

return RenderYaml(path, bindings)
}
45 changes: 45 additions & 0 deletions pkg/cd/template/utils.go
@@ -0,0 +1,45 @@
package template

import (
"strings"

console "github.com/pluralsh/console-client-go"
)

func clusterConfiguration(cluster *console.BaseClusterFragment) map[string]interface{} {
res := map[string]interface{}{
"ID": cluster.ID,
"Self": cluster.Self,
"Handle": cluster.Handle,
"Name": cluster.Name,
"Version": cluster.Version,
"CurrentVersion": cluster.CurrentVersion,
"KasUrl": cluster.KasURL,
"Metadata": cluster.Metadata,
}

for k, v := range res {
res[strings.ToLower(k)] = v
}
res["kasUrl"] = cluster.KasURL
res["currentVersion"] = cluster.CurrentVersion

return res
}

func configMap(svc *console.ServiceDeploymentExtended) map[string]string {
res := map[string]string{}
for _, config := range svc.Configuration {
res[config.Name] = config.Value
}

return res
}

func contexts(svc *console.ServiceDeploymentExtended) map[string]map[string]interface{} {
res := map[string]map[string]interface{}{}
for _, context := range svc.Contexts {
res[context.Name] = context.Configuration
}
return res
}

0 comments on commit 9b031ce

Please sign in to comment.