forked from submariner-io/submariner-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall.go
156 lines (126 loc) · 5.31 KB
/
firewall.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
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 diagnose
import (
"context"
"github.com/spf13/cobra"
"github.com/submariner-io/submariner-operator/pkg/internal/cli"
"github.com/submariner-io/submariner-operator/pkg/subctl/cmd"
subv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/submariner-io/submariner-operator/pkg/subctl/resource"
)
var diagnoseFirewallConfigCmd = &cobra.Command{
Use: "firewall",
Short: "Check the firewall configuration",
Long: "This command checks if the firewall is configured as per Submariner pre-requisites.",
}
var validationTimeout uint
func addDiagnoseFWConfigFlags(command *cobra.Command) {
command.Flags().UintVar(&validationTimeout, "validation-timeout", 90,
"timeout in seconds while validating the connection attempt")
addNamespaceFlag(command)
}
func init() {
diagnoseCmd.AddCommand(diagnoseFirewallConfigCmd)
}
func spawnSnifferPodOnGatewayNode(client kubernetes.Interface, namespace, podCommand string) (*resource.NetworkPod, error) {
scheduling := resource.PodScheduling{ScheduleOn: resource.GatewayNode, Networking: resource.HostNetworking}
return spawnPod(client, scheduling, "validate-sniffer", namespace, podCommand)
}
func spawnSnifferPodOnNode(client kubernetes.Interface, nodeName, namespace, podCommand string) (*resource.NetworkPod, error) {
scheduling := resource.PodScheduling{ScheduleOn: resource.CustomNode, NodeName: nodeName,
Networking: resource.HostNetworking}
return spawnPod(client, scheduling, "validate-sniffer", namespace, podCommand)
}
func spawnClientPodOnNonGatewayNode(client kubernetes.Interface, namespace, podCommand string) (*resource.NetworkPod, error) {
scheduling := resource.PodScheduling{ScheduleOn: resource.NonGatewayNode, Networking: resource.PodNetworking}
return spawnPod(client, scheduling, "validate-client", namespace, podCommand)
}
func spawnPod(client kubernetes.Interface, scheduling resource.PodScheduling, podName, namespace,
podCommand string) (*resource.NetworkPod, error) {
pod, err := resource.SchedulePod(&resource.PodConfig{
Name: podName,
ClientSet: client,
Scheduling: scheduling,
Namespace: namespace,
Command: podCommand,
})
if err != nil {
return nil, err
}
return pod, nil
}
func getActiveGatewayNodeName(cluster *cmd.Cluster, hostname string, status *cli.Status) string {
nodes, err := cluster.KubeClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
LabelSelector: "submariner.io/gateway=true",
})
if err != nil {
status.EndWithFailure("Error obtaining the Gateway Nodes in cluster %q: %v", cluster.Name, err)
return ""
}
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(cluster.KubeClient, node.Name, "default", "hostname")
if err != nil {
status.EndWithFailure("Error spawning the sniffer pod on the node %q: %v", node.Name, err)
return ""
}
defer sPod.DeletePod()
if err = sPod.AwaitPodCompletion(); err != nil {
status.EndWithFailure("Error waiting for the sniffer pod to finish its execution on node %q: %v", node.Name, err)
return ""
}
if sPod.PodOutput[:len(sPod.PodOutput)-1] == hostname {
return node.Name
}
}
status.EndWithFailure("Could not find the active Gateway node %q in local cluster in cluster %q",
hostname, cluster.Name)
return ""
}
func getLocalEndpointResource(cluster *cmd.Cluster, status *cli.Status) *subv1.Endpoint {
endpoints, err := cluster.SubmClient.SubmarinerV1().Endpoints(cmd.OperatorNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
status.EndWithFailure("Error obtaining the Endpoints in cluster %q: %v", cluster.Name, err)
return nil
}
for _, endpoint := range endpoints.Items {
if endpoint.Spec.ClusterID == cluster.Name {
return &endpoint
}
}
status.EndWithFailure("Could not find the local Endpoint in cluster %q", cluster.Name)
return nil
}
func getAnyRemoteEndpointResource(cluster *cmd.Cluster, status *cli.Status) *subv1.Endpoint {
endpoints, err := cluster.SubmClient.SubmarinerV1().Endpoints(cmd.OperatorNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
status.EndWithFailure("Error obtaining the Endpoints in cluster %q: %v", cluster.Name, err)
return nil
}
for _, endpoint := range endpoints.Items {
if endpoint.Spec.ClusterID != cluster.Name {
return &endpoint
}
}
status.EndWithFailure("Could not find any remote Endpoint in cluster %q", cluster.Name)
return nil
}