Use label to select Velero deployment in plugin cmd (#3447)

* 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>
This commit is contained in:
codegold79
2021-02-10 17:42:04 -08:00
committed by GitHub
parent 502fb8d7be
commit 6455350940
6 changed files with 57 additions and 7 deletions

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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{

View File

@@ -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)
}