-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource.go
116 lines (94 loc) · 2.27 KB
/
resource.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package service
import (
"reflect"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
apiv1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
const (
// Name is the identifier of the resource.
Name = "service"
)
const (
httpsPort = 443
masterServiceName = "master"
)
// Config represents the configuration used to create a new service resource.
type Config struct {
K8sClient kubernetes.Interface
Logger micrologger.Logger
}
// Resource implements the service resource.
type Resource struct {
k8sClient kubernetes.Interface
logger micrologger.Logger
}
// New creates a new configured service resource.
func New(config Config) (*Resource, error) {
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
r := &Resource{
k8sClient: config.K8sClient,
logger: config.Logger,
}
return r, nil
}
func (r *Resource) Name() string {
return Name
}
func isServiceModified(a, b *apiv1.Service) bool {
if a == nil || b == nil {
return true
}
if !portsEqual(a, b) {
return true
}
if !reflect.DeepEqual(a.Spec.Type, b.Spec.Type) {
return true
}
if !reflect.DeepEqual(a.Labels, b.Labels) {
return true
}
if !reflect.DeepEqual(a.Annotations, b.Annotations) {
return true
}
return false
}
func toService(v interface{}) (*apiv1.Service, error) {
if v == nil {
return nil, nil
}
service, ok := v.(*apiv1.Service)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", &apiv1.Service{}, v)
}
return service, nil
}
// portsEqual is a function that is checking if ports in the service have same important values.
func portsEqual(a, b *apiv1.Service) bool {
if len(a.Spec.Ports) != len(b.Spec.Ports) {
return false
}
for i := 0; i < len(a.Spec.Ports); i++ {
portA := a.Spec.Ports[i]
portB := b.Spec.Ports[i]
if portA.Name != portB.Name {
return false
}
if !reflect.DeepEqual(portA.Port, portB.Port) {
return false
}
if !reflect.DeepEqual(portA.TargetPort, portB.TargetPort) {
return false
}
if !reflect.DeepEqual(portA.Protocol, portB.Protocol) {
return false
}
}
return true
}