summaryrefslogtreecommitdiff
path: root/tests/e2e/utils/common.go
blob: f4f9f6f13e5e97da69fa762acc51c80efae6f5a8 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
Copyright 2019 The KubeEdge 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 utils

import (
	"bytes"
	"context"
	"crypto/tls"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"reflect"
	"strings"
	"time"

	MQTT "github.com/eclipse/paho.mqtt.golang"
	apps "k8s.io/api/apps/v1"
	v1 "k8s.io/api/core/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/apimachinery/pkg/util/wait"
	clientset "k8s.io/client-go/kubernetes"
	"k8s.io/klog/v2"

	"github.com/kubeedge/kubeedge/pkg/apis/devices/v1beta1"
	edgeclientset "github.com/kubeedge/kubeedge/pkg/client/clientset/versioned"
	"github.com/kubeedge/kubeedge/tests/e2e/constants"
)

const (
	Namespace             = "default"
	DeviceETPrefix        = "$hw/events/device/"
	TwinETUpdateSuffix    = "/twin/update"
	TwinETGetSuffix       = "/twin/get"
	TwinETGetResultSuffix = "/twin/get/result"

	ModBus            = "modbus"
	IncorrectInstance = "incorrect-instance"
	IncorrectModel    = "incorrect-model"
)

var TokenClient Token
var ClientOpts *MQTT.ClientOptions
var Client MQTT.Client
var TwinResult DeviceTwinResult

var CRDTestTimerGroup = NewTestTimerGroup()

// Token interface to validate the MQTT connection.
type Token interface {
	Wait() bool
	WaitTimeout(time.Duration) bool
	Error() error
}

// BaseMessage the base struct of event message
type BaseMessage struct {
	EventID   string `json:"event_id"`
	Timestamp int64  `json:"timestamp"`
}

// TwinValue the struct of twin value
type TwinValue struct {
	Value    *string        `json:"value,omitempty"`
	Metadata *ValueMetadata `json:"metadata,omitempty"`
}

// ValueMetadata the meta of value
type ValueMetadata struct {
	Timestamp int64 `json:"timestamp,omitempty"`
}

// TypeMetadata the meta of value type
type TypeMetadata struct {
	Type string `json:"type,omitempty"`
}

// TwinVersion twin version
type TwinVersion struct {
	CloudVersion int64 `json:"cloud"`
	EdgeVersion  int64 `json:"edge"`
}

// MsgTwin the struct of device twin
type MsgTwin struct {
	Expected        *TwinValue    `json:"expected,omitempty"`
	Actual          *TwinValue    `json:"actual,omitempty"`
	Optional        *bool         `json:"optional,omitempty"`
	Metadata        *TypeMetadata `json:"metadata,omitempty"`
	ExpectedVersion *TwinVersion  `json:"expected_version,omitempty"`
	ActualVersion   *TwinVersion  `json:"actual_version,omitempty"`
}

// DeviceTwinUpdate the struct of device twin update
type DeviceTwinUpdate struct {
	BaseMessage
	Twin map[string]*MsgTwin `json:"twin"`
}

// DeviceTwinResult device get result
type DeviceTwinResult struct {
	BaseMessage
	Twin map[string]*MsgTwin `json:"twin"`
}

type ServicebusResponse struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Body string `json:"body"`
}

func NewDeployment(name, imgURL string, replicas int32) *apps.Deployment {
	deployment := apps.Deployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      name,
			Labels:    map[string]string{"app": name},
			Namespace: Namespace,
		},
		Spec: apps.DeploymentSpec{
			Replicas: &replicas,
			Selector: &metav1.LabelSelector{
				MatchLabels: map[string]string{
					"app":                 name,
					constants.E2ELabelKey: constants.E2ELabelValue,
				},
			},
			Template: v1.PodTemplateSpec{
				ObjectMeta: metav1.ObjectMeta{
					Labels: map[string]string{
						"app":                 name,
						constants.E2ELabelKey: constants.E2ELabelValue,
					},
				},
				Spec: v1.PodSpec{
					Containers: []v1.Container{
						{
							Name:  name,
							Image: imgURL,
						},
					},
					NodeSelector: map[string]string{
						"node-role.kubernetes.io/edge": "",
					},
				},
			},
		},
	}
	return &deployment
}

func NewPod(podName, imgURL string) *v1.Pod {
	pod := v1.Pod{
		ObjectMeta: metav1.ObjectMeta{
			Name:      podName,
			Namespace: v1.NamespaceDefault,
			Labels: map[string]string{
				"app":                 podName,
				constants.E2ELabelKey: constants.E2ELabelValue,
			},
		},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:  podName,
					Image: imgURL,
				},
			},
			NodeSelector: map[string]string{
				"node-role.kubernetes.io/edge": "",
			},
		},
	}
	return &pod
}

func GetDeployment(c clientset.Interface, ns, name string) (*apps.Deployment, error) {
	return c.AppsV1().Deployments(ns).Get(context.TODO(), name, metav1.GetOptions{})
}

func CreateDeployment(c clientset.Interface, deployment *apps.Deployment) (*apps.Deployment, error) {
	return c.AppsV1().Deployments(deployment.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
}

// DeleteDeployment to delete deployment
func DeleteDeployment(c clientset.Interface, ns, name string) error {
	err := c.AppsV1().Deployments(ns).Delete(context.TODO(), name, metav1.DeleteOptions{})
	if err != nil && apierrors.IsNotFound(err) {
		return nil
	}

	return err
}

// HandleDeviceModel to handle DeviceModel operation to apiserver.
func HandleDeviceModel(c edgeclientset.Interface, operation string, UID string, protocolType string) error {
	switch operation {
	case http.MethodPost:
		body := newDeviceModelObject(protocolType, false)
		_, err := c.DevicesV1beta1().DeviceModels("default").Create(context.TODO(), body, metav1.CreateOptions{})
		return err

	case http.MethodPatch:
		body := newDeviceModelObject(protocolType, true)
		reqBytes, err := json.Marshal(body)
		if err != nil {
			Fatalf("Marshalling body failed: %v", err)
		}

		_, err = c.DevicesV1beta1().DeviceModels("default").Patch(context.TODO(), UID, types.MergePatchType, reqBytes, metav1.PatchOptions{})
		return err

	case http.MethodDelete:
		err := c.DevicesV1beta1().DeviceModels("default").Delete(context.TODO(), UID, metav1.DeleteOptions{})
		if err != nil && apierrors.IsNotFound(err) {
			return nil
		}
		return err
	}

	return nil
}

// HandleDeviceInstance to handle app deployment/delete using pod spec.
func HandleDeviceInstance(c edgeclientset.Interface, operation string, nodeName string, UID string, protocolType string) error {
	switch operation {
	case http.MethodPost:
		body := newDeviceInstanceObject(nodeName, protocolType, false)
		_, err := c.DevicesV1beta1().Devices("default").Create(context.TODO(), body, metav1.CreateOptions{})
		return err

	case http.MethodPatch:
		body := newDeviceInstanceObject(nodeName, protocolType, true)
		reqBytes, err := json.Marshal(body)
		if err != nil {
			Fatalf("Marshalling body failed: %v", err)
		}

		_, err = c.DevicesV1beta1().Devices("default").Patch(context.TODO(), UID, types.MergePatchType, reqBytes, metav1.PatchOptions{})
		return err

	case http.MethodDelete:
		err := c.DevicesV1beta1().Devices("default").Delete(context.TODO(), UID, metav1.DeleteOptions{})
		if err != nil && apierrors.IsNotFound(err) {
			return nil
		}
		return err
	}

	return nil
}

// newDeviceInstanceObject creates a new device instance object
func newDeviceInstanceObject(nodeName string, protocolType string, updated bool) *v1beta1.Device {
	var deviceInstance v1beta1.Device
	if !updated {
		switch protocolType {
		case ModBus:
			deviceInstance = NewModbusDeviceInstance(nodeName)
		case IncorrectInstance:
			deviceInstance = IncorrectDeviceInstance()
		}
	} else {
		switch protocolType {
		case ModBus:
			deviceInstance = UpdatedModbusDeviceInstance(nodeName)
		case IncorrectInstance:
			deviceInstance = IncorrectDeviceInstance()
		}
	}
	return &deviceInstance
}

// newDeviceModelObject creates a new device model object
func newDeviceModelObject(protocolType string, updated bool) *v1beta1.DeviceModel {
	var deviceModel v1beta1.DeviceModel
	if !updated {
		switch protocolType {
		case ModBus:
			deviceModel = NewModbusDeviceModel()
		case IncorrectModel:
			deviceModel = IncorrectDeviceModel()
		}
	} else {
		switch protocolType {
		case ModBus:
			deviceModel = UpdatedModbusDeviceModel()
		case IncorrectModel:
			deviceModel = IncorrectDeviceModel()
		}
	}
	return &deviceModel
}

func ListDeviceModel(c edgeclientset.Interface, ns string) ([]v1beta1.DeviceModel, error) {
	deviceModelList, err := c.DevicesV1beta1().DeviceModels(ns).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		return nil, err
	}
	return deviceModelList.Items, nil
}

func ListDevice(c edgeclientset.Interface, ns string) ([]v1beta1.Device, error) {
	deviceList, err := c.DevicesV1beta1().Devices(ns).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		return nil, err
	}
	return deviceList.Items, nil
}

// CheckDeviceModelExists verify whether the contents of the device model matches with what is expected
func CheckDeviceModelExists(deviceModels []v1beta1.DeviceModel, expectedDeviceModel *v1beta1.DeviceModel) error {
	modelExists := false
	for _, deviceModel := range deviceModels {
		if expectedDeviceModel.ObjectMeta.Name == deviceModel.ObjectMeta.Name {
			modelExists = true
			if !reflect.DeepEqual(expectedDeviceModel.TypeMeta, deviceModel.TypeMeta) ||
				expectedDeviceModel.ObjectMeta.Namespace != deviceModel.ObjectMeta.Namespace ||
				!reflect.DeepEqual(expectedDeviceModel.Spec, deviceModel.Spec) {
				return fmt.Errorf("the device model is not matching with what was expected")
			}
			break
		}
	}
	if !modelExists {
		return fmt.Errorf("the requested device model is not found")
	}

	return nil
}

func CheckDeviceExists(deviceList []v1beta1.Device, expectedDevice *v1beta1.Device) error {
	deviceExists := false
	for _, device := range deviceList {
		if expectedDevice.ObjectMeta.Name == device.ObjectMeta.Name {
			deviceExists = true
			if !reflect.DeepEqual(expectedDevice.TypeMeta, device.TypeMeta) ||
				expectedDevice.ObjectMeta.Namespace != device.ObjectMeta.Namespace ||
				!reflect.DeepEqual(expectedDevice.ObjectMeta.Labels, device.ObjectMeta.Labels) ||
				!reflect.DeepEqual(expectedDevice.Spec, device.Spec) {
				return fmt.Errorf("the device is not matching with what was expected")
			}
			break
		}
	}

	if !deviceExists {
		return fmt.Errorf("the requested device is not found")
	}

	return nil
}

// MqttClientInit create mqtt client config
func MqttClientInit(server, clientID, username, password string) *MQTT.ClientOptions {
	opts := MQTT.NewClientOptions().AddBroker(server).SetClientID(clientID).SetCleanSession(true)
	if username != "" {
		opts.SetUsername(username)
		if password != "" {
			opts.SetPassword(password)
		}
	}
	tlsConfig := &tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert}
	opts.SetTLSConfig(tlsConfig)
	return opts
}

// MqttConnect function felicitates the MQTT connection
func MqttConnect() error {
	// Initiate the MQTT connection
	ClientOpts = MqttClientInit("tcp://127.0.0.1:1884", "eventbus", "", "")
	Client = MQTT.NewClient(ClientOpts)
	if TokenClient = Client.Connect(); TokenClient.Wait() && TokenClient.Error() != nil {
		return fmt.Errorf("client.Connect() Error is %s" + TokenClient.Error().Error())
	}
	return nil
}

// ChangeTwinValue sends the updated twin value to the edge through the MQTT broker
func ChangeTwinValue(updateMessage DeviceTwinUpdate, deviceID string) error {
	twinUpdateBody, err := json.Marshal(updateMessage)
	if err != nil {
		return fmt.Errorf("Error in marshalling: %s" + err.Error())
	}
	deviceTwinUpdate := DeviceETPrefix + deviceID + TwinETUpdateSuffix
	TokenClient = Client.Publish(deviceTwinUpdate, 0, false, twinUpdateBody)
	if TokenClient.Wait() && TokenClient.Error() != nil {
		return fmt.Errorf("client.publish() Error in device twin update is %s" + TokenClient.Error().Error())
	}
	return nil
}

// GetTwin function is used to get the device twin details from the edge
func GetTwin(updateMessage DeviceTwinUpdate, deviceID string) error {
	getTwin := DeviceETPrefix + deviceID + TwinETGetSuffix
	twinUpdateBody, err := json.Marshal(updateMessage)
	if err != nil {
		return fmt.Errorf("Error in marshalling: %s" + err.Error())
	}
	TokenClient = Client.Publish(getTwin, 0, false, twinUpdateBody)
	if TokenClient.Wait() && TokenClient.Error() != nil {
		return fmt.Errorf("client.publish() Error in device twin get  is: %s " + TokenClient.Error().Error())
	}
	return nil
}

// subscribe function subscribes  the device twin information through the MQTT broker
func TwinSubscribe(deviceID string) {
	getTwinResult := DeviceETPrefix + deviceID + TwinETGetResultSuffix
	TokenClient = Client.Subscribe(getTwinResult, 0, OnTwinMessageReceived)
	if TokenClient.Wait() && TokenClient.Error() != nil {
		Errorf("subscribe() Error in device twin result get  is %v", TokenClient.Error().Error())
	}
	for {
		twin := DeviceTwinUpdate{}
		err := GetTwin(twin, deviceID)
		if err != nil {
			Errorf("Error in getting device twin: %v", err.Error())
		}
		time.Sleep(1 * time.Second)
		if TwinResult.Twin != nil {
			break
		}
	}
}

// OnTwinMessageReceived callback function which is called when message is received
func OnTwinMessageReceived(client MQTT.Client, message MQTT.Message) {
	err := json.Unmarshal(message.Payload(), &TwinResult)
	if err != nil {
		Errorf("Error in unmarshalling: %v", err.Error())
	}
}

// CompareTwin is used to compare 2 device Twins
func CompareTwin(deviceTwin map[string]*MsgTwin, expectedDeviceTwin map[string]*MsgTwin) bool {
	for key := range expectedDeviceTwin {
		if deviceTwin[key].Metadata != nil && deviceTwin[key].Expected.Value != nil {
			if *deviceTwin[key].Metadata != *expectedDeviceTwin[key].Metadata || *deviceTwin[key].Expected.Value != *expectedDeviceTwin[key].Expected.Value {
				return false
			}
		} else {
			return false
		}
	}
	return true
}

func SendMsg(url string, message []byte, header map[string]string) (bool, int) {
	var req *http.Request
	var err error

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{
		Transport: tr,
	}
	req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(message))
	if err != nil {
		// handle error
		Fatalf("Frame HTTP request failed, request: %s, reason: %v", req.URL.String(), err)
		return false, 0
	}
	for k, v := range header {
		req.Header.Add(k, v)
	}
	t := time.Now()
	resp, err := client.Do(req)
	if err != nil {
		// handle error
		Fatalf("HTTP request is failed: %v", err)
		return false, 0
	}
	defer resp.Body.Close()
	Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Since(t))
	return true, resp.StatusCode
}

func StartEchoServer() (string, error) {
	r := make(chan string)
	echo := func(response http.ResponseWriter, request *http.Request) {
		b, _ := io.ReadAll(request.Body)
		r <- string(b)
		if _, err := response.Write([]byte("Hello World")); err != nil {
			Errorf("Echo server write failed. reason: %s", err.Error())
		}
	}
	url := func(response http.ResponseWriter, request *http.Request) {
		b, _ := io.ReadAll(request.Body)
		var buff bytes.Buffer
		buff.WriteString("Reply from server: ")
		buff.Write(b)
		buff.WriteString(" Header of the message: [user]: " + request.Header.Get("user") +
			", [passwd]: " + request.Header.Get("passwd"))
		if _, err := response.Write(buff.Bytes()); err != nil {
			Errorf("Echo server write failed. reason: %s", err.Error())
		}
		r <- buff.String()
	}
	mux := http.NewServeMux()
	mux.HandleFunc("/echo", echo)
	mux.HandleFunc("/url", url)
	server := &http.Server{Addr: "127.0.0.1:9000", Handler: mux}
	go func() {
		err := server.ListenAndServe()
		Errorf("Echo server stop. reason: %s", err.Error())
	}()
	t := time.NewTimer(time.Second * 30)
	select {
	case resp := <-r:
		err := server.Shutdown(context.TODO())
		return resp, err
	case <-t.C:
		err := server.Shutdown(context.TODO())
		close(r)
		return "", err
	}
}

// subscribe function subscribes  the device twin information through the MQTT broker
func SubscribeMqtt(topic string) (string, error) {
	r := make(chan string)
	TokenClient = Client.Subscribe(topic, 0, func(client MQTT.Client, message MQTT.Message) {
		r <- string(message.Payload())
	})
	if TokenClient.Wait() && TokenClient.Error() != nil {
		return "", fmt.Errorf("subscribe() Error in topic %s. reason: %s", topic, TokenClient.Error().Error())
	}
	t := time.NewTimer(time.Second * 30)
	select {
	case result := <-r:
		Infof("subscribe topic %s to get result: %s", topic, result)
		return result, nil
	case <-t.C:
		close(r)
		return "", fmt.Errorf("wait for MQTT message time out. ")
	}
}

func PublishMqtt(topic, message string) error {
	TokenClient = Client.Publish(topic, 0, false, message)
	if TokenClient.Wait() && TokenClient.Error() != nil {
		return fmt.Errorf("client.publish() Error in topic %s. reason: %s. ", topic, TokenClient.Error().Error())
	}
	Infof("publish topic %s message %s", topic, message)
	return nil
}

func CallServicebus() (response string, err error) {
	var servicebusResponse ServicebusResponse
	payload := strings.NewReader(`{"method":"POST","targetURL":"http://127.0.0.1:9000/echo","payload":""}`)
	client := &http.Client{}
	req, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1:9060", payload)
	req.Header.Add("Content-Type", "application/json")
	resp, _ := client.Do(req)
	body, _ := io.ReadAll(resp.Body)
	err = json.Unmarshal(body, &servicebusResponse)
	response = servicebusResponse.Body
	return
}

func GetStatefulSet(c clientset.Interface, ns, name string) (*apps.StatefulSet, error) {
	return c.AppsV1().StatefulSets(ns).Get(context.TODO(), name, metav1.GetOptions{})
}

func CreateStatefulSet(c clientset.Interface, statefulSet *apps.StatefulSet) (*apps.StatefulSet, error) {
	return c.AppsV1().StatefulSets(statefulSet.Namespace).Create(context.TODO(), statefulSet, metav1.CreateOptions{})
}

// DeleteStatefulSet to delete statefulSet
func DeleteStatefulSet(c clientset.Interface, ns, name string) error {
	err := c.AppsV1().StatefulSets(ns).Delete(context.TODO(), name, metav1.DeleteOptions{})
	if err != nil && apierrors.IsNotFound(err) {
		return nil
	}

	return err
}

// NewTestStatefulSet create statefulSet for test
func NewTestStatefulSet(name, imgURL string, replicas int32) *apps.StatefulSet {
	return &apps.StatefulSet{
		ObjectMeta: metav1.ObjectMeta{
			Name:      name,
			Namespace: Namespace,
			Labels:    map[string]string{"app": name},
		},
		Spec: apps.StatefulSetSpec{
			Replicas: &replicas,
			Selector: &metav1.LabelSelector{
				MatchLabels: map[string]string{
					"app":                 name,
					constants.E2ELabelKey: constants.E2ELabelValue,
				},
			},
			Template: v1.PodTemplateSpec{
				ObjectMeta: metav1.ObjectMeta{
					Labels: map[string]string{
						"app":                 name,
						constants.E2ELabelKey: constants.E2ELabelValue,
					},
				},
				Spec: v1.PodSpec{
					NodeSelector: map[string]string{
						"node-role.kubernetes.io/edge": "",
					},
					Containers: []v1.Container{
						{
							Name:  "nginx",
							Image: imgURL,
						},
					},
				},
			},
		},
	}
}

// WaitForStatusReplicas waits for the ss.Status.Replicas to be equal to expectedReplicas
func WaitForStatusReplicas(c clientset.Interface, ss *apps.StatefulSet, expectedReplicas int32) {
	ns, name := ss.Namespace, ss.Name
	pollErr := wait.PollImmediate(5*time.Second, 240*time.Second,
		func() (bool, error) {
			ssGet, err := c.AppsV1().StatefulSets(ns).Get(context.TODO(), name, metav1.GetOptions{})
			if err != nil {
				return false, err
			}
			if ssGet.Status.ObservedGeneration < ss.Generation {
				return false, nil
			}
			if ssGet.Status.Replicas != expectedReplicas {
				klog.Infof("Waiting for stateful set status.replicas to become %d, currently %d", expectedReplicas, ssGet.Status.Replicas)
				return false, nil
			}
			return true, nil
		})
	if pollErr != nil {
		Fatalf("Failed waiting for stateful set status.replicas updated to %d: %v", expectedReplicas, pollErr)
	}
}