Files
katenary/doc/docs/packages/generator.md
2024-04-19 11:17:54 +02:00

29 KiB

generator

import "katenary/generator"

The generator package 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. Convertion 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.

Constants

const KATENARY_PREFIX = "katenary.v3/"

Variables

var (

    // Standard annotationss
    Annotations = map[string]string{
        KATENARY_PREFIX + "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)

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 GetLabelHelp

func GetLabelHelp(asMarkdown bool) string

Generate the help for the labels.

func GetLabelHelpFor

func GetLabelHelpFor(labelname string, asMarkdown bool) string

GetLabelHelpFor returns the help for a specific label.

func GetLabelNames

func GetLabelNames() []string

GetLabelNames returns a sorted list of all katenary label names.

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 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.

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.

TODO: maybe we can set it private.

type ChartTemplate struct {
    Content     []byte
    Servicename string
}

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) *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 string, 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) AddData

func (c *ConfigMap) AddData(key string, 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)

AddFile 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)

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 {
    Force        bool     // Force the chart directory deletion if it already exists.
    OutputDir    string   // The output directory of the chart.
    Profiles     []string // Profile to use for the conversion.
    HelmUpdate   bool     // If true, the "helm dep update" command will be run after the chart generation.
    AppVersion   *string  // Set the chart "appVersion" field. If nil, the version will be set to 0.1.0.
    ChartVersion string   // Set the chart "version" field.
}

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)
}

func NewFileMap

func NewFileMap(service types.ServiceConfig, appName string, kind string) DataMap

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

type Dependency

Dependency is a dependency of a chart to other charts.

type Dependency struct {
    Name       string         `yaml:"name"`
    Version    string         `yaml:"version"`
    Repository string         `yaml:"repository"`
    Alias      string         `yaml:"alias,omitempty"`
    Values     map[string]any `yaml:"-"` // do not export to Chart.yaml
}

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) 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) 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) SetEnvFrom

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

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 tempaltes, values, versions, helpers...

type HelmChart struct {
    Name         string                    `yaml:"name"`
    ApiVersion   string                    `yaml:"apiVersion"`
    Version      string                    `yaml:"version"`
    AppVersion   string                    `yaml:"appVersion"`
    Description  string                    `yaml:"description"`
    Dependencies []Dependency              `yaml:"dependencies,omitempty"`
    Templates    map[string]*ChartTemplate `yaml:"-"` // do not export to yaml
    Helper       string                    `yaml:"-"` // do not export to yaml
    Values       map[string]any            `yaml:"-"` // do not export to yaml
    VolumeMounts map[string]any            `yaml:"-"` // do not export to yaml
    // 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.

type Help

Help is the documentation of a label.

type Help struct {
    Short   string `yaml:"short"`
    Long    string `yaml:"long"`
    Example string `yaml:"example"`
    Type    string `yaml:"type"`
}

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 {
    Enabled     bool              `yaml:"enabled"`
    Host        string            `yaml:"host"`
    Path        string            `yaml:"path"`
    Class       string            `yaml:"class"`
    Annotations map[string]string `yaml:"annotations"`
}

type Label

Label is a katenary label to find in compose files.

type Label = string

Known labels.

const (
    LABEL_MAIN_APP     Label = KATENARY_PREFIX + "main-app"
    LABEL_VALUES       Label = KATENARY_PREFIX + "values"
    LABEL_SECRETS      Label = KATENARY_PREFIX + "secrets"
    LABEL_PORTS        Label = KATENARY_PREFIX + "ports"
    LABEL_INGRESS      Label = KATENARY_PREFIX + "ingress"
    LABEL_MAP_ENV      Label = KATENARY_PREFIX + "map-env"
    LABEL_HEALTHCHECK  Label = KATENARY_PREFIX + "health-check"
    LABEL_SAME_POD     Label = KATENARY_PREFIX + "same-pod"
    LABEL_DESCRIPTION  Label = KATENARY_PREFIX + "description"
    LABEL_IGNORE       Label = KATENARY_PREFIX + "ignore"
    LABEL_DEPENDENCIES Label = KATENARY_PREFIX + "dependencies"
    LABEL_CM_FILES     Label = KATENARY_PREFIX + "configmap-files"
    LABEL_CRONJOB      Label = KATENARY_PREFIX + "cronjob"
    LABEL_ENV_FROM     Label = KATENARY_PREFIX + "env-from"
)

type LabelType

LabelType identifies the type of label to generate in objects. TODO: is this still needed?

type LabelType uint8

const (
    DeploymentLabel LabelType = iota
    ServiceLabel
)

type PersistenceValue

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

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

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 string, 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 Value

Value will be saved in values.yaml. It contains configuraiton for all deployment and services. The content will be lile:

name_of_component:
  repository:
    image: image_name
    tag: image_tag
  persistence:
    enabled: true
    storageClass: storage_class_name
  ingress:
    enabled: true
    host: host_name
    path: path_name
  environment:
    ENV_VAR_1: value_1
    ENV_VAR_2: value_2
type Value struct {
    Repository      *RepositoryValue             `yaml:"repository,omitempty"`
    Persistence     map[string]*PersistenceValue `yaml:"persistence,omitempty"`
    Ingress         *IngressValue                `yaml:"ingress,omitempty"`
    ImagePullPolicy string                       `yaml:"imagePullPolicy,omitempty"`
    Environment     map[string]any               `yaml:"environment,omitempty"`
    Replicas        *uint32                      `yaml:"replicas,omitempty"`
    CronJob         *CronJobValue                `yaml:"cronjob,omitempty"`
}

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