Skip to content

Commit

Permalink
kic: Special remove old named objects
Browse files Browse the repository at this point in the history
Since kubevirt ipam controller resources were renamed
between 0.94.0 and 0.94.1,
we introduce the special remove that in case the old named
objects exists, it will remove them.

Signed-off-by: Or Shoval <[email protected]>
  • Loading branch information
oshoval committed Jul 2, 2024
1 parent 273d2e0 commit 0ca6603
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
87 changes: 87 additions & 0 deletions pkg/network/multus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
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 0ca6603

Please sign in to comment.