Skip to content

Commit

Permalink
Updated uninstall to remove both v1beta1 and v1 CRDs if present (#3997)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsu-igeek authored Jul 29, 2021
1 parent f4845e3 commit 5438ff7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
2 changes: 2 additions & 0 deletions pkg/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package client
import (
"os"

apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
k8scheme "k8s.io/client-go/kubernetes/scheme"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -156,6 +157,7 @@ func (f *factory) KubebuilderClient() (kbclient.Client, error) {
velerov1api.AddToScheme(scheme)
k8scheme.AddToScheme(scheme)
apiextv1beta1.AddToScheme(scheme)
apiextv1.AddToScheme(scheme)
kubebuilderClient, err := kbclient.New(clientConfig, kbclient.Options{
Scheme: scheme,
})
Expand Down
50 changes: 28 additions & 22 deletions pkg/cmd/cli/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (
"github.com/spf13/pflag"

corev1 "k8s.io/api/core/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
kubeerrs "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -125,31 +126,36 @@ func Run(ctx context.Context, kbClient kbclient.Client, namespace string, waitTo
}

// CRDs
veleroLabels := labels.FormatLabels(install.Labels())
crdList := apiextv1beta1.CustomResourceDefinitionList{}
opts := kbclient.ListOptions{
Namespace: namespace,
Raw: &metav1.ListOptions{
LabelSelector: veleroLabels,

veleroLabelSelector := labels.SelectorFromSet(install.Labels())
opts := []kbclient.DeleteAllOfOption{
kbclient.InNamespace(namespace),
kbclient.MatchingLabelsSelector{
Selector: veleroLabelSelector,
},
}
if err := kbClient.List(context.Background(), &crdList, &opts); err != nil {
errs = append(errs, errors.WithStack(err))
} else {
if len(crdList.Items) == 0 {
fmt.Print("Velero CRDs do not exist, skipping.\n")
v1CRDsRemoved := false
v1crd := &apiextv1.CustomResourceDefinition{}
if err := kbClient.DeleteAllOf(ctx, v1crd, opts...); err != nil {
if meta.IsNoMatchError(err) {
fmt.Println("V1 Velero CRDs not found, skipping...")
} else {
veleroLabelSelector := labels.SelectorFromSet(install.Labels())
opts := []kbclient.DeleteAllOfOption{
kbclient.InNamespace(namespace),
kbclient.MatchingLabelsSelector{
Selector: veleroLabelSelector,
},
}
crd := &apiextv1beta1.CustomResourceDefinition{}
if err := kbClient.DeleteAllOf(ctx, crd, opts...); err != nil {
errs = append(errs, errors.WithStack(err))
errs = append(errs, errors.WithStack(err))
}
} else {
v1CRDsRemoved = true
}

// Remove any old Velero v1beta1 CRDs hanging around.
v1beta1crd := &apiextv1beta1.CustomResourceDefinition{}
if err := kbClient.DeleteAllOf(ctx, v1beta1crd, opts...); err != nil {
if meta.IsNoMatchError(err) {
if !v1CRDsRemoved {
// Only mention this if there were no V1 CRDs removed
fmt.Println("V1Beta1 Velero CRDs not found, skipping...")
}
} else {
errs = append(errs, errors.WithStack(err))
}
}

Expand Down

0 comments on commit 5438ff7

Please sign in to comment.