Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Repository resource handles PermissionDenied status #200

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/clients/repositories/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

const (
errorRepositoryNotFound = "code = NotFound desc = repo"
errorPermissionDenied = "code = PermissionDenied desc = permission denied"
)

// RepositoryServiceClient wraps the functions to connect to argocd repositories
Expand Down Expand Up @@ -43,3 +44,11 @@ func IsErrorRepositoryNotFound(err error) bool {
}
return strings.Contains(err.Error(), errorRepositoryNotFound)
}

// IsErrorPermissionDenied helper function to test for errorPermissionDenied error.
func IsErrorPermissionDenied(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), errorPermissionDenied)
}
64 changes: 40 additions & 24 deletions pkg/controller/repositories/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,39 +121,23 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
}

observedRepository, err := e.client.Get(ctx, &repoQuery)
if err != nil {
return managed.ExternalObservation{}, resource.Ignore(repositories.IsErrorRepositoryNotFound, err)
}

passwordSecretResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.PasswordRef)
if err != nil {
return managed.ExternalObservation{}, err
}
sshPrivateKeyResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.SSHPrivateKeyRef)
if err != nil {
return managed.ExternalObservation{}, err
}
tlsClientCertDataResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.TLSClientCertDataRef)
if err != nil {
return managed.ExternalObservation{}, err
if err != nil && repositories.IsErrorPermissionDenied(err) || repositories.IsErrorRepositoryNotFound(err) {
return managed.ExternalObservation{
ResourceExists: false,
}, nil
}
tlsClientCertKeyResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.TLSClientCertKeyRef)

if err != nil {
return managed.ExternalObservation{}, err
}
githubAppPrivateKeyResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.GithubAppPrivateKeyRef)

resourceVersions, err := e.getSecretResource(ctx, cr)

if err != nil {
return managed.ExternalObservation{}, err
}

resourceVersions := secretResourceVersion{
Password: passwordSecretResourceVersion,
SSHPrivateKey: sshPrivateKeyResourceVersion,
TLSClientCertData: tlsClientCertDataResourceVersion,
TLSClientCertKey: tlsClientCertKeyResourceVersion,
GithubAppPrivateKey: githubAppPrivateKeyResourceVersion,
}

current := cr.Spec.ForProvider.DeepCopy()
lateInitializeRepository(&cr.Spec.ForProvider, observedRepository)

Expand Down Expand Up @@ -543,3 +527,35 @@ func (e *external) getPayload(ctx context.Context, ref *v1alpha1.SecretReference

return nil, nil
}

func (e *external) getSecretResource(ctx context.Context, cr *v1alpha1.Repository) (secretResourceVersion, error) {
passwordSecretResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.PasswordRef)
if err != nil {
return secretResourceVersion{}, err
}
sshPrivateKeyResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.SSHPrivateKeyRef)
if err != nil {
return secretResourceVersion{}, err
}
tlsClientCertDataResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.TLSClientCertDataRef)
if err != nil {
return secretResourceVersion{}, err
}
tlsClientCertKeyResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.TLSClientCertKeyRef)
if err != nil {
return secretResourceVersion{}, err
}
githubAppPrivateKeyResourceVersion, err := e.getSecretResourceVersion(ctx, cr.Spec.ForProvider.GithubAppPrivateKeyRef)
if err != nil {
return secretResourceVersion{}, err
}

return secretResourceVersion{
Password: passwordSecretResourceVersion,
SSHPrivateKey: sshPrivateKeyResourceVersion,
TLSClientCertData: tlsClientCertDataResourceVersion,
TLSClientCertKey: tlsClientCertKeyResourceVersion,
GithubAppPrivateKey: githubAppPrivateKeyResourceVersion,
}, nil

}
8 changes: 5 additions & 3 deletions pkg/controller/repositories/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ import (
)

var (
errBoom = errors.New("boom")
errNotFound = errors.New("code = NotFound desc = repo")
errBoom = errors.New("boom")
// Unused until issue https://github.com/argoproj/argo-cd/issues/20005 in Argo CD project is resolved
// errNotFound = errors.New("code = NotFound desc = repo")
errPermissionDenied = errors.New("code = PermissionDenied desc = permission denied")
testRepositoryExternalName = "testRepo"
testRepo = "https://gitlab.com/example-group/example-project.git"
testUsername = "testUser"
Expand Down Expand Up @@ -206,7 +208,7 @@ func TestObserve(t *testing.T) {
&argocdRepository.RepoQuery{
Repo: testRepositoryExternalName,
},
).Return(nil, errNotFound)
).Return(nil, errPermissionDenied) // Switch to errNotFound when issue https://github.com/argoproj/argo-cd/issues/20005 in Argo CD is solved
}),
cr: Repository(
withExternalName(testRepositoryExternalName),
Expand Down
Loading