...

Source file src/edge-infra.dev/pkg/edge/bsl-reconciler/organizations.go

Documentation: edge-infra.dev/pkg/edge/bsl-reconciler

     1  package edgebsl
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"edge-infra.dev/pkg/edge/api/graph/model"
     9  	btypes "edge-infra.dev/pkg/edge/api/types"
    10  )
    11  
    12  // processAllEdgeOrganizations processes all the edge organizations.
    13  func (b *BSL) processAllEdgeOrganizations(ctx context.Context, orgs []AllEdgeOrgsPageContent, sm btypes.SecretManagerService, tenantsNames map[string]struct{}) error {
    14  	log := b.logger.WithValues("bsl operator", "processing edge organizations")
    15  	for _, org := range orgs {
    16  		org := org
    17  		if err := b.processEdgeOrganization(ctx, &org, sm, tenantsNames); err != nil {
    18  			log.Error(err, "failed to process org")
    19  			return err
    20  		}
    21  	}
    22  	return nil
    23  }
    24  
    25  // edgeOrganizationUserTypes returns a slice of edge user types (super admin and org admin).
    26  func edgeOrganizationUserTypes(org *AllEdgeOrgsPageContent) []edgeUser {
    27  	return []edgeUser{
    28  		{
    29  			userType:   "superAdmin",
    30  			secretName: OrgNameToK8sName(org.OrganizationName),
    31  			bffUser:    true,
    32  		},
    33  		{
    34  			userType:   "orgAdmin",
    35  			secretName: fmt.Sprintf("%s-org-admin", OrgNameToK8sName(org.OrganizationName)),
    36  			bffUser:    false,
    37  		},
    38  	}
    39  }
    40  
    41  // createEdgeOrganizationUserTypes creates the edge user secret.
    42  func (b *BSL) createEdgeOrganizationUserTypes(ctx context.Context, sm btypes.SecretManagerService, org *AllEdgeOrgsPageContent) error {
    43  	log := b.logger.WithValues("bsl operator", "creating edge organization user", "organization name", org)
    44  	users := edgeOrganizationUserTypes(org)
    45  	for _, user := range users {
    46  		log.Info("creating edge organization user", "edge user type", user.userType)
    47  		if err := b.CreateUserAndSecret(ctx, sm, org.OrganizationName, &b.bslConfig, log, user.secretName, user.bffUser); err != nil {
    48  			b.metrics.ErrorInc("bsl_error", org.OrganizationName, err.Error())
    49  			log.Error(err, "failed to create user secret", "bffuser", user.bffUser)
    50  			return err
    51  		}
    52  	}
    53  	return nil
    54  }
    55  
    56  // processEdgeOrganization processes a single edge organization.
    57  func (b *BSL) processEdgeOrganization(ctx context.Context, org *AllEdgeOrgsPageContent, sm btypes.SecretManagerService, tenantsNames map[string]struct{}) error {
    58  	log := b.logger.WithValues("bsl operator", "processing edge organization", "organization name", org.OrganizationName)
    59  	b.metrics.OrgProcessedInc(org.OrganizationName)
    60  	log.Info(LogWithOrg(org.OrganizationName, "Processing organization"))
    61  	//create edge user groups (will represent roles)
    62  	if err := b.bslConfig.CreateEdgeOrgGroups(ctx, org.OrganizationName); err != nil {
    63  		b.metrics.ErrorInc("bsl_error", org.OrganizationName, err.Error())
    64  		log.Error(err, LogWithOrg(org.OrganizationName, "failed to create bsl edge org groups"))
    65  	}
    66  
    67  	if err := b.bslConfig.AssignRolesToGroups(ctx, org.OrganizationName); err != nil {
    68  		b.metrics.ErrorInc("bsl_error", org.OrganizationName, err.Error())
    69  		if !errors.Is(err, ErrorResourceAlreadyExists) {
    70  			log.Error(err, LogWithOrg(org.OrganizationName, "failed to assign edge roles to groups"))
    71  		}
    72  	}
    73  
    74  	if err := b.bslConfig.CleanUpGroupRoles(ctx, org.OrganizationName); err != nil {
    75  		log.Error(err, LogWithOrg(org.OrganizationName, "failed to revoke edge roles from groups"))
    76  		return err
    77  	}
    78  
    79  	if err := b.createEdgeOrganizationUserTypes(ctx, sm, org); err != nil {
    80  		return err
    81  	}
    82  
    83  	//create enterprise unit type in org {{provisioning.uri}}/enterprise-types
    84  	if err := b.bslConfig.CreateEnterpriseUnitType(ctx, org.OrganizationName, EnterpriseTypeName, EnterpriseTypeDescription); err != nil {
    85  		if !(err.Error() == ErrorResourceAlreadyExists.Error()) {
    86  			b.metrics.ErrorInc("bsl_error", org.OrganizationName, err.Error())
    87  			log.Error(err, LogWithOrg(org.OrganizationName, "failed to create bff bsl enterprise unit type"))
    88  		}
    89  	}
    90  
    91  	log.Info(LogWithOrg(org.OrganizationName, "Organization is successfully setup"))
    92  	if _, ok := tenantsNames[OrgNameToK8sName(org.OrganizationName)]; !ok {
    93  		_, err := b.TenantService.Create(ctx, &model.TenantInput{TenantBSLId: org.ID, OrgName: org.OrganizationName})
    94  		if err != nil {
    95  			log.Error(err, "An error occurred create tenants entry")
    96  			b.metrics.ErrorInc("sql_error", org.OrganizationName, err.Error())
    97  		} else {
    98  			tenantsNames[OrgNameToK8sName(org.OrganizationName)] = struct{}{}
    99  		}
   100  	}
   101  
   102  	return nil
   103  }
   104  

View as plain text