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

KIC: Cleanup of old named resources #1819

Merged
merged 1 commit into from
Jul 3, 2024
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
96 changes: 96 additions & 0 deletions pkg/network/kubevirt_ipam_controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package network

import (
"context"
"fmt"
"log"
"os"
"path/filepath"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

cnao "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/shared"
"github.com/kubevirt/cluster-network-addons-operator/pkg/render"
Expand Down Expand Up @@ -45,3 +52,92 @@ func renderKubevirtIPAMController(conf *cnao.NetworkAddonsConfigSpec, manifestDi

return objs, nil
}

// cleanUpKubevirtIpamController checks specific kic outdated objects or ones that are no longer compatible and deletes them.
func cleanUpKubevirtIpamController(conf *cnao.NetworkAddonsConfigSpec, ctx context.Context, client k8sclient.Client) []error {
if conf.KubevirtIpamController == nil {
return []error{}
}

errList := []error{}
errList = append(errList, cleanUpKubevirtIpamControllerOldNames(ctx, client)...)
return errList
}

// cleanUpKubevirtIpamControllerOldNames deletes kic objects with old name after a new name was introduces in version 0.94.1
// REQUIRED_FOR upgrade from kubevirt-ipam-controller == 0.94.0
func cleanUpKubevirtIpamControllerOldNames(ctx context.Context, client k8sclient.Client) []error {
namespace := os.Getenv("OPERAND_NAMESPACE")

resources := []struct {
gvk schema.GroupVersionKind
name string
}{
{
gvk: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"},
name: "kubevirt-ipam-claims-controller-manager",
},
{
gvk: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ServiceAccount"},
name: "kubevirt-ipam-claims-controller-manager",
},
{
gvk: schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "Role"},
name: "kubevirt-ipam-claims-leader-election-role",
},
{
gvk: schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"},
name: "kubevirt-ipam-claims-manager-role",
},
{
gvk: schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "RoleBinding"},
name: "kubevirt-ipam-claims-leader-election-rolebinding",
},
{
gvk: schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRoleBinding"},
name: "kubevirt-ipam-claims-manager-rolebinding",
},
{
gvk: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Service"},
name: "kubevirt-ipam-claims-webhook-service",
},
{
gvk: schema.GroupVersionKind{Group: "cert-manager.io", Version: "v1", Kind: "Certificate"},
name: "kubevirt-ipam-claims-serving-cert",
},
{
gvk: schema.GroupVersionKind{Group: "cert-manager.io", Version: "v1", Kind: "Issuer"},
name: "kubevirt-ipam-claims-selfsigned-issuer",
},
{
gvk: schema.GroupVersionKind{Group: "admissionregistration.k8s.io", Version: "v1", Kind: "MutatingWebhookConfiguration"},
name: "kubevirt-ipam-claims-mutating-webhook-configuration",
},
}

var errors []error
for _, resource := range resources {
existing := &unstructured.Unstructured{}
existing.SetGroupVersionKind(resource.gvk)

err := client.Get(ctx, types.NamespacedName{Name: resource.name, Namespace: namespace}, existing)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
errors = append(errors, err)
continue
}

objDesc := fmt.Sprintf("(%s) %s/%s", resource.gvk.String(), namespace, resource.name)
log.Printf("Cleaning up %s Object", objDesc)

err = client.Delete(ctx, existing)
if err != nil {
log.Printf("Failed Cleaning up %s Object", objDesc)
errors = append(errors, err)
}
}

return errors
}
1 change: 1 addition & 0 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func SpecialCleanUp(conf *cnao.NetworkAddonsConfigSpec, client k8sclient.Client,
ctx := context.TODO()

errs = append(errs, cleanUpMultus(conf, ctx, client)...)
errs = append(errs, cleanUpKubevirtIpamController(conf, ctx, client)...)
errs = append(errs, cleanUpNamespaceLabels(ctx, client)...)

if len(errs) > 0 {
Expand Down
Loading