forked from yamt/midonet-kubernetes
/
global.go
129 lines (118 loc) · 4.04 KB
/
global.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
// Copyright (C) 2018 Midokura SARL.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package converter
import (
"github.com/google/uuid"
mncli "github.com/midonet/midonet-kubernetes/pkg/client/clientset/versioned"
"github.com/midonet/midonet-kubernetes/pkg/midonet"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
)
const (
// A pseudo kind for global resources.
midonetGlobalKind = "midonet-global"
// The name of the global chain.
mainChainName = "chain"
)
// ServicesChainID is the ID of MidoNet Chain which contains the Rules
// for each services.
func ServicesChainID(config *Config) uuid.UUID {
baseID := MainChainID(config)
return SubID(baseID, "Services Chain")
}
// MainChainID is the ID of MidoNet Chain which contains the Rules
// to dispatch to other global Chains including ServicesChainID.
func MainChainID(config *Config) uuid.UUID {
return IDForKey(midonetGlobalKind, mainChainName, config)
}
// ClusterRouterID is the ID of the cluster router for this deployment.
func ClusterRouterID(config *Config) uuid.UUID {
baseID := MainChainID(config)
return SubID(baseID, "Cluster Router")
}
// DefaultTunnelZoneID is the ID of the default MidoNet Tunnel Zone for
// this deployment.
func DefaultTunnelZoneID(config *Config) uuid.UUID {
baseID := MainChainID(config)
return SubID(baseID, "Default Tunnel Zone")
}
func globalResources(config *Config) map[Key]([]BackendResource) {
tenant := config.Tenant
baseID := MainChainID(config)
mainChainID := baseID
clusterRouterID := ClusterRouterID(config)
tunnelZoneID := DefaultTunnelZoneID(config)
preChainID := SubID(baseID, "Pre Chain")
servicesChainID := ServicesChainID(config)
jumpToPreRuleID := SubID(baseID, "Jump To Pre")
jumpToServicesRuleID := SubID(baseID, "Jump To Services")
revSNATRuleID := SubID(baseID, "Reverse SNAT")
revDNATRuleID := SubID(baseID, "Reverse DNAT")
return map[Key]([]BackendResource){
{Kind: midonetGlobalKind, Name: "tunnel-zone"}: []BackendResource{
&midonet.TunnelZone{
ID: &tunnelZoneID,
Name: "DefaultTunnelZone",
Type: "vxlan",
},
},
{Kind: midonetGlobalKind, Name: "cluster-router"}: []BackendResource{
&midonet.Router{
ID: &clusterRouterID,
Name: "ClusterRouter",
TenantID: tenant,
},
},
// Chains shared among Bridges for Nodes
{Kind: midonetGlobalKind, Name: mainChainName}: []BackendResource{
&midonet.Chain{
ID: &mainChainID,
Name: "KUBE-MAIN",
TenantID: tenant,
},
&midonet.Chain{
ID: &preChainID,
Name: "KUBE-PRE",
TenantID: tenant,
},
&midonet.Chain{
ID: &servicesChainID,
Name: "KUBE-SERVICES",
TenantID: tenant,
},
midonet.JumpRule(&jumpToServicesRuleID, &mainChainID, &servicesChainID),
midonet.JumpRule(&jumpToPreRuleID, &mainChainID, &preChainID),
// Reverse NAT rules for Endpoints.
&midonet.Rule{
Parent: midonet.Parent{ID: &preChainID},
ID: &revDNATRuleID,
Type: "rev_dnat",
FlowAction: "accept",
},
&midonet.Rule{
Parent: midonet.Parent{ID: &preChainID},
ID: &revSNATRuleID,
Type: "rev_snat",
FlowAction: "continue",
},
},
}
}
// EnsureGlobalResources ensures to create Translations for global resources.
func EnsureGlobalResources(mncli mncli.Interface, config *Config, recorder record.EventRecorder) error {
resources := globalResources(config)
updater := NewTranslationUpdater(mncli, recorder)
return updater.Update(schema.GroupVersionKind{}, nil, resources)
}