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

feat: get helm config from a well known configmap #27

Merged
merged 15 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ repos:
language: system
files: "^(hack/addons/|charts/cluster-api-runtime-extensions-nutanix/templates/.+/manifests/|make/addons.mk$)"
pass_filenames: false
- id: addons-configmap
name: addons-configmap
entry: make generate-helm-configmap
language: system
files: "^(hack/addons/|charts/cluster-api-runtime-extensions-nutanix/templates/.+|make/addons.mk$)"
pass_filenames: false
- repo: https://github.com/tekwizely/pre-commit-golang
rev: v1.0.0-rc.1
hooks:
Expand Down Expand Up @@ -117,7 +123,7 @@ repos:
name: License headers - YAML and Makefiles
stages: [commit]
files: (^Makefile|\.(ya?ml|mk))$
exclude: ^(pkg/handlers/.+/embedded|examples|charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses)/.+\.ya?ml|docs/static/helm/index\.yaml$
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$
args:
- --license-filepath
- hack/license-header.txt
Expand Down
1 change: 1 addition & 0 deletions charts/cluster-api-runtime-extensions-nutanix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ A Helm chart for cluster-api-runtime-extensions-nutanix
| deployDefaultClusterClasses | bool | `true` | |
| deployment.replicas | int | `1` | |
| env | object | `{}` | |
| helmAddonsConfigMap | string | `"default-helm-addons-config"` | |
| hooks.clusterAutoscaler.crsStrategy.defaultInstallationConfigMap.name | string | `"cluster-autoscaler"` | |
| hooks.clusterAutoscaler.helmAddonStrategy.defaultValueTemplateConfigMap.create | bool | `true` | |
| hooks.clusterAutoscaler.helmAddonStrategy.defaultValueTemplateConfigMap.name | string | `"default-cluster-autoscaler-helm-values-template"` | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

#=================================================================
# DO NOT EDIT THIS FILE
# IT HAS BEEN GENERATED BY /hack/addons/update-nutanix-csi.sh
# IT HAS BEEN GENERATED BY /hack/addons/update-nutanix-storage-csi.sh
#=================================================================
apiVersion: v1
data:
nutanix-csi.yaml: |
nutanix-storage-csi.yaml: |
apiVersion: v1
kind: ServiceAccount
metadata:
Expand Down Expand Up @@ -540,4 +540,4 @@ data:
kind: ConfigMap
metadata:
creationTimestamp: null
name: nutanix-csi
name: nutanix-storage-csi
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ spec:
args:
- --webhook-cert-dir=/runtimehooks-certs/
- --defaults-namespace=$(POD_NAMESPACE)
- --helm-addons-configmap={{ .Values.helmAddonsConfigMap }}
- --cni.cilium.helm-addon.default-values-template-configmap-name={{ .Values.hooks.cni.cilium.helmAddonStrategy.defaultValueTemplateConfigMap.name }}
- --nfd.helm-addon.default-values-template-configmap-name={{ .Values.hooks.nfd.helmAddonStrategy.defaultValueTemplateConfigMap.name }}
{{- range $key, $value := .Values.extraArgs }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2023 D2iQ, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

#=================================================================
# DO NOT EDIT THIS FILE
# IT HAS BEEN GENERATED BY /hack/tools/helm-cm/main.go
#=================================================================
apiVersion: v1
data:
cilium: |
ChartName: cilium
ChartVersion: 1.15.0
RepositoryURL: https://helm.cilium.io/
cluster-autoscaler: |
ChartName: cluster-autoscaler
ChartVersion: 9.35.0
RepositoryURL: https://kubernetes.github.io/autoscaler
nfd: |
ChartName: node-feature-discovery
ChartVersion: 0.15.2
RepositoryURL: https://kubernetes-sigs.github.io/node-feature-discovery/charts
nutanix-snapshot-csi: |
ChartName: nutanix-csi-snapshot
ChartVersion: v6.3.2
RepositoryURL: https://nutanix.github.io/helm/
nutanix-storage-csi: |
ChartName: nutanix-csi-storage
ChartVersion: v2.6.6
RepositoryURL: https://nutanix.github.io/helm/
tigera-operator: |
ChartName: tigera-operator
ChartVersion: v3.26.4
RepositoryURL: https://docs.tigera.io/calico/charts
kind: ConfigMap
metadata:
creationTimestamp: null
name: {{ .Values.helmAddonsConfigMap }}
2 changes: 2 additions & 0 deletions charts/cluster-api-runtime-extensions-nutanix/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ hooks:
create: true
name: default-cluster-autoscaler-helm-values-template

helmAddonsConfigMap: default-helm-addons-config

deployDefaultClusterClasses: true

deployment:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/onsi/gomega v1.31.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.29.3
k8s.io/apiextensions-apiserver v0.29.3
k8s.io/apimachinery v0.29.3
Expand Down Expand Up @@ -135,7 +136,6 @@ require (
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/cluster-bootstrap v0.29.3 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
Expand Down
23 changes: 23 additions & 0 deletions hack/addons/add-warning-helm-configmap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR

# shellcheck source=hack/common.sh
source "${SCRIPT_DIR}/../common.sh"
ASSETS_DIR="$(mktemp -d -p "${TMPDIR:-/tmp}")"
mv "${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml" "${ASSETS_DIR}/helm-config.yaml"
# add warning not to edit file directly
cat <<EOF >"${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml"
$(cat "${GIT_REPO_ROOT}/hack/license-header.yaml.txt")

#=================================================================
# DO NOT EDIT THIS FILE
# IT HAS BEEN GENERATED BY /hack/tools/helm-cm/main.go
#=================================================================
$(cat "${ASSETS_DIR}/helm-config.yaml")
EOF

sed -i s/placeholder/"{{ .Values.helmAddonsConfigMap }}"/g "${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml"
19 changes: 19 additions & 0 deletions hack/addons/kustomize/nutanix-snapshot-csi/kustomization.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 D2iQ, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

metadata:
name: nutanix-csi-kustomize

namespace: kube-system

helmCharts:
- name: nutanix-csi-snapshot
repo: https://nutanix.github.io/helm/
releaseName: nutanix-csi-storage
version: ${NUTANIX_SNAPSHOT_CSI_CHART_VERSION}
includeCRDs: true
skipTests: true
namespace: nutanix-system
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ helmCharts:
- name: nutanix-csi-storage
repo: https://nutanix.github.io/helm/
releaseName: nutanix-csi-storage
version: ${NUTANIX_CSI_CHART_VERSION}
version: ${NUTANIX_STORAGE_CSI_CHART_VERSION}
valuesFile: helm-values.yaml
includeCRDs: true
skipTests: true
Expand Down
26 changes: 13 additions & 13 deletions hack/addons/update-nutanix-csi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ readonly SCRIPT_DIR
# shellcheck source=hack/common.sh
source "${SCRIPT_DIR}/../common.sh"

if [ -z "${NUTANIX_CSI_CHART_VERSION:-}" ]; then
echo "Missing environment variable: NUTANIX_CSI_CHART_VERSION"
if [ -z "${NUTANIX_STORAGE_CSI_CHART_VERSION:-}" ]; then
echo "Missing environment variable: NUTANIX_STORAGE_CSI_CHART_VERSION"
exit 1
fi

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

readonly FILE_NAME="nutanix-csi.yaml"
readonly FILE_NAME="nutanix-storage-csi.yaml"

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

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

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

# add warning not to edit file directly
cat <<EOF >"${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/csi/nutanix/manifests/nutanix-csi-configmap.yaml"
cat <<EOF >"${GIT_REPO_ROOT}/charts/cluster-api-runtime-extensions-nutanix/templates/csi/nutanix/manifests/nutanix-storage-csi-configmap.yaml"
$(cat "${GIT_REPO_ROOT}/hack/license-header.yaml.txt")

#=================================================================
# DO NOT EDIT THIS FILE
# IT HAS BEEN GENERATED BY /hack/addons/update-nutanix-csi.sh
# IT HAS BEEN GENERATED BY /hack/addons/update-nutanix-storage-csi.sh
#=================================================================
$(cat "${ASSETS_DIR}/nutanix-csi-configmap.yaml")
$(cat "${ASSETS_DIR}/nutanix-storage-csi-configmap.yaml")
EOF
167 changes: 167 additions & 0 deletions hack/tools/helm-cm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package main

import (
"bytes"
"context"
"flag"
"fmt"
"io/fs"
"os"
"path"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
ctrl "sigs.k8s.io/controller-runtime"
yamlMarshal "sigs.k8s.io/yaml"
)

const (
createHelmAddonsConfigMap = "helm-addons"
)

var log = ctrl.LoggerFrom(context.Background())

func main() {
args := os.Args
var (
kustomizeDirectory string
outputFile string
)
flagSet := flag.NewFlagSet(createHelmAddonsConfigMap, flag.ExitOnError)
flagSet.StringVar(&kustomizeDirectory, "kustomize-directory", "",
"Kustomize base directory for all addons")
flagSet.StringVar(&outputFile, "output-file", "",
"output file name to write config map to.")
err := flagSet.Parse(args[1:])
if err != nil {
log.Error(err, "failed to parse args")
}
cm, err := createConfigMapFromDir(kustomizeDirectory)
if err != nil {
log.Error(err, "failed to create configMap")
return
}
b, err := yamlMarshal.Marshal(*cm)
if err != nil {
log.Error(err, "failed ")
}
fullOutputfilePath := outputFile
if !path.IsAbs(outputFile) {
wd, err := os.Getwd()
if err != nil {
log.Error(err, "failed")
}
fullOutputfilePath = path.Join(wd, outputFile)
}
f, err := os.OpenFile(fullOutputfilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
log.Error(err, "failed to create file")
}
defer f.Close()
_, err = bytes.NewBuffer(b).WriteTo(f)
if err != nil {
log.Error(err, "failed to write to file")
}
}

type configMapInfo struct {
configMapFieldName string
RepositoryURL string `json:"RepositoryURL"`
ChartVersion string `json:"ChartVersion"`
ChartName string `json:"ChartName"`
}

func createConfigMapFromDir(kustomizeDir string) (*corev1.ConfigMap, error) {
fullPath := kustomizeDir
if !path.IsAbs(fullPath) {
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get wd %w", err)
}
fullPath = path.Join(wd, kustomizeDir)
}
configDirFS := os.DirFS(fullPath)
results := []configMapInfo{}
err := fs.WalkDir(configDirFS, ".", func(filepath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if strings.Contains(filepath, "kustomization.yaml.tmpl") && !isIgnored(filepath) {
f, err := os.Open(path.Join(fullPath, filepath))
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
obj := make(map[string]interface{})
err = yaml.NewYAMLOrJSONDecoder(f, 1024).Decode(&obj)
if err != nil {
return err
}
charts, ok := obj["helmCharts"]
if !ok {
log.Info("obj %v does not have field helmCharts. skipping \n", obj)
return nil
}
parsedCharts, ok := charts.([]interface{})
if !ok {
return fmt.Errorf("charts obj %v is not of type []interface", charts)
}
info, ok := parsedCharts[0].(map[string]interface{})
if !ok {
return fmt.Errorf("info obj %v is not of type map[string]interface", parsedCharts)
}
repo := info["repo"].(string)
name := info["name"].(string)
dirName := strings.Split(filepath, "/")[0]
i := configMapInfo{
configMapFieldName: dirName,
RepositoryURL: repo,
ChartName: name,
}
versionEnvVar := info["version"].(string)
version := os.ExpandEnv(versionEnvVar)
i.ChartVersion = version
results = append(results, i)
return nil
}
return nil
})

finalCM := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "placeholder",
},
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "ConfigMap",
},
Data: make(map[string]string),
}
for _, res := range results {
d, err := yamlMarshal.Marshal(res)
if err != nil {
return &finalCM, err
}
finalCM.Data[res.configMapFieldName] = string(d)
}
return &finalCM, err
}

var ignored = []string{
"aws-ccm",
"aws-ebs-csi",
}

func isIgnored(filepath string) bool {
for _, i := range ignored {
if strings.Contains(filepath, i) {
return true
}
}
return false
}
Loading
Loading