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 26, 2023
1 parent ccf7ce0 commit bab29b5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/repository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const (
FilterOpIgnore FilterOp = "ignore"
)

type RepoType string

const (
RepositoryTypeChartmuseum RepoType = "chartmuseum"
)

// PullStategy for pulling components in repository
type PullStategy struct {
// Interval for pulling
Expand Down
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
32 changes: 32 additions & 0 deletions controllers/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"net/http"
"reflect"
"strings"

Expand Down Expand Up @@ -143,6 +144,9 @@ 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 repoType, err := r.getRepoType(instanceDeepCopy); err == nil && repoType != "" {
instanceDeepCopy.Labels[corev1alpha1.RepositoryTypeLabel] = repoType
}
update = true
}
if v, ok := instanceDeepCopy.Labels[corev1alpha1.RepositorySourceLabel]; !ok || (v != string(corev1alpha1.Official) && v != string(corev1alpha1.Unknown)) {
Expand Down Expand Up @@ -291,3 +295,31 @@ func (r RepositoryReconciler) ensureRatingServiceAccount(ctx context.Context, na

return nil
}
func (r *RepositoryReconciler) getRepoType(repo *corev1alpha1.Repository) (string, error) {
if !repo.IsOCI() {
url := repo.Spec.URL + "/api/charts"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
if repo.Spec.AuthSecret != "" {
username, password, _, _, _, err := corev1alpha1.ParseRepoSecret(r.Client, repo)
if err != nil {
return "", err
}
req.SetBasicAuth(username, password)
}
req.Header.Add("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
if resp != nil && resp.StatusCode == http.StatusOK {
return string(corev1alpha1.RepositoryTypeChartmuseum), nil
}
defer resp.Body.Close()
}
return "", nil
}

0 comments on commit bab29b5

Please sign in to comment.