|
| 1 | +/* |
| 2 | +Copyright 2019 The KubeSphere Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package helmrelease |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "k8s.io/apimachinery/pkg/types" |
| 22 | + "k8s.io/klog" |
| 23 | + "kubesphere.io/kubesphere/pkg/apis/application/v1alpha1" |
| 24 | + "kubesphere.io/kubesphere/pkg/simple/client/openpitrix/helmrepoindex" |
| 25 | + "path" |
| 26 | + "strings" |
| 27 | +) |
| 28 | + |
| 29 | +func (r *ReconcileHelmRelease) GetChartData(rls *v1alpha1.HelmRelease) (chartName string, chartData []byte, err error) { |
| 30 | + if rls.Spec.RepoId != "" && rls.Spec.RepoId != v1alpha1.AppStoreRepoId { |
| 31 | + // load chart data from helm repo |
| 32 | + repo := v1alpha1.HelmRepo{} |
| 33 | + err := r.Get(context.TODO(), types.NamespacedName{Name: rls.Spec.RepoId}, &repo) |
| 34 | + if err != nil { |
| 35 | + klog.Errorf("get helm repo %s failed, error: %v", rls.Spec.RepoId, err) |
| 36 | + return chartName, chartData, ErrGetRepoFailed |
| 37 | + } |
| 38 | + |
| 39 | + index, err := helmrepoindex.ByteArrayToSavedIndex([]byte(repo.Status.Data)) |
| 40 | + |
| 41 | + if version := index.GetApplicationVersion(rls.Spec.ApplicationId, rls.Spec.ApplicationVersionId); version != nil { |
| 42 | + url := version.Spec.URLs[0] |
| 43 | + if !(strings.HasPrefix(url, "https://") || strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "s3://")) { |
| 44 | + url = repo.Spec.Url + "/" + url |
| 45 | + } |
| 46 | + buf, err := helmrepoindex.LoadChart(context.TODO(), url, &repo.Spec.Credential) |
| 47 | + if err != nil { |
| 48 | + klog.Infof("load chart failed, error: %s", err) |
| 49 | + return chartName, chartData, ErrLoadChartFailed |
| 50 | + } |
| 51 | + chartData = buf.Bytes() |
| 52 | + chartName = version.Name |
| 53 | + } else { |
| 54 | + klog.Errorf("get app version: %s failed", rls.Spec.ApplicationVersionId) |
| 55 | + return chartName, chartData, ErrGetAppVersionFailed |
| 56 | + } |
| 57 | + } else { |
| 58 | + // load chart data from helm application version |
| 59 | + appVersion := &v1alpha1.HelmApplicationVersion{} |
| 60 | + err = r.Get(context.TODO(), types.NamespacedName{Name: rls.Spec.ApplicationVersionId}, appVersion) |
| 61 | + if err != nil { |
| 62 | + klog.Errorf("get app version %s failed, error: %v", rls.Spec.ApplicationVersionId, err) |
| 63 | + return chartName, chartData, ErrGetAppVersionFailed |
| 64 | + } |
| 65 | + |
| 66 | + if r.StorageClient == nil { |
| 67 | + return "", nil, ErrS3Config |
| 68 | + } |
| 69 | + chartData, err = r.StorageClient.Read(path.Join(appVersion.GetWorkspace(), appVersion.Name)) |
| 70 | + if err != nil { |
| 71 | + klog.Errorf("load chart from storage failed, error: %s", err) |
| 72 | + return chartName, chartData, ErrLoadChartFromStorageFailed |
| 73 | + } |
| 74 | + |
| 75 | + chartName = appVersion.GetTrueName() |
| 76 | + } |
| 77 | + return |
| 78 | +} |
0 commit comments