Skip to content

Commit

Permalink
Use label to select Velero deployment in plugin cmd (vmware-tanzu#3447)
Browse files Browse the repository at this point in the history
* Use label to select Velero deployment in plugin cmd

Signed-off-by: F. Gold <fgold@vmware.com>

* Move veleroLabel constant closer to usage

Signed-off-by: F. Gold <fgold@vmware.com>

* Add changelog

Signed-off-by: F. Gold <fgold@vmware.com>

* Remove year from copyright in new file

Signed-off-by: F. Gold <fgold@vmware.com>

* Export and use install.Labels() function

Signed-off-by: F. Gold <fgold@vmware.com>
  • Loading branch information
codegold79 authored and gyaozhou committed May 14, 2022
1 parent 2aa6f39 commit 53575da
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/3447-codegold79
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use label to select Velero deployment in plugin cmd
3 changes: 1 addition & 2 deletions pkg/cmd/cli/plugin/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (

const (
pluginsVolumeName = "plugins"
veleroDeployment = "velero"
veleroContainer = "velero"
)

Expand All @@ -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)
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/cmd/cli/plugin/helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pkg/cmd/cli/plugin/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/install/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions pkg/install/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
DefaultVeleroNamespace = "velero"
)

func labels() map[string]string {
func Labels() map[string]string {
return map[string]string{
"component": "velero",
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func objectMeta(namespace, name string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels(),
Labels: Labels(),
}
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 53575da

Please sign in to comment.