Skip to content

Commit

Permalink
KIC: Cleanup of old named resources
Browse files Browse the repository at this point in the history
Since Kubevirt ipam controller resource names were renamed
since 0.94.0, we introduce the special remove that in case the old named
objects exist, will remove them.
This allows the lifecycle lane to pass, as it checks for leftovers,
which otherwise exists.

Signed-off-by: Or Shoval <[email protected]>
  • Loading branch information
oshoval committed Jul 3, 2024
1 parent 5680257 commit 6aec9f7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
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

0 comments on commit 6aec9f7

Please sign in to comment.