Fix schedule create dropping annotations

velero schedule create registers --annotations through BackupOptions.BindFlags,
but the Schedule ObjectMeta it builds only sets Labels, so the flag was accepted
and silently discarded.

This matters beyond the Schedule object itself: BackupBuilder.FromSchedule
falls back to schedule.Annotations when the template carries none, so every
backup generated by the schedule lost the annotations too. velero schedule
describe already prints these fields and the Schedule CRD already carries them,
so the create path was the only gap.

Same shape as #10526, which fixed the backup type being dropped on the same
struct literal.

Signed-off-by: Jeremy Schoemaker <jeremy@shoemoney.com>
This commit is contained in:
Jeremy Schoemaker
2026-09-19 17:32:48 -05:00
parent 60163e0827
commit 8206b79673
2 changed files with 64 additions and 3 deletions
+4 -3
View File
@@ -136,9 +136,10 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
schedule := &api.Schedule{
ObjectMeta: metav1.ObjectMeta{
Namespace: f.Namespace(),
Name: o.BackupOptions.Name,
Labels: o.BackupOptions.Labels.Data(),
Namespace: f.Namespace(),
Name: o.BackupOptions.Name,
Labels: o.BackupOptions.Labels.Data(),
Annotations: o.BackupOptions.Annotations.Data(),
},
Spec: api.ScheduleSpec{
Template: api.BackupSpec{
+60
View File
@@ -0,0 +1,60 @@
/*
Copyright 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 schedule
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
)
// TestCreateScheduleAppliesAnnotations verifies that --annotations reaches the created
// Schedule object. Backups generated from a Schedule inherit schedule.Annotations (see
// BackupBuilder.FromSchedule), so dropping them here silently strips the annotations from
// every backup the schedule produces.
func TestCreateScheduleAppliesAnnotations(t *testing.T) {
crClient := velerotest.NewFakeControllerRuntimeClient(t)
f := &factorymocks.Factory{}
f.On("Namespace").Return(cmdtest.VeleroNameSpace)
f.On("KubebuilderClient").Return(crClient, nil)
o := NewCreateOptions()
o.Schedule = "@daily"
o.BackupOptions.Name = "test-schedule"
require.NoError(t, o.BackupOptions.Annotations.Set("owner=team-a,purpose=nightly"))
require.NoError(t, o.BackupOptions.Labels.Set("env=prod"))
require.NoError(t, o.Run(&cobra.Command{}, f))
created := new(velerov1api.Schedule)
require.NoError(t, crClient.Get(t.Context(), ctrlclient.ObjectKey{
Namespace: cmdtest.VeleroNameSpace,
Name: "test-schedule",
}, created))
assert.Equal(t, map[string]string{"env": "prod"}, created.Labels)
assert.Equal(t, map[string]string{"owner": "team-a", "purpose": "nightly"}, created.Annotations)
}