From 438355338969a17874b04bdc24781fd32641fa13 Mon Sep 17 00:00:00 2001 From: zqq454224016 <454224016@qq.com> Date: Mon, 23 Oct 2023 18:43:39 +0800 Subject: [PATCH] feat: auto detect repositoryType Signed-off-by: zqq454224016 <454224016@qq.com> --- config/samples/example-test.sh | 1 + controllers/repository_controller.go | 29 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/config/samples/example-test.sh b/config/samples/example-test.sh index 540bb616..35229906 100755 --- a/config/samples/example-test.sh +++ b/config/samples/example-test.sh @@ -594,6 +594,7 @@ echo "\n" info "6.2.2 Add this private chartmuseum repository(basic auth enabled) to kubebb" kubectl apply -f config/samples/core_v1alpha1_repository_chartmuseum.yaml waitComponentStatus "kubebb-system" "repository-chartmuseum.nginx" "false" +kubectl get repo chartmuseum -nkubebb-system -ojson | jq -r ".metadata.labels" info "6.2.3 Plan a nignx with private chartmuseum(basic auth enabled) " kubectl apply -f config/samples/core_v1alpha1_componentplan_mynginx.yaml waitComponentPlanDone "kubebb-system" "mynginx" diff --git a/controllers/repository_controller.go b/controllers/repository_controller.go index 795c2a21..b1c2b197 100644 --- a/controllers/repository_controller.go +++ b/controllers/repository_controller.go @@ -17,8 +17,11 @@ limitations under the License. package controllers import ( + "bytes" "context" + "encoding/base64" "fmt" + "net/http" "reflect" "strings" @@ -140,8 +143,34 @@ func (r *RepositoryReconciler) checkInitial(ctx context.Context, logger logr.Log if instanceDeepCopy.Labels == nil { instanceDeepCopy.Labels = make(map[string]string) } + url := instanceDeepCopy.Spec.URL + "/api/charts" + data := []byte(``) + + req, err := http.NewRequest("GET", url, bytes.NewBuffer(data)) + if err != nil { + return err + } + req.Header.Add("Content-Type", "application/json") + if instanceDeepCopy.Spec.AuthSecret != "" { + secret := v1.Secret{} + if err := r.Client.Get(context.TODO(), types.NamespacedName{Namespace: instance.Namespace, Name: instance.Spec.AuthSecret}, &secret); err != nil { + return err + } + username := string(secret.Data["username"]) + password := string(secret.Data["password"]) + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password))) + } + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() if v, ok := instanceDeepCopy.Labels[corev1alpha1.RepositoryTypeLabel]; !ok || v != instanceDeepCopy.Spec.RepositoryType { + if resp.StatusCode == http.StatusOK { + instanceDeepCopy.Labels[corev1alpha1.RepositoryTypeLabel] = "chartmuseum" + } instanceDeepCopy.Labels[corev1alpha1.RepositoryTypeLabel] = instanceDeepCopy.Spec.RepositoryType update = true }