diff --git a/pkg/network/multus.go b/pkg/network/multus.go index 5c3fa6a881..79073e8e8a 100644 --- a/pkg/network/multus.go +++ b/pkg/network/multus.go @@ -79,6 +79,93 @@ func cleanUpMultusOldName(ctx context.Context, client k8sclient.Client) []error return []error{} } +// 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 +} + +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 +} + // RenderMultus generates the manifests of Multus func renderMultus(conf *cnao.NetworkAddonsConfigSpec, manifestDir string, openshiftNetworkConfig *osv1.Network, clusterInfo *ClusterInfo) ([]*unstructured.Unstructured, error) { if conf.Multus == nil || openshiftNetworkConfig != nil { diff --git a/pkg/network/network.go b/pkg/network/network.go index 51b8667c38..a17081d786 100644 --- a/pkg/network/network.go +++ b/pkg/network/network.go @@ -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 {