Initial version

This commit is contained in:
2021-11-30 12:04:28 +01:00
commit f095f39eaf
9 changed files with 567 additions and 0 deletions

80
helm/deployment.go Normal file
View File

@@ -0,0 +1,80 @@
package helm
import "strings"
type Deployment struct {
*K8sBase `yaml:",inline"`
Spec *DepSpec `yaml:"spec"`
}
func NewDeployment() *Deployment {
d := &Deployment{K8sBase: NewBase(), Spec: NewDepSpec()}
d.K8sBase.ApiVersion = "apps/v1"
d.K8sBase.Kind = "Deployment"
return d
}
type DepSpec struct {
Replicas int `yaml:"replicas"`
Selector map[string]interface{} `yaml:"selector"`
Template PodTemplate `yaml:"template"`
}
func NewDepSpec() *DepSpec {
return &DepSpec{
Replicas: 1,
}
}
type Value struct {
Name string `yaml:"name"`
Value interface{} `yaml:"value"`
}
type ContainerPort struct {
Name string
ContainerPort int `yaml:"containerPort"`
}
type Container struct {
Name string `yaml:"name,omitempty"`
Image string `yaml:"image"`
Ports []*ContainerPort `yaml:"ports,omitempty"`
Env []Value `yaml:"env,omitempty"`
Command []string `yaml:"command,omitempty"`
}
func NewContainer(name, image string, environment, labels map[string]string) *Container {
container := &Container{
Image: image,
Name: name,
Env: make([]Value, len(environment)),
}
toServices := make([]string, 0)
if bound, ok := labels[K+"/to-services"]; ok {
toServices = strings.Split(bound, ",")
}
idx := 0
for n, v := range environment {
for _, name := range toServices {
if name == n {
v = "{{ .Release.Name }}-" + v
}
}
container.Env[idx] = Value{Name: n, Value: v}
idx++
}
return container
}
type PodSpec struct {
InitContainers []*Container `yaml:"initContainers,omitempty"`
Containers []*Container `yaml:"containers"`
}
type PodTemplate struct {
Metadata Metadata `yaml:"metadata"`
Spec PodSpec `yaml:"spec"`
}

44
helm/ingress.go Normal file
View File

@@ -0,0 +1,44 @@
package helm
type Ingress struct {
*K8sBase
Spec IngressSpec
}
func NewIngress(name string) *Ingress {
i := &Ingress{}
i.K8sBase = NewBase()
i.K8sBase.Metadata.Name = "{{ .Release.Name }}-" + name
i.K8sBase.Kind = "Ingress"
i.ApiVersion = "networking.k8s.io/v1"
return i
}
type IngressSpec struct {
Rules []IngressRule
}
type IngressRule struct {
Host string
Http IngressHttp
}
type IngressHttp struct {
Paths []IngressPath
}
type IngressPath struct {
Path string
PathType string
Backend IngressBackend
}
type IngressBackend struct {
Service IngressService
}
type IngressService struct {
Name string
Port map[string]interface{}
}

42
helm/service.go Normal file
View File

@@ -0,0 +1,42 @@
package helm
type Service struct {
*K8sBase `yaml:",inline"`
Spec *ServiceSpec `yaml:"spec"`
}
func NewService() *Service {
s := &Service{
K8sBase: NewBase(),
Spec: NewServiceSpec(),
}
s.K8sBase.Kind = "Service"
s.K8sBase.ApiVersion = "v1"
return s
}
type ServicePort struct {
Protocol string `yaml:"protocol"`
Port int `yaml:"port"`
TargetPort int `yaml:"targetPort"`
}
func NewServicePort(port, target int) *ServicePort {
return &ServicePort{
Protocol: "TCP",
Port: port,
TargetPort: port,
}
}
type ServiceSpec struct {
Selector map[string]string
Ports []*ServicePort
}
func NewServiceSpec() *ServiceSpec {
return &ServiceSpec{
Selector: make(map[string]string),
Ports: make([]*ServicePort, 0),
}
}

55
helm/types.go Normal file
View File

@@ -0,0 +1,55 @@
package helm
import (
"os"
"strings"
)
const K = "katenary.io"
var Version = "1.0"
type Kinded interface {
Get() string
}
type Metadata struct {
Name string `yaml:"name,omitempty"`
Labels map[string]string `yaml:"labels"`
Annotations map[string]string `yaml:"annotations,omitempty"`
}
func NewMetadata() *Metadata {
return &Metadata{
Name: "",
Labels: make(map[string]string),
Annotations: make(map[string]string),
}
}
type K8sBase struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata *Metadata `yaml:"metadata"`
}
func NewBase() *K8sBase {
b := &K8sBase{
Metadata: NewMetadata(),
}
b.Metadata.Labels[K+"/project"] = getProjectName()
b.Metadata.Labels[K+"/release"] = "{{ .Release.Name }}"
b.Metadata.Annotations[K+"/version"] = Version
return b
}
func (k K8sBase) Get() string {
return k.Kind
}
func getProjectName() string {
p, _ := os.Getwd()
path := strings.Split(p, string(os.PathSeparator))
return path[len(path)-1]
}