More documentation
This commit is contained in:
@@ -7,12 +7,14 @@ import (
|
|||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Parser is a docker-compose parser.
|
||||||
type Parser struct {
|
type Parser struct {
|
||||||
Data *Compose
|
Data *Compose
|
||||||
}
|
}
|
||||||
|
|
||||||
var Appname = ""
|
var Appname = ""
|
||||||
|
|
||||||
|
// NewParser create a Parser and parse the file given in filename.
|
||||||
func NewParser(filename string) *Parser {
|
func NewParser(filename string) *Parser {
|
||||||
|
|
||||||
f, err := os.Open(filename)
|
f, err := os.Open(filename)
|
||||||
|
@@ -1,5 +1,21 @@
|
|||||||
package compose
|
package compose
|
||||||
|
|
||||||
|
// Compose is a complete docker-compse representation.
|
||||||
|
type Compose struct {
|
||||||
|
Version string `yaml:"version"`
|
||||||
|
Services map[string]Service `yaml:"services"`
|
||||||
|
Volumes map[string]interface{} `yaml:"volumes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCompose resturs a Compose object.
|
||||||
|
func NewCompose() *Compose {
|
||||||
|
c := &Compose{}
|
||||||
|
c.Services = make(map[string]Service)
|
||||||
|
c.Volumes = make(map[string]interface{})
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// Service represent a "service" in a docker-compose file.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
Image string `yaml:"image"`
|
Image string `yaml:"image"`
|
||||||
Ports []string `yaml:"ports"`
|
Ports []string `yaml:"ports"`
|
||||||
@@ -9,16 +25,3 @@ type Service struct {
|
|||||||
Volumes []string `yaml:"volumes"`
|
Volumes []string `yaml:"volumes"`
|
||||||
Expose []int `yaml:"expose"`
|
Expose []int `yaml:"expose"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Compose struct {
|
|
||||||
Version string `yaml:"version"`
|
|
||||||
Services map[string]Service `yaml:"services"`
|
|
||||||
Volumes map[string]interface{} `yaml:"volumes"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCompose() *Compose {
|
|
||||||
c := &Compose{}
|
|
||||||
c.Services = make(map[string]Service)
|
|
||||||
c.Volumes = make(map[string]interface{})
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
@@ -17,10 +17,13 @@ var serviceWaiters = make(map[string][]chan int)
|
|||||||
var locker = &sync.Mutex{}
|
var locker = &sync.Mutex{}
|
||||||
var serviceTick = make(chan int, 0)
|
var serviceTick = make(chan int, 0)
|
||||||
|
|
||||||
|
// Ingresses is kept in memory to create ingresses.
|
||||||
var Ingresses = make(map[string]*helm.Ingress, 0)
|
var Ingresses = make(map[string]*helm.Ingress, 0)
|
||||||
|
|
||||||
|
// Values is kept in memory to create a values.yaml file.
|
||||||
var Values = make(map[string]map[string]interface{})
|
var Values = make(map[string]map[string]interface{})
|
||||||
|
|
||||||
var DependScript = `
|
var dependScript = `
|
||||||
OK=0
|
OK=0
|
||||||
echo "Checking __service__ port"
|
echo "Checking __service__ port"
|
||||||
while [ $OK != 1 ]; do
|
while [ $OK != 1 ]; do
|
||||||
@@ -32,6 +35,7 @@ echo
|
|||||||
echo "Done"
|
echo "Done"
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// Create a Deployment for a given compose.Service. It returns a list of objects: a Deployment and a possible Service (kubernetes represnetation as maps).
|
||||||
func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
|
func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
|
||||||
|
|
||||||
Magenta("Generating deployment for ", name)
|
Magenta("Generating deployment for ", name)
|
||||||
@@ -86,7 +90,7 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
c := helm.NewContainer("check-"+dp, "busybox", nil, s.Labels)
|
c := helm.NewContainer("check-"+dp, "busybox", nil, s.Labels)
|
||||||
command := strings.ReplaceAll(strings.TrimSpace(DependScript), "__service__", dp)
|
command := strings.ReplaceAll(strings.TrimSpace(dependScript), "__service__", dp)
|
||||||
|
|
||||||
wait.Add(1)
|
wait.Add(1)
|
||||||
go func(dp string) {
|
go func(dp string) {
|
||||||
@@ -120,6 +124,7 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a service (k8s).
|
||||||
func createService(name string, s compose.Service) *helm.Service {
|
func createService(name string, s compose.Service) *helm.Service {
|
||||||
|
|
||||||
Magenta("Generating service for ", name)
|
Magenta("Generating service for ", name)
|
||||||
@@ -163,6 +168,7 @@ func createService(name string, s compose.Service) *helm.Service {
|
|||||||
return ks
|
return ks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create an ingress.
|
||||||
func createIngress(name string, port int, s compose.Service) {
|
func createIngress(name string, port int, s compose.Service) {
|
||||||
ingress := helm.NewIngress(name)
|
ingress := helm.NewIngress(name)
|
||||||
Values[name]["ingress"] = map[string]interface{}{
|
Values[name]["ingress"] = map[string]interface{}{
|
||||||
@@ -196,6 +202,7 @@ func createIngress(name string, port int, s compose.Service) {
|
|||||||
locker.Unlock()
|
locker.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function is called when a possible service is detected, it append the port in a map to make others to be able to get the service name. It also try to send the data to any "waiter" for this service.
|
||||||
func detected(name string, port int) {
|
func detected(name string, port int) {
|
||||||
locker.Lock()
|
locker.Lock()
|
||||||
servicesMap[name] = port
|
servicesMap[name] = port
|
||||||
@@ -217,6 +224,7 @@ func getPort(name string) (int, error) {
|
|||||||
return -1, errors.New("Not found")
|
return -1, errors.New("Not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Waits for a service to be discovered. Sometimes, a deployment depends on another one. See the detected() function.
|
||||||
func waitPort(name string) chan int {
|
func waitPort(name string) chan int {
|
||||||
locker.Lock()
|
locker.Lock()
|
||||||
c := make(chan int, 0)
|
c := make(chan int, 0)
|
||||||
@@ -231,7 +239,6 @@ func waitPort(name string) chan int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func buildSelector(name string, s compose.Service) map[string]string {
|
func buildSelector(name string, s compose.Service) map[string]string {
|
||||||
|
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"katenary.io/component": name,
|
"katenary.io/component": name,
|
||||||
"katenary.io/release": "{{ .Release.Name }}",
|
"katenary.io/release": "{{ .Release.Name }}",
|
||||||
|
@@ -2,6 +2,7 @@ package helm
|
|||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
|
// Deployment is a k8s deployment.
|
||||||
type Deployment struct {
|
type Deployment struct {
|
||||||
*K8sBase `yaml:",inline"`
|
*K8sBase `yaml:",inline"`
|
||||||
Spec *DepSpec `yaml:"spec"`
|
Spec *DepSpec `yaml:"spec"`
|
||||||
|
2
main.go
2
main.go
@@ -32,6 +32,8 @@ func main() {
|
|||||||
|
|
||||||
for name, s := range p.Data.Services {
|
for name, s := range p.Data.Services {
|
||||||
wait.Add(1)
|
wait.Add(1)
|
||||||
|
// it's mandatory to make the build in goroutines because some dependencies can
|
||||||
|
// wait for a port number. So the entire services are built in parallel.
|
||||||
go func(name string, s compose.Service) {
|
go func(name string, s compose.Service) {
|
||||||
o := generator.CreateReplicaObject(name, s)
|
o := generator.CreateReplicaObject(name, s)
|
||||||
files[name] = o
|
files[name] = o
|
||||||
|
Reference in New Issue
Block a user