From 4d75e2da4f2c1a64117abd3338a98659049c76d6 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 5 Dec 2022 16:10:38 -0500 Subject: [PATCH] lint; assert 'metrics' service (#131) --- controller/bootstrap.go | 35 ++++++++++++++++++++++++++++++++--- controller/edge.go | 25 ++++++++++++++++++++----- controller/share_private.go | 2 +- controller/share_public.go | 2 +- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/controller/bootstrap.go b/controller/bootstrap.go index 9b53e400..18558ccb 100644 --- a/controller/bootstrap.go +++ b/controller/bootstrap.go @@ -11,6 +11,7 @@ import ( "github.com/openziti/edge/rest_management_api_client/config" "github.com/openziti/edge/rest_management_api_client/edge_router_policy" "github.com/openziti/edge/rest_management_api_client/identity" + "github.com/openziti/edge/rest_management_api_client/service" "github.com/openziti/edge/rest_model" rest_model_edge "github.com/openziti/edge/rest_model" "github.com/openziti/sdk-golang/ziti" @@ -68,6 +69,10 @@ func Bootstrap(skipCtrl, skipFrontend bool, inCfg *Config) error { return err } + if err := assertMetricsService(edge); err != nil { + return err + } + return nil } @@ -145,13 +150,13 @@ func assertIdentity(zId string, edge *rest_management_api_client.ZitiEdgeManagem func bootstrapIdentity(name string, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) { idc, err := createIdentity(name, rest_model_edge.IdentityTypeDevice, nil, edge) if err != nil { - return "", errors.Wrap(err, "error creating 'ctrl' identity") + return "", errors.Wrapf(err, "error creating '%v' identity", name) } zId := idc.Payload.Data.ID cfg, err := enrollIdentity(zId, edge) if err != nil { - return "", errors.Wrap(err, "error enrolling 'ctrl' identity") + return "", errors.Wrapf(err, "error enrolling '%v' identity", name) } var out bytes.Buffer @@ -168,7 +173,6 @@ func bootstrapIdentity(name string, edge *rest_management_api_client.ZitiEdgeMan } func assertErpForIdentity(name, zId string, edge *rest_management_api_client.ZitiEdgeManagement) error { - logrus.Infof("asserting erps for '%v'", name) filter := fmt.Sprintf("name=\"%v\" and tags.zrok != null", name) limit := int64(0) offset := int64(0) @@ -191,3 +195,28 @@ func assertErpForIdentity(name, zId string, edge *rest_management_api_client.Zit logrus.Infof("asserted erps for '%v' (%v)", name, zId) return nil } + +func assertMetricsService(edge *rest_management_api_client.ZitiEdgeManagement) error { + filter := "name=\"metrics\" and tags.zrok != null" + limit := int64(0) + offset := int64(0) + listReq := &service.ListServicesParams{ + Filter: &filter, + Limit: &limit, + Offset: &offset, + } + listReq.SetTimeout(30 * time.Second) + listResp, err := edge.Service.ListServices(listReq, nil) + if err != nil { + return errors.Wrap(err, "error listing metrics service") + } + if len(listResp.Payload.Data) != 1 { + logrus.Infof("creating 'metrics' service") + _, err := createService("metrics", nil, nil, edge) + if err != nil { + return errors.Wrap(err, "error creating metrics service") + } + } + logrus.Infof("asserted metrics service") + return nil +} diff --git a/controller/edge.go b/controller/edge.go index 8373c35b..96e8326a 100644 --- a/controller/edge.go +++ b/controller/edge.go @@ -17,6 +17,7 @@ import ( rest_model_edge "github.com/openziti/edge/rest_model" sdk_config "github.com/openziti/sdk-golang/ziti/config" "github.com/openziti/sdk-golang/ziti/enroll" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "time" ) @@ -252,14 +253,29 @@ func deleteConfig(envZId, svcToken string, edge *rest_management_api_client.Ziti return nil } -func createService(envZId, svcToken, cfgId string, edge *rest_management_api_client.ZitiEdgeManagement) (serviceId string, err error) { +func createShareService(envZId, svcToken, cfgId string, edge *rest_management_api_client.ZitiEdgeManagement) (svcZId string, err error) { configs := []string{cfgId} + tags := zrokServiceTags(svcToken) + svcZId, err = createService(svcToken, configs, tags.SubTags, edge) + if err != nil { + return "", errors.Wrapf(err, "error creating service '%v'", svcToken) + } + logrus.Infof("created zrok service named '%v' (with ziti id '%v') for environment '%v'", svcToken, svcZId, envZId) + return svcZId, nil +} + +func createService(name string, cfgIds []string, moreTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) (svcZId string, err error) { encryptionRequired := true svc := &rest_model.ServiceCreate{ - Configs: configs, EncryptionRequired: &encryptionRequired, - Name: &svcToken, - Tags: zrokServiceTags(svcToken), + Name: &name, + } + if cfgIds != nil { + svc.Configs = cfgIds + } + tags := zrokTags() + for k, v := range moreTags { + tags.SubTags[k] = v } req := &edge_service.CreateServiceParams{ Service: svc, @@ -270,7 +286,6 @@ func createService(envZId, svcToken, cfgId string, edge *rest_management_api_cli if err != nil { return "", err } - logrus.Infof("created zrok service named '%v' (with ziti id '%v') for environment '%v'", svcToken, resp.Payload.Data.ID, envZId) return resp.Payload.Data.ID, nil } diff --git a/controller/share_private.go b/controller/share_private.go index 99707b3e..51f2d7c6 100644 --- a/controller/share_private.go +++ b/controller/share_private.go @@ -22,7 +22,7 @@ func (a *privateResourceAllocator) allocate(envZId, svcToken string, params serv return "", nil, err } - svcZId, err = createService(envZId, svcToken, cfgId, edge) + svcZId, err = createShareService(envZId, svcToken, cfgId, edge) if err != nil { return "", nil, err } diff --git a/controller/share_public.go b/controller/share_public.go index 3c706f5b..dd3d6cf1 100644 --- a/controller/share_public.go +++ b/controller/share_public.go @@ -22,7 +22,7 @@ func (a *publicResourceAllocator) allocate(envZId, svcToken string, params servi return "", nil, err } - svcZId, err = createService(envZId, svcToken, cfgId, edge) + svcZId, err = createShareService(envZId, svcToken, cfgId, edge) if err != nil { return "", nil, err }