Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use label to select Velero deployment in plugin cmd #3447

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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