-
Notifications
You must be signed in to change notification settings - Fork 58
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
Limit objects cached by DevWorkspace controller to reduce memory usage #652
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3d3732d
Restrict which k8s objects are cached in runtime
amisevsk d6a12b1
Restrict secrets and configmaps in the cache via new labels
amisevsk 1b2d3b7
Update how workspace metadata configmap is handled
amisevsk 4e4e5b3
Collect sync code into one interface to simplify syncing with cluster
amisevsk 1b4a7e0
Adapt more packages to use sync package for managing objects
amisevsk 0008111
Update devworkspaceRouting controller to use sync package for objects
amisevsk 2130ef1
Add documentation and logging to pkg/provision/sync package
amisevsk 43aae00
Fix cache initialization on Kubernetes
amisevsk 465405e
Use sync package for async storage deployment
amisevsk 8290933
Rework automount package to use ClusterAPI instead of client
amisevsk 299186d
Adapt automount git credentials to use new sync mechanism
amisevsk 76d43d7
Treat updating services specially since we have to copy ClusterIP
amisevsk 6c83e0a
Improve diff logging when updating items
amisevsk 8e07871
Restrict caches for Roles and Rolebindings by name
amisevsk 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 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 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,81 @@ | ||
// Copyright (c) 2019-2021 Red Hat, Inc. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cache | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/devfile/devworkspace-operator/pkg/constants" | ||
routev1 "github.com/openshift/api/route/v1" | ||
appsv1 "k8s.io/api/apps/v1" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"sigs.k8s.io/controller-runtime/pkg/cache" | ||
) | ||
|
||
// GetCacheFunc returns a new cache function that restricts the cluster items we store in the manager's | ||
// internal cache. This is required because the controller watches a lot of resource types, which can | ||
// result in very high memory usage on large clusters (e.g. clusters with tens of thousands of secrets). | ||
func GetCacheFunc() (cache.NewCacheFunc, error) { | ||
devworkspaceObjectSelector, err := labels.Parse(constants.DevWorkspaceIDLabel) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// We have to treat secrets and configmaps separately since we need to support auto-mounting | ||
secretObjectSelector, err := labels.Parse(fmt.Sprintf("%s=true", constants.DevWorkspaceWatchSecretLabel)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
configmapObjectSelector, err := labels.Parse(fmt.Sprintf("%s=true", constants.DevWorkspaceWatchConfigMapLabel)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
selectors := cache.SelectorsByObject{ | ||
&appsv1.Deployment{}: { | ||
Label: devworkspaceObjectSelector, | ||
}, | ||
&corev1.Pod{}: { | ||
Label: devworkspaceObjectSelector, | ||
}, | ||
&batchv1.Job{}: { | ||
Label: devworkspaceObjectSelector, | ||
}, | ||
&corev1.ServiceAccount{}: { | ||
Label: devworkspaceObjectSelector, | ||
}, | ||
&corev1.Service{}: { | ||
Label: devworkspaceObjectSelector, | ||
}, | ||
&networkingv1.Ingress{}: { | ||
Label: devworkspaceObjectSelector, | ||
}, | ||
&routev1.Route{}: { | ||
Label: devworkspaceObjectSelector, | ||
}, | ||
&corev1.ConfigMap{}: { | ||
Label: configmapObjectSelector, | ||
}, | ||
&corev1.Secret{}: { | ||
Label: secretObjectSelector, | ||
}, | ||
} | ||
|
||
return cache.BuilderWithOptions(cache.Options{ | ||
SelectorsByObject: selectors, | ||
}), nil | ||
} |
This file contains 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 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 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 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
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.
I wonder if we need a dedicated label for each of objects type?
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.
WDYM? For most objects, the
devworkspace_id
label is sufficient, but I had to add separate ones for configmaps/secrets since those by default do not have one label they always use. We might run into a similar issue to workaround for Deployments if we continue supportingasync
storage, but we could fudge that by usingdevworkspace_id: all
or something.We could use one label for both secrets and configmaps, but then we run into the issue of how to name it --
watch-resource
may be unclear as it only applies to configmaps/secrets, and we use different labels for other objects.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.
I think you got my question)
That's exactly what I have in mind, including the concern ) So, then I think it makes sense to leave as is.
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.
One tiny +1 to use common watch annotation - it will allow us to avoid having different articles/section to some docs, like here https://docs.google.com/document/d/1IR78XlxO37VTWXOu-uE-2nKC93D1GNhZomGoRaN518o/edit?usp=sharing
So, we'll just provide a single patch command instead two.
The concern:
may be addressed by the following explanation:
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.
I assume that there always is at least some kind of label on the secret/configmap that DWO handles, even if such labels differ depending on the purpose.
I think (I have not tried this out), it should be possible to write an "OR" label selector - if not using the existing code then by implementing a custom
labels.Selector
.I personally think requiring 2 labels on a single object for a single purpose is a little bit weird from the UX perspective.
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.
an "or" selector is apparently impossible, so please ignore me :)
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.
The reality is that there are multiple labels that can get applied to configmaps or secrets, and they each serve a different purpose
controller.devfile.io/watch-[secret|configmap]
: mark this secret/configmap as "of interest" to the controller; necessary due to caching changecontroller.devfile.io/mount-to-devworkspace
: mount this resource to the workspace; used by external tools/users to share info across multiple workspacescontroller.devfile.io/git-credential
: mark a secret as holding git credentials, which is handled differently from abovecontroller.devfile.io/devworkspace_id
: associate this resource with the workspace with workspace ID specified.Cases 2, 3 and 4 can exist independently of each other, e.g. a user-defined mounted configmap won't have the
devworkspace_id
label, and the metadata configmap we provision for workspaces won't have themount-to-devworkspace
label. As a result, there's no label selector we can use here, so we have to add thewatch
label to cover all use cases. Moreover, there will be cases when there are secrets/configmaps on the cluster that we're interested in that only have thecontroller.devfile.io/watch-[secret|configmap]
label and no others.Potentially, but it's still somewhat unclear: we watch PVCs without the label applied, and secrets/configmaps become invisible if the label is removed, even if it has the workspace ID label. I'm open to using one label for both, but I'm not sure it's a huge gain in documentation burden.