This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
26 changed files
with
610 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
hack/addons/kustomize/nutanix-snapshot-csi/kustomization.yaml.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.