-
Notifications
You must be signed in to change notification settings - Fork 66
✨ wire up ServiceAccount based caching layer #1074
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
Merged
tmshort
merged 2 commits into
operator-framework:main
from
everettraven:feature/wire-sa-cache
Jul 22, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package authentication | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
utilnet "k8s.io/apimachinery/pkg/util/net" | ||
) | ||
|
||
var _ http.RoundTripper = (*TokenInjectingRoundTripper)(nil) | ||
|
||
type TokenInjectingRoundTripper struct { | ||
Tripper http.RoundTripper | ||
TokenGetter *TokenGetter | ||
Key types.NamespacedName | ||
} | ||
|
||
func (tt *TokenInjectingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
resp, err := tt.do(req) | ||
if resp != nil && resp.StatusCode == http.StatusUnauthorized { | ||
tt.TokenGetter.Delete(tt.Key) | ||
resp, err = tt.do(req) | ||
} | ||
return resp, err | ||
} | ||
|
||
func (tt *TokenInjectingRoundTripper) do(req *http.Request) (*http.Response, error) { | ||
reqClone := utilnet.CloneRequest(req) | ||
token, err := tt.TokenGetter.Get(reqClone.Context(), tt.Key) | ||
if err != nil { | ||
return nil, err | ||
everettraven marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Always set the Authorization header to our retrieved token | ||
reqClone.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
return tt.Tripper.RoundTrip(reqClone) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ import ( | |
"fmt" | ||
"io" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
|
@@ -39,7 +38,6 @@ import ( | |
apimeta "k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
apimachyaml "k8s.io/apimachinery/pkg/util/yaml" | ||
|
@@ -54,7 +52,6 @@ import ( | |
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
|
||
"github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1" | ||
|
@@ -65,6 +62,7 @@ import ( | |
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" | ||
"github.com/operator-framework/operator-controller/internal/bundleutil" | ||
"github.com/operator-framework/operator-controller/internal/conditionsets" | ||
"github.com/operator-framework/operator-controller/internal/contentmanager" | ||
"github.com/operator-framework/operator-controller/internal/labels" | ||
"github.com/operator-framework/operator-controller/internal/resolve" | ||
"github.com/operator-framework/operator-controller/internal/rukpak/convert" | ||
|
@@ -83,8 +81,7 @@ type ClusterExtensionReconciler struct { | |
Resolver resolve.Resolver | ||
Unpacker rukpaksource.Unpacker | ||
ActionClientGetter helmclient.ActionClientGetter | ||
dynamicWatchMutex sync.RWMutex | ||
dynamicWatchGVKs sets.Set[schema.GroupVersionKind] | ||
Watcher contentmanager.Watcher | ||
controller crcontroller.Controller | ||
cache cache.Cache | ||
InstalledBundleGetter InstalledBundleGetter | ||
|
@@ -119,9 +116,6 @@ type Preflight interface { | |
//+kubebuilder:rbac:groups=core,resources=serviceaccounts/token,verbs=create | ||
//+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get | ||
|
||
// TODO: Remove these permissions as part of resolving https://github.com/operator-framework/operator-controller/issues/975 | ||
//+kubebuilder:rbac:groups=*,resources=*,verbs=* | ||
|
||
//+kubebuilder:rbac:groups=catalogd.operatorframework.io,resources=clustercatalogs,verbs=list;watch | ||
//+kubebuilder:rbac:groups=catalogd.operatorframework.io,resources=catalogmetadata,verbs=list;watch | ||
|
||
|
@@ -134,7 +128,7 @@ func (r *ClusterExtensionReconciler) Reconcile(ctx context.Context, req ctrl.Req | |
l.V(1).Info("reconcile starting") | ||
defer l.V(1).Info("reconcile ending") | ||
|
||
var existingExt = &ocv1alpha1.ClusterExtension{} | ||
existingExt := &ocv1alpha1.ClusterExtension{} | ||
if err := r.Client.Get(ctx, req.NamespacedName, existingExt); err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
@@ -403,36 +397,17 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp | |
return ctrl.Result{}, err | ||
} | ||
|
||
for _, obj := range relObjects { | ||
if err := func() error { | ||
r.dynamicWatchMutex.Lock() | ||
defer r.dynamicWatchMutex.Unlock() | ||
|
||
_, isWatched := r.dynamicWatchGVKs[obj.GetObjectKind().GroupVersionKind()] | ||
if !isWatched { | ||
if err := r.controller.Watch( | ||
source.Kind(r.cache, | ||
obj, | ||
crhandler.EnqueueRequestForOwner(r.Scheme(), r.RESTMapper(), ext, crhandler.OnlyControllerOwner()), | ||
predicate.Funcs{ | ||
CreateFunc: func(ce event.CreateEvent) bool { return false }, | ||
UpdateFunc: func(ue event.UpdateEvent) bool { return true }, | ||
DeleteFunc: func(de event.DeleteEvent) bool { return true }, | ||
GenericFunc: func(ge event.GenericEvent) bool { return true }, | ||
}, | ||
), | ||
); err != nil { | ||
return err | ||
} | ||
r.dynamicWatchGVKs[obj.GetObjectKind().GroupVersionKind()] = sets.Empty{} | ||
} | ||
return nil | ||
}(); err != nil { | ||
// Only attempt to watch resources if we are | ||
// installing / upgrading. Otherwise we may restart | ||
// watches that have already been established | ||
if state != stateUnchanged { | ||
if err := r.Watcher.Watch(ctx, r.controller, ext, relObjects); err != nil { | ||
ext.Status.InstalledBundle = nil | ||
setInstalledStatusConditionFailed(ext, fmt.Sprintf("%s:%v", ocv1alpha1.ReasonInstallationFailed, err)) | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
|
||
ext.Status.InstalledBundle = bundleutil.MetadataFor(resolvedBundle.Name, *resolvedBundleVersion) | ||
setInstalledStatusConditionSuccess(ext, fmt.Sprintf("Installed bundle %s successfully", resolvedBundle.Image)) | ||
|
||
|
@@ -533,13 +508,11 @@ func (r *ClusterExtensionReconciler) SetupWithManager(mgr ctrl.Manager) error { | |
}, | ||
})). | ||
Build(r) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: whitespace-only change |
||
if err != nil { | ||
return err | ||
} | ||
r.controller = controller | ||
r.cache = mgr.GetCache() | ||
r.dynamicWatchGVKs = sets.New[schema.GroupVersionKind]() | ||
|
||
return nil | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: change in whitespace