github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/system/events.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package system
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"encoding/json"
    23  	"errors"
    24  	"fmt"
    25  	"text/template"
    26  	"time"
    27  
    28  	"github.com/containerd/containerd"
    29  	_ "github.com/containerd/containerd/api/events" // Register grpc event types
    30  	"github.com/containerd/containerd/events"
    31  	"github.com/containerd/log"
    32  	"github.com/containerd/nerdctl/v2/pkg/api/types"
    33  	"github.com/containerd/nerdctl/v2/pkg/formatter"
    34  	"github.com/containerd/typeurl/v2"
    35  )
    36  
    37  // EventOut contains information about an event.
    38  type EventOut struct {
    39  	Timestamp time.Time
    40  	Namespace string
    41  	Topic     string
    42  	Event     string
    43  }
    44  
    45  // Events is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/events/events.go
    46  func Events(ctx context.Context, client *containerd.Client, options types.SystemEventsOptions) error {
    47  	eventsClient := client.EventService()
    48  	eventsCh, errCh := eventsClient.Subscribe(ctx)
    49  	var tmpl *template.Template
    50  	switch options.Format {
    51  	case "":
    52  		tmpl = nil
    53  	case "raw", "table", "wide":
    54  		return errors.New("unsupported format: \"raw\", \"table\", and \"wide\"")
    55  	default:
    56  		var err error
    57  		tmpl, err = formatter.ParseTemplate(options.Format)
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  	for {
    63  		var e *events.Envelope
    64  		select {
    65  		case e = <-eventsCh:
    66  		case err := <-errCh:
    67  			return err
    68  		}
    69  		if e != nil {
    70  			var out []byte
    71  			if e.Event != nil {
    72  				v, err := typeurl.UnmarshalAny(e.Event)
    73  				if err != nil {
    74  					log.G(ctx).WithError(err).Warn("cannot unmarshal an event from Any")
    75  					continue
    76  				}
    77  				out, err = json.Marshal(v)
    78  				if err != nil {
    79  					log.G(ctx).WithError(err).Warn("cannot marshal Any into JSON")
    80  					continue
    81  				}
    82  			}
    83  			if tmpl != nil {
    84  				out := EventOut{e.Timestamp, e.Namespace, e.Topic, string(out)}
    85  				var b bytes.Buffer
    86  				if err := tmpl.Execute(&b, out); err != nil {
    87  					return err
    88  				}
    89  				if _, err := fmt.Fprintln(options.Stdout, b.String()+"\n"); err != nil {
    90  					return err
    91  				}
    92  			} else {
    93  				if _, err := fmt.Fprintln(
    94  					options.Stdout,
    95  					e.Timestamp,
    96  					e.Namespace,
    97  					e.Topic,
    98  					string(out),
    99  				); err != nil {
   100  					return err
   101  				}
   102  			}
   103  		}
   104  	}
   105  }