mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-08-17 20:56:08 +00:00
* refactor: use k8s.io/api well-known label constants Several well-known Kubernetes label strings were hardcoded across the codebase instead of using the constants already exported by k8s.io/api/core/v1, which is an existing dependency: "kubernetes.io/hostname" -> corev1api.LabelHostname "kubernetes.io/os" -> corev1api.LabelOSStable "topology.kubernetes.io/zone" -> corev1api.LabelTopologyZone The local kube.NodeOSLabel and zoneLabel consts, which duplicated the upstream values verbatim, are now defined in terms of the upstream constants rather than repeating the literal. Both are kept: NodeOSLabel is exported and referenced from four packages alongside NodeOSLinux and NodeOSWindows, which have no upstream equivalent, and zoneLabel sits beside the deprecated-label fallback it is compared against. No functional change - every replacement is a constant with an identical value. Signed-off-by: Harshit saini <harshitsaini1188@gmail.com> * Add changelog for #10279 Signed-off-by: Harshit saini <harshitsaini1188@gmail.com> * Cover the selected-node path in createRestorePod TestCreateRestorePod only exercised selectedNode == "", so the branch that pins the restore pod to a node was never executed. Add a case with a selected node and assert the resulting pod carries the hostname label in its node selector. Signed-off-by: Harshit saini <harshitsaini1188@gmail.com> * Also use constants for the arch and deprecated zone labels Extends the same replacement to the two remaining well-known labels raised on the issue: "kubernetes.io/arch" -> corev1api.LabelArchStable "failure-domain.beta.kubernetes.io/zone" -> corev1api.LabelFailureDomainBetaZone zoneLabelDeprecated in item_backupper.go was the last local const still repeating a literal that upstream already exports, so the zone pair now reads consistently against k8s.io/api. The deprecation note upstream applies to the label itself, not the constant; Velero reads that label deliberately as the fallback for PVs created before the topology labels existed. Signed-off-by: Harshit saini <harshitsaini1188@gmail.com> --------- Signed-off-by: Harshit saini <harshitsaini1188@gmail.com>
168 lines
6.8 KiB
Go
168 lines
6.8 KiB
Go
/*
|
|
Copyright 2019, 2020 the Velero contributors.
|
|
|
|
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 install
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
corev1api "k8s.io/api/core/v1"
|
|
|
|
"github.com/vmware-tanzu/velero/pkg/util/kube"
|
|
)
|
|
|
|
func TestDeployment(t *testing.T) {
|
|
deploy := Deployment("velero")
|
|
|
|
assert.Equal(t, "velero", deploy.ObjectMeta.Namespace)
|
|
|
|
deploy = Deployment("velero", WithRestoreOnly(true))
|
|
assert.Equal(t, "--restore-only", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment("velero", WithEnvFromSecretKey("my-var", "my-secret", "my-key"))
|
|
envSecret := deploy.Spec.Template.Spec.Containers[0].Env[3]
|
|
assert.Equal(t, "my-var", envSecret.Name)
|
|
assert.Equal(t, "my-secret", envSecret.ValueFrom.SecretKeyRef.LocalObjectReference.Name)
|
|
assert.Equal(t, "my-key", envSecret.ValueFrom.SecretKeyRef.Key)
|
|
|
|
deploy = Deployment("velero", WithImage("velero/velero:v0.11"))
|
|
assert.Equal(t, "velero/velero:v0.11", deploy.Spec.Template.Spec.Containers[0].Image)
|
|
assert.Equal(t, corev1api.PullIfNotPresent, deploy.Spec.Template.Spec.Containers[0].ImagePullPolicy)
|
|
|
|
deploy = Deployment("velero", WithSecret(true))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Env, 7)
|
|
assert.Len(t, deploy.Spec.Template.Spec.Volumes, 3)
|
|
|
|
deploy = Deployment("velero", WithDefaultRepoMaintenanceFrequency(24*time.Hour))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--default-repo-maintain-frequency=24h0m0s", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment("velero", WithGarbageCollectionFrequency(24*time.Hour))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--garbage-collection-frequency=24h0m0s", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment("velero", WithFeatures([]string{"EnableCSI", "foo", "bar", "baz"}))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--features=EnableCSI,foo,bar,baz", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment("velero", WithPlugins([]string{
|
|
"harbor-repo.vmware.com/harbor-ci/velero/velero-plugin-for-aws:v1.2.0",
|
|
" \n vsphereveleroplugin/velero-plugin-for-vsphere:v1.1.1 ",
|
|
}))
|
|
assert.Len(t, deploy.Spec.Template.Spec.InitContainers, 2)
|
|
assert.Equal(t, "harbor-repo.vmware.com/harbor-ci/velero/velero-plugin-for-aws:v1.2.0", deploy.Spec.Template.Spec.InitContainers[0].Image)
|
|
assert.Equal(t, "vsphereveleroplugin/velero-plugin-for-vsphere:v1.1.1", deploy.Spec.Template.Spec.InitContainers[1].Image)
|
|
assert.Equal(t, "vsphereveleroplugin-velero-plugin-for-vsphere", deploy.Spec.Template.Spec.InitContainers[1].Name)
|
|
|
|
deploy = Deployment("velero", WithUploaderType("kopia"))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--uploader-type=kopia", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment("velero", WithServiceAccountName("test-sa"))
|
|
assert.Equal(t, "test-sa", deploy.Spec.Template.Spec.ServiceAccountName)
|
|
|
|
deploy = Deployment("velero", WithDisableInformerCache(true))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--disable-informer-cache=true", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment("velero", WithKeepLatestMaintenanceJobs(3))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--keep-latest-maintenance-jobs=3", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment(
|
|
"velero",
|
|
WithPodResources(
|
|
kube.PodResources{
|
|
CPURequest: "100m",
|
|
MemoryRequest: "256Mi",
|
|
CPULimit: "200m",
|
|
MemoryLimit: "512Mi",
|
|
},
|
|
),
|
|
)
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 5)
|
|
assert.Equal(t, "--maintenance-job-cpu-limit=200m", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
assert.Equal(t, "--maintenance-job-cpu-request=100m", deploy.Spec.Template.Spec.Containers[0].Args[2])
|
|
assert.Equal(t, "--maintenance-job-mem-limit=512Mi", deploy.Spec.Template.Spec.Containers[0].Args[3])
|
|
assert.Equal(t, "--maintenance-job-mem-request=256Mi", deploy.Spec.Template.Spec.Containers[0].Args[4])
|
|
|
|
deploy = Deployment("velero", WithBackupRepoConfigMap("test-backup-repo-config"))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--backup-repository-configmap=test-backup-repo-config", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment("velero", WithRepoMaintenanceJobConfigMap("test-repo-maintenance-config"))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--repo-maintenance-job-configmap=test-repo-maintenance-config", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
deploy = Deployment("velero", WithDefaultResourceModifierConfigMap("default-restore-modifiers"))
|
|
assert.Len(t, deploy.Spec.Template.Spec.Containers[0].Args, 2)
|
|
assert.Equal(t, "--default-resource-modifier-configmap=default-restore-modifiers", deploy.Spec.Template.Spec.Containers[0].Args[1])
|
|
|
|
assert.Equal(t, &corev1api.Affinity{
|
|
NodeAffinity: &corev1api.NodeAffinity{
|
|
RequiredDuringSchedulingIgnoredDuringExecution: &corev1api.NodeSelector{
|
|
NodeSelectorTerms: []corev1api.NodeSelectorTerm{
|
|
{
|
|
MatchExpressions: []corev1api.NodeSelectorRequirement{
|
|
{
|
|
Key: corev1api.LabelOSStable,
|
|
Values: []string{"windows"},
|
|
Operator: corev1api.NodeSelectorOpNotIn,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, deploy.Spec.Template.Spec.Affinity)
|
|
}
|
|
|
|
func TestDeploymentWithPriorityClassName(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
priorityClassName string
|
|
expectedValue string
|
|
}{
|
|
{
|
|
name: "with priority class name",
|
|
priorityClassName: "high-priority",
|
|
expectedValue: "high-priority",
|
|
},
|
|
{
|
|
name: "without priority class name",
|
|
priorityClassName: "",
|
|
expectedValue: "",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Create a deployment with the priority class name option
|
|
var opts []podTemplateOption
|
|
if tc.priorityClassName != "" {
|
|
opts = append(opts, WithPriorityClassName(tc.priorityClassName))
|
|
}
|
|
|
|
deployment := Deployment("velero", opts...)
|
|
|
|
// Verify the priority class name is set correctly
|
|
assert.Equal(t, tc.expectedValue, deployment.Spec.Template.Spec.PriorityClassName)
|
|
})
|
|
}
|
|
}
|