Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 5800608

Browse files
faiqjimmidyson
authored andcommitted
feat: get helm config from a well known configmap (#27)
* ci: adds tooling to create configmap * feat: use a configmap to get helmchart info * fix: precommit issues * fix: typo in cilium * fix: remove workspace files * build: template name for configmap * refactor: names for helm chart info getter * refactor: use nutanix-storage name instead of nutnaix-csi * refactor: move to globaloptions * fix: adds snapshot to helm config * fix: comments after review * fix: adds a warning and removes ebs csi * fix: typo * fix: adds missing script file * fix: precommit
1 parent 58b18ad commit 5800608

File tree

26 files changed

+610
-145
lines changed

26 files changed

+610
-145
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ repos:
3434
language: system
3535
files: "^(hack/addons/|charts/cluster-api-runtime-extensions-nutanix/templates/.+/manifests/|make/addons.mk$)"
3636
pass_filenames: false
37+
- id: addons-configmap
38+
name: addons-configmap
39+
entry: make generate-helm-configmap
40+
language: system
41+
files: "^(hack/addons/|charts/cluster-api-runtime-extensions-nutanix/templates/.+|make/addons.mk$)"
42+
pass_filenames: false
3743
- repo: https://github.com/tekwizely/pre-commit-golang
3844
rev: v1.0.0-rc.1
3945
hooks:
@@ -117,7 +123,7 @@ repos:
117123
name: License headers - YAML and Makefiles
118124
stages: [commit]
119125
files: (^Makefile|\.(ya?ml|mk))$
120-
exclude: ^(pkg/handlers/.+/embedded|examples|charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses)/.+\.ya?ml|docs/static/helm/index\.yaml$
126+
exclude: ^(pkg/handlers/.+/embedded|examples|charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses)/.+\.ya?ml|docs/static/helm/index\.yaml|charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml$
121127
args:
122128
- --license-filepath
123129
- hack/license-header.txt

charts/cluster-api-runtime-extensions-nutanix/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ A Helm chart for cluster-api-runtime-extensions-nutanix
3131
| deployDefaultClusterClasses | bool | `true` | |
3232
| deployment.replicas | int | `1` | |
3333
| env | object | `{}` | |
34+
| helmAddonsConfigMap | string | `"default-helm-addons-config"` | |
3435
| hooks.clusterAutoscaler.crsStrategy.defaultInstallationConfigMap.name | string | `"cluster-autoscaler"` | |
3536
| hooks.clusterAutoscaler.helmAddonStrategy.defaultValueTemplateConfigMap.create | bool | `true` | |
3637
| hooks.clusterAutoscaler.helmAddonStrategy.defaultValueTemplateConfigMap.name | string | `"default-cluster-autoscaler-helm-values-template"` | |

charts/cluster-api-runtime-extensions-nutanix/templates/csi/nutanix/manifests/nutanix-csi-configmap.yaml renamed to charts/cluster-api-runtime-extensions-nutanix/templates/csi/nutanix/manifests/nutanix-storage-csi-configmap.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
#=================================================================
55
# DO NOT EDIT THIS FILE
6-
# IT HAS BEEN GENERATED BY /hack/addons/update-nutanix-csi.sh
6+
# IT HAS BEEN GENERATED BY /hack/addons/update-nutanix-storage-csi.sh
77
#=================================================================
88
apiVersion: v1
99
data:
10-
nutanix-csi.yaml: |
10+
nutanix-storage-csi.yaml: |
1111
apiVersion: v1
1212
kind: ServiceAccount
1313
metadata:
@@ -540,4 +540,4 @@ data:
540540
kind: ConfigMap
541541
metadata:
542542
creationTimestamp: null
543-
name: nutanix-csi
543+
name: nutanix-storage-csi

charts/cluster-api-runtime-extensions-nutanix/templates/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ spec:
3030
args:
3131
- --webhook-cert-dir=/runtimehooks-certs/
3232
- --defaults-namespace=$(POD_NAMESPACE)
33+
- --helm-addons-configmap={{ .Values.helmAddonsConfigMap }}
3334
- --cni.cilium.helm-addon.default-values-template-configmap-name={{ .Values.hooks.cni.cilium.helmAddonStrategy.defaultValueTemplateConfigMap.name }}
3435
- --nfd.helm-addon.default-values-template-configmap-name={{ .Values.hooks.nfd.helmAddonStrategy.defaultValueTemplateConfigMap.name }}
3536
{{- range $key, $value := .Values.extraArgs }}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#=================================================================
5+
# DO NOT EDIT THIS FILE
6+
# IT HAS BEEN GENERATED BY /hack/tools/helm-cm/main.go
7+
#=================================================================
8+
apiVersion: v1
9+
data:
10+
cilium: |
11+
ChartName: cilium
12+
ChartVersion: 1.15.0
13+
RepositoryURL: https://helm.cilium.io/
14+
cluster-autoscaler: |
15+
ChartName: cluster-autoscaler
16+
ChartVersion: 9.35.0
17+
RepositoryURL: https://kubernetes.github.io/autoscaler
18+
nfd: |
19+
ChartName: node-feature-discovery
20+
ChartVersion: 0.15.2
21+
RepositoryURL: https://kubernetes-sigs.github.io/node-feature-discovery/charts
22+
nutanix-snapshot-csi: |
23+
ChartName: nutanix-csi-snapshot
24+
ChartVersion: v6.3.2
25+
RepositoryURL: https://nutanix.github.io/helm/
26+
nutanix-storage-csi: |
27+
ChartName: nutanix-csi-storage
28+
ChartVersion: v2.6.6
29+
RepositoryURL: https://nutanix.github.io/helm/
30+
tigera-operator: |
31+
ChartName: tigera-operator
32+
ChartVersion: v3.26.4
33+
RepositoryURL: https://docs.tigera.io/calico/charts
34+
kind: ConfigMap
35+
metadata:
36+
creationTimestamp: null
37+
name: {{ .Values.helmAddonsConfigMap }}

charts/cluster-api-runtime-extensions-nutanix/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ hooks:
6666
create: true
6767
name: default-cluster-autoscaler-helm-values-template
6868

69+
helmAddonsConfigMap: default-helm-addons-config
70+
6971
deployDefaultClusterClasses: true
7072

7173
deployment:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/onsi/gomega v1.31.1
2222
github.com/spf13/pflag v1.0.5
2323
github.com/stretchr/testify v1.9.0
24+
gopkg.in/yaml.v2 v2.4.0
2425
k8s.io/api v0.29.3
2526
k8s.io/apiextensions-apiserver v0.29.3
2627
k8s.io/apimachinery v0.29.3
@@ -135,7 +136,6 @@ require (
135136
google.golang.org/protobuf v1.33.0 // indirect
136137
gopkg.in/inf.v0 v0.9.1 // indirect
137138
gopkg.in/ini.v1 v1.67.0 // indirect
138-
gopkg.in/yaml.v2 v2.4.0 // indirect
139139
gopkg.in/yaml.v3 v3.0.1 // indirect
140140
k8s.io/cluster-bootstrap v0.29.3 // indirect
141141
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
readonly SCRIPT_DIR
7+
8+
# shellcheck source=hack/common.sh
9+
source "${SCRIPT_DIR}/../common.sh"
10+
ASSETS_DIR="$(mktemp -d -p "${TMPDIR:-/tmp}")"
11+
mv "${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml" "${ASSETS_DIR}/helm-config.yaml"
12+
# add warning not to edit file directly
13+
cat <<EOF >"${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml"
14+
$(cat "${GIT_REPO_ROOT}/hack/license-header.yaml.txt")
15+
16+
#=================================================================
17+
# DO NOT EDIT THIS FILE
18+
# IT HAS BEEN GENERATED BY /hack/tools/helm-cm/main.go
19+
#=================================================================
20+
$(cat "${ASSETS_DIR}/helm-config.yaml")
21+
EOF
22+
23+
sed -i s/placeholder/"{{ .Values.helmAddonsConfigMap }}"/g "${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
apiVersion: kustomize.config.k8s.io/v1beta1
5+
kind: Kustomization
6+
7+
metadata:
8+
name: nutanix-csi-kustomize
9+
10+
namespace: kube-system
11+
12+
helmCharts:
13+
- name: nutanix-csi-snapshot
14+
repo: https://nutanix.github.io/helm/
15+
releaseName: nutanix-csi-storage
16+
version: ${NUTANIX_SNAPSHOT_CSI_CHART_VERSION}
17+
includeCRDs: true
18+
skipTests: true
19+
namespace: nutanix-system

hack/addons/kustomize/nutanix-csi/kustomization.yaml.tmpl renamed to hack/addons/kustomize/nutanix-storage-csi/kustomization.yaml.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ helmCharts:
1313
- name: nutanix-csi-storage
1414
repo: https://nutanix.github.io/helm/
1515
releaseName: nutanix-csi-storage
16-
version: ${NUTANIX_CSI_CHART_VERSION}
16+
version: ${NUTANIX_STORAGE_CSI_CHART_VERSION}
1717
valuesFile: helm-values.yaml
1818
includeCRDs: true
1919
skipTests: true

hack/addons/update-nutanix-csi.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@ readonly SCRIPT_DIR
88
# shellcheck source=hack/common.sh
99
source "${SCRIPT_DIR}/../common.sh"
1010

11-
if [ -z "${NUTANIX_CSI_CHART_VERSION:-}" ]; then
12-
echo "Missing environment variable: NUTANIX_CSI_CHART_VERSION"
11+
if [ -z "${NUTANIX_STORAGE_CSI_CHART_VERSION:-}" ]; then
12+
echo "Missing environment variable: NUTANIX_STORAGE_CSI_CHART_VERSION"
1313
exit 1
1414
fi
1515

1616
ASSETS_DIR="$(mktemp -d -p "${TMPDIR:-/tmp}")"
1717
readonly ASSETS_DIR
1818
trap_add "rm -rf ${ASSETS_DIR}" EXIT
1919

20-
readonly FILE_NAME="nutanix-csi.yaml"
20+
readonly FILE_NAME="nutanix-storage-csi.yaml"
2121

22-
readonly KUSTOMIZE_BASE_DIR="${SCRIPT_DIR}/kustomize/nutanix-csi"
23-
mkdir -p "${ASSETS_DIR}/nutanix-csi"
24-
envsubst -no-unset <"${KUSTOMIZE_BASE_DIR}/kustomization.yaml.tmpl" >"${ASSETS_DIR}/nutanix-csi/kustomization.yaml"
25-
cp -r "${KUSTOMIZE_BASE_DIR}"/*.yaml "${ASSETS_DIR}/nutanix-csi/"
22+
readonly KUSTOMIZE_BASE_DIR="${SCRIPT_DIR}/kustomize/nutanix-storage-csi"
23+
mkdir -p "${ASSETS_DIR}/nutanix-storage-csi"
24+
envsubst -no-unset <"${KUSTOMIZE_BASE_DIR}/kustomization.yaml.tmpl" >"${ASSETS_DIR}/nutanix-storage-csi/kustomization.yaml"
25+
cp -r "${KUSTOMIZE_BASE_DIR}"/*.yaml "${ASSETS_DIR}/nutanix-storage-csi/"
2626

27-
kustomize build --enable-helm "${ASSETS_DIR}/nutanix-csi/" >"${ASSETS_DIR}/${FILE_NAME}"
27+
kustomize build --enable-helm "${ASSETS_DIR}/nutanix-storage-csi/" >"${ASSETS_DIR}/${FILE_NAME}"
2828

29-
kubectl create configmap nutanix-csi --dry-run=client --output yaml \
29+
kubectl create configmap nutanix-storage-csi --dry-run=client --output yaml \
3030
--from-file "${ASSETS_DIR}/${FILE_NAME}" \
31-
>"${ASSETS_DIR}/nutanix-csi-configmap.yaml"
31+
>"${ASSETS_DIR}/nutanix-storage-csi-configmap.yaml"
3232

3333
# add warning not to edit file directly
34-
cat <<EOF >"${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/csi/nutanix/manifests/nutanix-csi-configmap.yaml"
34+
cat <<EOF >"${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/csi/nutanix/manifests/nutanix-storage-csi-configmap.yaml"
3535
$(cat "${GIT_REPO_ROOT}/hack/license-header.yaml.txt")
3636
3737
#=================================================================
3838
# DO NOT EDIT THIS FILE
39-
# IT HAS BEEN GENERATED BY /hack/addons/update-nutanix-csi.sh
39+
# IT HAS BEEN GENERATED BY /hack/addons/update-nutanix-storage-csi.sh
4040
#=================================================================
41-
$(cat "${ASSETS_DIR}/nutanix-csi-configmap.yaml")
41+
$(cat "${ASSETS_DIR}/nutanix-storage-csi-configmap.yaml")
4242
EOF

hack/tools/helm-cm/main.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"bytes"
8+
"context"
9+
"flag"
10+
"fmt"
11+
"io/fs"
12+
"os"
13+
"path"
14+
"strings"
15+
16+
corev1 "k8s.io/api/core/v1"
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
"k8s.io/apimachinery/pkg/util/yaml"
19+
ctrl "sigs.k8s.io/controller-runtime"
20+
yamlMarshal "sigs.k8s.io/yaml"
21+
)
22+
23+
const (
24+
createHelmAddonsConfigMap = "helm-addons"
25+
)
26+
27+
var log = ctrl.LoggerFrom(context.Background())
28+
29+
func main() {
30+
args := os.Args
31+
var (
32+
kustomizeDirectory string
33+
outputFile string
34+
)
35+
flagSet := flag.NewFlagSet(createHelmAddonsConfigMap, flag.ExitOnError)
36+
flagSet.StringVar(&kustomizeDirectory, "kustomize-directory", "",
37+
"Kustomize base directory for all addons")
38+
flagSet.StringVar(&outputFile, "output-file", "",
39+
"output file name to write config map to.")
40+
err := flagSet.Parse(args[1:])
41+
if err != nil {
42+
log.Error(err, "failed to parse args")
43+
}
44+
cm, err := createConfigMapFromDir(kustomizeDirectory)
45+
if err != nil {
46+
log.Error(err, "failed to create configMap")
47+
return
48+
}
49+
b, err := yamlMarshal.Marshal(*cm)
50+
if err != nil {
51+
log.Error(err, "failed ")
52+
}
53+
fullOutputfilePath := outputFile
54+
if !path.IsAbs(outputFile) {
55+
wd, err := os.Getwd()
56+
if err != nil {
57+
log.Error(err, "failed")
58+
}
59+
fullOutputfilePath = path.Join(wd, outputFile)
60+
}
61+
f, err := os.OpenFile(fullOutputfilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
62+
if err != nil {
63+
log.Error(err, "failed to create file")
64+
}
65+
defer f.Close()
66+
_, err = bytes.NewBuffer(b).WriteTo(f)
67+
if err != nil {
68+
log.Error(err, "failed to write to file")
69+
}
70+
}
71+
72+
type configMapInfo struct {
73+
configMapFieldName string
74+
RepositoryURL string `json:"RepositoryURL"`
75+
ChartVersion string `json:"ChartVersion"`
76+
ChartName string `json:"ChartName"`
77+
}
78+
79+
func createConfigMapFromDir(kustomizeDir string) (*corev1.ConfigMap, error) {
80+
fullPath := kustomizeDir
81+
if !path.IsAbs(fullPath) {
82+
wd, err := os.Getwd()
83+
if err != nil {
84+
return nil, fmt.Errorf("failed to get wd %w", err)
85+
}
86+
fullPath = path.Join(wd, kustomizeDir)
87+
}
88+
configDirFS := os.DirFS(fullPath)
89+
results := []configMapInfo{}
90+
err := fs.WalkDir(configDirFS, ".", func(filepath string, d fs.DirEntry, err error) error {
91+
if err != nil {
92+
return err
93+
}
94+
if strings.Contains(filepath, "kustomization.yaml.tmpl") && !isIgnored(filepath) {
95+
f, err := os.Open(path.Join(fullPath, filepath))
96+
if err != nil {
97+
return fmt.Errorf("failed to open file: %w", err)
98+
}
99+
defer f.Close()
100+
obj := make(map[string]interface{})
101+
err = yaml.NewYAMLOrJSONDecoder(f, 1024).Decode(&obj)
102+
if err != nil {
103+
return err
104+
}
105+
charts, ok := obj["helmCharts"]
106+
if !ok {
107+
log.Info("obj %v does not have field helmCharts. skipping \n", obj)
108+
return nil
109+
}
110+
parsedCharts, ok := charts.([]interface{})
111+
if !ok {
112+
return fmt.Errorf("charts obj %v is not of type []interface", charts)
113+
}
114+
info, ok := parsedCharts[0].(map[string]interface{})
115+
if !ok {
116+
return fmt.Errorf("info obj %v is not of type map[string]interface", parsedCharts)
117+
}
118+
repo := info["repo"].(string)
119+
name := info["name"].(string)
120+
dirName := strings.Split(filepath, "/")[0]
121+
i := configMapInfo{
122+
configMapFieldName: dirName,
123+
RepositoryURL: repo,
124+
ChartName: name,
125+
}
126+
versionEnvVar := info["version"].(string)
127+
version := os.ExpandEnv(versionEnvVar)
128+
i.ChartVersion = version
129+
results = append(results, i)
130+
return nil
131+
}
132+
return nil
133+
})
134+
135+
finalCM := corev1.ConfigMap{
136+
ObjectMeta: metav1.ObjectMeta{
137+
Name: "placeholder",
138+
},
139+
TypeMeta: metav1.TypeMeta{
140+
APIVersion: corev1.SchemeGroupVersion.String(),
141+
Kind: "ConfigMap",
142+
},
143+
Data: make(map[string]string),
144+
}
145+
for _, res := range results {
146+
d, err := yamlMarshal.Marshal(res)
147+
if err != nil {
148+
return &finalCM, err
149+
}
150+
finalCM.Data[res.configMapFieldName] = string(d)
151+
}
152+
return &finalCM, err
153+
}
154+
155+
var ignored = []string{
156+
"aws-ccm",
157+
"aws-ebs-csi",
158+
}
159+
160+
func isIgnored(filepath string) bool {
161+
for _, i := range ignored {
162+
if strings.Contains(filepath, i) {
163+
return true
164+
}
165+
}
166+
return false
167+
}

0 commit comments

Comments
 (0)