diff --git a/pkg/components/contour/component.go b/pkg/components/contour/component.go index a070d9322..e6de7c09f 100644 --- a/pkg/components/contour/component.go +++ b/pkg/components/contour/component.go @@ -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" @@ -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. @@ -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 } @@ -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 { diff --git a/pkg/components/contour/component_test.go b/pkg/components/contour/component_test.go index 62c9d4ac2..0063d246d 100644 --- a/pkg/components/contour/component_test.go +++ b/pkg/components/contour/component_test.go @@ -55,7 +55,7 @@ component "contour" { func TestRenderManifestWithServiceMonitor(t *testing.T) { configHCL := ` component "contour" { - service_monitor = true + enable_monitoring = true } ` testRenderManifest(t, configHCL) diff --git a/pkg/components/contour/manifest.go b/pkg/components/contour/manifest.go new file mode 100644 index 000000000..27bc1b8e4 --- /dev/null +++ b/pkg/components/contour/manifest.go @@ -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 }} +`