/
mapping.go
96 lines (81 loc) · 3.19 KB
/
mapping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package access
import (
"github.com/kyma-project/kyma/components/application-broker/internal"
"github.com/kyma-project/kyma/components/application-broker/pkg/apis/applicationconnector/v1alpha1"
emListers "github.com/kyma-project/kyma/components/application-broker/pkg/client/listers/applicationconnector/v1alpha1"
"github.com/pkg/errors"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
)
// ServiceEnabledChecker provides a method to check if a Service is enabled.
type ServiceEnabledChecker interface {
// IsServiceEnabled returns true if the service is enabled
IsServiceEnabled(svc internal.Service) bool
}
// NewApplicationMappingService creates new instance of NewApplicationMappingService
func NewApplicationMappingService(lister emListers.ApplicationMappingLister) *ApplicationMappingService {
return &ApplicationMappingService{
lister: lister,
}
}
// ApplicationMappingService provides methods which checks access based on ApplicationMapping objects.
type ApplicationMappingService struct {
lister emListers.ApplicationMappingLister
}
// IsApplicationEnabled checks, if ApplicationMapping with given name in the namespace exists
func (c *ApplicationMappingService) IsApplicationEnabled(namespace, name string) (bool, error) {
_, err := c.lister.ApplicationMappings(namespace).Get(name)
switch {
case err == nil:
return true, nil
case k8sErrors.IsNotFound(err):
return false, nil
default:
return false, errors.Wrapf(err, "while getting ApplicationMapping %s/%s", namespace, name)
}
}
// NewServiceChecker creates a ServiceEnabledChecker which is able to check if a service of an application
// is enabled in the namespace.
func (c *ApplicationMappingService) NewServiceChecker(namespace, name string) (ServiceEnabledChecker, error) {
am, err := c.lister.ApplicationMappings(namespace).Get(name)
switch {
case err == nil:
return newApplicationServiceChecker(am), nil
case k8sErrors.IsNotFound(err):
return &allServicesDisabled{}, nil
default:
return nil, errors.Wrapf(err, "while getting ApplicationMapping %s/%s", namespace, name)
}
}
func newApplicationServiceChecker(am *v1alpha1.ApplicationMapping) ServiceEnabledChecker {
if am.IsAllApplicationServicesEnabled() {
return &allServicesEnabled{}
}
serviceIDs := make(map[internal.ApplicationServiceID]struct{})
for _, svc := range am.Spec.Services {
serviceIDs[internal.ApplicationServiceID(svc.ID)] = struct{}{}
}
return &applicationServiceChecker{
serviceIDs: serviceIDs,
}
}
// applicationServiceChecker is a default ServiceEnabledChecker implementation
type applicationServiceChecker struct {
serviceIDs map[internal.ApplicationServiceID]struct{}
}
// IsServiceEnabled returns true if the service is enabled
func (c *applicationServiceChecker) IsServiceEnabled(svc internal.Service) bool {
_, exists := c.serviceIDs[svc.ID]
return exists
}
type allServicesDisabled struct {
}
// IsServiceEnabled implements ServiceEnabledChecker, always returns false
func (c *allServicesDisabled) IsServiceEnabled(svc internal.Service) bool {
return false
}
type allServicesEnabled struct {
}
// IsServiceEnabled implements ServiceEnabledChecker, always returns true
func (c *allServicesEnabled) IsServiceEnabled(svc internal.Service) bool {
return true
}