Skip to content

Commit

Permalink
feat: auto detect repositoryType
Browse files Browse the repository at this point in the history
Signed-off-by: zqq454224016 <[email protected]>
  • Loading branch information
zqq454224016 committed Oct 24, 2023
1 parent ccf7ce0 commit dce8276
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/samples/example-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 41 additions & 0 deletions controllers/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package controllers

import (
"context"
"encoding/base64"
"fmt"
"net/http"
"reflect"
"strings"

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
}

0 comments on commit dce8276

Please sign in to comment.