2018-10-29 16:01:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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 settings
|
|
|
|
|
|
|
|
import (
|
2020-03-24 13:44:13 +00:00
|
|
|
"context"
|
2018-10-29 16:01:41 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-07-31 16:16:28 +00:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2019-05-08 00:21:13 +00:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
2023-02-16 14:05:40 +00:00
|
|
|
v1 "k8s.io/api/networking/v1"
|
2018-10-29 16:01:41 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.IngressNginxDescribe("[Status] status update", func() {
|
2018-10-29 16:01:41 +00:00
|
|
|
f := framework.NewDefaultFramework("status-update")
|
|
|
|
host := "status-update"
|
|
|
|
address := getHostIP()
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should update status field after client-go reconnection", func() {
|
2018-10-29 16:01:41 +00:00
|
|
|
port, cmd, err := f.KubectlProxy(0)
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error starting kubectl proxy")
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2020-09-26 23:27:19 +00:00
|
|
|
err = f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error {
|
|
|
|
args := []string{}
|
|
|
|
// flags --publish-service and --publish-status-address are mutually exclusive
|
2020-02-16 14:58:37 +00:00
|
|
|
|
2020-09-26 23:27:19 +00:00
|
|
|
for _, v := range deployment.Spec.Template.Spec.Containers[0].Args {
|
|
|
|
if strings.Contains(v, "--publish-service") {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-16 14:58:37 +00:00
|
|
|
|
2020-09-26 23:27:19 +00:00
|
|
|
if strings.Contains(v, "--update-status") {
|
|
|
|
continue
|
2018-10-29 16:01:41 +00:00
|
|
|
}
|
2020-02-16 14:58:37 +00:00
|
|
|
|
2020-09-26 23:27:19 +00:00
|
|
|
args = append(args, v)
|
|
|
|
}
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2020-09-26 23:27:19 +00:00
|
|
|
args = append(args, fmt.Sprintf("--apiserver-host=http://%s:%d", address.String(), port))
|
|
|
|
args = append(args, "--publish-status-address=1.1.0.0")
|
|
|
|
|
|
|
|
deployment.Spec.Template.Spec.Containers[0].Args = args
|
|
|
|
_, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
|
|
|
|
return err
|
|
|
|
})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error updating ingress controller deployment flags")
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2022-02-02 13:12:22 +00:00
|
|
|
f.NewEchoDeployment()
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2023-07-03 12:50:52 +00:00
|
|
|
f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil))
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxConfiguration(
|
2018-10-29 16:01:41 +00:00
|
|
|
func(cfg string) bool {
|
|
|
|
return strings.Contains(cfg, fmt.Sprintf("server_name %s", host))
|
|
|
|
})
|
|
|
|
|
|
|
|
framework.Logf("waiting for leader election and initial status update")
|
2020-07-01 21:19:51 +00:00
|
|
|
framework.Sleep(30 * time.Second)
|
2018-10-29 16:01:41 +00:00
|
|
|
|
|
|
|
err = cmd.Process.Kill()
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error terminating kubectl proxy")
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2023-07-03 12:50:52 +00:00
|
|
|
ing, err := f.KubeClientSet.NetworkingV1().Ingresses(f.Namespace).Get(context.TODO(), host, metav1.GetOptions{})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error getting %s/%v Ingress", f.Namespace, host)
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2023-01-18 13:10:34 +00:00
|
|
|
ing.Status.LoadBalancer.Ingress = []v1.IngressLoadBalancerIngress{}
|
2021-08-21 20:42:00 +00:00
|
|
|
_, err = f.KubeClientSet.NetworkingV1().Ingresses(f.Namespace).UpdateStatus(context.TODO(), ing, metav1.UpdateOptions{})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error cleaning Ingress status")
|
2020-07-01 21:19:51 +00:00
|
|
|
framework.Sleep(10 * time.Second)
|
2018-10-29 16:01:41 +00:00
|
|
|
|
2022-08-22 22:38:16 +00:00
|
|
|
err = f.KubeClientSet.CoordinationV1().
|
|
|
|
Leases(f.Namespace).
|
2021-08-21 20:42:00 +00:00
|
|
|
Delete(context.TODO(), "ingress-controller-leader", metav1.DeleteOptions{})
|
2022-08-22 22:38:16 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error deleting leader election lease")
|
2018-10-29 16:01:41 +00:00
|
|
|
|
|
|
|
_, cmd, err = f.KubectlProxy(port)
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error starting kubectl proxy")
|
2018-10-29 16:01:41 +00:00
|
|
|
defer func() {
|
2020-07-31 03:59:09 +00:00
|
|
|
defer ginkgo.GinkgoRecover()
|
|
|
|
|
2018-10-29 16:01:41 +00:00
|
|
|
if cmd != nil {
|
|
|
|
err := cmd.Process.Kill()
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error terminating kubectl proxy")
|
2018-10-29 16:01:41 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
//nolint:staticcheck // TODO: will replace it since wait.Poll is deprecated
|
2020-02-16 18:27:58 +00:00
|
|
|
err = wait.Poll(5*time.Second, 4*time.Minute, func() (done bool, err error) {
|
2021-08-21 20:42:00 +00:00
|
|
|
ing, err = f.KubeClientSet.NetworkingV1().Ingresses(f.Namespace).Get(context.TODO(), host, metav1.GetOptions{})
|
2018-10-29 16:01:41 +00:00
|
|
|
if err != nil {
|
2019-02-22 14:03:42 +00:00
|
|
|
return false, nil
|
2018-10-29 16:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ing.Status.LoadBalancer.Ingress) != 1 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error waiting for ingress status")
|
2023-01-18 13:10:34 +00:00
|
|
|
assert.Equal(ginkgo.GinkgoT(), ing.Status.LoadBalancer.Ingress, []v1.IngressLoadBalancerIngress{
|
2018-10-29 16:01:41 +00:00
|
|
|
{IP: "1.1.0.0"},
|
2023-01-18 13:10:34 +00:00
|
|
|
})
|
2018-10-29 16:01:41 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
func getHostIP() net.IP {
|
|
|
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
localAddr, ok := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
assert.True(ginkgo.GinkgoT(), ok, "unexpected type: %T", conn.LocalAddr())
|
2018-10-29 16:01:41 +00:00
|
|
|
|
|
|
|
return localAddr.IP
|
|
|
|
}
|