Files
open-cluster-management/pkg/addon/webhook/v1beta1/managedclusteraddon_conversion.go
Qing Hao c516beffa6
Some checks failed
Post / images (amd64, addon-manager) (push) Failing after 46s
Post / images (amd64, placement) (push) Failing after 41s
Post / images (amd64, registration-operator) (push) Failing after 39s
Post / images (amd64, work) (push) Failing after 42s
Post / images (arm64, addon-manager) (push) Failing after 39s
Post / images (arm64, placement) (push) Failing after 39s
Post / images (arm64, registration) (push) Failing after 40s
Post / images (arm64, registration-operator) (push) Failing after 42s
Post / images (arm64, work) (push) Failing after 39s
Post / images (amd64, registration) (push) Failing after 7m46s
Post / image manifest (addon-manager) (push) Has been skipped
Post / image manifest (placement) (push) Has been skipped
Post / image manifest (registration) (push) Has been skipped
Post / image manifest (registration-operator) (push) Has been skipped
Post / image manifest (work) (push) Has been skipped
Post / trigger clusteradm e2e (push) Has been skipped
Post / coverage (push) Failing after 14m33s
Scorecard supply-chain security / Scorecard analysis (push) Failing after 1m25s
Close stale issues and PRs / stale (push) Successful in 46s
Add addon conversion webhook for v1alpha1/v1beta1 API migration (#1289)
* Add addon conversion webhook for v1alpha1/v1beta1 API migration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Qing Hao <qhao@redhat.com>

* Fix GroupVersion compatibility issues after API dependency update

This commit fixes compilation and test errors introduced by updating
the API dependency to use native conversion functions from PR #411.

Changes include:

1. Fix GroupVersion type mismatches across the codebase:
   - Updated OwnerReference creation to use schema.GroupVersion
   - Fixed webhook scheme registration to use proper GroupVersion type
   - Applied fixes to addon, placement, migration, work, and registration controllers

2. Enhance addon conversion webhook:
   - Use native API conversion functions from addon/v1beta1/conversion.go
   - Fix InstallNamespace annotation key to match expected format
   - Add custom logic to populate deprecated ConfigReferent field in ConfigReferences
   - Properly preserve annotations during v1alpha1 <-> v1beta1 conversion

3. Remove duplicate conversion code:
   - Deleted pkg/addon/webhook/conversion/ directory (~500 lines)
   - Now using native conversion functions from the API repository

4. Patch vendored addon-framework:
   - Fixed GroupVersion errors in agentdeploy utils

All unit tests pass successfully (97 packages, 0 failures).

Signed-off-by: Qing Hao <qhao@redhat.com>

---------

Signed-off-by: Qing Hao <qhao@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
2025-12-24 08:26:35 +00:00

105 lines
4.1 KiB
Go

// Copyright Contributors to the Open Cluster Management project
package v1beta1
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/conversion"
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
addonv1beta1 "open-cluster-management.io/api/addon/v1beta1"
internalv1alpha1 "open-cluster-management.io/ocm/pkg/addon/webhook/v1alpha1"
)
const (
// InstallNamespaceAnnotation is the annotation key for storing installNamespace
// This is used because installNamespace field was removed in v1beta1
InstallNamespaceAnnotation = "addon.open-cluster-management.io/v1alpha1-install-namespace"
)
// ConvertTo converts this ManagedClusterAddOn (v1beta1) to the Hub version (v1alpha1)
func (src *ManagedClusterAddOn) ConvertTo(dstRaw conversion.Hub) error {
dst, ok := dstRaw.(*internalv1alpha1.ManagedClusterAddOn)
if !ok {
return fmt.Errorf("expected *internalv1alpha1.ManagedClusterAddOn but got %T", dstRaw)
}
klog.V(4).Infof("Converting ManagedClusterAddOn %s/%s from v1beta1 to v1alpha1 (Hub)",
src.Namespace, src.Name)
// Convert the embedded v1beta1 type to v1alpha1 using the native conversion
var v1alpha1Obj addonv1alpha1.ManagedClusterAddOn
if err := addonv1beta1.Convert_v1beta1_ManagedClusterAddOn_To_v1alpha1_ManagedClusterAddOn(
&src.ManagedClusterAddOn, &v1alpha1Obj, nil); err != nil {
return fmt.Errorf("failed to convert ManagedClusterAddOn: %w", err)
}
// Set TypeMeta for the target version - the native conversion doesn't copy these fields
// We must set the hub version (v1alpha1) here, not preserve the source version
v1alpha1Obj.TypeMeta = metav1.TypeMeta{
Kind: "ManagedClusterAddOn",
APIVersion: addonv1alpha1.GroupVersion.String(),
}
// Restore installNamespace from annotation
// This field was removed in v1beta1, so we store it in annotation
if installNs, ok := src.Annotations[InstallNamespaceAnnotation]; ok {
v1alpha1Obj.Spec.InstallNamespace = installNs
}
// Manually populate deprecated ConfigReferent field in ConfigReferences
// The native conversion doesn't handle this deprecated field from v1alpha1.ConfigReference
// We need to copy from DesiredConfig.ConfigReferent if DesiredConfig exists
for i := range v1alpha1Obj.Status.ConfigReferences {
if v1alpha1Obj.Status.ConfigReferences[i].DesiredConfig != nil {
v1alpha1Obj.Status.ConfigReferences[i].ConfigReferent = v1alpha1Obj.Status.ConfigReferences[i].DesiredConfig.ConfigReferent
}
}
// Copy to the internal wrapper type
dst.ManagedClusterAddOn = v1alpha1Obj
return nil
}
// ConvertFrom converts from the Hub version (v1alpha1) to this version (v1beta1)
func (dst *ManagedClusterAddOn) ConvertFrom(srcRaw conversion.Hub) error {
src, ok := srcRaw.(*internalv1alpha1.ManagedClusterAddOn)
if !ok {
return fmt.Errorf("expected *internalv1alpha1.ManagedClusterAddOn but got %T", srcRaw)
}
klog.V(4).Infof("Converting ManagedClusterAddOn %s/%s from v1alpha1 (Hub) to v1beta1",
src.Namespace, src.Name)
// Convert the embedded v1alpha1 type to v1beta1 using the native conversion
var v1beta1Obj addonv1beta1.ManagedClusterAddOn
if err := addonv1beta1.Convert_v1alpha1_ManagedClusterAddOn_To_v1beta1_ManagedClusterAddOn(
&src.ManagedClusterAddOn, &v1beta1Obj, nil); err != nil {
return fmt.Errorf("failed to convert ManagedClusterAddOn: %w", err)
}
// Set TypeMeta for the target version - the native conversion doesn't copy these fields
// We must set the target version (v1beta1) here, not preserve the source version
v1beta1Obj.TypeMeta = metav1.TypeMeta{
Kind: "ManagedClusterAddOn",
APIVersion: addonv1beta1.GroupVersion.String(),
}
// Copy to the wrapper type
dst.ManagedClusterAddOn = v1beta1Obj
// Save installNamespace to annotation (removed in v1beta1)
// This field exists in v1alpha1 but not in v1beta1, so we preserve it in annotations
if src.Spec.InstallNamespace != "" {
if dst.ManagedClusterAddOn.Annotations == nil {
dst.ManagedClusterAddOn.Annotations = make(map[string]string)
}
dst.ManagedClusterAddOn.Annotations[InstallNamespaceAnnotation] = src.Spec.InstallNamespace
}
return nil
}