github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtimeversion/account_mapping.go (about) 1 package runtimeversion 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/sirupsen/logrus" 8 v1 "k8s.io/api/core/v1" 9 apierr "k8s.io/apimachinery/pkg/api/errors" 10 "sigs.k8s.io/controller-runtime/pkg/client" 11 ) 12 13 type AccountVersionMapping struct { 14 ctx context.Context 15 k8sClient client.Client 16 17 namespace string 18 name string 19 20 log logrus.FieldLogger 21 } 22 23 const ( 24 globalAccountPrefix = "GA_" 25 subaccountPrefix = "SA_" 26 ) 27 28 func NewAccountVersionMapping(ctx context.Context, cli client.Client, 29 namespace, name string, log logrus.FieldLogger) *AccountVersionMapping { 30 31 return &AccountVersionMapping{ 32 ctx: ctx, 33 namespace: namespace, 34 name: name, 35 k8sClient: cli, 36 log: log, 37 } 38 } 39 40 // Get retrieves Kyma version from ConfigMap for given accounts IDs 41 func (m *AccountVersionMapping) Get(globalAccountID, subaccountID string) (string, bool, error) { 42 config := &v1.ConfigMap{} 43 key := client.ObjectKey{Namespace: m.namespace, Name: m.name} 44 err := m.k8sClient.Get(m.ctx, key, config) 45 46 switch { 47 case apierr.IsNotFound(err): 48 m.log.Infof("Kyma Version per Account configuration %s/%s not found", m.namespace, m.name) 49 return "", false, nil 50 case err != nil: 51 return "", false, fmt.Errorf("while getting kyma version config map: %w", err) 52 } 53 54 // SubAccount version mapping has higher priority than GlobalAccount version 55 ver, found := config.Data[subaccountPrefix+subaccountID] 56 if !found { 57 ver, found = config.Data[globalAccountPrefix+globalAccountID] 58 return ver, found, nil 59 } 60 61 return ver, found, nil 62 }