2017-11-10 02:00:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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 framework
|
|
|
|
|
|
|
|
import (
|
2020-03-24 13:44:13 +00:00
|
|
|
"context"
|
2022-07-08 16:27:47 +00:00
|
|
|
"errors"
|
2023-05-10 14:43:02 +00:00
|
|
|
"fmt"
|
2022-07-08 16:27:47 +00:00
|
|
|
"os"
|
2019-07-07 16:34:56 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-31 16:16:28 +00:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-02-19 03:08:14 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-03-28 23:43:18 +00:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
2017-11-10 02:00:38 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
)
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
// EchoService name of the deployment for the echo app
|
|
|
|
const EchoService = "echo"
|
|
|
|
|
|
|
|
// SlowEchoService name of the deployment for the echo app
|
|
|
|
const SlowEchoService = "slow-echo"
|
|
|
|
|
2023-05-10 14:43:02 +00:00
|
|
|
// HTTPBunService name of the deployment for the httpbun app
|
|
|
|
const HTTPBunService = "httpbun"
|
|
|
|
|
|
|
|
// NipService name of external service using nip.io
|
|
|
|
const NIPService = "external-nip"
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2024-09-06 14:59:43 +00:00
|
|
|
// HTTPBunImage is the default image that is used to deploy HTTPBun with the framework
|
2023-06-12 10:25:49 +00:00
|
|
|
var HTTPBunImage = os.Getenv("HTTPBUN_IMAGE")
|
|
|
|
|
|
|
|
// EchoImage is the default image to be used by the echo service
|
2023-09-11 03:02:10 +00:00
|
|
|
const EchoImage = "registry.k8s.io/ingress-nginx/e2e-test-echo@sha256:4938d1d91a2b7d19454460a8c1b010b89f6ff92d2987fd889ac3e8fc3b70d91a" //#nosec G101
|
2023-06-12 10:25:49 +00:00
|
|
|
|
|
|
|
// TODO: change all Deployment functions to use these options
|
2023-08-31 07:36:48 +00:00
|
|
|
// in order to reduce complexity and have a unified API across the
|
2023-06-12 10:25:49 +00:00
|
|
|
// framework
|
2022-02-02 13:12:22 +00:00
|
|
|
type deploymentOptions struct {
|
2023-01-16 02:46:50 +00:00
|
|
|
name string
|
2023-06-12 10:25:49 +00:00
|
|
|
namespace string
|
|
|
|
image string
|
2023-01-16 02:46:50 +00:00
|
|
|
replicas int
|
|
|
|
svcAnnotations map[string]string
|
2017-11-21 23:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 13:12:22 +00:00
|
|
|
// WithDeploymentNamespace allows configuring the deployment's namespace
|
|
|
|
func WithDeploymentNamespace(n string) func(*deploymentOptions) {
|
|
|
|
return func(o *deploymentOptions) {
|
|
|
|
o.namespace = n
|
|
|
|
}
|
2018-12-20 13:48:03 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 14:17:27 +00:00
|
|
|
// WithSvcTopologyAnnotations create svc with topology mode sets to auto
|
2023-01-16 02:46:50 +00:00
|
|
|
func WithSvcTopologyAnnotations() func(*deploymentOptions) {
|
|
|
|
return func(o *deploymentOptions) {
|
|
|
|
o.svcAnnotations = map[string]string{
|
2023-08-11 14:17:27 +00:00
|
|
|
corev1.AnnotationTopologyMode: "auto",
|
2023-01-16 02:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 13:12:22 +00:00
|
|
|
// WithDeploymentName allows configuring the deployment's names
|
|
|
|
func WithDeploymentName(n string) func(*deploymentOptions) {
|
|
|
|
return func(o *deploymentOptions) {
|
|
|
|
o.name = n
|
|
|
|
}
|
2021-11-12 19:46:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 13:12:22 +00:00
|
|
|
// WithDeploymentReplicas allows configuring the deployment's replicas count
|
|
|
|
func WithDeploymentReplicas(r int) func(*deploymentOptions) {
|
|
|
|
return func(o *deploymentOptions) {
|
|
|
|
o.replicas = r
|
|
|
|
}
|
2021-11-12 19:46:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 21:22:15 +00:00
|
|
|
func WithName(n string) func(*deploymentOptions) {
|
|
|
|
return func(o *deploymentOptions) {
|
|
|
|
o.name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:43:02 +00:00
|
|
|
// WithImage allows configuring the image for the deployments
|
|
|
|
func WithImage(i string) func(*deploymentOptions) {
|
|
|
|
return func(o *deploymentOptions) {
|
|
|
|
o.image = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 16:27:47 +00:00
|
|
|
// NewEchoDeployment creates a new single replica deployment of the echo server image in a particular namespace
|
2022-02-02 13:12:22 +00:00
|
|
|
func (f *Framework) NewEchoDeployment(opts ...func(*deploymentOptions)) {
|
|
|
|
options := &deploymentOptions{
|
|
|
|
namespace: f.Namespace,
|
|
|
|
name: EchoService,
|
|
|
|
replicas: 1,
|
2023-06-12 10:25:49 +00:00
|
|
|
image: EchoImage,
|
2022-02-02 13:12:22 +00:00
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(options)
|
|
|
|
}
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
f.EnsureDeployment(newDeployment(
|
|
|
|
options.name,
|
|
|
|
options.namespace,
|
|
|
|
options.image,
|
|
|
|
80,
|
|
|
|
int32(options.replicas),
|
2022-12-28 20:59:27 +00:00
|
|
|
nil, nil, nil,
|
2020-02-06 21:08:44 +00:00
|
|
|
[]corev1.VolumeMount{},
|
|
|
|
[]corev1.Volume{},
|
2022-12-28 20:59:27 +00:00
|
|
|
true,
|
2023-06-12 10:25:49 +00:00
|
|
|
))
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
f.EnsureService(&corev1.Service{
|
2019-09-01 18:16:52 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-01-16 02:46:50 +00:00
|
|
|
Name: options.name,
|
|
|
|
Namespace: options.namespace,
|
|
|
|
Annotations: options.svcAnnotations,
|
2019-09-01 18:16:52 +00:00
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(80),
|
|
|
|
Protocol: corev1.ProtocolTCP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{
|
2022-02-02 13:12:22 +00:00
|
|
|
"app": options.name,
|
2019-09-01 18:16:52 +00:00
|
|
|
},
|
|
|
|
},
|
2023-06-12 10:25:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := WaitForEndpoints(
|
|
|
|
f.KubeClientSet,
|
|
|
|
DefaultTimeout,
|
|
|
|
options.name,
|
|
|
|
options.namespace,
|
|
|
|
options.replicas,
|
|
|
|
)
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "waiting for endpoints to become ready")
|
2018-07-19 13:37:28 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:43:02 +00:00
|
|
|
// BuildNipHost used to generate a nip host for DNS resolving
|
|
|
|
func BuildNIPHost(ip string) string {
|
|
|
|
return fmt.Sprintf("%s.nip.io", ip)
|
|
|
|
}
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
// GetNipHost used to generate a nip host for external DNS resolving
|
|
|
|
// for the instance deployed by the framework
|
|
|
|
func (f *Framework) GetNIPHost() string {
|
|
|
|
return BuildNIPHost(f.HTTPBunIP)
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:43:02 +00:00
|
|
|
// BuildNIPExternalNameService used to generate a service pointing to nip.io to
|
|
|
|
// help resolve to an IP address
|
|
|
|
func BuildNIPExternalNameService(f *Framework, ip, portName string) *corev1.Service {
|
|
|
|
return &corev1.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: NIPService,
|
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: BuildNIPHost(ip),
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: portName,
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(80),
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHttpbunDeployment creates a new single replica deployment of the httpbun
|
|
|
|
// server image in a particular namespace we return the ip for testing purposes
|
|
|
|
func (f *Framework) NewHttpbunDeployment(opts ...func(*deploymentOptions)) string {
|
|
|
|
options := &deploymentOptions{
|
|
|
|
namespace: f.Namespace,
|
|
|
|
name: HTTPBunService,
|
|
|
|
replicas: 1,
|
2023-06-12 10:25:49 +00:00
|
|
|
image: HTTPBunImage,
|
2023-05-10 14:43:02 +00:00
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(options)
|
|
|
|
}
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
// Create the HTTPBun Deployment
|
|
|
|
f.EnsureDeployment(newDeployment(
|
|
|
|
options.name,
|
|
|
|
options.namespace,
|
|
|
|
options.image,
|
|
|
|
80,
|
|
|
|
int32(options.replicas),
|
2023-06-20 09:42:22 +00:00
|
|
|
nil, nil,
|
2023-08-11 14:17:27 +00:00
|
|
|
// Required to get hostname information
|
2023-06-20 09:42:22 +00:00
|
|
|
[]corev1.EnvVar{
|
|
|
|
{
|
|
|
|
Name: "HTTPBUN_INFO_ENABLED",
|
|
|
|
Value: "1",
|
|
|
|
},
|
|
|
|
},
|
2023-05-10 14:43:02 +00:00
|
|
|
[]corev1.VolumeMount{},
|
|
|
|
[]corev1.Volume{},
|
|
|
|
true,
|
2023-06-12 10:25:49 +00:00
|
|
|
))
|
2023-05-10 14:43:02 +00:00
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
// Create a service pointing to deployment
|
|
|
|
f.EnsureService(&corev1.Service{
|
2023-05-10 14:43:02 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: options.name,
|
|
|
|
Namespace: options.namespace,
|
|
|
|
Annotations: options.svcAnnotations,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(80),
|
|
|
|
Protocol: corev1.ProtocolTCP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{
|
|
|
|
"app": options.name,
|
|
|
|
},
|
|
|
|
},
|
2023-06-12 10:25:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Wait for deployment to become available
|
|
|
|
err := WaitForEndpoints(
|
|
|
|
f.KubeClientSet,
|
|
|
|
DefaultTimeout,
|
|
|
|
options.name,
|
|
|
|
options.namespace,
|
|
|
|
options.replicas,
|
|
|
|
)
|
2023-05-10 14:43:02 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "waiting for endpoints to become ready")
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
// Get cluster ip for HTTPBun to be used in tests
|
|
|
|
e, err := f.KubeClientSet.
|
|
|
|
CoreV1().
|
|
|
|
Endpoints(f.Namespace).
|
|
|
|
Get(context.TODO(), HTTPBunService, metav1.GetOptions{})
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "failed to get httpbun endpoint")
|
|
|
|
|
|
|
|
return e.Subsets[0].Addresses[0].IP
|
2023-05-10 14:43:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 10:37:19 +00:00
|
|
|
// NewSlowEchoDeployment creates a new deployment of the slow echo server image in a particular namespace.
|
|
|
|
func (f *Framework) NewSlowEchoDeployment() {
|
2020-11-12 22:36:48 +00:00
|
|
|
cfg := `#
|
2020-05-31 03:25:56 +00:00
|
|
|
events {
|
|
|
|
worker_connections 1024;
|
|
|
|
multi_accept on;
|
|
|
|
}
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2020-05-31 03:25:56 +00:00
|
|
|
http {
|
|
|
|
default_type 'text/plain';
|
|
|
|
client_max_body_size 0;
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2020-05-31 03:25:56 +00:00
|
|
|
server {
|
|
|
|
access_log on;
|
|
|
|
access_log /dev/stdout;
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2020-05-31 03:25:56 +00:00
|
|
|
listen 80;
|
|
|
|
|
|
|
|
location / {
|
|
|
|
content_by_lua_block {
|
|
|
|
ngx.print("ok")
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2020-05-31 03:25:56 +00:00
|
|
|
location ~ ^/sleep/(?<sleepTime>[0-9]+)$ {
|
|
|
|
content_by_lua_block {
|
|
|
|
ngx.sleep(ngx.var.sleepTime)
|
|
|
|
ngx.print("ok after " .. ngx.var.sleepTime .. " seconds")
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 18:16:52 +00:00
|
|
|
}
|
2018-12-11 10:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
`
|
|
|
|
|
2020-11-12 22:36:48 +00:00
|
|
|
f.NGINXWithConfigDeployment(SlowEchoService, cfg)
|
|
|
|
}
|
|
|
|
|
2022-07-08 16:27:47 +00:00
|
|
|
func (f *Framework) GetNginxBaseImage() string {
|
|
|
|
nginxBaseImage := os.Getenv("NGINX_BASE_IMAGE")
|
|
|
|
|
|
|
|
if nginxBaseImage == "" {
|
|
|
|
assert.NotEmpty(ginkgo.GinkgoT(), errors.New("NGINX_BASE_IMAGE not defined"), "NGINX_BASE_IMAGE not defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nginxBaseImage
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:23:24 +00:00
|
|
|
// NGINXDeployment creates a new simple NGINX Deployment using NGINX base image
|
|
|
|
// and passing the desired configuration
|
2023-08-31 07:36:48 +00:00
|
|
|
func (f *Framework) NGINXDeployment(name, cfg string, waitendpoint bool) {
|
2020-11-12 22:36:48 +00:00
|
|
|
cfgMap := map[string]string{
|
|
|
|
"nginx.conf": cfg,
|
|
|
|
}
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
_, err := f.KubeClientSet.
|
|
|
|
CoreV1().
|
|
|
|
ConfigMaps(f.Namespace).
|
|
|
|
Create(context.TODO(), &corev1.ConfigMap{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Data: cfgMap,
|
|
|
|
}, metav1.CreateOptions{})
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "creating configmap")
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2022-07-08 16:27:47 +00:00
|
|
|
deployment := newDeployment(name, f.Namespace, f.GetNginxBaseImage(), 80, 1,
|
2022-12-28 20:59:27 +00:00
|
|
|
nil, nil, nil,
|
2019-09-01 18:16:52 +00:00
|
|
|
[]corev1.VolumeMount{
|
|
|
|
{
|
2020-11-12 22:36:48 +00:00
|
|
|
Name: name,
|
2020-05-31 03:25:56 +00:00
|
|
|
MountPath: "/etc/nginx/nginx.conf",
|
|
|
|
SubPath: "nginx.conf",
|
2019-09-01 18:16:52 +00:00
|
|
|
ReadOnly: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]corev1.Volume{
|
|
|
|
{
|
2020-11-12 22:36:48 +00:00
|
|
|
Name: name,
|
2019-09-01 18:16:52 +00:00
|
|
|
VolumeSource: corev1.VolumeSource{
|
|
|
|
ConfigMap: &corev1.ConfigMapVolumeSource{
|
|
|
|
LocalObjectReference: corev1.LocalObjectReference{
|
2020-11-12 22:36:48 +00:00
|
|
|
Name: name,
|
2019-09-01 18:16:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-12-28 20:59:27 +00:00
|
|
|
}, true,
|
2019-09-01 18:16:52 +00:00
|
|
|
)
|
|
|
|
|
2020-02-14 00:19:07 +00:00
|
|
|
f.EnsureDeployment(deployment)
|
2019-09-01 18:16:52 +00:00
|
|
|
|
|
|
|
service := &corev1.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2020-11-12 22:36:48 +00:00
|
|
|
Name: name,
|
2019-09-01 18:16:52 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(80),
|
|
|
|
Protocol: corev1.ProtocolTCP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{
|
2020-11-12 22:36:48 +00:00
|
|
|
"app": name,
|
2019-09-01 18:16:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-14 00:19:07 +00:00
|
|
|
f.EnsureService(service)
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2022-05-26 13:23:24 +00:00
|
|
|
if waitendpoint {
|
|
|
|
err = WaitForEndpoints(f.KubeClientSet, DefaultTimeout, name, f.Namespace, 1)
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "waiting for endpoints to become ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NGINXWithConfigDeployment creates an NGINX deployment using a configmap containing the nginx.conf configuration
|
2023-08-31 07:36:48 +00:00
|
|
|
func (f *Framework) NGINXWithConfigDeployment(name, cfg string) {
|
2022-05-26 13:23:24 +00:00
|
|
|
f.NGINXDeployment(name, cfg, true)
|
2018-07-19 13:37:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 14:49:00 +00:00
|
|
|
// NewGRPCBinDeployment creates a new deployment of the
|
|
|
|
// moul/grpcbin image for GRPC tests
|
|
|
|
func (f *Framework) NewGRPCBinDeployment() {
|
|
|
|
name := "grpcbin"
|
|
|
|
|
|
|
|
probe := &corev1.Probe{
|
2020-02-19 03:08:14 +00:00
|
|
|
InitialDelaySeconds: 1,
|
|
|
|
PeriodSeconds: 1,
|
2020-02-14 00:19:07 +00:00
|
|
|
SuccessThreshold: 1,
|
2020-02-13 14:49:00 +00:00
|
|
|
TimeoutSeconds: 1,
|
2022-04-10 15:58:05 +00:00
|
|
|
ProbeHandler: corev1.ProbeHandler{
|
2020-02-13 14:49:00 +00:00
|
|
|
TCPSocket: &corev1.TCPSocketAction{
|
|
|
|
Port: intstr.FromInt(9000),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-14 00:19:07 +00:00
|
|
|
sel := map[string]string{
|
|
|
|
"app": name,
|
|
|
|
}
|
|
|
|
|
|
|
|
f.EnsureDeployment(&appsv1.Deployment{
|
2020-02-13 14:49:00 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: appsv1.DeploymentSpec{
|
|
|
|
Replicas: NewInt32(1),
|
|
|
|
Selector: &metav1.LabelSelector{
|
2020-02-14 00:19:07 +00:00
|
|
|
MatchLabels: sel,
|
2020-02-13 14:49:00 +00:00
|
|
|
},
|
|
|
|
Template: corev1.PodTemplateSpec{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2020-02-14 00:19:07 +00:00
|
|
|
Labels: sel,
|
2020-02-13 14:49:00 +00:00
|
|
|
},
|
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
TerminationGracePeriodSeconds: NewInt64(0),
|
|
|
|
Containers: []corev1.Container{
|
|
|
|
{
|
|
|
|
Name: name,
|
|
|
|
Image: "moul/grpcbin",
|
2020-02-14 00:19:07 +00:00
|
|
|
Env: []corev1.EnvVar{},
|
2020-02-13 14:49:00 +00:00
|
|
|
Ports: []corev1.ContainerPort{
|
|
|
|
{
|
|
|
|
Name: "insecure",
|
|
|
|
ContainerPort: 9000,
|
2020-02-13 19:39:20 +00:00
|
|
|
Protocol: corev1.ProtocolTCP,
|
2020-02-13 14:49:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "secure",
|
|
|
|
ContainerPort: 9001,
|
2020-02-13 19:39:20 +00:00
|
|
|
Protocol: corev1.ProtocolTCP,
|
2020-02-13 14:49:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ReadinessProbe: probe,
|
|
|
|
LivenessProbe: probe,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-14 00:19:07 +00:00
|
|
|
})
|
2020-02-13 14:49:00 +00:00
|
|
|
|
|
|
|
service := &corev1.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "insecure",
|
|
|
|
Port: 9000,
|
|
|
|
TargetPort: intstr.FromInt(9000),
|
|
|
|
Protocol: corev1.ProtocolTCP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "secure",
|
|
|
|
Port: 9001,
|
2020-07-21 13:03:45 +00:00
|
|
|
TargetPort: intstr.FromInt(9001),
|
2020-02-13 14:49:00 +00:00
|
|
|
Protocol: corev1.ProtocolTCP,
|
|
|
|
},
|
|
|
|
},
|
2020-02-14 00:19:07 +00:00
|
|
|
Selector: sel,
|
2020-02-13 14:49:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-14 00:19:07 +00:00
|
|
|
f.EnsureService(service)
|
2020-02-13 14:49:00 +00:00
|
|
|
|
|
|
|
err := WaitForEndpoints(f.KubeClientSet, DefaultTimeout, name, f.Namespace, 1)
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "waiting for endpoints to become ready")
|
2020-02-13 14:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 20:59:27 +00:00
|
|
|
func newDeployment(name, namespace, image string, port int32, replicas int32, command []string, args []string, env []corev1.EnvVar,
|
2023-08-31 07:36:48 +00:00
|
|
|
volumeMounts []corev1.VolumeMount, volumes []corev1.Volume, setProbe bool,
|
|
|
|
) *appsv1.Deployment {
|
2018-11-29 13:53:48 +00:00
|
|
|
probe := &corev1.Probe{
|
2020-09-30 14:24:51 +00:00
|
|
|
InitialDelaySeconds: 2,
|
2020-02-19 03:08:14 +00:00
|
|
|
PeriodSeconds: 1,
|
2018-11-29 13:53:48 +00:00
|
|
|
SuccessThreshold: 1,
|
2020-09-30 14:24:51 +00:00
|
|
|
TimeoutSeconds: 2,
|
|
|
|
FailureThreshold: 6,
|
2022-04-10 15:58:05 +00:00
|
|
|
ProbeHandler: corev1.ProbeHandler{
|
2018-11-29 13:53:48 +00:00
|
|
|
HTTPGet: &corev1.HTTPGetAction{
|
|
|
|
Port: intstr.FromString("http"),
|
|
|
|
Path: "/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
d := &appsv1.Deployment{
|
2017-11-10 02:00:38 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2018-07-19 13:37:28 +00:00
|
|
|
Name: name,
|
2019-09-01 18:16:52 +00:00
|
|
|
Namespace: namespace,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
2019-03-28 23:43:18 +00:00
|
|
|
Spec: appsv1.DeploymentSpec{
|
2017-11-21 23:00:19 +00:00
|
|
|
Replicas: NewInt32(replicas),
|
2017-11-10 02:00:38 +00:00
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: map[string]string{
|
2018-07-19 13:37:28 +00:00
|
|
|
"app": name,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Template: corev1.PodTemplateSpec{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
2018-07-19 13:37:28 +00:00
|
|
|
"app": name,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: corev1.PodSpec{
|
2017-12-27 11:16:59 +00:00
|
|
|
TerminationGracePeriodSeconds: NewInt64(0),
|
2017-11-10 02:00:38 +00:00
|
|
|
Containers: []corev1.Container{
|
|
|
|
{
|
2018-07-19 13:37:28 +00:00
|
|
|
Name: name,
|
|
|
|
Image: image,
|
2017-11-10 02:00:38 +00:00
|
|
|
Env: []corev1.EnvVar{},
|
|
|
|
Ports: []corev1.ContainerPort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
2018-07-19 13:37:28 +00:00
|
|
|
ContainerPort: port,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
2022-12-28 20:59:27 +00:00
|
|
|
VolumeMounts: volumeMounts,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-01 18:16:52 +00:00
|
|
|
Volumes: volumes,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-28 20:59:27 +00:00
|
|
|
if setProbe {
|
|
|
|
d.Spec.Template.Spec.Containers[0].ReadinessProbe = probe
|
|
|
|
d.Spec.Template.Spec.Containers[0].LivenessProbe = probe
|
|
|
|
}
|
2019-09-01 18:16:52 +00:00
|
|
|
if len(command) > 0 {
|
|
|
|
d.Spec.Template.Spec.Containers[0].Command = command
|
|
|
|
}
|
|
|
|
|
2022-12-28 20:59:27 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
d.Spec.Template.Spec.Containers[0].Args = args
|
|
|
|
}
|
|
|
|
if len(env) > 0 {
|
|
|
|
d.Spec.Template.Spec.Containers[0].Env = env
|
|
|
|
}
|
2019-09-01 18:16:52 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
func (f *Framework) NewDeployment(name, image string, port, replicas int32) {
|
2022-12-28 20:59:27 +00:00
|
|
|
f.NewDeploymentWithOpts(name, image, port, replicas, nil, nil, nil, nil, nil, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDeployment creates a new deployment in a particular namespace.
|
2023-08-31 07:36:48 +00:00
|
|
|
func (f *Framework) NewDeploymentWithOpts(name, image string, port, replicas int32, command, args []string, env []corev1.EnvVar, volumeMounts []corev1.VolumeMount, volumes []corev1.Volume, setProbe bool) {
|
2022-12-28 20:59:27 +00:00
|
|
|
deployment := newDeployment(name, f.Namespace, image, port, replicas, command, args, env, volumeMounts, volumes, setProbe)
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2020-02-14 00:19:07 +00:00
|
|
|
f.EnsureDeployment(deployment)
|
2017-11-10 02:00:38 +00:00
|
|
|
|
|
|
|
service := &corev1.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2018-07-19 13:37:28 +00:00
|
|
|
Name: name,
|
2019-02-22 14:03:42 +00:00
|
|
|
Namespace: f.Namespace,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 80,
|
2018-07-19 13:37:28 +00:00
|
|
|
TargetPort: intstr.FromInt(int(port)),
|
2018-11-29 13:53:48 +00:00
|
|
|
Protocol: corev1.ProtocolTCP,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{
|
2018-07-19 13:37:28 +00:00
|
|
|
"app": name,
|
2017-11-10 02:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-14 00:19:07 +00:00
|
|
|
f.EnsureService(service)
|
2018-11-29 13:53:48 +00:00
|
|
|
|
2019-12-13 02:07:50 +00:00
|
|
|
err := WaitForEndpoints(f.KubeClientSet, DefaultTimeout, name, f.Namespace, int(replicas))
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "waiting for endpoints to become ready")
|
2017-11-10 02:00:38 +00:00
|
|
|
}
|
2019-07-07 16:34:56 +00:00
|
|
|
|
|
|
|
// DeleteDeployment deletes a deployment with a particular name and waits for the pods to be deleted
|
|
|
|
func (f *Framework) DeleteDeployment(name string) error {
|
2020-03-24 13:44:13 +00:00
|
|
|
d, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Get(context.TODO(), name, metav1.GetOptions{})
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "getting deployment")
|
2020-03-20 16:02:15 +00:00
|
|
|
|
|
|
|
grace := int64(0)
|
2020-03-24 13:44:13 +00:00
|
|
|
err = f.KubeClientSet.AppsV1().Deployments(f.Namespace).Delete(context.TODO(), name, metav1.DeleteOptions{
|
2020-03-20 16:02:15 +00:00
|
|
|
GracePeriodSeconds: &grace,
|
|
|
|
})
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "deleting deployment")
|
2020-03-20 16:02:15 +00:00
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
return waitForPodsDeleted(f.KubeClientSet, 2*time.Minute, f.Namespace, &metav1.ListOptions{
|
2019-07-07 16:34:56 +00:00
|
|
|
LabelSelector: labelSelectorToString(d.Spec.Selector.MatchLabels),
|
|
|
|
})
|
|
|
|
}
|
2019-09-01 18:16:52 +00:00
|
|
|
|
|
|
|
// ScaleDeploymentToZero scales a deployment with a particular name and waits for the pods to be deleted
|
|
|
|
func (f *Framework) ScaleDeploymentToZero(name string) {
|
2020-03-24 13:44:13 +00:00
|
|
|
d, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Get(context.TODO(), name, metav1.GetOptions{})
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "getting deployment")
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.NotNil(ginkgo.GinkgoT(), d, "expected a deployment but none returned")
|
2019-09-01 18:16:52 +00:00
|
|
|
|
|
|
|
d.Spec.Replicas = NewInt32(0)
|
2020-02-14 00:19:07 +00:00
|
|
|
|
2020-03-24 13:44:13 +00:00
|
|
|
d, err = f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(context.TODO(), d, metav1.UpdateOptions{})
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "getting deployment")
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.NotNil(ginkgo.GinkgoT(), d, "expected a deployment but none returned")
|
2019-09-01 18:16:52 +00:00
|
|
|
|
2020-01-05 14:15:38 +00:00
|
|
|
err = WaitForEndpoints(f.KubeClientSet, DefaultTimeout, name, f.Namespace, 0)
|
2020-02-19 03:08:14 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "waiting for no endpoints")
|
2019-09-01 18:16:52 +00:00
|
|
|
}
|
2020-09-26 23:27:19 +00:00
|
|
|
|
|
|
|
// UpdateIngressControllerDeployment updates the ingress-nginx deployment
|
|
|
|
func (f *Framework) UpdateIngressControllerDeployment(fn func(deployment *appsv1.Deployment) error) error {
|
|
|
|
err := UpdateDeployment(f.KubeClientSet, f.Namespace, "nginx-ingress-controller", 1, fn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.updateIngressNGINXPod()
|
|
|
|
}
|