From 6e42fa0225b9f1253da5dd59bce96a97418d1906 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 5 Dec 2022 16:48:59 -0500 Subject: [PATCH] assert metrics serp (#131) --- controller/bootstrap.go | 51 +++++++++++++++++++++++++++++++------ controller/edge.go | 25 +++++++++++++----- controller/share_private.go | 2 +- controller/share_public.go | 2 +- 4 files changed, 64 insertions(+), 16 deletions(-) diff --git a/controller/bootstrap.go b/controller/bootstrap.go index 18558ccb..4fa81e6f 100644 --- a/controller/bootstrap.go +++ b/controller/bootstrap.go @@ -12,6 +12,7 @@ import ( "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_management_api_client/service_edge_router_policy" "github.com/openziti/edge/rest_model" rest_model_edge "github.com/openziti/edge/rest_model" "github.com/openziti/sdk-golang/ziti" @@ -69,7 +70,12 @@ func Bootstrap(skipCtrl, skipFrontend bool, inCfg *Config) error { return err } - if err := assertMetricsService(edge); err != nil { + var metricsSvcZId string + if metricsSvcZId, err = assertMetricsService(cfg, edge); err != nil { + return err + } + + if err := assertMetricsSerp(metricsSvcZId, cfg, edge); err != nil { return err } @@ -196,8 +202,8 @@ func assertErpForIdentity(name, zId string, edge *rest_management_api_client.Zit return nil } -func assertMetricsService(edge *rest_management_api_client.ZitiEdgeManagement) error { - filter := "name=\"metrics\" and tags.zrok != null" +func assertMetricsService(cfg *Config, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) { + filter := fmt.Sprintf("name=\"%v\" and tags.zrok != null", cfg.Metrics.ServiceName) limit := int64(0) offset := int64(0) listReq := &service.ListServicesParams{ @@ -208,15 +214,44 @@ func assertMetricsService(edge *rest_management_api_client.ZitiEdgeManagement) e listReq.SetTimeout(30 * time.Second) listResp, err := edge.Service.ListServices(listReq, nil) if err != nil { - return errors.Wrap(err, "error listing metrics service") + return "", errors.Wrapf(err, "error listing '%v' service", cfg.Metrics.ServiceName) + } + var svcZId string + if len(listResp.Payload.Data) != 1 { + logrus.Infof("creating '%v' service", cfg.Metrics.ServiceName) + svcZId, err = createService("metrics", nil, nil, edge) + if err != nil { + return "", errors.Wrapf(err, "error creating '%v' service", cfg.Metrics.ServiceName) + } + } else { + svcZId = *listResp.Payload.Data[0].ID + } + + logrus.Infof("asserted '%v' service (%v)", cfg.Metrics.ServiceName, svcZId) + return svcZId, nil +} + +func assertMetricsSerp(metricsSvcZId string, cfg *Config, edge *rest_management_api_client.ZitiEdgeManagement) error { + filter := fmt.Sprintf("allOf(serviceRoles) = \"@%v\" and allOf(edgeRouterRoles) = \"#all\" and tags.zrok != null", metricsSvcZId) + limit := int64(0) + offset := int64(0) + listReq := &service_edge_router_policy.ListServiceEdgeRouterPoliciesParams{ + Filter: &filter, + Limit: &limit, + Offset: &offset, + } + listReq.SetTimeout(30 * time.Second) + listResp, err := edge.ServiceEdgeRouterPolicy.ListServiceEdgeRouterPolicies(listReq, nil) + if err != nil { + return errors.Wrapf(err, "error listing '%v' serps", cfg.Metrics.ServiceName) } if len(listResp.Payload.Data) != 1 { - logrus.Infof("creating 'metrics' service") - _, err := createService("metrics", nil, nil, edge) + logrus.Infof("creating '%v' serp", cfg.Metrics.ServiceName) + _, err := createServiceEdgeRouterPolicy(cfg.Metrics.ServiceName, metricsSvcZId, nil, edge) if err != nil { - return errors.Wrap(err, "error creating metrics service") + return errors.Wrapf(err, "error creating '%v' serp", cfg.Metrics.ServiceName) } } - logrus.Infof("asserted metrics service") + logrus.Infof("asserted '%v' serp", cfg.Metrics.ServiceName) return nil } diff --git a/controller/edge.go b/controller/edge.go index 96e8326a..84d8a9f4 100644 --- a/controller/edge.go +++ b/controller/edge.go @@ -22,16 +22,29 @@ import ( "time" ) -func createServiceEdgeRouterPolicy(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { +func createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { + serpZId, err := createServiceEdgeRouterPolicy(svcToken, svcZId, zrokServiceTags(svcToken).SubTags, edge) + if err != nil { + return err + } + logrus.Infof("created service edge router policy '%v' for service '%v' for environment '%v'", serpZId, svcZId, envZId) + return nil +} + +func createServiceEdgeRouterPolicy(name, svcZId string, moreTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) { edgeRouterRoles := []string{"#all"} semantic := rest_model.SemanticAllOf serviceRoles := []string{fmt.Sprintf("@%v", svcZId)} + tags := zrokTags() + for k, v := range moreTags { + tags.SubTags[k] = v + } serp := &rest_model.ServiceEdgeRouterPolicyCreate{ EdgeRouterRoles: edgeRouterRoles, - Name: &svcToken, + Name: &name, Semantic: &semantic, ServiceRoles: serviceRoles, - Tags: zrokServiceTags(svcToken), + Tags: tags, } serpParams := &service_edge_router_policy.CreateServiceEdgeRouterPolicyParams{ Policy: serp, @@ -40,10 +53,9 @@ func createServiceEdgeRouterPolicy(envZId, svcToken, svcZId string, edge *rest_m serpParams.SetTimeout(30 * time.Second) resp, err := edge.ServiceEdgeRouterPolicy.CreateServiceEdgeRouterPolicy(serpParams, nil) if err != nil { - return err + return "", errors.Wrapf(err, "error creating serp '%v' for service '%v'", name, svcZId) } - logrus.Infof("created service edge router policy '%v' for service '%v' for environment '%v'", resp.Payload.Data.ID, svcZId, envZId) - return nil + return resp.Payload.Data.ID, nil } func deleteServiceEdgeRouterPolicy(envZId, svcToken string, edge *rest_management_api_client.ZitiEdgeManagement) error { @@ -277,6 +289,7 @@ func createService(name string, cfgIds []string, moreTags map[string]interface{} for k, v := range moreTags { tags.SubTags[k] = v } + svc.Tags = tags req := &edge_service.CreateServiceParams{ Service: svc, Context: context.Background(), diff --git a/controller/share_private.go b/controller/share_private.go index 51f2d7c6..5fa9dc3c 100644 --- a/controller/share_private.go +++ b/controller/share_private.go @@ -31,7 +31,7 @@ func (a *privateResourceAllocator) allocate(envZId, svcToken string, params serv return "", nil, err } - if err := createServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil { + if err := createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil { return "", nil, err } diff --git a/controller/share_public.go b/controller/share_public.go index dd3d6cf1..2bd136c6 100644 --- a/controller/share_public.go +++ b/controller/share_public.go @@ -35,7 +35,7 @@ func (a *publicResourceAllocator) allocate(envZId, svcToken string, params servi return "", nil, err } - if err := createServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil { + if err := createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil { return "", nil, err }