Skip to content

Commit

Permalink
Add tests for generic removal via plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev committed Feb 8, 2024
1 parent f2d4da3 commit 06222f7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
8 changes: 6 additions & 2 deletions cmd/plugin/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var deleteCmd = &cobra.Command{
# Important! As a consequence of this operation, all the corresponding resources managed by
# the AWS infrastructure provider and Cluster API Providers are orphaned and there might be
# ongoing costs incurred as a result of this.
capioperator delete --core cluster-api --infrastructure aws
capioperator delete --core --infrastructure aws
# Delete the AWS infrastructure provider and related CRDs. Please note that this forces deletion of
# all the related objects (e.g. AWSClusters, AWSMachines etc.).
Expand Down Expand Up @@ -118,7 +118,7 @@ func init() {
"Forces the deletion of the provider's CRDs (and of all the related objects)")

deleteCmd.Flags().BoolVar(&deleteOpts.coreProvider, "core", false,
"Core provider (e.g. cluster-api) to delete from the management cluster")
"Core provider to delete from the management cluster. If not set, core provider is not removed. Cluster cannot have more then 1 core provider in total.")
deleteCmd.Flags().StringSliceVarP(&deleteOpts.infrastructureProviders, "infrastructure", "i", nil,
"Infrastructure provider and namespace (e.g. aws:<namespace>) to delete from the management cluster")
deleteCmd.Flags().StringSliceVarP(&deleteOpts.bootstrapProviders, "bootstrap", "b", nil,
Expand Down Expand Up @@ -335,6 +335,8 @@ func deleteProviders(ctx context.Context, client ctrlclient.Client, providerList
}

if deleteOpts.includeCRDs && len(providerList.GetItems()) == 0 {
log.Info("Removing CRDs")

group := gvk.GroupKind()
group.Kind = strings.Replace(strings.ToLower(group.Kind), "list", "s", 1)
crd := &apiextensionsv1.CustomResourceDefinition{ObjectMeta: metav1.ObjectMeta{Name: group.String()}}
Expand All @@ -344,5 +346,7 @@ func deleteProviders(ctx context.Context, client ctrlclient.Client, providerList
}
}

log.Info("All requested providers are deleted")

return ready, nil
}
48 changes: 48 additions & 0 deletions cmd/plugin/cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import (
"testing"

. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func TestSelectorFromProvider(t *testing.T) {
Expand Down Expand Up @@ -70,3 +74,47 @@ func TestSelectorFromProvider(t *testing.T) {
})
}
}

func TestDeleteProviders(t *testing.T) {
tests := []struct {
name string
list genericProviderList
providers []genericProvider
selector fields.Set
}{{
name: "Delete providers",
list: &operatorv1.AddonProviderList{},
providers: []genericProvider{&operatorv1.AddonProvider{
ObjectMeta: metav1.ObjectMeta{
Name: "addon",
Namespace: "default",
},
}, &operatorv1.AddonProvider{
ObjectMeta: metav1.ObjectMeta{
Name: "other",
Namespace: "default",
},
}},
selector: fields.Set{"metadata.namespace": "default"},
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
for _, provider := range tt.providers {
g.Expect(env.Create(ctx, provider)).To(Succeed())
}

_, err := deleteProviders(ctx, env, tt.list, ctrlclient.MatchingFieldsSelector{
Selector: fields.SelectorFromSet(tt.selector),
})
g.Expect(err).NotTo(HaveOccurred())

for _, genericProvider := range tt.providers {
g.Eventually(func() error {
return env.Get(ctx, ctrlclient.ObjectKeyFromObject(genericProvider), genericProvider)
}, waitShort).Should(HaveOccurred())
}
})
}
}
21 changes: 21 additions & 0 deletions cmd/plugin/cmd/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (

"sigs.k8s.io/cluster-api-operator/internal/envtest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
)

const (
Expand All @@ -41,6 +44,24 @@ func TestMain(m *testing.M) {

env = envtest.New()

if err := env.Manager.GetCache().IndexField(ctx, &operatorv1.AddonProvider{},
"metadata.name",
func(obj client.Object) []string {
return []string{obj.GetName()}
},
); err != nil {
panic(fmt.Sprintf("Error setting up name index field: %v", err))
}

if err := env.Manager.GetCache().IndexField(ctx, &operatorv1.AddonProvider{},
"metadata.namespace",
func(obj client.Object) []string {
return []string{obj.GetNamespace()}
},
); err != nil {
panic(fmt.Sprintf("Error setting up namespace index field: %v", err))
}

go func() {
if err := env.Start(ctx); err != nil {
panic(fmt.Sprintf("Failed to start the envtest manager: %v", err))
Expand Down
13 changes: 9 additions & 4 deletions cmd/plugin/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

admissionv1 "k8s.io/api/admissionregistration/v1"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand All @@ -48,6 +49,14 @@ var (
scheme = runtime.NewScheme()
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
utilruntime.Must(operatorv1.AddToScheme(scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme))
utilruntime.Must(clusterctlv1.AddToScheme(scheme))
}

type genericProvider interface {
ctrlclient.Object
operatorv1.GenericProvider
Expand All @@ -70,10 +79,6 @@ func CreateKubeClient(kubeconfigPath, kubeconfigContext string) (ctrlclient.Clie
return nil, fmt.Errorf("error loading client config: %w", err)
}

utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
utilruntime.Must(operatorv1.AddToScheme(scheme))

client, err := ctrlclient.New(config, ctrlclient.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
Expand Down

0 comments on commit 06222f7

Please sign in to comment.