diff --git a/pkg/controller/application/helm_repo_controller.go b/pkg/controller/application/helm_repo_controller.go index 30f6edb67..993a45131 100644 --- a/pkg/controller/application/helm_repo_controller.go +++ b/pkg/controller/application/helm_repo_controller.go @@ -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 diff --git a/pkg/kapis/application/v2/handler_appversion.go b/pkg/kapis/application/v2/handler_appversion.go index cc953f2c2..753e80d3c 100644 --- a/pkg/kapis/application/v2/handler_appversion.go +++ b/pkg/kapis/application/v2/handler_appversion.go @@ -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 } diff --git a/pkg/simple/client/application/helper.go b/pkg/simple/client/application/helper.go index 893a096f0..dcc0d9df1 100644 --- a/pkg/simple/client/application/helper.go +++ b/pkg/simple/client/application/helper.go @@ -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) { diff --git a/staging/src/kubesphere.io/utils/helm/executor.go b/staging/src/kubesphere.io/utils/helm/executor.go index 34e6a6dfd..c478322ac 100644 --- a/staging/src/kubesphere.io/utils/helm/executor.go +++ b/staging/src/kubesphere.io/utils/helm/executor.go @@ -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 {