diff --git a/config/samples/example-test.sh b/config/samples/example-test.sh index 540bb616..d69113b7 100755 --- a/config/samples/example-test.sh +++ b/config/samples/example-test.sh @@ -594,6 +594,11 @@ 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" +repoType=$(kubectl get repo repository-chartmuseum -nkubebb-system -ojson | jq -r '.metadata.labels."kubebb.repository.type"') +if [[ ${repoType} != "chartmuseum" ]]; then + echo "get repotype failed" + exit 1 +fi 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..2ad5ec65 100644 --- a/controllers/repository_controller.go +++ b/controllers/repository_controller.go @@ -18,7 +18,9 @@ package controllers import ( "context" + "encoding/base64" "fmt" + "net/http" "reflect" "strings" @@ -143,6 +145,26 @@ func (r *RepositoryReconciler) checkInitial(ctx context.Context, logger logr.Log if v, ok := instanceDeepCopy.Labels[corev1alpha1.RepositoryTypeLabel]; !ok || v != instanceDeepCopy.Spec.RepositoryType { instanceDeepCopy.Labels[corev1alpha1.RepositoryTypeLabel] = instanceDeepCopy.Spec.RepositoryType + if !instanceDeepCopy.IsOCI() { + url := instanceDeepCopy.Spec.URL + var auth string + 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 { + logger.Error(err, "unable to fetch secret") + } + username := string(secret.Data["username"]) + password := string(secret.Data["password"]) + auth = username + ":" + password + } + resp, err := r.getRepoType(url, auth) + if err != nil { + logger.Error(err, "get repotype fail") + } + if resp != nil && resp.StatusCode == http.StatusOK { + instanceDeepCopy.Labels[corev1alpha1.RepositoryTypeLabel] = "chartmuseum" + } + } update = true } if v, ok := instanceDeepCopy.Labels[corev1alpha1.RepositorySourceLabel]; !ok || (v != string(corev1alpha1.Official) && v != string(corev1alpha1.Unknown)) { @@ -291,3 +313,22 @@ func (r RepositoryReconciler) ensureRatingServiceAccount(ctx context.Context, na return nil } +func (r *RepositoryReconciler) getRepoType(apiURL, auth string) (*http.Response, error) { + req, err := http.NewRequest("GET", apiURL, nil) + if err != nil { + return nil, err + } + req.Header.Add("Content-Type", "application/json") + if auth != "" { + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth))) + } + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + return resp, nil +}