|
| 1 | +package cdk8sappproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + addonsv1alpha1 "github.com/PatrickLaabs/cluster-api-addon-provider-cdk8s/api/v1alpha1" |
| 6 | + "github.com/go-git/go-git/v5" |
| 7 | + plumbingtransport "github.com/go-git/go-git/v5/plumbing/transport" |
| 8 | + "github.com/go-git/go-git/v5/plumbing/transport/http" |
| 9 | + "github.com/go-logr/logr" |
| 10 | + "github.com/pkg/errors" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + "strings" |
| 14 | +) |
| 15 | + |
| 16 | +func (r *Reconciler) getGitAuth(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, authSecretRef *corev1.LocalObjectReference, logger logr.Logger, operation string) (*http.BasicAuth, error) { |
| 17 | + if authSecretRef == nil { |
| 18 | + return &http.BasicAuth{}, nil |
| 19 | + } |
| 20 | + |
| 21 | + logger.Info("AuthSecretRef specified, attempting to fetch secret", "secretName", authSecretRef.Name, "operation", operation) |
| 22 | + |
| 23 | + authSecret := &corev1.Secret{} |
| 24 | + secretKey := client.ObjectKey{Namespace: cdk8sAppProxy.Namespace, Name: authSecretRef.Name} |
| 25 | + if err := r.Get(ctx, secretKey, authSecret); err != nil { |
| 26 | + logger.Error(err, "Failed to get auth secret", "secretName", secretKey.String(), "operation", operation) |
| 27 | + |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + username, okUser := authSecret.Data["username"] |
| 32 | + password, okPass := authSecret.Data["password"] |
| 33 | + |
| 34 | + if !okUser || !okPass { |
| 35 | + err := errors.New("auth secret missing username or password fields") |
| 36 | + logger.Error(err, "Invalid auth secret", "secretName", secretKey.String(), "operation", operation) |
| 37 | + |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + logger.Info("Successfully fetched auth secret", "operation", operation) |
| 42 | + |
| 43 | + return &http.BasicAuth{ |
| 44 | + Username: string(username), |
| 45 | + Password: string(password), |
| 46 | + }, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (r *Reconciler) cloneGitRepository(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, gitSpec *addonsv1alpha1.GitRepositorySpec, tempDir string, logger logr.Logger, operation string) error { |
| 50 | + cloneOptions := &git.CloneOptions{ |
| 51 | + URL: gitSpec.URL, |
| 52 | + Progress: &gitProgressLogger{logger: logger.WithName("git-clone")}, |
| 53 | + } |
| 54 | + |
| 55 | + // Handle authentication if needed |
| 56 | + if gitSpec.AuthSecretRef != nil { |
| 57 | + auth, err := r.getGitAuth(ctx, cdk8sAppProxy, gitSpec.AuthSecretRef, logger, operation) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + cloneOptions.Auth = auth |
| 62 | + } |
| 63 | + |
| 64 | + logger.Info("Executing git clone with go-git", "url", gitSpec.URL, "targetDir", tempDir, "operation", operation) |
| 65 | + |
| 66 | + _, err := git.PlainCloneContext(ctx, tempDir, false, cloneOptions) |
| 67 | + if err != nil { |
| 68 | + logger.Error(err, "go-git PlainCloneContext failed", "operation", operation) |
| 69 | + reason := addonsv1alpha1.GitCloneFailedReason |
| 70 | + if errors.Is(err, plumbingtransport.ErrAuthenticationRequired) || strings.Contains(err.Error(), "authentication required") { |
| 71 | + reason = addonsv1alpha1.GitAuthenticationFailedReason |
| 72 | + } |
| 73 | + |
| 74 | + // Only call handleError if we have a valid cdk8sAppProxy (not during deletion cleanup) |
| 75 | + if cdk8sAppProxy != nil { |
| 76 | + return r.handleError(ctx, cdk8sAppProxy, reason, "go-git clone failed during "+operation, err) |
| 77 | + } |
| 78 | + |
| 79 | + return err |
| 80 | + } |
| 81 | + logger.Info("Successfully cloned git repository with go-git", "operation", operation) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
0 commit comments