diff --git a/vmware-event-router/README.MD b/vmware-event-router/README.MD index ad818e4b..7ae21015 100644 --- a/vmware-event-router/README.MD +++ b/vmware-event-router/README.MD @@ -10,8 +10,7 @@ events to configurable event `processors` (see below). **Supported event providers:** - [VMware vCenter Server](https://www.vmware.com/products/vcenter-server.html) -- vCenter Simulator [vcsim](https://github.com/vmware/govmomi/tree/master/vcsim) (for - testing purposes only) +- vCenter Simulator [vcsim](https://github.com/vmware/govmomi/tree/master/vcsim) (DEPRECATED) **Supported event processors:** @@ -231,8 +230,13 @@ vCenter Server and the respective type values and examples for these fields. #### Provider Type `vcsim` +⚠️ This provider is **DEPRECATED** and will be removed in future versions. The +`vcenter` provider will work correctly against a `vcsim` instance. + The following table lists allowed and required fields for connecting to the -govmomi vCenter Simulator [vcsim](https://github.com/vmware/govmomi/tree/master/vcsim) and the respective type values and examples for these fields. +govmomi vCenter Simulator +[vcsim](https://github.com/vmware/govmomi/tree/master/vcsim) and the respective +type values and examples for these fields. | Field | Type | Description | Required | Example | | ------------- | ------- | ------------------------------------- | -------- | -------------------------------- | diff --git a/vmware-event-router/go.mod b/vmware-event-router/go.mod index 9279a8fd..20b959bd 100644 --- a/vmware-event-router/go.mod +++ b/vmware-event-router/go.mod @@ -16,7 +16,7 @@ require ( github.com/openfaas-incubator/connector-sdk v0.0.0-20200902074656-7f648543d4aa github.com/openfaas/faas-provider v0.15.1 github.com/pkg/errors v0.9.1 - github.com/vmware/govmomi v0.22.2 + github.com/vmware/govmomi v0.24.1-0.20210210035757-ed60338583b0 go.uber.org/zap v1.16.0 golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect diff --git a/vmware-event-router/go.sum b/vmware-event-router/go.sum index 6b8c8506..d2c9617b 100644 --- a/vmware-event-router/go.sum +++ b/vmware-event-router/go.sum @@ -632,8 +632,8 @@ github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw= -github.com/vmware/govmomi v0.22.2 h1:hmLv4f+RMTTseqtJRijjOWzwELiaLMIoHv2D6H3bF4I= -github.com/vmware/govmomi v0.22.2/go.mod h1:Y+Wq4lst78L85Ge/F8+ORXIWiKYqaro1vhAulACy9Lc= +github.com/vmware/govmomi v0.24.1-0.20210210035757-ed60338583b0 h1:pV5tmtOx0iZXhs1qP4di1lAKWoKz+ysR7oLf7u19scM= +github.com/vmware/govmomi v0.24.1-0.20210210035757-ed60338583b0/go.mod h1:Y+Wq4lst78L85Ge/F8+ORXIWiKYqaro1vhAulACy9Lc= github.com/vmware/vmw-guestinfo v0.0.0-20170707015358-25eff159a728/go.mod h1:x9oS4Wk2s2u4tS29nEaDLdzvuHdB19CvSGJjPgkZJNk= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= diff --git a/vmware-event-router/internal/config/v1alpha1/provider.go b/vmware-event-router/internal/config/v1alpha1/provider.go index 54e876e1..28a2bd49 100644 --- a/vmware-event-router/internal/config/v1alpha1/provider.go +++ b/vmware-event-router/internal/config/v1alpha1/provider.go @@ -25,6 +25,7 @@ type Provider struct { // +optional VCenter *ProviderConfigVCenter `yaml:"vcenter,omitempty" json:"vcenter,omitempty" jsonschema:"oneof_required=vcenter"` // VCenter simulator configuration settings + // DEPRECATED: use provider vcenter instead // +optional VCSIM *ProviderConfigVCSIM `yaml:"vcsim,omitempty" json:"vcsim,omitempty" jsonschema:"oneof_required=vcsim"` // VCD configuration settings diff --git a/vmware-event-router/internal/provider/vcenter/vcenter_test.go b/vmware-event-router/internal/provider/vcenter/vcenter_test.go new file mode 100644 index 00000000..38548be0 --- /dev/null +++ b/vmware-event-router/internal/provider/vcenter/vcenter_test.go @@ -0,0 +1,170 @@ +// +build unit + +package vcenter + +import ( + "context" + "fmt" + "testing" + "time" + + cloudevents "github.com/cloudevents/sdk-go/v2" + "github.com/vmware/govmomi" + "github.com/vmware/govmomi/session" + "github.com/vmware/govmomi/simulator" + "github.com/vmware/govmomi/vim25" + "go.uber.org/zap/zaptest" + "golang.org/x/sync/errgroup" + "knative.dev/pkg/logging" + + "github.com/vmware-samples/vcenter-event-broker-appliance/vmware-event-router/internal/metrics" +) + +func TestEventStream_stream(t *testing.T) { + type args struct { + enableCheckpoint bool + } + type fields struct { + begin time.Time + } + tests := []struct { + name string + args args + fields fields + want int + wantErr error + }{ + { + name: "expect no events", + fields: fields{ + begin: time.Time{}, // will be "now" in test + }, + args: args{ + enableCheckpoint: false, + }, + want: 0, + wantErr: context.Canceled, + }, + { + name: "expect all events", + fields: fields{ + begin: time.Now().UTC().Add(time.Hour * -1), + }, + args: args{ + enableCheckpoint: false, + }, + want: 26, // current number returned by default VPX simulator model + wantErr: context.Canceled, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + simulator.Run(func(simCtx context.Context, client *vim25.Client) error { + logger := zaptest.NewLogger(t).Sugar() + simCtx = logging.WithLogger(simCtx, logger) + + ctx, cancel := context.WithCancel(simCtx) + defer cancel() + + c := govmomi.Client{ + Client: client, + SessionManager: session.NewManager(client), + } + + vc := &EventStream{ + client: c, + Logger: logger, + checkpoint: tt.args.enableCheckpoint, + stats: metrics.EventStats{ + EventsTotal: new(int), + EventsErr: new(int), + EventsSec: new(float64), + }, + } + + begin := time.Now().UTC() + if !tt.fields.begin.IsZero() { + begin = tt.fields.begin + } + + coll, err := newHistoryCollector(ctx, client, &begin) + if err != nil { + t.Fatalf("create history collector: %v", err) + } + + proc := fakeProcessor{ + expect: tt.want, + doneCh: make(chan struct{}), + } + + eg, egCtx := errgroup.WithContext(ctx) + + // stream + eg.Go(func() error { + return vc.stream(egCtx, &proc, coll, tt.args.enableCheckpoint) + }) + + // give streamer a bit time to establish vc connection + time.Sleep(time.Second) + + // processor + eg.Go(func() error { + if proc.expect == 0 { + cancel() + return nil + } + + select { + case <-egCtx.Done(): + return egCtx.Err() + case <-proc.doneCh: + cancel() + return nil + } + }) + + // cancel early in case of issues + eg.Go(func() error { + select { + case <-time.After(time.Second * 3): + return fmt.Errorf("timed out") + case <-egCtx.Done(): + return egCtx.Err() + } + }) + + if err = eg.Wait(); err != tt.wantErr { + t.Errorf("stream() error = %v, wantErr %v", err, tt.wantErr) + } + + if tt.want != proc.got { + t.Errorf("stream() events got = %v, want %v", proc.got, tt.want) + } + + return nil + }) + }) + } +} + +type fakeProcessor struct { + got int + expect int + doneCh chan struct{} +} + +func (f *fakeProcessor) Process(ctx context.Context, ce cloudevents.Event) error { + logging.FromContext(ctx).Debugf("processing event: %s", ce.String()) + f.got++ + if f.expect == f.got { + close(f.doneCh) + } + return nil +} + +func (f *fakeProcessor) PushMetrics(_ context.Context, _ metrics.Receiver) { +} + +func (f *fakeProcessor) Shutdown(_ context.Context) error { + return nil +} diff --git a/vmware-event-router/internal/provider/vcsim/vcsim.go b/vmware-event-router/internal/provider/vcsim/vcsim.go index d44624b2..33b53d5b 100644 --- a/vmware-event-router/internal/provider/vcsim/vcsim.go +++ b/vmware-event-router/internal/provider/vcsim/vcsim.go @@ -93,6 +93,9 @@ func NewEventStream(ctx context.Context, cfg *config.ProviderConfigVCSIM, ms met } go vcsim.PushMetrics(ctx, ms) + + // this processor is deprecated since we have the appropriate fixes in govmomi + vcsim.Logger.Warn("this processor is deprecated, use vcenter processor instead") return &vcsim, nil }