Skip to content

Commit 9b031ce

Browse files
Add plural cd services template (#509)
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.
1 parent 97979db commit 9b031ce

6 files changed

Lines changed: 158 additions & 30 deletions

File tree

.github/workflows/add-asana-comment.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

cmd/plural/cd_services.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
gqlclient "github.com/pluralsh/console-client-go"
8+
"github.com/pluralsh/plural-cli/pkg/cd/template"
89
"github.com/pluralsh/plural-cli/pkg/console"
910
"github.com/pluralsh/plural-cli/pkg/utils"
1011
"github.com/pluralsh/polly/containers"
@@ -95,6 +96,24 @@ func (p *Plural) cdServiceCommands() []cli.Command {
9596
},
9697
Usage: "describe cluster service",
9798
},
99+
{
100+
Name: "template",
101+
Action: p.handleTemplateService,
102+
Usage: "Dry-runs templating a .liquid or .tpl file with either a full service as params or custom config",
103+
Flags: []cli.Flag{
104+
cli.StringFlag{
105+
Name: "service",
106+
Usage: "specify the service you want to use as context while templating"},
107+
cli.StringFlag{
108+
Name: "configuration",
109+
Usage: "hand-coded configuration for templating (useful if you want to test before creating a service)",
110+
},
111+
cli.StringFlag{
112+
Name: "file",
113+
Usage: "The .liquid or .tpl file you want to attempt to template.",
114+
},
115+
},
116+
},
98117
{
99118
Name: "delete",
100119
ArgsUsage: "SERVICE_ID",
@@ -213,6 +232,50 @@ func (p *Plural) handleCreateClusterService(c *cli.Context) error {
213232
})
214233
}
215234

235+
func (p *Plural) handleTemplateService(c *cli.Context) error {
236+
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
237+
return err
238+
}
239+
240+
printResult := func(out []byte) error {
241+
fmt.Println()
242+
fmt.Println(string(out))
243+
return nil
244+
}
245+
246+
if identifier := c.String("service"); identifier != "" {
247+
serviceId, clusterName, serviceName, err := getServiceIdClusterNameServiceName(identifier)
248+
if err != nil {
249+
return err
250+
}
251+
252+
existing, err := p.ConsoleClient.GetClusterService(serviceId, serviceName, clusterName)
253+
if err != nil {
254+
return err
255+
}
256+
if existing == nil {
257+
return fmt.Errorf("Service %s does not exist", identifier)
258+
}
259+
260+
res, err := template.RenderService(c.String("file"), existing)
261+
if err != nil {
262+
return err
263+
}
264+
return printResult(res)
265+
}
266+
267+
bindings := map[string]interface{}{}
268+
if err := utils.YamlFile(c.String("configuration"), &bindings); err != nil {
269+
return err
270+
}
271+
272+
res, err := template.RenderYaml(c.String("file"), bindings)
273+
if err != nil {
274+
return err
275+
}
276+
return printResult(res)
277+
}
278+
216279
func (p *Plural) handleCloneClusterService(c *cli.Context) error {
217280
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
218281
return err

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ require (
5151
github.com/mitchellh/mapstructure v1.5.0
5252
github.com/norwoodj/helm-docs v1.11.2
5353
github.com/olekukonko/tablewriter v0.0.5
54-
github.com/osteele/liquid v1.3.2
54+
github.com/osteele/liquid v1.4.0
5555
github.com/packethost/packngo v0.29.0
5656
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
5757
github.com/pluralsh/cluster-api-migration v0.2.15
58-
github.com/pluralsh/console-client-go v0.5.3
58+
github.com/pluralsh/console-client-go v0.5.5
5959
github.com/pluralsh/gqlclient v1.11.0
6060
github.com/pluralsh/plural-operator v0.5.5
61-
github.com/pluralsh/polly v0.1.7
61+
github.com/pluralsh/polly v0.1.8
6262
github.com/pluralsh/terraform-delinker v0.0.2
6363
github.com/posthog/posthog-go v0.0.0-20230801140217-d607812dee69
6464
github.com/rivo/tview v0.0.0-20230615085408-bb9595ee0f4d

go.sum

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,8 +1382,8 @@ github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
13821382
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
13831383
github.com/orcaman/concurrent-map v1.0.0 h1:I/2A2XPCb4IuQWcQhBhSwGfiuybl/J0ev9HDbW65HOY=
13841384
github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
1385-
github.com/osteele/liquid v1.3.2 h1:G+MvVYt1HX2xuv99JgdrhV7zRVdlvFnNi8M5rN8gQmI=
1386-
github.com/osteele/liquid v1.3.2/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
1385+
github.com/osteele/liquid v1.4.0 h1:WS6lT3MFWUAxNbveF22tMLluOWNghGnKCZHLn7NbJGs=
1386+
github.com/osteele/liquid v1.4.0/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
13871387
github.com/osteele/tuesday v1.0.3 h1:SrCmo6sWwSgnvs1bivmXLvD7Ko9+aJvvkmDjB5G4FTU=
13881388
github.com/osteele/tuesday v1.0.3/go.mod h1:pREKpE+L03UFuR+hiznj3q7j3qB1rUZ4XfKejwWFF2M=
13891389
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
@@ -1424,10 +1424,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
14241424
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
14251425
github.com/pluralsh/cluster-api-migration v0.2.15 h1:TIfusD+wnhZTGmwNfIlKlKJOT2dE3rUaZawDJw98GjY=
14261426
github.com/pluralsh/cluster-api-migration v0.2.15/go.mod h1:J6lEvC/70KouikX16mE331cxc3y3sBwtmfHGwZqu06w=
1427-
github.com/pluralsh/console-client-go v0.1.17 h1:QMtnWdRvV13/sND/CFjFBUR8nyg3JJgwXReSyM6bK7A=
1428-
github.com/pluralsh/console-client-go v0.1.17/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
1429-
github.com/pluralsh/console-client-go v0.5.3 h1:RB4XtKlvh8+BM5o1o0h+W6zHculBGbL6q5lI/yRnqJE=
1430-
github.com/pluralsh/console-client-go v0.5.3/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
1427+
github.com/pluralsh/console-client-go v0.5.5 h1:SvpI1bCQTUMA1VAdSHH5ESxr6r9xLxkbySlzxOTBO40=
1428+
github.com/pluralsh/console-client-go v0.5.5/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
14311429
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
14321430
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
14331431
github.com/pluralsh/gqlclient v1.11.0 h1:FfXW7FiEJLHOfTAa7NxDb8jb3aMZNIpCAcG+bg8uHYA=
@@ -1438,8 +1436,8 @@ github.com/pluralsh/oauth v0.9.2 h1:tM9hBK4tCnJUeCOgX0ctxBBCS3hiCDPoxkJLODtedmQ=
14381436
github.com/pluralsh/oauth v0.9.2/go.mod h1:aTUw/75rzcsbvW+/TLvWtHVDXFIdtFrDtUncOq9vHyM=
14391437
github.com/pluralsh/plural-operator v0.5.5 h1:57GxniNjUa3hpHgvFr9oDonFgvDUC8XDD5B0e7Xduzk=
14401438
github.com/pluralsh/plural-operator v0.5.5/go.mod h1:WIXiz26/WDcUn0FA7Q1jPxmfsm98U1/JL8YpIdKVLX0=
1441-
github.com/pluralsh/polly v0.1.7 h1:MUuTb6rCUV1doisaFGC+iz+33ZPU4FZHOb/kFwSDkjw=
1442-
github.com/pluralsh/polly v0.1.7/go.mod h1:Yo1/jcW+4xwhWG+ZJikZy4J4HJkMNPZ7sq5auL2c/tY=
1439+
github.com/pluralsh/polly v0.1.8 h1:fkF5fLNofN4CyOs89lQfKeZaSXgRe8MnXz9VK5MzvRU=
1440+
github.com/pluralsh/polly v0.1.8/go.mod h1:W9IBX3e3xEjJuRjAQRfFJpH+UkNjddVY5YjMhyisQqQ=
14431441
github.com/pluralsh/terraform-delinker v0.0.2 h1:8SbUVxQa5To13ZZV2H64JLDLMEKolZWsqC+osyaTAu0=
14441442
github.com/pluralsh/terraform-delinker v0.0.2/go.mod h1:mU4F5OtfAIG5Xobbk+3uiU++AWKyfZPyyMmVZd23bV4=
14451443
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

pkg/cd/template/render.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package template
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
console "github.com/pluralsh/console-client-go"
9+
"github.com/pluralsh/polly/template"
10+
)
11+
12+
func RenderYaml(path string, bindings map[string]interface{}) ([]byte, error) {
13+
content, err := os.ReadFile(path)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
if strings.HasSuffix(path, ".tpl") {
19+
return template.RenderTpl(content, bindings)
20+
}
21+
22+
if strings.HasSuffix(path, ".liquid") {
23+
return template.RenderLiquid(content, bindings)
24+
}
25+
26+
return content, fmt.Errorf("Not a .liquid or .tpl file")
27+
}
28+
29+
func RenderService(path string, svc *console.ServiceDeploymentExtended) ([]byte, error) {
30+
bindings := map[string]interface{}{
31+
"Configuration": configMap(svc),
32+
"Cluster": clusterConfiguration(svc.Cluster),
33+
"Contexts": contexts(svc),
34+
}
35+
36+
for k, v := range bindings {
37+
bindings[strings.ToLower(k)] = v
38+
}
39+
40+
return RenderYaml(path, bindings)
41+
}

pkg/cd/template/utils.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package template
2+
3+
import (
4+
"strings"
5+
6+
console "github.com/pluralsh/console-client-go"
7+
)
8+
9+
func clusterConfiguration(cluster *console.BaseClusterFragment) map[string]interface{} {
10+
res := map[string]interface{}{
11+
"ID": cluster.ID,
12+
"Self": cluster.Self,
13+
"Handle": cluster.Handle,
14+
"Name": cluster.Name,
15+
"Version": cluster.Version,
16+
"CurrentVersion": cluster.CurrentVersion,
17+
"KasUrl": cluster.KasURL,
18+
"Metadata": cluster.Metadata,
19+
}
20+
21+
for k, v := range res {
22+
res[strings.ToLower(k)] = v
23+
}
24+
res["kasUrl"] = cluster.KasURL
25+
res["currentVersion"] = cluster.CurrentVersion
26+
27+
return res
28+
}
29+
30+
func configMap(svc *console.ServiceDeploymentExtended) map[string]string {
31+
res := map[string]string{}
32+
for _, config := range svc.Configuration {
33+
res[config.Name] = config.Value
34+
}
35+
36+
return res
37+
}
38+
39+
func contexts(svc *console.ServiceDeploymentExtended) map[string]map[string]interface{} {
40+
res := map[string]map[string]interface{}{}
41+
for _, context := range svc.Contexts {
42+
res[context.Name] = context.Configuration
43+
}
44+
return res
45+
}

0 commit comments

Comments
 (0)