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

Commit

Permalink
contour: Use helm chart to install
Browse files Browse the repository at this point in the history
Signed-off-by: Suraj Deshmukh <suraj@kinvolk.io>
  • Loading branch information
surajssd committed May 11, 2020
1 parent 691af3b commit 956f5c6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
55 changes: 21 additions & 34 deletions pkg/components/contour/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ package contour

import (
"fmt"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/pkg/assets"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/util/walkers"
)

const name = "contour"
Expand All @@ -38,7 +34,7 @@ func init() {
// Values provided for IngressHosts is used as value for the annotation `external-dns.alpha.kubernetes.io/hostname`
// This annotation is added to Envoy service.
type component struct {
ServiceMonitor bool `hcl:"service_monitor,optional"`
EnableMonitoring bool `hcl:"enable_monitoring,optional"`
// IngressHosts field is added in order to make contour work with ExternalDNS component.
// Values provided for IngressHosts is used as value for the annotation `external-dns.alpha.kubernetes.io/hostname`.
// This annotation is added to Envoy Service, in order for ExternalDNS to create DNS entries.
Expand All @@ -47,11 +43,10 @@ type component struct {
// https://github.com/kinvolk/PROJECT-Lokomotive-Kubernetes/issues/474
IngressHosts []string `hcl:"ingress_hosts,optional"`

// IngressHostsRaw is not accessible to the user
IngressHostsRaw string
NodeAffinity []util.NodeAffinity `hcl:"node_affinity,block"`
NodeAffinityRaw string

NodeAffinity []util.NodeAffinity `hcl:"node_affinity,block"`
Tolerations []util.Toleration `hcl:"toleration,block"`
Tolerations []util.Toleration `hcl:"toleration,block"`
TolerationsRaw string
}

Expand All @@ -73,41 +68,33 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex
}

func (c *component) RenderManifests() (map[string]string, error) {
ret := make(map[string]string)

walk := walkers.DumpingWalker(ret, ".yaml")
if err := assets.Assets.WalkFiles(fmt.Sprintf("/components/%s/%s", name, name), walk); err != nil {
return nil, errors.Wrap(err, "failed to walk assets")
}

// Create service and service monitor for Prometheus to scrape metrics
if c.ServiceMonitor {
if err := assets.Assets.WalkFiles(fmt.Sprintf("/components/%s/manifests-metrics", name), walk); err != nil {
return nil, errors.Wrap(err, "failed to walk assets")
}
helmChart, err := util.LoadChartFromAssets("/components/contour")
if err != nil {
return nil, fmt.Errorf("load chart from assets: %w", err)
}

// To store the comma separated string representation of IngressHosts
c.IngressHostsRaw = strings.Join(c.IngressHosts, ",")

// Generate YAML for the Rook operator deployment.
var err error
c.TolerationsRaw, err = util.RenderTolerations(c.Tolerations)
if err != nil {
return nil, fmt.Errorf("failed to marshal operator tolerations: %w", err)
}

// Parse template with values
for k, v := range template {
rendered, err := util.RenderTemplate(v, c)
if err != nil {
return nil, fmt.Errorf("template rendering failed for %q: %w", k, err)
}
c.NodeAffinityRaw, err = util.RenderNodeAffinity(c.NodeAffinity)
if err != nil {
return nil, fmt.Errorf("failed to marshal node affinity: %w", err)
}

values, err := util.RenderTemplate(chartValuesTmpl, c)
if err != nil {
return nil, fmt.Errorf("rendering values template failed: %w", err)
}

ret[k] = rendered
// Generate YAML for the Contour deployment.
renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace, values)
if err != nil {
return nil, fmt.Errorf("rendering chart failed: %w", err)
}

return ret, nil
return renderedFiles, nil
}

func (c *component) Metadata() components.Metadata {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/contour/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ component "contour" {
func TestRenderManifestWithServiceMonitor(t *testing.T) {
configHCL := `
component "contour" {
service_monitor = true
enable_monitoring = true
}
`
testRenderManifest(t, configHCL)
Expand Down
38 changes: 38 additions & 0 deletions pkg/components/contour/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2020 The Lokomotive Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package contour has code related to deployment of contour component.
package contour

const chartValuesTmpl = `
{{- if .EnableMonitoring }}
monitoring:
enable: {{ .EnableMonitoring }}
{{- end }}
{{- if .IngressHosts }}
ingressHosts: {{ .IngressHosts }}
{{- end }}
{{- if .NodeAffinity }}
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions: {{ .NodeAffinityRaw }}
{{- end}}
{{- if .Tolerations }}
tolerations: {{ .TolerationsRaw }}
{{- end }}
`

0 comments on commit 956f5c6

Please sign in to comment.