Skip to content

Instantly share code, notes, and snippets.

@ilackarms
Created February 25, 2019 17:57
Show Gist options
  • Save ilackarms/7b95d948d68c6021b8c61f94846bcb92 to your computer and use it in GitHub Desktop.
Save ilackarms/7b95d948d68c6021b8c61f94846bcb92 to your computer and use it in GitHub Desktop.
shows how to write kube-friendly yaml files for gloo using Go code
package main
import (
"github.com/ghodss/yaml"
gateway "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/plugins/transformation"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/crd"
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
"io"
"os"
)
/*
Example of how to generate Kube-friendly yaml from Gloo Go structs
Running this will produce a file virtual-service.yaml with contents:
apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
creationTimestamp: null
name: something
namespace: mynamespace
spec:
virtualHost:
domains:
- my-domain.com
routes:
- matcher:
methods:
- GET
prefix: /test-route
routeAction:
multi:
destinations:
- destination:
upstream:
name: my-upstream
namespace: my-namespace
weight: 1
- destination:
upstream:
name: your-upstream
namespace: your-namespace
weight: 2
routePlugins:
prefixRewrite:
prefixRewrite: /
status: {}
*/
type tuple struct {
Name string
Crd crd.Crd
Resource resources.InputResource
}
var resourcesToWrite = []tuple{
{
Name: "virtual-service.yaml",
Crd: gateway.VirtualServiceCrd,
Resource: &gateway.VirtualService{
Metadata: core.Metadata{
Namespace: "mynamespace",
Name: "something",
},
VirtualHost: &v1.VirtualHost{
Domains: []string{"my-domain.com"},
Routes: []*v1.Route{
{
Matcher: &v1.Matcher{
PathSpecifier: &v1.Matcher_Prefix{Prefix: "/test-route"},
Methods: []string{"GET"},
},
Action: &v1.Route_RouteAction{
RouteAction: &v1.RouteAction{
Destination: &v1.RouteAction_Multi{
Multi: &v1.MultiDestination{
Destinations: []*v1.WeightedDestination{
{
Destination: &v1.Destination{
Upstream: core.ResourceRef{
Name: "my-upstream",
Namespace: "my-namespace",
},
},
Weight: 1,
},
{
Destination: &v1.Destination{
Upstream: core.ResourceRef{
Name: "your-upstream",
Namespace: "your-namespace",
},
},
Weight: 2,
},
},
},
},
},
},
RoutePlugins: &v1.RoutePlugins{
PrefixRewrite: &transformation.PrefixRewrite{
PrefixRewrite: "/",
},
},
},
},
},
},
},
}
func main() {
for _, res := range resourcesToWrite {
if err := write(res); err != nil {
panic(err)
}
}
}
func write(t tuple) error {
f, err := os.Create(t.Name)
if err != nil {
return err
}
defer f.Close()
err = writeYaml(t.Crd, t.Resource, f)
if err != nil {
return err
}
return nil
}
func writeYaml(crd crd.Crd, resource resources.InputResource, w io.Writer) error {
crdResource := crd.KubeResource(resource)
b, err := yaml.Marshal(crdResource)
if err != nil {
return err
}
_, err = w.Write(b)
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment