Skip to content

Commit

Permalink
Do not duplicate the configMap
Browse files Browse the repository at this point in the history
  • Loading branch information
shafeeqes committed Oct 9, 2023
1 parent 7874e3c commit 3a4505a
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 680 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
template:
metadata:
annotations:
checksum/configmap-cilium: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
checksum/configmap-cilium: "{{ .Values.global.configMapHash }}"
{{- if and .Values.global.prometheus.enabled (not .Values.global.prometheus.serviceMonitor.enabled) }}
prometheus.io/port: "{{ .Values.global.prometheus.port }}"
prometheus.io/scrape: "true"
Expand Down
6 changes: 6 additions & 0 deletions charts/internal/cilium/charts/config/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
name: config
version: 0.1.0
description: Helm chart for Cilium configuration
sources:
- https://github.com/gardener/gardener-extension-networking-cilium
659 changes: 0 additions & 659 deletions charts/internal/cilium/charts/operator/templates/configmap.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spec:
template:
metadata:
annotations:
checksum/configmap-cilium: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
checksum/configmap-cilium: "{{ .Values.global.configMapHash }}"
{{- if and .Values.global.prometheus.enabled (not .Values.global.prometheus.serviceMonitor.enabled) }}
prometheus.io/port: {{ .Values.global.operatorPrometheus.port | quote }}
prometheus.io/scrape: "true"
Expand Down
2 changes: 2 additions & 0 deletions charts/internal/cilium/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ requirements:

# global groups all configuration options that have effect on all sub-charts
global:
configMapHash: ""

egressGateway:
enabled: false

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/spf13/pflag v1.0.5
go.uber.org/mock v0.2.0
golang.org/x/tools v0.12.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.28.2
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
Expand Down Expand Up @@ -107,7 +108,6 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
istio.io/api v0.0.0-20230217221049-9d422bf48675 // indirect
istio.io/client-go v1.17.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions pkg/charts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type globalConfig struct {
SnatToUpstreamDNS snatToUpstreamDNS `json:"snatToUpstreamDNS"`
SnatOutOfCluster snatOutOfCluster `json:"snatOutOfCluster"`
AutoDirectNodeRoutes bool `json:"autoDirectNodeRoutes"`
ConfigMapHash string `json:"configMapHash"`
}

// etcd related configuration for cilium
Expand Down
9 changes: 6 additions & 3 deletions pkg/charts/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var defaultGlobalConfig = globalConfig{
Enabled: false,
},
AutoDirectNodeRoutes: false,
ConfigMapHash: "",
}

func newGlobalConfig() globalConfig {
Expand All @@ -131,8 +132,8 @@ func newRequirementsConfig() requirementsConfig {
}

// ComputeCiliumChartValues computes the values for the cilium chart.
func ComputeCiliumChartValues(config *ciliumv1alpha1.NetworkConfig, network *extensionsv1alpha1.Network, cluster *extensionscontroller.Cluster, ipamMode string) (*ciliumConfig, error) {
requirementsConfig, globalConfig, err := generateChartValues(config, network, cluster, ipamMode)
func ComputeCiliumChartValues(config *ciliumv1alpha1.NetworkConfig, network *extensionsv1alpha1.Network, cluster *extensionscontroller.Cluster, ipamMode, configMapHash string) (*ciliumConfig, error) {
requirementsConfig, globalConfig, err := generateChartValues(config, network, cluster, ipamMode, configMapHash)
if err != nil {
return nil, fmt.Errorf("error when generating config values %w", err)
}
Expand All @@ -143,12 +144,14 @@ func ComputeCiliumChartValues(config *ciliumv1alpha1.NetworkConfig, network *ext
}, nil
}

func generateChartValues(config *ciliumv1alpha1.NetworkConfig, network *extensionsv1alpha1.Network, cluster *extensionscontroller.Cluster, ipamMode string) (requirementsConfig, globalConfig, error) {
func generateChartValues(config *ciliumv1alpha1.NetworkConfig, network *extensionsv1alpha1.Network, cluster *extensionscontroller.Cluster, ipamMode, configMapHash string) (requirementsConfig, globalConfig, error) {
var (
requirementsConfig = newRequirementsConfig()
globalConfig = newGlobalConfig()
)

globalConfig.ConfigMapHash = configMapHash

if network.Spec.PodCIDR != "" {
globalConfig.PodCIDR = network.Spec.PodCIDR
}
Expand Down
46 changes: 43 additions & 3 deletions pkg/charts/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
package charts

import (
"fmt"

extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/chartrenderer"
"github.com/gardener/gardener/pkg/utils"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gardener/gardener-extension-networking-cilium/charts"
Expand All @@ -29,16 +34,51 @@ import (
const CiliumConfigKey = "config.yaml"

// RenderCiliumChart renders the cilium chart with the given values.
func RenderCiliumChart(renderer chartrenderer.Interface, config *ciliumv1alpha1.NetworkConfig, network *extensionsv1alpha1.Network, cluster *extensionscontroller.Cluster, ipamMode string) ([]byte, error) {
values, err := ComputeCiliumChartValues(config, network, cluster, ipamMode)
func RenderCiliumChart(renderer chartrenderer.Interface, config *ciliumv1alpha1.NetworkConfig, network *extensionsv1alpha1.Network, cluster *extensionscontroller.Cluster, ipamMode, configMapHash string) ([]byte, error) {
var release *chartrenderer.RenderedChart

values, err := ComputeCiliumChartValues(config, network, cluster, ipamMode, configMapHash)
if err != nil {
return nil, err
}

release, err = renderer.RenderEmbeddedFS(charts.InternalChart, cilium.CiliumChartPath, cilium.ReleaseName, metav1.NamespaceSystem, values)
if err != nil {
return nil, err
}

release, err := renderer.RenderEmbeddedFS(charts.InternalChart, cilium.CiliumChartPath, cilium.ReleaseName, metav1.NamespaceSystem, values)
newConfigMapHash, err := getConfigMapHash(release)
if err != nil {
return nil, err
}

if newConfigMapHash != configMapHash {
// Render the charts with the new configMap hash.
values, err := ComputeCiliumChartValues(config, network, cluster, ipamMode, newConfigMapHash)
if err != nil {
return nil, err
}

release, err = renderer.RenderEmbeddedFS(charts.InternalChart, cilium.CiliumChartPath, cilium.ReleaseName, metav1.NamespaceSystem, values)
if err != nil {
return nil, err
}
}

return release.Manifest(), nil
}

func getConfigMapHash(release *chartrenderer.RenderedChart) (string, error) {
configMap := &corev1.ConfigMap{}
configMapPath := "cilium/charts/config/templates/configmap.yaml"
configMapFile, ok := release.Files()[configMapPath]
if !ok {
return "", fmt.Errorf("configmap not found in the given path: %s", configMapPath)
}

if err := yaml.Unmarshal([]byte(configMapFile), &configMap); err != nil {
return "", fmt.Errorf("error unmarshalling configMap: %w, %s", err, configMapFile)
}

return utils.ComputeConfigMapChecksum(configMap.Data), nil
}
28 changes: 16 additions & 12 deletions pkg/controller/actuator_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
v1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
gardenerkubernetes "github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/utils"
"github.com/gardener/gardener/pkg/utils/chart"
"github.com/gardener/gardener/pkg/utils/managedresources"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -135,12 +136,12 @@ func (a *actuator) Reconcile(ctx context.Context, _ logr.Logger, network *extens
return fmt.Errorf("could not create chart renderer for shoot '%s': %w", network.Namespace, err)
}

ipamMode, err := getIPAMMode(ctx, a.client, cluster)
configMap, err := getCiliumConfigMap(ctx, a.client, cluster)
if err != nil {
return err
return fmt.Errorf("error getting cilium configMap: %w", err)
}

ciliumChart, err := chartspkg.RenderCiliumChart(chartRenderer, networkConfig, network, cluster, ipamMode)
ciliumChart, err := chartspkg.RenderCiliumChart(chartRenderer, networkConfig, network, cluster, getIPAMMode(configMap), getConfigMapHash(configMap))
if err != nil {
return err
}
Expand All @@ -167,15 +168,18 @@ func getCiliumConfigMap(ctx context.Context, cl client.Client, cluster *extensio
return configmap, nil
}

func getIPAMMode(ctx context.Context, cl client.Client, cluster *extensionscontroller.Cluster) (string, error) {
configmap, err := getCiliumConfigMap(ctx, cl, cluster)
if err != nil {
return "", err
}
if configmap != nil {
if ipamMode, ok := configmap.Data["ipam"]; ok {
return ipamMode, nil
func getIPAMMode(configMap *corev1.ConfigMap) string {
if configMap != nil {
if ipamMode, ok := configMap.Data["ipam"]; ok {
return ipamMode
}
}
return "kubernetes", nil
return "kubernetes"
}

func getConfigMapHash(configMap *corev1.ConfigMap) string {
if configMap != nil {
return utils.ComputeConfigMapChecksum(configMap.Data)
}
return ""
}

0 comments on commit 3a4505a

Please sign in to comment.