/
mesh_types.go
163 lines (133 loc) · 4.67 KB
/
mesh_types.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package v1alpha1
import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/consul-k8s/api/common"
capi "github.com/hashicorp/consul/api"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
MeshKubeKind = "mesh"
)
func init() {
SchemeBuilder.Register(&Mesh{}, &MeshList{})
}
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// Mesh is the Schema for the mesh API
// +kubebuilder:printcolumn:name="Synced",type="string",JSONPath=".status.conditions[?(@.type==\"Synced\")].status",description="The sync status of the resource with Consul"
// +kubebuilder:printcolumn:name="Last Synced",type="date",JSONPath=".status.lastSyncedTime",description="The last successful synced time of the resource with Consul"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="The age of the resource"
type Mesh struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec MeshSpec `json:"spec,omitempty"`
Status `json:"status,omitempty"`
}
//+kubebuilder:object:root=true
// MeshList contains a list of Mesh
type MeshList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Mesh `json:"items"`
}
// MeshSpec defines the desired state of Mesh
type MeshSpec struct {
TransparentProxy TransparentProxyMeshConfig `json:"transparentProxy,omitempty"`
}
// TransparentProxyMeshConfig controls configuration specific to proxies in "transparent" mode. Added in v1.10.0.
type TransparentProxyMeshConfig struct {
// MeshDestinationsOnly determines whether sidecar proxies operating in "transparent" mode can proxy traffic
// to IP addresses not registered in Consul's catalog. If enabled, traffic will only be proxied to upstreams
// with service registrations in the catalog.
MeshDestinationsOnly bool `json:"meshDestinationsOnly,omitempty"`
}
func (in *TransparentProxyMeshConfig) toConsul() capi.TransparentProxyMeshConfig {
return capi.TransparentProxyMeshConfig{MeshDestinationsOnly: in.MeshDestinationsOnly}
}
func (in *Mesh) GetObjectMeta() metav1.ObjectMeta {
return in.ObjectMeta
}
func (in *Mesh) AddFinalizer(name string) {
in.ObjectMeta.Finalizers = append(in.Finalizers(), name)
}
func (in *Mesh) RemoveFinalizer(name string) {
var newFinalizers []string
for _, oldF := range in.Finalizers() {
if oldF != name {
newFinalizers = append(newFinalizers, oldF)
}
}
in.ObjectMeta.Finalizers = newFinalizers
}
func (in *Mesh) Finalizers() []string {
return in.ObjectMeta.Finalizers
}
func (in *Mesh) ConsulKind() string {
return capi.MeshConfig
}
func (in *Mesh) ConsulMirroringNS() string {
return common.DefaultConsulNamespace
}
func (in *Mesh) KubeKind() string {
return MeshKubeKind
}
func (in *Mesh) SyncedCondition() (status corev1.ConditionStatus, reason, message string) {
cond := in.Status.GetCondition(ConditionSynced)
if cond == nil {
return corev1.ConditionUnknown, "", ""
}
return cond.Status, cond.Reason, cond.Message
}
func (in *Mesh) SyncedConditionStatus() corev1.ConditionStatus {
cond := in.Status.GetCondition(ConditionSynced)
if cond == nil {
return corev1.ConditionUnknown
}
return cond.Status
}
func (in *Mesh) ConsulName() string {
return in.ObjectMeta.Name
}
func (in *Mesh) ConsulGlobalResource() bool {
return true
}
func (in *Mesh) KubernetesName() string {
return in.ObjectMeta.Name
}
func (in *Mesh) SetSyncedCondition(status corev1.ConditionStatus, reason string, message string) {
in.Status.Conditions = Conditions{
{
Type: ConditionSynced,
Status: status,
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: message,
},
}
}
func (in *Mesh) SetLastSyncedTime(time *metav1.Time) {
in.Status.LastSyncedTime = time
}
func (in *Mesh) ToConsul(datacenter string) capi.ConfigEntry {
return &capi.MeshConfigEntry{
TransparentProxy: in.Spec.TransparentProxy.toConsul(),
Meta: meta(datacenter),
}
}
func (in *Mesh) MatchesConsul(candidate capi.ConfigEntry) bool {
configEntry, ok := candidate.(*capi.MeshConfigEntry)
if !ok {
return false
}
// No datacenter is passed to ToConsul as we ignore the Meta field when checking for equality.
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.MeshConfigEntry{}, "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty())
}
func (in *Mesh) Validate(_ bool) error {
return nil
}
// DefaultNamespaceFields has no behaviour here as meshes have no namespace specific fields.
func (in *Mesh) DefaultNamespaceFields(_ bool, _ string, _ bool, _ string) {
return
}