Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SecretName in TLS + adapt github actions #95

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .github/workflows/go-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- master
- develop
jobs:
test:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -25,6 +25,20 @@ jobs:
- name: Launch Test
run: |
go vet ./... && go test -coverprofile=coverprofile.out -json -v ./... > gotest.json
- uses: actions/upload-artifact@v4
with:
name: tests-results
path: |
coverprofile.out
gotest.json
sonar:
runs-on: ubuntu-latest
needs: tests
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: tests-results
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
Expand Down
25 changes: 25 additions & 0 deletions generator/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ var unwantedLines = []string{
"status:",
}

var ingressTLSHelp = `# Ingress TLS configuration
# If enabled, a secret containing the certificate and the key should be
# created by the ingress controller. If the name if emtpy, so the secret
# name is generated. You can specify the secret name to use your own secret.
`

// keyRegExp checks if the line starts by a #
var keyRegExp = regexp.MustCompile(`^\s*[^#]+:.*`)

Expand Down Expand Up @@ -486,6 +492,24 @@ func addYAMLSelectorPath(values []byte) []byte {
return []byte(strings.Join(toReturn, "\n"))
}

// addTLSHelp adds a comment to the values.yaml file to explain how to
// use the tls option.
func addTLSHelp(values []byte) []byte {
lines := strings.Split(string(values), "\n")
for i, line := range lines {
if strings.Contains(line, "tls:") {
spaces := utils.CountStartingSpaces(line)
spacesString := strings.Repeat(" ", spaces)
// indent ingressClassHelper comment
ingressTLSHelp := strings.ReplaceAll(ingressTLSHelp, "\n", "\n"+spacesString)
ingressTLSHelp = strings.TrimRight(ingressTLSHelp, " ")
ingressTLSHelp = spacesString + ingressTLSHelp
lines[i] = ingressTLSHelp + line
}
}
return []byte(strings.Join(lines, "\n"))
}

func buildCharYamlFile(chart *HelmChart, project *types.Project, chartPath string) {
// calculate the sha1 hash of the services
yamlChart, err := utils.EncodeBasicYaml(chart)
Expand Down Expand Up @@ -537,6 +561,7 @@ func buildValues(chart *HelmChart, project *types.Project, valuesPath string) {
values = addVariablesDoc(values, project)
values = addMainTagAppDoc(values, project)
values = addResourceHelp(values)
values = addTLSHelp(values)
values = addYAMLSelectorPath(values)
values = append([]byte(headerHelp), values...)

Expand Down
32 changes: 18 additions & 14 deletions generator/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _ Yaml = (*Ingress)(nil)
type Ingress struct {
*networkv1.Ingress
service *types.ServiceConfig `yaml:"-"`
appName string `yaml:"-"`
}

// NewIngress creates a new Ingress from a compose service.
Expand All @@ -42,7 +43,11 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress {

// create the ingress
pathType := networkv1.PathTypeImplementationSpecific
serviceName := `{{ include "` + appName + `.fullname" . }}-` + service.Name

// fix the service name, and create the full name from variable name
// which is injected in the YAML() method
serviceName := strings.ReplaceAll(service.Name, "_", "-")
fullName := `{{ $fullname }}-` + serviceName

// Add the ingress host to the values.yaml
if Chart.Values[service.Name] == nil {
Expand All @@ -63,7 +68,7 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress {

servicePortName := utils.GetServiceNameByPort(int(*mapping.Port))
ingressService := &networkv1.IngressServiceBackend{
Name: serviceName,
Name: fullName,
Port: networkv1.ServiceBackendPort{},
}
if servicePortName != "" {
Expand All @@ -74,26 +79,27 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress {

ing := &Ingress{
service: &service,
appName: appName,
Ingress: &networkv1.Ingress{
TypeMeta: metav1.TypeMeta{
Kind: "Ingress",
APIVersion: "networking.k8s.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: utils.TplName(service.Name, appName),
Labels: GetLabels(service.Name, appName),
Name: fullName,
Labels: GetLabels(serviceName, appName),
Annotations: Annotations,
},
Spec: networkv1.IngressSpec{
IngressClassName: &ingressClassName,
Rules: []networkv1.IngressRule{
{
Host: utils.TplValue(service.Name, "ingress.host"),
Host: utils.TplValue(serviceName, "ingress.host"),
IngressRuleValue: networkv1.IngressRuleValue{
HTTP: &networkv1.HTTPIngressRuleValue{
Paths: []networkv1.HTTPIngressPath{
{
Path: utils.TplValue(service.Name, "ingress.path"),
Path: utils.TplValue(serviceName, "ingress.path"),
PathType: &pathType,
Backend: networkv1.IngressBackend{
Service: ingressService,
Expand All @@ -107,9 +113,9 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress {
TLS: []networkv1.IngressTLS{
{
Hosts: []string{
`{{ tpl .Values.` + service.Name + `.ingress.host . }}`,
`{{ tpl .Values.` + serviceName + `.ingress.host . }}`,
},
SecretName: `{{ include "` + appName + `.fullname" . }}-` + service.Name + `-tls`,
SecretName: `{{ .Values.` + serviceName + `.ingress.tls.secretName | default $tlsname }}`,
},
},
},
Expand All @@ -131,19 +137,15 @@ func (ingress *Ingress) Yaml() ([]byte, error) {
}

serviceName := ingress.service.Name
if err != nil {
return nil, err
}

ret = UnWrapTPL(ret)

lines := strings.Split(string(ret), "\n")

// first pass, wrap the tls part with `{{- if .Values.serviceName.ingress.tlsEnabled -}}`
// and `{{- end -}}`

from := -1
to := -1
spaces := -1
from, to, spaces := -1, -1, -1
for i, line := range lines {
if strings.Contains(line, "tls:") {
from = i
Expand All @@ -167,6 +169,8 @@ func (ingress *Ingress) Yaml() ([]byte, error) {

out := []string{
`{{- if .Values.` + serviceName + `.ingress.enabled -}}`,
`{{- $fullname := include "` + ingress.appName + `.fullname" . -}}`,
`{{- $tlsname := printf "%s-%s-tls" $fullname "` + ingress.service.Name + `" -}}`,
}
for _, line := range lines {
if strings.Contains(line, "loadBalancer: ") {
Expand Down
85 changes: 84 additions & 1 deletion generator/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ services:
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := internalCompileTest(t, "-s", "templates/web/ingress.yaml", "--set", "web.ingress.enabled=true")
output := internalCompileTest(
t,
"-s", "templates/web/ingress.yaml",
"--set", "web.ingress.enabled=true",
)
ingress := v1.Ingress{}
if err := yaml.Unmarshal([]byte(output), &ingress); err != nil {
t.Errorf(unmarshalError, err)
Expand All @@ -43,3 +47,82 @@ services:
t.Errorf("Expected host to be my.test.tld, got %s", ingress.Spec.Rules[0].Host)
}
}

func TestTLS(t *testing.T) {
composeFile := `
services:
web:
image: nginx:1.29
ports:
- 80:80
- 443:443
labels:
%s/ingress: |-
hostname: my.test.tld
port: 80
`
composeFile = fmt.Sprintf(composeFile, labels.KatenaryLabelPrefix)
tmpDir := setup(composeFile)
defer teardown(tmpDir)

currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := internalCompileTest(
t,
"-s", "templates/web/ingress.yaml",
"--set", "web.ingress.enabled=true",
)
ingress := v1.Ingress{}
if err := yaml.Unmarshal([]byte(output), &ingress); err != nil {
t.Errorf(unmarshalError, err)
}
// find the tls section
tls := ingress.Spec.TLS
if len(tls) != 1 {
t.Errorf("Expected 1 tls section, got %d", len(tls))
}
}

func TestTLSName(t *testing.T) {
composeFile := `
services:
web:
image: nginx:1.29
ports:
- 80:80
- 443:443
labels:
%s/ingress: |-
hostname: my.test.tld
port: 80
`
composeFile = fmt.Sprintf(composeFile, labels.KatenaryLabelPrefix)
tmpDir := setup(composeFile)
defer teardown(tmpDir)

currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)

output := internalCompileTest(
t,
"-s",
"templates/web/ingress.yaml",
"--set", "web.ingress.enabled=true",
"--set", "web.ingress.tls.secretName=mysecret",
)
ingress := v1.Ingress{}
if err := yaml.Unmarshal([]byte(output), &ingress); err != nil {
t.Errorf(unmarshalError, err)
}
// find the tls section
tls := ingress.Spec.TLS
if len(tls) != 1 {
t.Errorf("Expected 1 tls section, got %d", len(tls))
}
if tls[0].SecretName != "mysecret" {
t.Errorf("Expected secretName to be mysecret, got %s", tls[0].SecretName)
}
}
7 changes: 6 additions & 1 deletion generator/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type PersistenceValue struct {
}

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

// IngressValue is a ingress configuration that will be saved in values.yaml.
Expand Down Expand Up @@ -92,6 +93,10 @@ func (v *Value) AddIngress(host, path string) {
Host: host,
Path: path,
Class: "-",
TLS: TLS{
Enabled: true,
SecretName: "",
},
}
}

Expand Down
Loading