/
mco_deployments.go
76 lines (66 loc) · 2.65 KB
/
mco_deployments.go
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
// Copyright (c) 2021 Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
package utils
import (
"errors"
appv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
func GetDeployment(opt TestOptions, isHub bool, name string,
namespace string) (*appv1.Deployment, error) {
clientKube := getKubeClient(opt, isHub)
dep, err := clientKube.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Failed to get deployment %s in namespace %s due to %v", name, namespace, err)
}
return dep, err
}
func GetDeploymentWithLabel(opt TestOptions, isHub bool, label string,
namespace string) (*appv1.DeploymentList, error) {
clientKube := getKubeClient(opt, isHub)
deps, err := clientKube.AppsV1().Deployments(namespace).List(metav1.ListOptions{
LabelSelector: label,
})
if err != nil {
klog.Errorf("Failed to get deployment with label selector %s in namespace %s due to %v", label, namespace, err)
}
return deps, err
}
func DeleteDeployment(opt TestOptions, isHub bool, name string, namespace string) error {
clientKube := getKubeClient(opt, isHub)
err := clientKube.AppsV1().Deployments(namespace).Delete(name, &metav1.DeleteOptions{})
if err != nil {
klog.Errorf("Failed to delete deployment %s in namespace %s due to %v", name, namespace, err)
}
return err
}
func UpdateDeployment(opt TestOptions, isHub bool, name string, namespace string,
dep *appv1.Deployment) (*appv1.Deployment, error) {
clientKube := getKubeClient(opt, isHub)
updateDep, err := clientKube.AppsV1().Deployments(namespace).Update(dep)
if err != nil {
klog.Errorf("Failed to update deployment %s in namespace %s due to %v", name, namespace, err)
}
return updateDep, err
}
func UpdateDeploymentReplicas(opt TestOptions, deployName, crProperty string, desiredReplicas, expectedReplicas int32) error {
clientDynamic := GetKubeClientDynamic(opt, true)
deploy, err := GetDeployment(opt, true, deployName, MCO_NAMESPACE)
if err != nil {
return err
}
deploy.Spec.Replicas = &desiredReplicas
UpdateDeployment(opt, true, deployName, MCO_NAMESPACE, deploy)
obs, err := clientDynamic.Resource(NewMCOMObservatoriumGVR()).Namespace(MCO_NAMESPACE).Get(MCO_CR_NAME, metav1.GetOptions{})
if err != nil {
return err
}
thanos := obs.Object["spec"].(map[string]interface{})["thanos"]
currentReplicas := thanos.(map[string]interface{})[crProperty].(map[string]interface{})["replicas"].(int64)
if int(currentReplicas) != int(expectedReplicas) {
klog.Errorf("Failed to update deployment %s replicas to %v", deployName, expectedReplicas)
return errors.New("the replicas was not updated successfully")
}
return nil
}