Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nabuskey committed Nov 18, 2023
1 parent a37b5d9 commit dd48340
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
8 changes: 8 additions & 0 deletions api/v1alpha1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type GitRepositorySpec struct {
// +kubebuilder:validation:Required
// +kubebuilder:validation:Pattern=`^https?:\/\/.+$`
GitURL string `json:"gitURL"`
// SecretRef is the reference to secret that contain Git server credentials
// +kubebuilder:validation:Optional
SecretRef SecretReference `json:"secretRef"`
}

type GitRepositorySource struct {
Expand All @@ -26,6 +29,11 @@ type GitRepositorySource struct {
Type string `json:"type"`
}

type SecretReference struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}

type Commit struct {
// Hash is the digest of the most recent commit
// +kubebuilder:validation:Optional
Expand Down
16 changes: 16 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 5 additions & 30 deletions pkg/controllers/gitrepository/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,22 @@ func getOrganizationName(repo v1alpha1.GitRepository) string {
}

func (r *RepositoryReconciler) getCredentials(ctx context.Context, repo *v1alpha1.GitRepository) (string, string, error) {
lb, err := r.getLocalBuild(ctx, repo)
if err != nil {
return "", "", err
}

var secret v1.Secret
err = r.Client.Get(ctx, types.NamespacedName{
Namespace: lb.Status.Gitea.AdminUserSecretNamespace,
Name: lb.Status.Gitea.AdminUserSecretName,
err := r.Client.Get(ctx, types.NamespacedName{
Namespace: repo.Spec.SecretRef.Namespace,
Name: repo.Spec.SecretRef.Name,
}, &secret)
if err != nil {
return "", "", err
}

username, ok := secret.Data[giteaAdminUsernameKey]
if !ok {
return "", "", fmt.Errorf("%s key not found in secret %s in %s ns", giteaAdminUsernameKey, lb.Status.Gitea.AdminUserSecretName, lb.Status.Gitea.AdminUserSecretNamespace)
return "", "", fmt.Errorf("%s key not found in secret %s in %s ns", giteaAdminUsernameKey, repo.Spec.SecretRef.Name, repo.Spec.SecretRef.Namespace)
}
password, ok := secret.Data[giteaAdminPasswordKey]
if !ok {
return "", "", fmt.Errorf("%s key not found in secret %s in %s ns", giteaAdminPasswordKey, lb.Status.Gitea.AdminUserSecretName, lb.Status.Gitea.AdminUserSecretNamespace)
return "", "", fmt.Errorf("%s key not found in secret %s in %s ns", giteaAdminPasswordKey, repo.Spec.SecretRef.Name, repo.Spec.SecretRef.Namespace)
}
return string(username), string(password), nil
}
Expand All @@ -92,26 +87,6 @@ func (r *RepositoryReconciler) getBasicAuth(ctx context.Context, repo *v1alpha1.
}, nil
}

func (r *RepositoryReconciler) getLocalBuild(ctx context.Context, repo *v1alpha1.GitRepository) (v1alpha1.Localbuild, error) {
if repo.ObjectMeta.OwnerReferences != nil {
for i := range repo.ObjectMeta.OwnerReferences {
ref := repo.ObjectMeta.OwnerReferences[i]
if ref.Kind == "Localbuild" {
var lb v1alpha1.Localbuild
err := r.Client.Get(ctx, types.NamespacedName{
Name: ref.Name,
}, &lb)
if err != nil {
return v1alpha1.Localbuild{}, err
}

return lb, nil
}
}
}
return v1alpha1.Localbuild{}, fmt.Errorf("local build not found for %s", repo.Name)
}

func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)

Expand Down
10 changes: 4 additions & 6 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (r *LocalbuildReconciler) reconcileEmbeddedApps(ctx context.Context, req ct
func (r *LocalbuildReconciler) reconcileBootstrapApps(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
bootStrapApps := []string{"argocd", "nginx", "gitea"}
for _, n := range bootStrapApps {
repo := v1alpha1.GitRepository{
repo := &v1alpha1.GitRepository{
ObjectMeta: metav1.ObjectMeta{
Name: n,
Namespace: globals.GetProjectNamespace(resource.Name),
Expand All @@ -316,9 +316,8 @@ func (r *LocalbuildReconciler) reconcileBootstrapApps(ctx context.Context, req c
},
}

_, err := controllerutil.CreateOrUpdate(ctx, r.Client, &repo, func() error {
err := controllerutil.SetOwnerReference(resource, &repo, r.Scheme)
if err != nil {
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, repo, func() error {
if err := controllerutil.SetControllerReference(resource, repo, r.Scheme); err != nil {
return err
}
return nil
Expand All @@ -341,8 +340,7 @@ func (r *LocalbuildReconciler) reconcileBootstrapApps(ctx context.Context, req c
nil,
)
_, err = controllerutil.CreateOrUpdate(ctx, r.Client, app, func() error {
err := controllerutil.SetOwnerReference(resource, &repo, r.Scheme)
if err != nil {
if err := controllerutil.SetControllerReference(resource, app, r.Scheme); err != nil {
return err
}
return nil
Expand Down
12 changes: 12 additions & 0 deletions pkg/controllers/resources/idpbuilder.cnoe.io_gitrepositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ spec:
description: GitURL is the base URL of Git server
pattern: ^https?:\/\/.+$
type: string
secretRef:
description: SecretRef is the reference to secret that contain Git
server credentials
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
source:
properties:
embeddedAppName:
Expand Down

0 comments on commit dd48340

Please sign in to comment.