forked from vmware-tanzu/nsx-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
223 lines (195 loc) · 8.66 KB
/
client.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright © 2021 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
package nsx
import (
"errors"
"net/http"
"strings"
"github.com/sirupsen/logrus"
vspherelog "github.com/vmware/vsphere-automation-sdk-go/runtime/log"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
nsx_policy "github.com/vmware/vsphere-automation-sdk-go/services/nsxt"
mpsearch "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/search"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/trust_management"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/trust_management/principal_identities"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains/security_policies"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points"
projects "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects"
infra "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra/realized_state"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/subnets"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/subnets/ip_pools"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/subnets/ports"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/search"
"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/ratelimiter"
)
const (
VPC = iota
SecurityPolicy
ServiceAccount
StaticRoute
AllFeatures
)
var (
FeaturesName = [AllFeatures]string{"VPC", "SECURITY_POLICY", "NSX_SERVICE_ACCOUNT", "STATIC_ROUTE"}
)
type Client struct {
NsxConfig *config.NSXOperatorConfig
RestConnector *client.RestConnector
QueryClient search.QueryClient
GroupClient domains.GroupsClient
SecurityClient domains.SecurityPoliciesClient
RuleClient security_policies.RulesClient
InfraClient nsx_policy.InfraClient
StaticRouteClient vpcs.StaticRoutesClient
VPCClient projects.VpcsClient
IPBlockClient infra.IpBlocksClient
ClusterControlPlanesClient enforcement_points.ClusterControlPlanesClient
SubnetStatusClient subnets.StatusClient
RealizedEntitiesClient realized_state.RealizedEntitiesClient
ProjectInfraClient projects.InfraClient
MPQueryClient mpsearch.QueryClient
CertificatesClient trust_management.CertificatesClient
PrincipalIdentitiesClient trust_management.PrincipalIdentitiesClient
WithCertificateClient principal_identities.WithCertificateClient
PortClient subnets.PortsClient
PortStateClient ports.StateClient
IPPoolClient subnets.IpPoolsClient
IPAllocationClient ip_pools.IpAllocationsClient
OrgRootClient nsx_policy.OrgRootClient
SubnetsClient vpcs.SubnetsClient
RealizedStateClient realized_state.RealizedEntitiesClient
NSXChecker NSXHealthChecker
NSXVerChecker NSXVersionChecker
}
var (
nsx320Version = [3]int64{3, 2, 0}
nsx401Version = [3]int64{4, 0, 1}
nsx410Version = [3]int64{4, 1, 0}
nsx411Version = [3]int64{4, 1, 1}
)
type NSXHealthChecker struct {
cluster *Cluster
}
type NSXVersionChecker struct {
cluster *Cluster
securityPolicySupported bool
nsxServiceAccountSupported bool
vpcSupported bool
featureSupported [AllFeatures]bool
}
func (ck *NSXHealthChecker) CheckNSXHealth(req *http.Request) error {
health := ck.cluster.Health()
if GREEN == health || ORANGE == health {
return nil
} else {
log.V(1).Info("NSX cluster status is down: ", " Current status: ", health)
return errors.New("NSX Current Status is down")
}
}
func restConnector(c *Cluster) *client.RestConnector {
connector, _ := c.NewRestConnector()
return connector
}
func GetClient(cf *config.NSXOperatorConfig) *Client {
// Set log level for vsphere-automation-sdk-go
logger := logrus.New()
vspherelog.SetLogger(logger)
c := NewConfig(strings.Join(cf.NsxApiManagers, ","), cf.NsxApiUser, cf.NsxApiPassword, cf.CaFile, 10, 3, 20, 20, true, true, true, ratelimiter.AIMD, cf.GetTokenProvider(), nil, cf.Thumbprint)
cluster, _ := NewCluster(c)
queryClient := search.NewQueryClient(restConnector(cluster))
groupClient := domains.NewGroupsClient(restConnector(cluster))
securityClient := domains.NewSecurityPoliciesClient(restConnector(cluster))
ruleClient := security_policies.NewRulesClient(restConnector(cluster))
infraClient := nsx_policy.NewInfraClient(restConnector(cluster))
staticRouteClient := vpcs.NewStaticRoutesClient(restConnector(cluster))
vpcClient := projects.NewVpcsClient(restConnector(cluster))
ipBlockClient := infra.NewIpBlocksClient(restConnector(cluster))
clusterControlPlanesClient := enforcement_points.NewClusterControlPlanesClient(restConnector(cluster))
realizedEntitiesClient := realized_state.NewRealizedEntitiesClient(restConnector(cluster))
mpQueryClient := mpsearch.NewQueryClient(restConnector(cluster))
certificatesClient := trust_management.NewCertificatesClient(restConnector(cluster))
principalIdentitiesClient := trust_management.NewPrincipalIdentitiesClient(restConnector(cluster))
withCertificateClient := principal_identities.NewWithCertificateClient(restConnector(cluster))
projectInfraClient := projects.NewInfraClient(restConnector(cluster))
portClient := subnets.NewPortsClient(restConnector(cluster))
portStateClient := ports.NewStateClient(restConnector(cluster))
ipPoolClient := subnets.NewIpPoolsClient(restConnector(cluster))
ipAllocationClient := ip_pools.NewIpAllocationsClient(restConnector(cluster))
orgRootClient := nsx_policy.NewOrgRootClient(restConnector(cluster))
subnetsClient := vpcs.NewSubnetsClient(restConnector(cluster))
subnetStatusClient := subnets.NewStatusClient(restConnector(cluster))
realizedStateClient := realized_state.NewRealizedEntitiesClient(restConnector(cluster))
nsxChecker := &NSXHealthChecker{
cluster: cluster,
}
nsxVersionChecker := &NSXVersionChecker{
cluster: cluster,
featureSupported: [AllFeatures]bool{},
}
nsxClient := &Client{
NsxConfig: cf,
RestConnector: restConnector(cluster),
QueryClient: queryClient,
GroupClient: groupClient,
SecurityClient: securityClient,
RuleClient: ruleClient,
InfraClient: infraClient,
StaticRouteClient: staticRouteClient,
VPCClient: vpcClient,
IPBlockClient: ipBlockClient,
ClusterControlPlanesClient: clusterControlPlanesClient,
RealizedEntitiesClient: realizedEntitiesClient,
MPQueryClient: mpQueryClient,
CertificatesClient: certificatesClient,
PrincipalIdentitiesClient: principalIdentitiesClient,
WithCertificateClient: withCertificateClient,
ProjectInfraClient: projectInfraClient,
PortClient: portClient,
PortStateClient: portStateClient,
SubnetStatusClient: subnetStatusClient,
NSXChecker: *nsxChecker,
NSXVerChecker: *nsxVersionChecker,
IPPoolClient: ipPoolClient,
IPAllocationClient: ipAllocationClient,
OrgRootClient: orgRootClient,
SubnetsClient: subnetsClient,
RealizedStateClient: realizedStateClient,
}
// NSX version check will be restarted during SecurityPolicy reconcile
// So, it's unnecessary to exit even if failed in the first time
if !nsxClient.NSXCheckVersion(SecurityPolicy) {
err := errors.New("SecurityPolicy feature support check failed")
log.Error(err, "initial NSX version check for SecurityPolicy got error")
}
if !nsxClient.NSXCheckVersion(ServiceAccount) {
err := errors.New("NSXServiceAccount feature support check failed")
log.Error(err, "initial NSX version check for NSXServiceAccount got error")
}
return nsxClient
}
func (client *Client) NSXCheckVersion(feature int) bool {
if client.NSXVerChecker.featureSupported[feature] {
return true
}
nsxVersion, err := client.NSXVerChecker.cluster.GetVersion()
if err != nil {
log.Error(err, "get version error")
return false
}
err = nsxVersion.Validate()
if err != nil {
log.Error(err, "validate version error")
return false
}
if !nsxVersion.featureSupported(feature) {
err = errors.New("NSX version check failed")
log.Error(err, FeaturesName[feature]+"feature is not supported", "current version", nsxVersion.NodeVersion)
return false
}
client.NSXVerChecker.featureSupported[feature] = true
return true
}