Files
katenary/doc/docs/packages/generator.md

28 KiB

generator

import "katenary/generator"

Package generator generates kubernetes objects from a "compose" file and transforms them into a helm chart.

The generator package is the core of katenary. It is responsible for generating kubernetes objects from a compose file and transforming them into a helm chart. Conversion manipulates Yaml representation of kubernetes object to add conditions, labels, annotations, etc. to the objects. It also create the values to be set to the values.yaml file.

The generate.Convert() create an HelmChart object and call "Generate()" method to convert from a compose file to a helm chart. It saves the helm chart in the given directory.

If you want to change or override the write behavior, you can use the HelmChart.Generate() function and implement your own write function. This function returns the helm chart object containing all kubernetes objects and helm chart ingormation. It does not write the helm chart to the disk.

Variables

var (

    // Standard annotationss
    Annotations = map[string]string{
        labels.LabelName("version"): Version,
    }
)

Version is the version of katenary. It is set at compile time.

var Version = "master" // changed at compile time

func Convert

func Convert(config ConvertOptions, dockerComposeFile ...string) error

Convert a compose (docker, podman...) project to a helm chart. It calls Generate() to generate the chart and then write it to the disk.

func GetLabels

func GetLabels(serviceName, appName string) map[string]string

GetLabels returns the labels for a service. It uses the appName to replace the __replace__ in the labels. This is used to generate the labels in the templates.

func GetMatchLabels

func GetMatchLabels(serviceName, appName string) map[string]string

GetMatchLabels returns the matchLabels for a service. It uses the appName to replace the __replace__ in the labels. This is used to generate the matchLabels in the templates.

func GetVersion

func GetVersion() string

GetVersion return the version of katneary. It's important to understand that the version is set at compile time for the github release. But, it the user get katneary using `go install`, the version should be different.

func Helper

func Helper(name string) string

Helper returns the _helpers.tpl file for a chart.

func NewCronJob

func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) (*CronJob, *RBAC)

NewCronJob creates a new CronJob from a compose service. The appName is the name of the application taken from the project name.

func ToK8SYaml

func ToK8SYaml(obj any) ([]byte, error)

func UnWrapTPL

func UnWrapTPL(in []byte) []byte

UnWrapTPL removes the line wrapping from a template.

type ChartTemplate

ChartTemplate is a template of a chart. It contains the content of the template and the name of the service. This is used internally to generate the templates.

type ChartTemplate struct {
    Servicename string
    Content     []byte
}

type ConfigMap

ConfigMap is a kubernetes ConfigMap. Implements the DataMap interface.

type ConfigMap struct {
    *corev1.ConfigMap
    // contains filtered or unexported fields
}

func NewConfigMap

func NewConfigMap(service types.ServiceConfig, appName string, forFile bool) *ConfigMap

NewConfigMap creates a new ConfigMap from a compose service. The appName is the name of the application taken from the project name. The ConfigMap is filled by environment variables and labels "map-env".

func NewConfigMapFromDirectory

func NewConfigMapFromDirectory(service types.ServiceConfig, appName, path string) *ConfigMap

NewConfigMapFromDirectory creates a new ConfigMap from a compose service. This path is the path to the file or directory. If the path is a directory, all files in the directory are added to the ConfigMap. Each subdirectory are ignored. Note that the Generate() function will create the subdirectories ConfigMaps.

func (*ConfigMap) AddBinaryData

func (c *ConfigMap) AddBinaryData(key string, value []byte)

AddBinaryData adds binary data to the configmap. Append or overwrite the value if the key already exists.

func (*ConfigMap) AddData

func (c *ConfigMap) AddData(key, value string)

AddData adds a key value pair to the configmap. Append or overwrite the value if the key already exists.

func (*ConfigMap) AppendDir

func (c *ConfigMap) AppendDir(path string) error

AppendDir adds files from given path to the configmap. It is not recursive, to add all files in a directory, you need to call this function for each subdirectory.

func (*ConfigMap) AppendFile

func (c *ConfigMap) AppendFile(path string) error

func (*ConfigMap) Filename

func (c *ConfigMap) Filename() string

Filename returns the filename of the configmap. If the configmap is used for files, the filename contains the path.

func (*ConfigMap) SetData

func (c *ConfigMap) SetData(data map[string]string)

SetData sets the data of the configmap. It replaces the entire data.

func (*ConfigMap) Yaml

func (c *ConfigMap) Yaml() ([]byte, error)

Yaml returns the yaml representation of the configmap

type ConfigMapMount

type ConfigMapMount struct {
    // contains filtered or unexported fields
}

type ConvertOptions

ConvertOptions are the options to convert a compose project to a helm chart.

type ConvertOptions struct {
    AppVersion   *string
    OutputDir    string
    ChartVersion string
    Icon         string
    Profiles     []string
    EnvFiles     []string
    Force        bool
    HelmUpdate   bool
}

type CronJob

CronJob is a kubernetes CronJob.

type CronJob struct {
    *batchv1.CronJob
    // contains filtered or unexported fields
}

func (*CronJob) Filename

func (c *CronJob) Filename() string

Filename returns the filename of the cronjob.

Implements the Yaml interface.

func (*CronJob) Yaml

func (c *CronJob) Yaml() ([]byte, error)

Yaml returns the yaml representation of the cronjob.

Implements the Yaml interface.

type CronJobValue

CronJobValue is a cronjob configuration that will be saved in values.yaml.

type CronJobValue struct {
    Repository      *RepositoryValue `yaml:"repository,omitempty"`
    Environment     map[string]any   `yaml:"environment,omitempty"`
    ImagePullPolicy string           `yaml:"imagePullPolicy,omitempty"`
    Schedule        string           `yaml:"schedule"`
}

type DataMap

DataMap is a kubernetes ConfigMap or Secret. It can be used to add data to the ConfigMap or Secret.

type DataMap interface {
    SetData(map[string]string)
    AddData(string, string)
}

type Deployment

Deployment is a kubernetes Deployment.

type Deployment struct {
    *appsv1.Deployment `yaml:",inline"`
    // contains filtered or unexported fields
}

func NewDeployment

func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment

NewDeployment creates a new Deployment from a compose service. The appName is the name of the application taken from the project name. It also creates the Values map that will be used to create the values.yaml file.

func (*Deployment) AddContainer

func (d *Deployment) AddContainer(service types.ServiceConfig)

AddContainer adds a container to the deployment.

func (*Deployment) AddHealthCheck

func (d *Deployment) AddHealthCheck(service types.ServiceConfig, container *corev1.Container)

func (*Deployment) AddIngress

func (d *Deployment) AddIngress(service types.ServiceConfig, appName string) *Ingress

AddIngress adds an ingress to the deployment. It creates the ingress object.

func (*Deployment) AddLegacyVolume

func (d *Deployment) AddLegacyVolume(name, kind string)

func (*Deployment) AddVolumes

func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string)

AddVolumes adds a volume to the deployment. It does not create the PVC, it only adds the volumes to the deployment. If the volume is a bind volume it will warn the user that it is not supported yet.

func (*Deployment) BindFrom

func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment)

func (*Deployment) BindMapFilesToContainer

func (d *Deployment) BindMapFilesToContainer(service types.ServiceConfig, secrets []string, appName string) (*corev1.Container, int)

func (*Deployment) DependsOn

func (d *Deployment) DependsOn(to *Deployment, servicename string) error

DependsOn adds a initContainer to the deployment that will wait for the service to be up.

func (*Deployment) Filename

func (d *Deployment) Filename() string

Filename returns the filename of the deployment.

func (*Deployment) MountExchangeVolumes

func (d *Deployment) MountExchangeVolumes()

func (*Deployment) SetEnvFrom

func (d *Deployment) SetEnvFrom(service types.ServiceConfig, appName string, samePod ...bool)

SetEnvFrom sets the environment variables to a configmap. The configmap is created.

func (*Deployment) Yaml

func (d *Deployment) Yaml() ([]byte, error)

Yaml returns the yaml representation of the deployment.

type FileMapUsage

FileMapUsage is the usage of the filemap.

type FileMapUsage uint8

FileMapUsage constants.

const (
    FileMapUsageConfigMap FileMapUsage = iota // pure configmap for key:values.
    FileMapUsageFiles                         // files in a configmap.
)

type HelmChart

HelmChart is a Helm Chart representation. It contains all the templates, values, versions, helpers...

type HelmChart struct {
    Templates    map[string]*ChartTemplate `yaml:"-"`
    Values       map[string]any            `yaml:"-"`
    VolumeMounts map[string]any            `yaml:"-"`

    Name         string                    `yaml:"name"`
    Icon         string                    `yaml:"icon,omitempty"`
    APIVersion   string                    `yaml:"apiVersion"`
    Version      string                    `yaml:"version"`
    AppVersion   string                    `yaml:"appVersion"`
    Description  string                    `yaml:"description"`
    Helper       string                    `yaml:"-"`
    Dependencies []labelStructs.Dependency `yaml:"dependencies,omitempty"`
    // contains filtered or unexported fields
}

func Generate

func Generate(project *types.Project) (*HelmChart, error)

Generate a chart from a compose project. This does not write files to disk, it only creates the HelmChart object.

The Generate function will create the HelmChart object this way:

  • Detect the service port name or leave the port number if not found.
  • Create a deployment for each service that are not ingnore.
  • Create a service and ingresses for each service that has ports and/or declared ingresses.
  • Create a PVC or Configmap volumes for each volume.
  • Create init containers for each service which has dependencies to other services.
  • Create a chart dependencies.
  • Create a configmap and secrets from the environment variables.
  • Merge the same-pod services.

func NewChart

func NewChart(name string) *HelmChart

NewChart creates a new empty chart with the given name.

func (*HelmChart) SaveTemplates

func (chart *HelmChart) SaveTemplates(templateDir string)

SaveTemplates the templates of the chart to the given directory.

type Ingress

type Ingress struct {
    *networkv1.Ingress
    // contains filtered or unexported fields
}

func NewIngress

func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress

NewIngress creates a new Ingress from a compose service.

func (*Ingress) Filename

func (ingress *Ingress) Filename() string

func (*Ingress) Yaml

func (ingress *Ingress) Yaml() ([]byte, error)

type IngressValue

IngressValue is a ingress configuration that will be saved in values.yaml.

type IngressValue struct {
    Annotations map[string]string `yaml:"annotations"`
    Host        string            `yaml:"host"`
    Path        string            `yaml:"path"`
    Class       string            `yaml:"class"`
    Enabled     bool              `yaml:"enabled"`
    TLS         TLS               `yaml:"tls"`
}

type PersistenceValue

PersistenceValue is a persistence configuration that will be saved in values.yaml.

type PersistenceValue struct {
    StorageClass string   `yaml:"storageClass"`
    Size         string   `yaml:"size"`
    AccessMode   []string `yaml:"accessMode"`
    Enabled      bool     `yaml:"enabled"`
}

type RBAC

RBAC is a kubernetes RBAC containing a role, a rolebinding and an associated serviceaccount.

type RBAC struct {
    RoleBinding    *RoleBinding
    Role           *Role
    ServiceAccount *ServiceAccount
}

func NewRBAC

func NewRBAC(service types.ServiceConfig, appName string) *RBAC

NewRBAC creates a new RBAC from a compose service. The appName is the name of the application taken from the project name.

type RepositoryValue

RepositoryValue is a docker repository image and tag that will be saved in values.yaml.

type RepositoryValue struct {
    Image string `yaml:"image"`
    Tag   string `yaml:"tag"`
}

type Role

Role is a kubernetes Role.

type Role struct {
    *rbacv1.Role
    // contains filtered or unexported fields
}

func (*Role) Filename

func (r *Role) Filename() string

func (*Role) Yaml

func (r *Role) Yaml() ([]byte, error)

type RoleBinding

RoleBinding is a kubernetes RoleBinding.

type RoleBinding struct {
    *rbacv1.RoleBinding
    // contains filtered or unexported fields
}

func (*RoleBinding) Filename

func (r *RoleBinding) Filename() string

func (*RoleBinding) Yaml

func (r *RoleBinding) Yaml() ([]byte, error)

type Secret

Secret is a kubernetes Secret.

Implements the DataMap interface.

type Secret struct {
    *corev1.Secret
    // contains filtered or unexported fields
}

func NewSecret

func NewSecret(service types.ServiceConfig, appName string) *Secret

NewSecret creates a new Secret from a compose service

func (*Secret) AddData

func (s *Secret) AddData(key, value string)

AddData adds a key value pair to the secret.

func (*Secret) Filename

func (s *Secret) Filename() string

Filename returns the filename of the secret.

func (*Secret) SetData

func (s *Secret) SetData(data map[string]string)

SetData sets the data of the secret.

func (*Secret) Yaml

func (s *Secret) Yaml() ([]byte, error)

Yaml returns the yaml representation of the secret.

type Service

Service is a kubernetes Service.

type Service struct {
    *v1.Service `yaml:",inline"`
    // contains filtered or unexported fields
}

func NewService

func NewService(service types.ServiceConfig, appName string) *Service

NewService creates a new Service from a compose service.

func (*Service) AddPort

func (s *Service) AddPort(port types.ServicePortConfig, serviceName ...string)

AddPort adds a port to the service.

func (*Service) Filename

func (s *Service) Filename() string

Filename returns the filename of the service.

func (*Service) Yaml

func (s *Service) Yaml() ([]byte, error)

Yaml returns the yaml representation of the service.

type ServiceAccount

ServiceAccount is a kubernetes ServiceAccount.

type ServiceAccount struct {
    *corev1.ServiceAccount
    // contains filtered or unexported fields
}

func (*ServiceAccount) Filename

func (r *ServiceAccount) Filename() string

func (*ServiceAccount) Yaml

func (r *ServiceAccount) Yaml() ([]byte, error)

type TLS

type TLS struct {
    Enabled    bool   `yaml:"enabled"`
    SecretName string `yaml:"secretName"`
}

type Value

Value will be saved in values.yaml. It contains configuration for all deployment and services.

type Value struct {
    Repository      *RepositoryValue             `yaml:"repository,omitempty"`
    Persistence     map[string]*PersistenceValue `yaml:"persistence,omitempty"`
    Ingress         *IngressValue                `yaml:"ingress,omitempty"`
    Environment     map[string]any               `yaml:"environment,omitempty"`
    Replicas        *uint32                      `yaml:"replicas,omitempty"`
    CronJob         *CronJobValue                `yaml:"cronjob,omitempty"`
    NodeSelector    map[string]string            `yaml:"nodeSelector"`
    Resources       map[string]any               `yaml:"resources"`
    ImagePullPolicy string                       `yaml:"imagePullPolicy,omitempty"`
    ServiceAccount  string                       `yaml:"serviceAccount"`
}

func NewValue

func NewValue(service types.ServiceConfig, main ...bool) *Value

NewValue creates a new Value from a compose service. The value contains the necessary information to deploy the service (image, tag, replicas, etc.).

If `main` is true, the tag will be empty because it will be set in the helm chart appVersion.

func (*Value) AddIngress

func (v *Value) AddIngress(host, path string)

func (*Value) AddPersistence

func (v *Value) AddPersistence(volumeName string)

AddPersistence adds persistence configuration to the Value.

type VolumeClaim

VolumeClaim is a kubernetes VolumeClaim. This is a PersistentVolumeClaim.

type VolumeClaim struct {
    *v1.PersistentVolumeClaim
    // contains filtered or unexported fields
}

func NewVolumeClaim

func NewVolumeClaim(service types.ServiceConfig, volumeName, appName string) *VolumeClaim

NewVolumeClaim creates a new VolumeClaim from a compose service.

func (*VolumeClaim) Filename

func (v *VolumeClaim) Filename() string

Filename returns the suggested filename for a VolumeClaim.

func (*VolumeClaim) Yaml

func (v *VolumeClaim) Yaml() ([]byte, error)

Yaml marshals a VolumeClaim into yaml.

type Yaml

Yaml is a kubernetes object that can be converted to yaml.

type Yaml interface {
    Yaml() ([]byte, error)
    Filename() string
}

Generated by gomarkdoc