diff --git a/changelogs/unreleased/3447-codegold79 b/changelogs/unreleased/3447-codegold79 new file mode 100644 index 000000000..ba34dd8c6 --- /dev/null +++ b/changelogs/unreleased/3447-codegold79 @@ -0,0 +1 @@ +Use label to select Velero deployment in plugin cmd diff --git a/pkg/cmd/cli/plugin/add.go b/pkg/cmd/cli/plugin/add.go index 92b5bd9b6..7c0b98929 100644 --- a/pkg/cmd/cli/plugin/add.go +++ b/pkg/cmd/cli/plugin/add.go @@ -37,7 +37,6 @@ import ( const ( pluginsVolumeName = "plugins" - veleroDeployment = "velero" veleroContainer = "velero" ) @@ -57,7 +56,7 @@ func NewAddCommand(f client.Factory) *cobra.Command { cmd.CheckError(err) } - veleroDeploy, err := kubeClient.AppsV1().Deployments(f.Namespace()).Get(context.TODO(), veleroDeployment, metav1.GetOptions{}) + veleroDeploy, err := veleroDeployment(context.TODO(), kubeClient, f.Namespace()) if err != nil { cmd.CheckError(err) } diff --git a/pkg/cmd/cli/plugin/helpers.go b/pkg/cmd/cli/plugin/helpers.go new file mode 100644 index 000000000..8336b7ee5 --- /dev/null +++ b/pkg/cmd/cli/plugin/helpers.go @@ -0,0 +1,50 @@ +/* +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 plugin + +import ( + "context" + + "github.com/pkg/errors" + appsv1api "k8s.io/api/apps/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/kubernetes" + + "github.com/vmware-tanzu/velero/pkg/install" +) + +// veleroDeployment returns a Velero deployment object, selected using a label. +func veleroDeployment(ctx context.Context, kubeClient kubernetes.Interface, namespace string) (*appsv1api.Deployment, error) { + veleroLabels := labels.FormatLabels(install.Labels()) + + deployList, err := kubeClient. + AppsV1(). + Deployments(namespace). + List(ctx, metav1.ListOptions{ + LabelSelector: veleroLabels, + }) + if err != nil { + return nil, err + } + + if len(deployList.Items) < 1 { + return nil, errors.New("Velero deployment not found") + } + + return &deployList.Items[0], nil +} diff --git a/pkg/cmd/cli/plugin/remove.go b/pkg/cmd/cli/plugin/remove.go index 755140a46..41543ad9b 100644 --- a/pkg/cmd/cli/plugin/remove.go +++ b/pkg/cmd/cli/plugin/remove.go @@ -41,7 +41,7 @@ func NewRemoveCommand(f client.Factory) *cobra.Command { cmd.CheckError(err) } - veleroDeploy, err := kubeClient.AppsV1().Deployments(f.Namespace()).Get(context.TODO(), veleroDeployment, metav1.GetOptions{}) + veleroDeploy, err := veleroDeployment(context.TODO(), kubeClient, f.Namespace()) if err != nil { cmd.CheckError(err) } diff --git a/pkg/install/deployment.go b/pkg/install/deployment.go index 758736a75..cdc501c19 100644 --- a/pkg/install/deployment.go +++ b/pkg/install/deployment.go @@ -140,7 +140,7 @@ func Deployment(namespace string, opts ...podTemplateOption) *appsv1.Deployment args = append(args, "--default-volumes-to-restic=true") } - containerLabels := labels() + containerLabels := Labels() containerLabels["deploy"] = "velero" deployment := &appsv1.Deployment{ diff --git a/pkg/install/resources.go b/pkg/install/resources.go index 8d4c33bd4..885232045 100644 --- a/pkg/install/resources.go +++ b/pkg/install/resources.go @@ -54,7 +54,7 @@ var ( DefaultVeleroNamespace = "velero" ) -func labels() map[string]string { +func Labels() map[string]string { return map[string]string{ "component": "velero", } @@ -89,7 +89,7 @@ func objectMeta(namespace, name string) metav1.ObjectMeta { return metav1.ObjectMeta{ Name: name, Namespace: namespace, - Labels: labels(), + Labels: Labels(), } } @@ -236,7 +236,7 @@ func AllCRDs() *unstructured.UnstructuredList { resources.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"}) for _, crd := range crds.CRDs { - crd.SetLabels(labels()) + crd.SetLabels(Labels()) appendUnstructured(resources, crd) }