lint; assert 'metrics' service (#131)

This commit is contained in:
Michael Quigley 2022-12-05 16:10:38 -05:00
parent 15d289cc87
commit 4d75e2da4f
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 54 additions and 10 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}