Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
contour: Enable installation using helm-controller
Browse files Browse the repository at this point in the history
Signed-off-by: Suraj Deshmukh <[email protected]>
  • Loading branch information
surajssd committed Aug 5, 2021
1 parent 6c69be2 commit 5906514
Showing 1 changed file with 69 additions and 10 deletions.
79 changes: 69 additions & 10 deletions pkg/components/contour/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@ package contour

import (
"fmt"
"time"

api "github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8syaml "sigs.k8s.io/yaml"

internaltemplate "github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
"github.com/kinvolk/lokomotive/pkg/version"
)

const (
// Name represents Contour component name as it should be referenced in function calls
// and in configuration.
Name = "contour"

namespace = "projectcontour"

serviceTypeNodePort = "NodePort"
serviceTypeLoadBalancer = "LoadBalancer"
)
Expand Down Expand Up @@ -86,23 +93,29 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex
return diagnostics
}

func (c *component) RenderManifests() (map[string]string, error) {
helmChart, err := components.Chart(Name)
if err != nil {
return nil, fmt.Errorf("retrieving chart from assets: %w", err)
}
func (c *component) generateValues() (string, error) {
var err error

c.TolerationsRaw, err = util.RenderTolerations(c.Tolerations)
if err != nil {
return nil, fmt.Errorf("failed to marshal operator tolerations: %w", err)
return "", fmt.Errorf("failed to marshal operator tolerations: %w", err)
}

c.NodeAffinityRaw, err = util.RenderNodeAffinity(c.NodeAffinity)
if err != nil {
return nil, fmt.Errorf("failed to marshal node affinity: %w", err)
return "", fmt.Errorf("failed to marshal node affinity: %w", err)
}

return internaltemplate.Render(chartValuesTmpl, c)
}

func (c *component) RenderManifests() (map[string]string, error) {
helmChart, err := components.Chart(Name)
if err != nil {
return nil, fmt.Errorf("retrieving chart from assets: %w", err)
}

values, err := internaltemplate.Render(chartValuesTmpl, c)
values, err := c.generateValues()
if err != nil {
return nil, fmt.Errorf("rendering values template failed: %w", err)
}
Expand All @@ -120,11 +133,57 @@ func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: Name,
Namespace: k8sutil.Namespace{
Name: "projectcontour",
Name: namespace,
},
}
}

func (c *component) GenerateHelmRelease() (*api.HelmRelease, error) {
return nil, components.NotImplementedErr
valuesYaml, err := c.generateValues()
if err != nil {
return nil, fmt.Errorf("rendering values template failed: %w", err)
}

values, err := k8syaml.YAMLToJSON([]byte(valuesYaml))
if err != nil {
return nil, fmt.Errorf("converting YAML to JSON: %w", err)
}

timeout := time.Minute * 10

return &api.HelmRelease{
ObjectMeta: metav1.ObjectMeta{
Name: Name,
Namespace: "flux-system",
},
Spec: api.HelmReleaseSpec{
Chart: api.HelmChartTemplate{
Spec: api.HelmChartTemplateSpec{
Chart: "./assets/charts/components/contour/",
SourceRef: api.CrossNamespaceObjectReference{
Kind: "GitRepository",
Name: "lokomotive-" + version.Version,
},
},
},
ReleaseName: Name,
Install: &api.Install{
CRDs: api.CreateReplace,
CreateNamespace: true,
Remediation: &api.InstallRemediation{
Retries: -1,
},
},
Upgrade: &api.Upgrade{
Force: true,
CRDs: api.CreateReplace,
},
Interval: metav1.Duration{time.Minute},
Timeout: &metav1.Duration{timeout},
TargetNamespace: namespace,
Values: &apiextensionsv1.JSON{
Raw: values,
},
},
}, nil
}

0 comments on commit 5906514

Please sign in to comment.