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 23, 2023
1 parent ccf7ce0 commit 4383553
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/samples/example-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions controllers/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package controllers

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

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

0 comments on commit 4383553

Please sign in to comment.