-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow exposing gateway on nodeport service - status updates (#1392)
* allow exposing gateway on nodeport service - status updates Signed-off-by: Shubham Chauhan <[email protected]> * ut fix Signed-off-by: Shubham Chauhan <[email protected]> * var/file updates Signed-off-by: Shubham Chauhan <[email protected]> * store test rbac update Signed-off-by: Shubham Chauhan <[email protected]> --------- Signed-off-by: Shubham Chauhan <[email protected]>
- Loading branch information
1 parent
a66d864
commit f099e47
Showing
11 changed files
with
203 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ rules: | |
- "" | ||
resources: | ||
- namespaces | ||
- nodes | ||
- secrets | ||
- services | ||
verbs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package kubernetes | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type nodeDetails struct { | ||
name string | ||
address string | ||
} | ||
|
||
// kubernetesProviderStore holds cached information for the kubernetes provider. | ||
type kubernetesProviderStore struct { | ||
// nodes holds information required for updating Gateway status with the Node | ||
// addresses, in case the Gateway is exposed on every Node of the cluster, using | ||
// Service of type NodePort. | ||
nodes map[string]nodeDetails | ||
} | ||
|
||
func newProviderStore() *kubernetesProviderStore { | ||
return &kubernetesProviderStore{ | ||
nodes: make(map[string]nodeDetails), | ||
} | ||
} | ||
|
||
func (p *kubernetesProviderStore) addNode(n *corev1.Node) { | ||
details := nodeDetails{name: n.Name} | ||
|
||
var internalIP, externalIP string | ||
for _, addr := range n.Status.Addresses { | ||
if addr.Type == corev1.NodeExternalIP { | ||
externalIP = addr.Address | ||
} | ||
if addr.Type == corev1.NodeInternalIP { | ||
internalIP = addr.Address | ||
} | ||
} | ||
|
||
// In certain scenarios (like in local KinD clusters), the Node | ||
// externalIP is not provided, in that case we default back | ||
// to the internalIP of the Node. | ||
if externalIP != "" { | ||
details.address = externalIP | ||
} else if internalIP != "" { | ||
details.address = internalIP | ||
} | ||
p.nodes[n.Name] = details | ||
} | ||
|
||
func (p *kubernetesProviderStore) removeNode(n *corev1.Node) { | ||
delete(p.nodes, n.Name) | ||
} | ||
|
||
func (p *kubernetesProviderStore) listNodeAddresses() []string { | ||
addrs := []string{} | ||
for _, n := range p.nodes { | ||
if n.address != "" { | ||
addrs = append(addrs, n.address) | ||
} | ||
} | ||
return addrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package kubernetes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestNodeDetailsAddressStore(t *testing.T) { | ||
store := newProviderStore() | ||
testCases := []struct { | ||
name string | ||
nodeObject *corev1.Node | ||
expectedAddresses []string | ||
}{ | ||
{ | ||
name: "No node addresses", | ||
nodeObject: &corev1.Node{ | ||
ObjectMeta: v1.ObjectMeta{Name: "node1"}, | ||
Status: corev1.NodeStatus{Addresses: []corev1.NodeAddress{{}}}, | ||
}, | ||
expectedAddresses: []string{}, | ||
}, | ||
{ | ||
name: "only external address", | ||
nodeObject: &corev1.Node{ | ||
ObjectMeta: v1.ObjectMeta{Name: "node1"}, | ||
Status: corev1.NodeStatus{Addresses: []corev1.NodeAddress{{ | ||
Address: "1.1.1.1", | ||
Type: corev1.NodeExternalIP, | ||
}}}, | ||
}, | ||
expectedAddresses: []string{"1.1.1.1"}, | ||
}, | ||
{ | ||
name: "only internal address", | ||
nodeObject: &corev1.Node{ | ||
ObjectMeta: v1.ObjectMeta{Name: "node1"}, | ||
Status: corev1.NodeStatus{Addresses: []corev1.NodeAddress{{ | ||
Address: "1.1.1.1", | ||
Type: corev1.NodeInternalIP, | ||
}}}, | ||
}, | ||
expectedAddresses: []string{"1.1.1.1"}, | ||
}, | ||
{ | ||
name: "prefer external address", | ||
nodeObject: &corev1.Node{ | ||
ObjectMeta: v1.ObjectMeta{Name: "node1"}, | ||
Status: corev1.NodeStatus{Addresses: []corev1.NodeAddress{ | ||
{ | ||
Address: "1.1.1.1", | ||
Type: corev1.NodeExternalIP, | ||
}, | ||
{ | ||
Address: "2.2.2.2", | ||
Type: corev1.NodeInternalIP, | ||
}, | ||
}}, | ||
}, | ||
expectedAddresses: []string{"1.1.1.1"}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
store.addNode(tc.nodeObject) | ||
assert.Equal(t, tc.expectedAddresses, store.listNodeAddresses()) | ||
store.removeNode(tc.nodeObject) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters