diff --git a/Documentation/kubernetes.md b/Documentation/kubernetes.md index 09e5d9e78..166e81765 100644 --- a/Documentation/kubernetes.md +++ b/Documentation/kubernetes.md @@ -31,7 +31,8 @@ If you want to deploy `flannel` securely in a shared namespace or want more fine Other options include [Kyverno](https://kyverno.io/policies/pod-security/) and [OPA Gatekeeper](https://github.com/open-policy-agent/gatekeeper). # Annotations -* `flannel.alpha.coreos.com/public-ip-overwrite`: Allows to overwrite the public IP of a node. Useful if the public IP can not determined from the node, e.G. because it is behind a NAT. It can be automatically set to a nodes `ExternalIP` using the [flannel-node-annotator](https://github.com/alvaroaleman/flannel-node-annotator) +* `flannel.alpha.coreos.com/public-ip`, `flannel.alpha.coreos.com/public-ipv6`: Define the used public IP of the node. If configured when Flannel starts it'll be used as the `public-ip` and `public-ipv6` flag. +* `flannel.alpha.coreos.com/public-ip-overwrite`, `flannel.alpha.coreos.com/public-ipv6-overwrite`: Allows to overwrite the public IP of a node. Useful if the public IP can not determined from the node, e.G. because it is behind a NAT. It can be automatically set to a nodes `ExternalIP` using the [flannel-node-annotator](https://github.com/alvaroaleman/flannel-node-annotator) ## Older versions of Kubernetes diff --git a/main.go b/main.go index 019b995a5..5588267b0 100644 --- a/main.go +++ b/main.go @@ -262,6 +262,15 @@ func main() { // Work out which interface to use var extIface *backend.ExternalInterface + + annotatedPublicIP, annotatedPublicIPv6 := sm.GetStoredPublicIP(ctx) + if annotatedPublicIP != "" { + opts.publicIP = annotatedPublicIP + } + if annotatedPublicIPv6 != "" { + opts.publicIPv6 = annotatedPublicIPv6 + } + optsPublicIP := ipmatch.PublicIPOpts{ PublicIP: opts.publicIP, PublicIPv6: opts.publicIPv6, diff --git a/pkg/subnet/etcd/local_manager.go b/pkg/subnet/etcd/local_manager.go index ca84ad355..bf90b9192 100644 --- a/pkg/subnet/etcd/local_manager.go +++ b/pkg/subnet/etcd/local_manager.go @@ -83,6 +83,10 @@ func (m *LocalManager) GetStoredMacAddresses(ctx context.Context) (string, strin return "", "" } +func (m *LocalManager) GetStoredPublicIP(ctx context.Context) (string, string) { + return "", "" +} + func (m *LocalManager) GetNetworkConfig(ctx context.Context) (*subnet.Config, error) { cfg, err := m.registry.getNetworkConfig(ctx) if err != nil { diff --git a/pkg/subnet/kube/kube.go b/pkg/subnet/kube/kube.go index 671330a98..87c3026f9 100644 --- a/pkg/subnet/kube/kube.go +++ b/pkg/subnet/kube/kube.go @@ -641,3 +641,22 @@ func (ksm *kubeSubnetManager) GetStoredMacAddresses(ctx context.Context) (string return "", "" } + +// GetStoredPublicIP reads if there are any public IP configured as annotation when flannel starts +func (ksm *kubeSubnetManager) GetStoredPublicIP(ctx context.Context) (string, string) { + // get mac info from Name func. + node, err := ksm.client.CoreV1().Nodes().Get(ctx, ksm.nodeName, metav1.GetOptions{}) + if err != nil { + log.Errorf("Failed to get node for backend data: %v", err) + return "", "" + } + + if node != nil && node.Annotations != nil { + log.Infof("List of node(%s) annotations: %#+v", ksm.nodeName, node.Annotations) + publicIP := node.Annotations[ksm.annotations.BackendPublicIP] + publicIPv6 := node.Annotations[ksm.annotations.BackendPublicIPv6] + return publicIP, publicIPv6 + } + + return "", "" +} diff --git a/pkg/subnet/subnet.go b/pkg/subnet/subnet.go index 3d5be7b84..eaa5a393e 100644 --- a/pkg/subnet/subnet.go +++ b/pkg/subnet/subnet.go @@ -115,6 +115,7 @@ type Manager interface { CompleteLease(ctx context.Context, lease *lease.Lease, wg *sync.WaitGroup) error GetStoredMacAddresses(ctx context.Context) (string, string) + GetStoredPublicIP(ctx context.Context) (string, string) Name() string }