forked from submariner-io/submariner-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
229 lines (187 loc) · 6.95 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
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
224
225
226
227
228
229
/*
© 2021 Red Hat, Inc. and others
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 cmd
import (
"bytes"
"fmt"
"io/ioutil"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1opts "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/submariner-io/admiral/pkg/resource"
submarinerv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
subClientsetv1 "github.com/submariner-io/submariner/pkg/client/clientset/versioned"
"github.com/submariner-io/submariner-operator/apis/submariner/v1alpha1"
subOperatorClientset "github.com/submariner-io/submariner-operator/pkg/client/clientset/versioned"
"github.com/submariner-io/submariner-operator/pkg/subctl/operator/submarinercr"
)
func getMultipleRestConfigs(kubeConfigPath string, kubeContexts []string) ([]restConfig, error) {
var restConfigs []restConfig
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.DefaultClientConfig = &clientcmd.DefaultClientConfig
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
rules.ExplicitPath = kubeConfigPath
contexts := []string{}
if len(kubeContexts) > 0 {
contexts = append(contexts, kubeContexts...)
} else {
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides)
rawConfig, err := kubeConfig.RawConfig()
if err != nil {
return restConfigs, err
}
for context := range rawConfig.Contexts {
contexts = append(contexts, context)
}
}
for _, context := range contexts {
if context != "" {
overrides.CurrentContext = context
config, err := getClientConfigAndClusterName(rules, overrides)
if err != nil {
return nil, err
}
restConfigs = append(restConfigs, config)
}
}
return restConfigs, nil
}
func getSubmarinerResourceWithError(config *rest.Config) (*v1alpha1.Submariner, error) {
submarinerClient, err := subOperatorClientset.NewForConfig(config)
if err != nil {
return nil, err
}
submariner, err := submarinerClient.SubmarinerV1alpha1().Submariners(OperatorNamespace).
Get(submarinercr.SubmarinerName, v1opts.GetOptions{})
if err != nil {
return nil, err
}
return submariner, nil
}
func getSubmarinerResource(config *rest.Config) *v1alpha1.Submariner {
submariner, err := getSubmarinerResourceWithError(config)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
exitOnError("Error obtaining the Submariner resource", err)
}
return submariner
}
func getLocalEndpointResource(config *rest.Config, clusterID string) *submarinerv1.Endpoint {
submarinerClient, err := subClientsetv1.NewForConfig(config)
exitOnError("Unable to get the Submariner client", err)
endpoints, err := submarinerClient.SubmarinerV1().Endpoints(OperatorNamespace).List(v1opts.ListOptions{})
if err != nil {
exitOnError(fmt.Sprintf("Error obtaining the Endpoints in the cluster %q", clusterID), err)
}
for _, endpoint := range endpoints.Items {
if endpoint.Spec.ClusterID == clusterID {
return &endpoint
}
}
return nil
}
func getActiveGatewayNodeName(clientSet *kubernetes.Clientset, hostname string) string {
nodes, err := clientSet.CoreV1().Nodes().List(v1opts.ListOptions{})
if err != nil {
exitOnError("Error listing the Nodes in the local cluster", err)
}
for _, node := range nodes.Items {
if node.Name == hostname {
return hostname
}
// On some platforms, the nodeName does not match with the hostname.
// Submariner Endpoint stores the hostname info in the endpoint and not the nodeName. So, we spawn a
// tiny pod to read the hostname and return the corresponding node.
sPod, err := spawnSnifferPodOnNode(clientSet, node.Name, "default", "hostname")
if err != nil {
return ""
}
defer sPod.DeletePod()
if err = sPod.AwaitPodCompletion(); err != nil {
return ""
}
if sPod.PodOutput[:len(sPod.PodOutput)-1] == hostname {
return node.Name
}
}
return ""
}
func getAnyRemoteEndpointResource(config *rest.Config, clusterID string) *submarinerv1.Endpoint {
submarinerClient, err := subClientsetv1.NewForConfig(config)
exitOnError("Unable to get the Submariner client", err)
endpoints, err := submarinerClient.SubmarinerV1().Endpoints(OperatorNamespace).List(v1opts.ListOptions{})
if err != nil {
return nil
}
for _, endpoint := range endpoints.Items {
if endpoint.Spec.ClusterID != clusterID {
return &endpoint
}
}
return nil
}
func getGatewaysResource(config *rest.Config) *submarinerv1.GatewayList {
submarinerClient, err := subClientsetv1.NewForConfig(config)
exitOnError("Unable to get the Submariner client", err)
gateways, err := submarinerClient.SubmarinerV1().Gateways(OperatorNamespace).
List(v1opts.ListOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
exitOnError("Error obtaining the Gateways resource", err)
}
return gateways
}
func getBrokerRestConfigAndNamespace(submariner *v1alpha1.Submariner,
serviceDisc *v1alpha1.ServiceDiscovery) (*rest.Config, string, error) {
if submariner != nil {
// Try to authorize against the submariner Cluster resource as we know the CRD should exist and the credentials
// should allow read access.
restConfig, _, err := resource.GetAuthorizedRestConfig(submariner.Spec.BrokerK8sApiServer, submariner.Spec.BrokerK8sApiServerToken,
submariner.Spec.BrokerK8sCA, rest.TLSClientConfig{}, schema.GroupVersionResource{
Group: submarinerv1.SchemeGroupVersion.Group,
Version: submarinerv1.SchemeGroupVersion.Version,
Resource: "clusters",
}, submariner.Spec.BrokerK8sRemoteNamespace)
return restConfig, submariner.Spec.BrokerK8sRemoteNamespace, err
}
if serviceDisc != nil {
// Try to authorize against the ServiceImport resource as we know the CRD should exist and the credentials
// should allow read access.
restConfig, _, err := resource.GetAuthorizedRestConfig(serviceDisc.Spec.BrokerK8sApiServer, serviceDisc.Spec.BrokerK8sApiServerToken,
serviceDisc.Spec.BrokerK8sCA, rest.TLSClientConfig{}, schema.GroupVersionResource{
Group: "multicluster.x-k8s.io",
Version: "v1alpha1",
Resource: "serviceimports",
}, serviceDisc.Spec.BrokerK8sRemoteNamespace)
return restConfig, serviceDisc.Spec.BrokerK8sRemoteNamespace, err
}
return nil, "", nil
}
func compareFiles(file1, file2 string) (bool, error) {
first, err := ioutil.ReadFile(file1)
if err != nil {
return false, err
}
second, err := ioutil.ReadFile(file2)
if err != nil {
return false, err
}
return bytes.Equal(first, second), nil
}