Skip to content

Commit 83dbd19

Browse files
committed
Cleanup some TODOs.
* Use LocalObjectReference for the auth. * Improve error messages. Signed-off-by: Kevin McDermott <[email protected]>
1 parent 51dac02 commit 83dbd19

File tree

8 files changed

+60
-21
lines changed

8 files changed

+60
-21
lines changed

api/v1alpha1/polledrepository_types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ type PolledRepositorySpec struct {
6565
}
6666

6767
// AuthSecret references a secret for authenticating the request.
68-
// TODO: This should use a LocalObjectReference, and not allow the secret
69-
// reference to go across namespaces.
7068
type AuthSecret struct {
7169
// This is a local reference to the named secret to fetch.
7270
// This secret is expected to have a "token" key with a valid GitHub/GitLab
7371
// auth token.
74-
corev1.SecretReference `json:"secretRef,omitempty"`
72+
SecretRef corev1.LocalObjectReference `json:"secretRef,omitempty"`
7573
//+kubebuilder:default:="token"
7674
Key string `json:"key,omitempty"`
7775
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/polling.gitops.tools_polledrepositories.yaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ spec:
6565
auth token.
6666
properties:
6767
name:
68-
description: name is unique within a namespace to reference
69-
a secret resource.
70-
type: string
71-
namespace:
72-
description: namespace defines the space within which the
73-
secret name must be unique.
68+
default: ""
69+
description: |-
70+
Name of the referent.
71+
This field is effectively required, but due to backwards compatibility is
72+
allowed to be empty. Instances of this type with an empty value here are
73+
almost certainly wrong.
74+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
7475
type: string
7576
type: object
7677
x-kubernetes-map-type: atomic

internal/controller/polledrepository_controller.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ func (r *PolledRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Req
8585

8686
authToken, err := r.authTokenForRepo(ctx, reqLogger, req.Namespace, repo)
8787
if err != nil {
88-
return reconcile.Result{}, fmt.Errorf("failed to get auth token")
88+
reqLogger.Error(err, "Getting the auth token failed")
89+
return reconcile.Result{}, fmt.Errorf("failed to get auth token: %w", err)
8990
}
9091

9192
// TODO: handle pollerFactory returning nil/error
@@ -117,14 +118,12 @@ func (r *PolledRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Req
117118
repo.Status.PollStatus = newStatus
118119
if err := r.Client.Status().Update(ctx, &repo); err != nil {
119120
reqLogger.Error(err, "unable to update Repository status")
120-
// TODO: improve the error
121-
return ctrl.Result{}, err
121+
return ctrl.Result{}, fmt.Errorf("failed to update status after change detected: %w", err)
122122
}
123123

124124
if err := r.EventDispatcher.Dispatch(ctx, repo, commit); err != nil {
125125
reqLogger.Error(err, "failed to dispatch commit")
126-
// TODO: improve the error
127-
return ctrl.Result{}, err
126+
return ctrl.Result{}, fmt.Errorf("failed to send notification to %q: %w", repo.Spec.Endpoint, err)
128127
}
129128

130129
reqLogger.Info("requeueing next check", "frequency", repo.Spec.Frequency.Duration)
@@ -147,9 +146,8 @@ func (r *PolledRepositoryReconciler) authTokenForRepo(ctx context.Context, logge
147146
if repo.Spec.Auth.Key != "" {
148147
key = repo.Spec.Auth.Key
149148
}
150-
authToken, err := r.SecretGetter.SecretToken(ctx, types.NamespacedName{Name: repo.Spec.Auth.Name, Namespace: namespace}, key)
149+
authToken, err := r.SecretGetter.SecretToken(ctx, types.NamespacedName{Name: repo.Spec.Auth.SecretRef.Name, Namespace: namespace}, key)
151150
if err != nil {
152-
logger.Error(err, "Getting the auth token failed", "name", repo.Spec.Auth.Name, "namespace", namespace, "key", key)
153151
return "", err
154152
}
155153

@@ -159,13 +157,16 @@ func (r *PolledRepositoryReconciler) authTokenForRepo(ctx context.Context, logge
159157
func repoFromURL(s string) (string, string, error) {
160158
parsed, err := url.Parse(s)
161159
if err != nil {
162-
return "", "", fmt.Errorf("failed to parse repo from URL %#v: %s", s, err)
160+
return "", "", fmt.Errorf("failed to parse repo from URL %s: %s", s, err)
163161
}
164162
host := parsed.Host
165163
if strings.HasSuffix(host, "github.com") {
166164
host = "api." + host
167165
}
166+
167+
// TODO: This should create a new URL with scheme and host?
168168
endpoint := fmt.Sprintf("%s://%s", parsed.Scheme, host)
169+
169170
return strings.TrimPrefix(strings.TrimSuffix(parsed.Path, ".git"), "/"), endpoint, nil
170171
}
171172

internal/controller/polledrepository_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func TestReconciliation(t *testing.T) {
225225
func withSecretRef(s *corev1.Secret) func(*pollingv1.PolledRepository) {
226226
return func(r *pollingv1.PolledRepository) {
227227
r.Spec.Auth = &pollingv1.AuthSecret{
228-
SecretReference: corev1.SecretReference{
228+
SecretRef: corev1.LocalObjectReference{
229229
Name: s.Name,
230230
},
231231
}

pkg/secrets/secrets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (k KubeSecretGetter) SecretToken(ctx context.Context, id types.NamespacedNa
4646
}
4747
token, ok := loaded.Data[key]
4848
if !ok {
49-
return "", fmt.Errorf("secret invalid, no %#v key in %s/%s", key, id.Namespace, id.Name)
49+
return "", fmt.Errorf("secret invalid, no key %q in %s", key, id)
5050
}
5151
return string(token), nil
5252
}

pkg/secrets/secrets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestSecretTokenWithMissingKey(t *testing.T) {
4646
g := New(fake.NewFakeClient(createSecret(testID, "secret-token")))
4747

4848
_, err := g.SecretToken(context.TODO(), testID, "unknown")
49-
if err.Error() != `secret invalid, no "unknown" key in test-ns/test-secret` {
49+
if err.Error() != `secret invalid, no key "unknown" in test-ns/test-secret` {
5050
t.Fatal(err)
5151
}
5252
}

test/utils/resources.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package utils
2+
3+
import (
4+
corev1 "k8s.io/api/core/v1"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
// NewSecret creates and returns a new Secret.
9+
//
10+
// The data is converted to map[string][]byte as a convenience.
11+
func NewSecret(data map[string]string, opts ...func(*corev1.Secret)) *corev1.Secret {
12+
cm := &corev1.Secret{
13+
TypeMeta: metav1.TypeMeta{
14+
APIVersion: "v1",
15+
Kind: "Secret",
16+
},
17+
ObjectMeta: metav1.ObjectMeta{
18+
Name: "demo-secret",
19+
// TODO: pass in a types.NamespacedName
20+
Namespace: "testing",
21+
},
22+
Data: dataToBytes(data),
23+
}
24+
25+
for _, o := range opts {
26+
o(cm)
27+
}
28+
29+
return cm
30+
}
31+
32+
func dataToBytes(src map[string]string) map[string][]byte {
33+
result := map[string][]byte{}
34+
for k, v := range src {
35+
result[k] = []byte(v)
36+
}
37+
38+
return result
39+
}

0 commit comments

Comments
 (0)