Skip to content

Commit a15ee57

Browse files
committed
wrap service account error
Signed-off-by: rashmi_kh <[email protected]>
1 parent 46cec30 commit a15ee57

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

internal/authentication/tokengetter.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package authentication
22

33
import (
44
"context"
5+
"fmt"
56
"sync"
67
"time"
78

89
authenticationv1 "k8s.io/api/authentication/v1"
10+
"k8s.io/apimachinery/pkg/api/errors"
911
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1012
"k8s.io/apimachinery/pkg/types"
1113
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
@@ -19,6 +21,21 @@ type TokenGetter struct {
1921
mu sync.RWMutex
2022
}
2123

24+
type ServiceAccountNotFoundError struct {
25+
ServiceAccountName string // The name of the missing ServiceAccount.
26+
ServiceAccountNamespace string // The namespace where the ServiceAccount should exist
27+
Err error // The underlying error
28+
}
29+
30+
func (e *ServiceAccountNotFoundError) Unwrap() error {
31+
return e.Err
32+
}
33+
34+
// Error implements the error interface for ServiceAccountNotFoundError.
35+
func (e *ServiceAccountNotFoundError) Error() string {
36+
return fmt.Sprintf("service account \"%s\" not found in namespace \"%s\": unable to authenticate with the Kubernetes cluster.", e.ServiceAccountName, e.ServiceAccountNamespace)
37+
}
38+
2239
type TokenGetterOption func(*TokenGetter)
2340

2441
const (
@@ -86,7 +103,9 @@ func (t *TokenGetter) getToken(ctx context.Context, key types.NamespacedName) (*
86103
Spec: authenticationv1.TokenRequestSpec{ExpirationSeconds: ptr.To(int64(t.expirationDuration / time.Second))},
87104
}, metav1.CreateOptions{})
88105
if err != nil {
89-
return nil, err
106+
if errors.IsNotFound(err) {
107+
return nil, &ServiceAccountNotFoundError{ServiceAccountName: key.Name, ServiceAccountNamespace: key.Namespace}
108+
}
90109
}
91110
return &req.Status, nil
92111
}

internal/authentication/tokengetter_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ func TestTokenGetterGet(t *testing.T) {
7272
"test-namespace-3", "test-token-3", "failed to get token"},
7373
{"Testing error when getting token from fake client", "test-service-account-4",
7474
"test-namespace-4", "error when fetching token", "error when fetching token"},
75+
{"Testing service account not found", "missing-sa",
76+
"test-namespace-5", "", "service account \"missing-sa\" not found in namespace \"test-namespace-5\": unable to authenticate with the Kubernetes cluster."},
7577
}
7678

7779
for _, tc := range tests {
7880
got, err := tg.Get(context.Background(), types.NamespacedName{Namespace: tc.namespace, Name: tc.serviceAccountName})
7981
if err != nil {
8082
t.Logf("%s: expected: %v, got: %v", tc.testName, tc.want, err)
81-
assert.EqualError(t, err, tc.errorMsg)
83+
assert.EqualError(t, err, tc.errorMsg, "Error message should match expected output")
8284
} else {
8385
t.Logf("%s: expected: %v, got: %v", tc.testName, tc.want, got)
8486
assert.Equal(t, tc.want, got, tc.errorMsg)

internal/controllers/clusterextension_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050

5151
ocv1 "github.com/operator-framework/operator-controller/api/v1"
5252
catalogd "github.com/operator-framework/operator-controller/catalogd/api/v1"
53+
"github.com/operator-framework/operator-controller/internal/authentication"
5354
"github.com/operator-framework/operator-controller/internal/bundleutil"
5455
"github.com/operator-framework/operator-controller/internal/conditionsets"
5556
"github.com/operator-framework/operator-controller/internal/contentmanager"
@@ -206,6 +207,12 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1.Cl
206207
installedBundle, err := r.InstalledBundleGetter.GetInstalledBundle(ctx, ext)
207208
if err != nil {
208209
setInstallStatus(ext, nil)
210+
var saerr *authentication.ServiceAccountNotFoundError
211+
if errors.As(err, &saerr) {
212+
setInstalledStatusConditionUnknown(ext, saerr.Error())
213+
setStatusProgressing(ext, errors.New("installation cannot proceed due to missing ServiceAccount"))
214+
return ctrl.Result{}, err
215+
}
209216
setInstalledStatusConditionUnknown(ext, err.Error())
210217
setStatusProgressing(ext, errors.New("retrying to get installed bundle"))
211218
return ctrl.Result{}, err

0 commit comments

Comments
 (0)