-
Notifications
You must be signed in to change notification settings - Fork 277
add and patch containerd/pkg/shim to be used with new Windows shims #2601
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| //go:build windows | ||
|
|
||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| 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 shim | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "sync" | ||
| "time" | ||
|
|
||
| v1 "github.com/containerd/containerd/api/services/ttrpc/events/v1" | ||
| "github.com/containerd/containerd/api/types" | ||
| "github.com/containerd/containerd/v2/core/events" | ||
| "github.com/containerd/containerd/v2/pkg/namespaces" | ||
| "github.com/containerd/containerd/v2/pkg/protobuf" | ||
| "github.com/containerd/containerd/v2/pkg/ttrpcutil" | ||
| "github.com/containerd/log" | ||
| "github.com/containerd/ttrpc" | ||
| "github.com/containerd/typeurl/v2" | ||
| ) | ||
|
|
||
| const ( | ||
| queueSize = 2048 | ||
| maxRequeue = 5 | ||
| ) | ||
|
|
||
| type item struct { | ||
| ev *types.Envelope | ||
| ctx context.Context | ||
| count int | ||
| } | ||
|
|
||
| type publisherConfig struct { | ||
| ttrpcOpts []ttrpc.ClientOpts | ||
| } | ||
|
|
||
| type PublisherOpts func(*publisherConfig) | ||
|
|
||
| func WithPublishTTRPCOpts(opts ...ttrpc.ClientOpts) PublisherOpts { | ||
| return func(cfg *publisherConfig) { | ||
| cfg.ttrpcOpts = append(cfg.ttrpcOpts, opts...) | ||
| } | ||
| } | ||
|
|
||
| // NewPublisher creates a new remote events publisher | ||
| func NewPublisher(address string, opts ...PublisherOpts) (*RemoteEventsPublisher, error) { | ||
| client, err := ttrpcutil.NewClient(address) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| l := &RemoteEventsPublisher{ | ||
| client: client, | ||
| closed: make(chan struct{}), | ||
| requeue: make(chan *item, queueSize), | ||
| } | ||
|
|
||
| go l.processQueue() | ||
| return l, nil | ||
| } | ||
|
|
||
| // RemoteEventsPublisher forwards events to a ttrpc server | ||
| type RemoteEventsPublisher struct { | ||
| client *ttrpcutil.Client | ||
| closed chan struct{} | ||
| closer sync.Once | ||
| requeue chan *item | ||
| } | ||
|
|
||
| // Done returns a channel which closes when done | ||
| func (l *RemoteEventsPublisher) Done() <-chan struct{} { | ||
| return l.closed | ||
| } | ||
|
|
||
| // Close closes the remote connection and closes the done channel | ||
| func (l *RemoteEventsPublisher) Close() (err error) { | ||
| err = l.client.Close() | ||
| l.closer.Do(func() { | ||
| close(l.closed) | ||
| }) | ||
| return err | ||
| } | ||
|
|
||
| func (l *RemoteEventsPublisher) processQueue() { | ||
| for i := range l.requeue { | ||
| if i.count > maxRequeue { | ||
| log.L.Errorf("evicting %s from queue because of retry count", i.ev.Topic) | ||
| // drop the event | ||
| continue | ||
| } | ||
|
|
||
| if err := l.forwardRequest(i.ctx, &v1.ForwardRequest{Envelope: i.ev}); err != nil { | ||
| log.L.WithError(err).Error("forward event") | ||
| l.queue(i) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (l *RemoteEventsPublisher) queue(i *item) { | ||
| go func() { | ||
| i.count++ | ||
| // re-queue after a short delay | ||
| time.Sleep(time.Duration(1*i.count) * time.Second) | ||
| l.requeue <- i | ||
| }() | ||
| } | ||
|
|
||
| // Publish publishes the event by forwarding it to the configured ttrpc server | ||
| func (l *RemoteEventsPublisher) Publish(ctx context.Context, topic string, event events.Event) error { | ||
| ns, err := namespaces.NamespaceRequired(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| evt, err := typeurl.MarshalAnyToProto(event) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| i := &item{ | ||
| ev: &types.Envelope{ | ||
| Timestamp: protobuf.ToTimestamp(time.Now()), | ||
| Namespace: ns, | ||
| Topic: topic, | ||
| Event: evt, | ||
| }, | ||
| ctx: ctx, | ||
| } | ||
|
|
||
| if err := l.forwardRequest(i.ctx, &v1.ForwardRequest{Envelope: i.ev}); err != nil { | ||
| l.queue(i) | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *RemoteEventsPublisher) forwardRequest(ctx context.Context, req *v1.ForwardRequest) error { | ||
| service, err := l.client.EventsService() | ||
| if err == nil { | ||
| fCtx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
| _, err = service.Forward(fCtx, req) | ||
| cancel() | ||
| if err == nil { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| if !errors.Is(err, ttrpc.ErrClosed) { | ||
| return err | ||
| } | ||
|
|
||
| // Reconnect and retry request | ||
| if err = l.client.Reconnect(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| service, err = l.client.EventsService() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // try again with a fresh context, otherwise we may get a context timeout unexpectedly. | ||
| fCtx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
| _, err = service.Forward(fCtx, req) | ||
| cancel() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| 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.
Hey can we check in the exact 2.X package then do the delta commit to make it work. That will give us a history that is the changes you made and will make it easier for us to see what needs to be patched back to containerd/containerd to get this to work upstream.
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.
Any of the _windows are not really that important as its obvious where they changed. But I'm just surprised to see we had to change the shim.go file. That "should" be platform agnostic
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.
Yes sure. I have bifurcated the commits into 2 as requested.