2015-06-20 14:57:07 -04:00
|
|
|
/*
|
2016-06-02 20:25:58 -04:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-06-20 14:57:07 -04:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-02-24 14:32:30 -05:00
|
|
|
package node
|
2015-06-20 14:57:07 -04:00
|
|
|
|
|
|
|
|
import (
|
2020-02-07 21:16:47 -05:00
|
|
|
"context"
|
2015-06-20 14:57:07 -04:00
|
|
|
"fmt"
|
|
|
|
|
"path/filepath"
|
2016-03-09 16:32:32 -05:00
|
|
|
"sync"
|
2015-06-20 14:57:07 -04:00
|
|
|
"time"
|
|
|
|
|
|
2019-05-31 21:58:28 -04:00
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
2017-01-11 09:09:48 -05:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-13 17:17:54 -05:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2017-01-16 06:43:56 -05:00
|
|
|
"k8s.io/apiserver/pkg/authentication/serviceaccount"
|
2017-06-23 16:56:37 -04:00
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
2017-04-17 13:56:40 -04:00
|
|
|
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
2018-05-09 09:29:15 -04:00
|
|
|
commonutils "k8s.io/kubernetes/test/e2e/common"
|
2016-04-07 13:21:31 -04:00
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2021-02-24 14:32:30 -05:00
|
|
|
e2eauth "k8s.io/kubernetes/test/e2e/framework/auth"
|
2019-05-07 20:09:50 -04:00
|
|
|
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
|
2021-02-24 14:32:30 -05:00
|
|
|
e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
|
2015-06-20 14:57:07 -04:00
|
|
|
|
2019-05-16 11:31:47 -04:00
|
|
|
"github.com/onsi/ginkgo"
|
2015-06-20 14:57:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2016-04-07 13:21:31 -04:00
|
|
|
serverStartTimeout = framework.PodStartTimeout + 3*time.Minute
|
2015-06-20 14:57:07 -04:00
|
|
|
)
|
|
|
|
|
|
2021-02-24 14:32:30 -05:00
|
|
|
var _ = SIGDescribe("[Feature:Example]", func() {
|
2016-04-07 13:21:31 -04:00
|
|
|
f := framework.NewDefaultFramework("examples")
|
2016-06-16 20:00:00 -04:00
|
|
|
|
2016-10-18 09:00:38 -04:00
|
|
|
var c clientset.Interface
|
2015-06-20 14:57:07 -04:00
|
|
|
var ns string
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.BeforeEach(func() {
|
2016-10-18 09:00:38 -04:00
|
|
|
c = f.ClientSet
|
2016-04-07 13:21:31 -04:00
|
|
|
ns = f.Namespace.Name
|
2017-01-13 17:17:54 -05:00
|
|
|
|
|
|
|
|
// this test wants powerful permissions. Since the namespace names are unique, we can leave this
|
|
|
|
|
// lying around so we don't have to race any caches
|
2021-02-24 14:32:30 -05:00
|
|
|
err := e2eauth.BindClusterRoleInNamespace(c.RbacV1(), "edit", f.Namespace.Name,
|
2019-05-31 21:58:28 -04:00
|
|
|
rbacv1.Subject{Kind: rbacv1.ServiceAccountKind, Namespace: f.Namespace.Name, Name: "default"})
|
2019-04-09 12:52:36 -04:00
|
|
|
framework.ExpectNoError(err)
|
2017-01-13 17:17:54 -05:00
|
|
|
|
2021-02-24 14:32:30 -05:00
|
|
|
err = e2eauth.WaitForAuthorizationUpdate(c.AuthorizationV1(),
|
2017-01-13 17:17:54 -05:00
|
|
|
serviceaccount.MakeUsername(f.Namespace.Name, "default"),
|
2017-01-14 17:22:45 -05:00
|
|
|
f.Namespace.Name, "create", schema.GroupResource{Resource: "pods"}, true)
|
2017-01-13 17:17:54 -05:00
|
|
|
framework.ExpectNoError(err)
|
2015-06-20 14:57:07 -04:00
|
|
|
})
|
|
|
|
|
|
2021-02-24 14:32:30 -05:00
|
|
|
ginkgo.Describe("Liveness", func() {
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.It("liveness pods should be automatically restarted", func() {
|
2018-08-31 10:43:15 -04:00
|
|
|
test := "test/fixtures/doc-yaml/user-guide/liveness"
|
2018-05-09 09:29:15 -04:00
|
|
|
execYaml := readFile(test, "exec-liveness.yaml.in")
|
|
|
|
|
httpYaml := readFile(test, "http-liveness.yaml.in")
|
2015-07-24 04:03:48 -04:00
|
|
|
|
2020-09-08 04:26:10 -04:00
|
|
|
framework.RunKubectlOrDieInput(ns, execYaml, "create", "-f", "-")
|
|
|
|
|
framework.RunKubectlOrDieInput(ns, httpYaml, "create", "-f", "-")
|
2016-03-09 16:32:32 -05:00
|
|
|
|
|
|
|
|
// Since both containers start rapidly, we can easily run this test in parallel.
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
passed := true
|
2015-07-24 04:03:48 -04:00
|
|
|
checkRestart := func(podName string, timeout time.Duration) {
|
2019-05-07 20:09:50 -04:00
|
|
|
err := e2epod.WaitForPodNameRunningInNamespace(c, podName, ns)
|
2019-05-09 15:11:09 -04:00
|
|
|
framework.ExpectNoError(err)
|
2016-04-07 13:21:31 -04:00
|
|
|
for t := time.Now(); time.Since(t) < timeout; time.Sleep(framework.Poll) {
|
2020-02-07 21:16:47 -05:00
|
|
|
pod, err := c.CoreV1().Pods(ns).Get(context.TODO(), podName, metav1.GetOptions{})
|
2016-04-07 13:21:31 -04:00
|
|
|
framework.ExpectNoError(err, fmt.Sprintf("getting pod %s", podName))
|
2017-04-17 13:56:40 -04:00
|
|
|
stat := podutil.GetExistingContainerStatus(pod.Status.ContainerStatuses, podName)
|
2019-08-27 05:18:43 -04:00
|
|
|
framework.Logf("Pod: %s, restart count:%d", stat.Name, stat.RestartCount)
|
2016-03-09 16:32:32 -05:00
|
|
|
if stat.RestartCount > 0 {
|
2019-08-27 05:18:43 -04:00
|
|
|
framework.Logf("Saw %v restart, succeeded...", podName)
|
2016-03-09 16:32:32 -05:00
|
|
|
wg.Done()
|
2015-07-24 04:03:48 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-27 05:18:43 -04:00
|
|
|
framework.Logf("Failed waiting for %v restart! ", podName)
|
2016-03-09 16:32:32 -05:00
|
|
|
passed = false
|
|
|
|
|
wg.Done()
|
2015-07-24 04:03:48 -04:00
|
|
|
}
|
2016-03-09 16:32:32 -05:00
|
|
|
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.By("Check restarts")
|
2016-03-09 16:32:32 -05:00
|
|
|
|
|
|
|
|
// Start the "actual test", and wait for both pods to complete.
|
|
|
|
|
// If 2 fail: Something is broken with the test (or maybe even with liveness).
|
|
|
|
|
// If 1 fails: Its probably just an error in the examples/ files themselves.
|
|
|
|
|
wg.Add(2)
|
|
|
|
|
for _, c := range []string{"liveness-http", "liveness-exec"} {
|
|
|
|
|
go checkRestart(c, 2*time.Minute)
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
if !passed {
|
2019-08-27 05:18:43 -04:00
|
|
|
framework.Failf("At least one liveness example failed. See the logs above.")
|
2016-03-09 16:32:32 -05:00
|
|
|
}
|
2015-07-24 04:03:48 -04:00
|
|
|
})
|
|
|
|
|
})
|
2015-07-24 06:27:55 -04:00
|
|
|
|
2021-02-24 14:32:30 -05:00
|
|
|
ginkgo.Describe("Secret", func() {
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.It("should create a pod that reads a secret", func() {
|
2018-08-31 10:43:15 -04:00
|
|
|
test := "test/fixtures/doc-yaml/user-guide/secrets"
|
|
|
|
|
secretYaml := readFile(test, "secret.yaml")
|
2018-05-09 09:29:15 -04:00
|
|
|
podYaml := readFile(test, "secret-pod.yaml.in")
|
2016-09-01 10:37:13 -04:00
|
|
|
|
2016-02-26 19:35:21 -05:00
|
|
|
podName := "secret-test-pod"
|
2015-07-24 06:27:55 -04:00
|
|
|
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.By("creating secret and pod")
|
2020-09-08 04:26:10 -04:00
|
|
|
framework.RunKubectlOrDieInput(ns, secretYaml, "create", "-f", "-")
|
|
|
|
|
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-")
|
2019-05-07 20:09:50 -04:00
|
|
|
err := e2epod.WaitForPodNoLongerRunningInNamespace(c, podName, ns)
|
2019-05-09 15:11:09 -04:00
|
|
|
framework.ExpectNoError(err)
|
2015-07-24 06:27:55 -04:00
|
|
|
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.By("checking if secret was read correctly")
|
2016-04-07 13:21:31 -04:00
|
|
|
_, err = framework.LookForStringInLog(ns, "secret-test-pod", "test-container", "value-1", serverStartTimeout)
|
2019-05-09 15:11:09 -04:00
|
|
|
framework.ExpectNoError(err)
|
2015-07-24 06:27:55 -04:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-02-24 14:32:30 -05:00
|
|
|
ginkgo.Describe("Downward API", func() {
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.It("should create a pod that prints his name and namespace", func() {
|
2018-08-31 10:43:15 -04:00
|
|
|
test := "test/fixtures/doc-yaml/user-guide/downward-api"
|
2018-05-09 09:29:15 -04:00
|
|
|
podYaml := readFile(test, "dapi-pod.yaml.in")
|
2015-07-24 06:27:55 -04:00
|
|
|
podName := "dapi-test-pod"
|
|
|
|
|
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.By("creating the pod")
|
2020-09-08 04:26:10 -04:00
|
|
|
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-")
|
2019-05-07 20:09:50 -04:00
|
|
|
err := e2epod.WaitForPodNoLongerRunningInNamespace(c, podName, ns)
|
2019-05-09 15:11:09 -04:00
|
|
|
framework.ExpectNoError(err)
|
2015-07-24 06:27:55 -04:00
|
|
|
|
2019-05-16 11:31:47 -04:00
|
|
|
ginkgo.By("checking if name and namespace were passed correctly")
|
2016-04-07 13:21:31 -04:00
|
|
|
_, err = framework.LookForStringInLog(ns, podName, "test-container", fmt.Sprintf("MY_POD_NAMESPACE=%v", ns), serverStartTimeout)
|
2019-05-09 15:11:09 -04:00
|
|
|
framework.ExpectNoError(err)
|
2016-04-07 13:21:31 -04:00
|
|
|
_, err = framework.LookForStringInLog(ns, podName, "test-container", fmt.Sprintf("MY_POD_NAME=%v", podName), serverStartTimeout)
|
2019-05-09 15:11:09 -04:00
|
|
|
framework.ExpectNoError(err)
|
2015-07-24 06:27:55 -04:00
|
|
|
})
|
|
|
|
|
})
|
2015-06-20 14:57:07 -04:00
|
|
|
})
|
|
|
|
|
|
2018-08-31 10:43:15 -04:00
|
|
|
func readFile(test, file string) string {
|
|
|
|
|
from := filepath.Join(test, file)
|
2021-02-24 14:32:30 -05:00
|
|
|
data, err := e2etestfiles.Read(from)
|
2020-06-22 18:33:41 -04:00
|
|
|
if err != nil {
|
|
|
|
|
framework.Fail(err.Error())
|
|
|
|
|
}
|
|
|
|
|
return commonutils.SubstituteImageName(string(data))
|
2016-11-17 16:13:45 -05:00
|
|
|
}
|