|
| 1 | +package apiserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "regexp" |
| 8 | + "time" |
| 9 | + |
| 10 | + g "github.com/onsi/ginkgo/v2" |
| 11 | + o "github.com/onsi/gomega" |
| 12 | + |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/util/wait" |
| 15 | + "k8s.io/client-go/util/retry" |
| 16 | + admissionapi "k8s.io/pod-security-admission/api" |
| 17 | + |
| 18 | + configv1 "github.com/openshift/api/config/v1" |
| 19 | + |
| 20 | + exutil "github.com/openshift/origin/test/extended/util" |
| 21 | + "github.com/openshift/origin/test/extended/util/image" |
| 22 | +) |
| 23 | + |
| 24 | +// These tests are duplicating check-endpoints and monitor test functionality. They are required |
| 25 | +// for cert rotation suites where running monitor alongside test is impossible as we need to |
| 26 | +// skew time to emulate the age of the cluster which is disruptive. |
| 27 | +var _ = g.Describe("[Conformance][sig-api-machinery][Feature:APIServer] kube-apiserver should be accessible via", func() { |
| 28 | + defer g.GinkgoRecover() |
| 29 | + oc := exutil.NewCLIWithPodSecurityLevel("apiserver", admissionapi.LevelPrivileged) |
| 30 | + |
| 31 | + for description, apiPath := range map[string]string{ |
| 32 | + "service network": "kubernetes.default.svc", |
| 33 | + "api-int": "api-int", |
| 34 | + "api-ext": "api-ext", |
| 35 | + } { |
| 36 | + g.It(fmt.Sprintf("%s endpoint", description), func() { |
| 37 | + // skip on microshift |
| 38 | + isMicroShift, err := exutil.IsMicroShiftCluster(oc.AdminKubeClient()) |
| 39 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 40 | + if isMicroShift { |
| 41 | + g.Skip("Not supported on Microshift") |
| 42 | + } |
| 43 | + |
| 44 | + // external controlplane topology doesn't have master nodes |
| 45 | + controlPlaneTopology, err := exutil.GetControlPlaneTopology(oc) |
| 46 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 47 | + if *controlPlaneTopology == configv1.ExternalTopologyMode { |
| 48 | + g.Skip("ExternalControlPlaneTopology doesn't have master node kubeconfigs") |
| 49 | + } |
| 50 | + // get external/internal URLs |
| 51 | + infra, err := oc.AdminConfigClient().ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{}) |
| 52 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 53 | + |
| 54 | + if apiPath == "api-ext" { |
| 55 | + externalAPIUrl, err := url.Parse(infra.Status.APIServerURL) |
| 56 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 57 | + apiPath = externalAPIUrl.Host |
| 58 | + } |
| 59 | + if apiPath == "api-int" { |
| 60 | + internalAPIUrl, err := url.Parse(infra.Status.APIServerInternalURL) |
| 61 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 62 | + apiPath = internalAPIUrl.Host |
| 63 | + } |
| 64 | + err = retry.OnError( |
| 65 | + wait.Backoff{ |
| 66 | + Duration: 2 * time.Second, |
| 67 | + Steps: 3, |
| 68 | + Factor: 5.0, |
| 69 | + Jitter: 0.1, |
| 70 | + }, |
| 71 | + func(err error) bool { |
| 72 | + // retry error when kube-apiserver was temporarily unavailable, this matches oc error coming from: |
| 73 | + // https://github.com/kubernetes/kubernetes/blob/cbb5ea8210596ada1efce7e7a271ca4217ae598e/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go#L237-L243 |
| 74 | + matched, _ := regexp.MatchString("The connection to the server .+ was refused - did you specify the right host or port", err.Error()) |
| 75 | + return !matched |
| 76 | + }, |
| 77 | + func() error { |
| 78 | + pod, err := exutil.NewPodExecutor(oc, "kube-apiserver-access", image.ShellImage()) |
| 79 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 80 | + cmd := fmt.Sprintf("curl -kLs https://%s/readyz", apiPath) |
| 81 | + out, err := pod.Exec(cmd) |
| 82 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 83 | + o.Expect(out).To(o.ContainSubstring("ok")) |
| 84 | + return nil |
| 85 | + }) |
| 86 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 87 | + }) |
| 88 | + } |
| 89 | +}) |
0 commit comments