github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtime/opt_components_service.go (about) 1 package runtime 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/kyma-project/kyma-environment-broker/internal" 8 ) 9 10 // ComponentDisabler disables component form the given list and returns modified list 11 type ComponentDisabler interface { 12 Disable(components internal.ComponentConfigurationInputList) internal.ComponentConfigurationInputList 13 } 14 15 // ComponentsDisablers represents type for defining components disabler list 16 type ComponentsDisablers map[string]ComponentDisabler 17 18 // OptionalComponentsService provides functionality for executing component disablers 19 type OptionalComponentsService struct { 20 registered map[string]ComponentDisabler 21 } 22 23 // NewOptionalComponentsService returns new instance of ResourceSupervisorAggregator 24 func NewOptionalComponentsService(initialList ComponentsDisablers) *OptionalComponentsService { 25 return &OptionalComponentsService{ 26 registered: initialList, 27 } 28 } 29 30 // GetAllOptionalComponentsNames returns list of registered components disablers names 31 func (f *OptionalComponentsService) GetAllOptionalComponentsNames() []string { 32 var names []string 33 for name := range f.registered { 34 names = append(names, name) 35 } 36 37 return names 38 } 39 40 // AddComponentToDisable adds a component to the list of registered components disablers names 41 func (f *OptionalComponentsService) AddComponentToDisable(name string, disabler ComponentDisabler) { 42 f.registered[name] = disabler 43 } 44 45 // ExecuteDisablers executes disablers on given input and returns modified list. 46 // 47 // BE AWARE: in current implementation the input is also modified. 48 func (f *OptionalComponentsService) ExecuteDisablers(components internal.ComponentConfigurationInputList, names ...string) (internal.ComponentConfigurationInputList, error) { 49 var filterOut = components 50 for _, name := range names { 51 concreteDisabler, exists := f.registered[name] 52 if !exists { 53 return nil, fmt.Errorf("disabler for component %s was not found", name) 54 } 55 56 filterOut = concreteDisabler.Disable(filterOut) 57 } 58 59 return filterOut, nil 60 } 61 62 // ComputeComponentsToDisable returns disabler names that needs to be executed. 63 // Comparing components names as case insensitive. 64 func (f *OptionalComponentsService) ComputeComponentsToDisable(optComponentsToKeep []string) []string { 65 var ( 66 allOptComponents = f.GetAllOptionalComponentsNames() 67 optComponentsToInstall = toNormalizedMap(optComponentsToKeep) 68 optComponentsToDisable []string 69 ) 70 71 for _, name := range allOptComponents { 72 if _, found := optComponentsToInstall[strings.ToLower(name)]; found { 73 continue 74 } 75 optComponentsToDisable = append(optComponentsToDisable, name) 76 } 77 return optComponentsToDisable 78 } 79 80 func toNormalizedMap(in []string) map[string]struct{} { 81 out := map[string]struct{}{} 82 83 for _, entry := range in { 84 out[strings.ToLower(entry)] = struct{}{} 85 } 86 87 return out 88 }