Refactor FormatVersion function to handle invalid characters (#6244)

* Refactor FormatVersion function to handle invalid characters

* Use SHA1 instead of MD5
This commit is contained in:
inksnw
2024-10-30 12:51:05 +08:00
committed by GitHub
parent 7992b75072
commit 9aa17af5db
4 changed files with 16 additions and 17 deletions

View File

@@ -215,14 +215,14 @@ func repoParseRequest(cli client.Client, versions helmrepo.ChartVersions, helmRe
appVersionDigestMap := make(map[string]string)
for _, i := range appVersionList.Items {
key := fmt.Sprintf("%s-%s", i.GetLabels()[appv2.AppIDLabelKey], i.Spec.VersionName)
LegalVersion := application.FormatVersion(i.Spec.VersionName)
key := fmt.Sprintf("%s-%s", i.GetLabels()[appv2.AppIDLabelKey], LegalVersion)
appVersionDigestMap[key] = i.Spec.Digest
}
for _, ver := range versions {
ver.Version = application.FormatVersion(ver.Version)
legalVersion := application.FormatVersion(ver.Version)
shortName := application.GenerateShortNameMD5Hash(ver.Name)
key := fmt.Sprintf("%s-%s-%s", helmRepo.Name, shortName, ver.Version)
key := fmt.Sprintf("%s-%s-%s", helmRepo.Name, shortName, legalVersion)
dig := appVersionDigestMap[key]
if dig == ver.Digest {
continue

View File

@@ -61,8 +61,8 @@ func (h *appHandler) CreateOrUpdateAppVersion(req *restful.Request, resp *restfu
return
}
appVersion := &appv2.ApplicationVersion{}
vRequest.VersionName = application.FormatVersion(vRequest.VersionName)
appVersion.Name = fmt.Sprintf("%s-%s", createAppVersionRequest.AppName, vRequest.VersionName)
legalVersion := application.FormatVersion(vRequest.VersionName)
appVersion.Name = fmt.Sprintf("%s-%s", createAppVersionRequest.AppName, legalVersion)
if h.conflictedDone(req, resp, "version", appVersion) {
return
}

View File

@@ -5,6 +5,7 @@ import (
"bytes"
"context"
"crypto/md5"
"crypto/sha1"
"crypto/tls"
"encoding/hex"
"encoding/json"
@@ -13,7 +14,6 @@ import (
"io"
"net/http"
"net/url"
"regexp"
"strings"
"time"
@@ -198,7 +198,8 @@ func CreateOrUpdateAppVersion(ctx context.Context, client runtimeclient.Client,
//1. create or update app version
appVersion := appv2.ApplicationVersion{}
appVersion.Name = fmt.Sprintf("%s-%s", app.Name, vRequest.VersionName)
legalVersion := FormatVersion(vRequest.VersionName)
appVersion.Name = fmt.Sprintf("%s-%s", app.Name, legalVersion)
mutateFn := func() error {
if err := controllerutil.SetControllerReference(&app, &appVersion, scheme.Scheme); err != nil {
@@ -570,15 +571,13 @@ func GenerateShortNameMD5Hash(input string) string {
}
func FormatVersion(input string) string {
re := regexp.MustCompile(`[^a-z0-9-.]`)
errs := validation.IsDNS1123Subdomain(input)
if len(errs) != 0 {
klog.Warningf("Version %s does not meet the Kubernetes naming standard, replacing invalid characters with '-'", input)
input = re.ReplaceAllStringFunc(input, func(s string) string {
return "-"
})
if len(validation.IsDNS1123Subdomain(input)) == 0 {
return input
}
return input
hash := sha1.Sum([]byte(input))
formattedVersion := hex.EncodeToString(hash[:])[:12]
klog.Warningf("Version: %s does not meet the Kubernetes naming standard, replacing with SHA-1 hash: %s", input, formattedVersion)
return formattedVersion
}
func GetHelmKubeConfig(ctx context.Context, cluster *clusterv1alpha1.Cluster, runClient client.Client) (config []byte, err error) {

View File

@@ -447,7 +447,7 @@ func (e *executor) setupChartData(release string, kubeconfig []byte, chartData,
}
func generateName(name, action string) string {
return fmt.Sprintf("helm-executor-%s-%s-%s", action, name, rand.String(6))
return fmt.Sprintf("helm-%s-%s-%s", action, name, rand.String(6))
}
func (e *executor) createConfigMap(ctx context.Context, kubeconfig []byte, name string, release string, chartData, values []byte, labels, annotations map[string]string, caBundle string) error {