edge_ctrl -> zrok_edge_sdk; serp (#112)

This commit is contained in:
Michael Quigley 2022-12-14 13:58:41 -05:00
parent 06ce17ae8f
commit 7b520597d6
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
11 changed files with 114 additions and 101 deletions

View File

@ -4,8 +4,8 @@ import (
"context"
"fmt"
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/edge_ctrl"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service"
"github.com/openziti/edge/rest_management_api_client"
@ -96,7 +96,7 @@ func (h *accessHandler) Handle(params service.AccessParams, principal *rest_mode
}
func createServicePolicyDialForEnvironment(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement, tags ...*rest_model.Tags) error {
allTags := edge_ctrl.ZrokServiceTags(svcToken)
allTags := zrok_edge_sdk.ZrokServiceTags(svcToken)
for _, t := range tags {
for k, v := range t.SubTags {
allTags.SubTags[k] = v

View File

@ -5,8 +5,8 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/openziti-test-kitchen/zrok/controller/edge_ctrl"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk"
"github.com/openziti-test-kitchen/zrok/model"
"github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/openziti/edge/rest_management_api_client"
@ -261,7 +261,7 @@ func assertMetricsService(cfg *Config, edge *rest_management_api_client.ZitiEdge
var svcZId string
if len(listResp.Payload.Data) != 1 {
logrus.Infof("creating '%v' service", cfg.Metrics.ServiceName)
svcZId, err = edge_ctrl.CreateService("metrics", nil, nil, edge)
svcZId, err = zrok_edge_sdk.CreateService("metrics", nil, nil, edge)
if err != nil {
return "", errors.Wrapf(err, "error creating '%v' service", cfg.Metrics.ServiceName)
}
@ -289,7 +289,7 @@ func assertMetricsSerp(metricsSvcZId string, cfg *Config, edge *rest_management_
}
if len(listResp.Payload.Data) != 1 {
logrus.Infof("creating '%v' serp", cfg.Metrics.ServiceName)
_, err := createServiceEdgeRouterPolicy(cfg.Metrics.ServiceName, metricsSvcZId, nil, edge)
_, err := zrok_edge_sdk.CreateServiceEdgeRouterPolicy(cfg.Metrics.ServiceName, metricsSvcZId, nil, edge)
if err != nil {
return errors.Wrapf(err, "error creating '%v' serp", cfg.Metrics.ServiceName)
}
@ -314,7 +314,7 @@ func assertCtrlMetricsBind(ctrlZId, metricsSvcZId string, edge *rest_management_
}
if len(listResp.Payload.Data) != 1 {
logrus.Info("creating 'ctrl-metrics-bind' service policy")
if err := createNamedBindServicePolicy("ctrl-metrics-bind", metricsSvcZId, ctrlZId, edge, edge_ctrl.ZrokTags()); err != nil {
if err := createNamedBindServicePolicy("ctrl-metrics-bind", metricsSvcZId, ctrlZId, edge, zrok_edge_sdk.ZrokTags()); err != nil {
return errors.Wrap(err, "error creating 'ctrl-metrics-bind' service policy")
}
}
@ -338,7 +338,7 @@ func assertFrontendMetricsDial(frontendZId, metricsSvcZId string, edge *rest_man
}
if len(listResp.Payload.Data) != 1 {
logrus.Info("creating 'frontend-metrics-dial' service policy")
if err := createNamedDialServicePolicy("frontend-metrics-dial", metricsSvcZId, frontendZId, edge, edge_ctrl.ZrokTags()); err != nil {
if err := createNamedDialServicePolicy("frontend-metrics-dial", metricsSvcZId, frontendZId, edge, zrok_edge_sdk.ZrokTags()); err != nil {
return errors.Wrap(err, "error creating 'frontend-metrics-dial' service policy")
}
}

View File

@ -3,7 +3,7 @@ package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/jmoiron/sqlx"
"github.com/openziti-test-kitchen/zrok/controller/edge_ctrl"
"github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/environment"
"github.com/openziti/edge/rest_management_api_client"
@ -87,7 +87,7 @@ func (h *disableHandler) removeServicesForEnvironment(envId int, tx *sqlx.Tx, ed
for _, svc := range svcs {
svcToken := svc.Token
logrus.Infof("garbage collecting service '%v' for environment '%v'", svcToken, env.ZId)
if err := deleteServiceEdgeRouterPolicy(env.ZId, svcToken, edge); err != nil {
if err := zrok_edge_sdk.DeleteServiceEdgeRouterPolicy(env.ZId, svcToken, edge); err != nil {
logrus.Error(err)
}
if err := deleteServicePolicyDial(env.ZId, svcToken, edge); err != nil {
@ -99,7 +99,7 @@ func (h *disableHandler) removeServicesForEnvironment(envId int, tx *sqlx.Tx, ed
if err := deleteConfig(env.ZId, svcToken, edge); err != nil {
logrus.Error(err)
}
if err := edge_ctrl.DeleteService(env.ZId, svc.ZId, edge); err != nil {
if err := zrok_edge_sdk.DeleteService(env.ZId, svc.ZId, edge); err != nil {
logrus.Error(err)
}
logrus.Infof("removed service '%v' for environment '%v'", svc.Token, env.ZId)

View File

@ -3,13 +3,12 @@ package controller
import (
"context"
"fmt"
"github.com/openziti-test-kitchen/zrok/controller/edge_ctrl"
"github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk"
"github.com/openziti-test-kitchen/zrok/model"
"github.com/openziti/edge/rest_management_api_client"
"github.com/openziti/edge/rest_management_api_client/config"
"github.com/openziti/edge/rest_management_api_client/edge_router_policy"
identity_edge "github.com/openziti/edge/rest_management_api_client/identity"
"github.com/openziti/edge/rest_management_api_client/service_edge_router_policy"
"github.com/openziti/edge/rest_management_api_client/service_policy"
"github.com/openziti/edge/rest_model"
rest_model_edge "github.com/openziti/edge/rest_model"
@ -20,75 +19,6 @@ import (
"time"
)
func createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error {
serpZId, err := createServiceEdgeRouterPolicy(svcToken, svcZId, edge_ctrl.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 := edge_ctrl.ZrokTags()
for k, v := range moreTags {
tags.SubTags[k] = v
}
serp := &rest_model.ServiceEdgeRouterPolicyCreate{
EdgeRouterRoles: edgeRouterRoles,
Name: &name,
Semantic: &semantic,
ServiceRoles: serviceRoles,
Tags: tags,
}
serpParams := &service_edge_router_policy.CreateServiceEdgeRouterPolicyParams{
Policy: serp,
Context: context.Background(),
}
serpParams.SetTimeout(30 * time.Second)
resp, err := edge.ServiceEdgeRouterPolicy.CreateServiceEdgeRouterPolicy(serpParams, nil)
if err != nil {
return "", errors.Wrapf(err, "error creating serp '%v' for service '%v'", name, svcZId)
}
return resp.Payload.Data.ID, nil
}
func deleteServiceEdgeRouterPolicy(envZId, svcToken string, edge *rest_management_api_client.ZitiEdgeManagement) error {
filter := fmt.Sprintf("tags.zrokServiceToken=\"%v\"", svcToken)
limit := int64(1)
offset := int64(0)
listReq := &service_edge_router_policy.ListServiceEdgeRouterPoliciesParams{
Filter: &filter,
Limit: &limit,
Offset: &offset,
Context: context.Background(),
}
listReq.SetTimeout(30 * time.Second)
listResp, err := edge.ServiceEdgeRouterPolicy.ListServiceEdgeRouterPolicies(listReq, nil)
if err != nil {
return err
}
if len(listResp.Payload.Data) == 1 {
serpId := *(listResp.Payload.Data[0].ID)
req := &service_edge_router_policy.DeleteServiceEdgeRouterPolicyParams{
ID: serpId,
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.ServiceEdgeRouterPolicy.DeleteServiceEdgeRouterPolicy(req, nil)
if err != nil {
return err
}
logrus.Infof("deleted service edge router policy '%v' for environment '%v'", serpId, envZId)
} else {
logrus.Infof("did not find a service edge router policy")
}
return nil
}
func createServicePolicyBind(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error {
semantic := rest_model.SemanticAllOf
identityRoles := []string{fmt.Sprintf("@%v", envZId)}
@ -103,7 +33,7 @@ func createServicePolicyBind(envZId, svcToken, svcZId string, edge *rest_managem
Semantic: &semantic,
ServiceRoles: serviceRoles,
Type: &dialBind,
Tags: edge_ctrl.ZrokServiceTags(svcToken),
Tags: zrok_edge_sdk.ZrokServiceTags(svcToken),
}
req := &service_policy.CreateServicePolicyParams{
Policy: svcp,
@ -157,7 +87,7 @@ func deleteServicePolicyBind(envZId, svcToken string, edge *rest_management_api_
}
func createServicePolicyDial(envZId, svcToken, svcZId string, dialZIds []string, edge *rest_management_api_client.ZitiEdgeManagement, tags ...*rest_model.Tags) error {
allTags := edge_ctrl.ZrokServiceTags(svcToken)
allTags := zrok_edge_sdk.ZrokServiceTags(svcToken)
for _, t := range tags {
for k, v := range t.SubTags {
allTags.SubTags[k] = v
@ -284,7 +214,7 @@ func createConfig(envZId, svcToken string, authSchemeStr string, authUsers []*mo
ConfigTypeID: &zrokProxyConfigId,
Data: cfg,
Name: &svcToken,
Tags: edge_ctrl.ZrokServiceTags(svcToken),
Tags: zrok_edge_sdk.ZrokServiceTags(svcToken),
}
cfgReq := &config.CreateConfigParams{
Config: cfgCrt,
@ -331,8 +261,8 @@ func deleteConfig(envZId, svcToken string, edge *rest_management_api_client.Ziti
func createShareService(envZId, svcToken, cfgId string, edge *rest_management_api_client.ZitiEdgeManagement) (svcZId string, err error) {
configs := []string{cfgId}
tags := edge_ctrl.ZrokServiceTags(svcToken)
svcZId, err = edge_ctrl.CreateService(svcToken, configs, tags.SubTags, edge)
tags := zrok_edge_sdk.ZrokServiceTags(svcToken)
svcZId, err = zrok_edge_sdk.CreateService(svcToken, configs, tags.SubTags, edge)
if err != nil {
return "", errors.Wrapf(err, "error creating service '%v'", svcToken)
}
@ -349,7 +279,7 @@ func createEdgeRouterPolicy(name, zId string, edge *rest_management_api_client.Z
IdentityRoles: identityRoles,
Name: &name,
Semantic: &semantic,
Tags: edge_ctrl.ZrokTags(),
Tags: zrok_edge_sdk.ZrokTags(),
}
req := &edge_router_policy.CreateEdgeRouterPolicyParams{
Policy: erp,
@ -408,7 +338,7 @@ func createEnvironmentIdentity(accountEmail string, client *rest_management_api_
func createIdentity(name string, identityType rest_model_edge.IdentityType, moreTags map[string]interface{}, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) {
isAdmin := false
tags := edge_ctrl.ZrokTags()
tags := zrok_edge_sdk.ZrokTags()
for k, v := range moreTags {
tags.SubTags[k] = v
}

View File

@ -3,8 +3,8 @@ package controller
import (
"context"
"fmt"
"github.com/openziti-test-kitchen/zrok/controller/edge_ctrl"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk"
"github.com/openziti/edge/rest_management_api_client"
"github.com/openziti/edge/rest_management_api_client/config"
"github.com/openziti/edge/rest_management_api_client/service"
@ -72,7 +72,7 @@ func gcServices(edge *rest_management_api_client.ZitiEdgeManagement, liveMap map
for _, svc := range listResp.Payload.Data {
if _, found := liveMap[*svc.Name]; !found {
logrus.Infof("garbage collecting, zitiSvcId='%v', zrokSvcId='%v'", *svc.ID, *svc.Name)
if err := deleteServiceEdgeRouterPolicy("gc", *svc.Name, edge); err != nil {
if err := zrok_edge_sdk.DeleteServiceEdgeRouterPolicy("gc", *svc.Name, edge); err != nil {
logrus.Errorf("error garbage collecting service edge router policy: %v", err)
}
if err := deleteServicePolicyDial("gc", *svc.Name, edge); err != nil {
@ -84,7 +84,7 @@ func gcServices(edge *rest_management_api_client.ZitiEdgeManagement, liveMap map
if err := deleteConfig("gc", *svc.Name, edge); err != nil {
logrus.Errorf("error garbage collecting config: %v", err)
}
if err := edge_ctrl.DeleteService("gc", *svc.ID, edge); err != nil {
if err := zrok_edge_sdk.DeleteService("gc", *svc.ID, edge); err != nil {
logrus.Errorf("error garbage collecting service: %v", err)
}
} else {
@ -109,7 +109,7 @@ func gcServiceEdgeRouterPolicies(edge *rest_management_api_client.ZitiEdgeManage
for _, serp := range listResp.Payload.Data {
if _, found := liveMap[*serp.Name]; !found {
logrus.Infof("garbage collecting, svcId='%v'", *serp.Name)
if err := deleteServiceEdgeRouterPolicy("gc", *serp.Name, edge); err != nil {
if err := zrok_edge_sdk.DeleteServiceEdgeRouterPolicy("gc", *serp.Name, edge); err != nil {
logrus.Errorf("error garbage collecting service edge router policy: %v", err)
}
} else {

View File

@ -1,6 +1,7 @@
package controller
import (
"github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk"
"github.com/openziti-test-kitchen/zrok/model"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service"
"github.com/openziti/edge/rest_management_api_client"
@ -31,7 +32,7 @@ func (a *privateResourceAllocator) allocate(envZId, svcToken string, params serv
return "", nil, err
}
if err := createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil {
if err := zrok_edge_sdk.CreateShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil {
return "", nil, err
}

View File

@ -1,6 +1,7 @@
package controller
import (
"github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk"
"github.com/openziti-test-kitchen/zrok/model"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service"
"github.com/openziti/edge/rest_management_api_client"
@ -35,7 +36,7 @@ func (a *publicResourceAllocator) allocate(envZId, svcToken string, frontendZIds
return "", nil, err
}
if err := createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil {
if err := zrok_edge_sdk.CreateShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil {
return "", nil, err
}

View File

@ -4,8 +4,8 @@ import (
"context"
"fmt"
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/edge_ctrl"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service"
"github.com/openziti/edge/rest_management_api_client"
@ -78,7 +78,7 @@ func (h *unshareHandler) Handle(params service.UnshareParams, principal *rest_mo
if ssvc.Reserved == params.Body.Reserved {
// single tag-based service deallocator; should work regardless of sharing mode
if err := h.deallocateResources(senv, ssvc, svcToken, svcZId, edge); err != nil {
if err := h.deallocateResources(senv, svcToken, svcZId, edge); err != nil {
logrus.Errorf("error unsharing ziti resources for '%v': %v", ssvc, err)
return service.NewUnshareInternalServerError()
}
@ -122,8 +122,8 @@ func (h *unshareHandler) findServiceZId(svcToken string, edge *rest_management_a
return "", errors.Errorf("service '%v' not found", svcToken)
}
func (h *unshareHandler) deallocateResources(senv *store.Environment, ssvc *store.Service, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error {
if err := deleteServiceEdgeRouterPolicy(senv.ZId, svcToken, edge); err != nil {
func (h *unshareHandler) deallocateResources(senv *store.Environment, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error {
if err := zrok_edge_sdk.DeleteServiceEdgeRouterPolicy(senv.ZId, svcToken, edge); err != nil {
return err
}
if err := deleteServicePolicyDial(senv.ZId, svcToken, edge); err != nil {
@ -135,7 +135,7 @@ func (h *unshareHandler) deallocateResources(senv *store.Environment, ssvc *stor
if err := deleteConfig(senv.ZId, svcToken, edge); err != nil {
return err
}
if err := edge_ctrl.DeleteService(senv.ZId, svcZId, edge); err != nil {
if err := zrok_edge_sdk.DeleteService(senv.ZId, svcZId, edge); err != nil {
return err
}
return nil

View File

@ -0,0 +1,81 @@
package zrok_edge_sdk
import (
"context"
"fmt"
"github.com/openziti/edge/rest_management_api_client"
"github.com/openziti/edge/rest_management_api_client/service_edge_router_policy"
"github.com/openziti/edge/rest_model"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"time"
)
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: &name,
Semantic: &semantic,
ServiceRoles: serviceRoles,
Tags: tags,
}
serpParams := &service_edge_router_policy.CreateServiceEdgeRouterPolicyParams{
Policy: serp,
Context: context.Background(),
}
serpParams.SetTimeout(30 * time.Second)
resp, err := edge.ServiceEdgeRouterPolicy.CreateServiceEdgeRouterPolicy(serpParams, nil)
if err != nil {
return "", errors.Wrapf(err, "error creating serp '%v' for service '%v'", name, svcZId)
}
return resp.Payload.Data.ID, nil
}
func DeleteServiceEdgeRouterPolicy(envZId, svcToken string, edge *rest_management_api_client.ZitiEdgeManagement) error {
filter := fmt.Sprintf("tags.zrokServiceToken=\"%v\"", svcToken)
limit := int64(1)
offset := int64(0)
listReq := &service_edge_router_policy.ListServiceEdgeRouterPoliciesParams{
Filter: &filter,
Limit: &limit,
Offset: &offset,
Context: context.Background(),
}
listReq.SetTimeout(30 * time.Second)
listResp, err := edge.ServiceEdgeRouterPolicy.ListServiceEdgeRouterPolicies(listReq, nil)
if err != nil {
return err
}
if len(listResp.Payload.Data) == 1 {
serpId := *(listResp.Payload.Data[0].ID)
req := &service_edge_router_policy.DeleteServiceEdgeRouterPolicyParams{
ID: serpId,
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.ServiceEdgeRouterPolicy.DeleteServiceEdgeRouterPolicy(req, nil)
if err != nil {
return err
}
logrus.Infof("deleted service edge router policy '%v' for environment '%v'", serpId, envZId)
} else {
logrus.Infof("did not find a service edge router policy")
}
return nil
}

View File

@ -1,4 +1,4 @@
package edge_ctrl
package zrok_edge_sdk
import (
"context"

View File

@ -1,4 +1,4 @@
package edge_ctrl
package zrok_edge_sdk
import (
"github.com/openziti-test-kitchen/zrok/build"