Skip to content

Commit

Permalink
Migrate ako crd to v1beta1 in akoo (#172)
Browse files Browse the repository at this point in the history
* Update v1beta1 ako crd in akoo
  • Loading branch information
chenlin07 authored Oct 27, 2023
1 parent 50b8bc6 commit 1c17e4c
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 871 deletions.
21 changes: 11 additions & 10 deletions api/v1alpha1/akodeploymentconfig_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/aviclient"
)
Expand All @@ -44,46 +45,46 @@ func (r *AKODeploymentConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Validator = &AKODeploymentConfig{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *AKODeploymentConfig) ValidateCreate() error {
func (r *AKODeploymentConfig) ValidateCreate() (admission.Warnings, error) {
akoDeploymentConfigLog.Info("validate create", "name", r.Name)

var allErrs field.ErrorList
allErrs = append(allErrs, r.validateClusterSelector(nil)...)
allErrs = append(allErrs, r.validateAVI(nil)...)
if len(allErrs) == 0 {
return nil
return nil, nil
}
return apierrors.NewInvalid(GroupVersion.WithKind("AKODeploymentConfig").GroupKind(), r.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AKODeploymentConfig").GroupKind(), r.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *AKODeploymentConfig) ValidateUpdate(old runtime.Object) error {
func (r *AKODeploymentConfig) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
akoDeploymentConfigLog.Info("validate update", "name", r.Name)
oldADC, ok := old.(*AKODeploymentConfig)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a AKODeploymentConfig but got a %T", old))
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a AKODeploymentConfig but got a %T", old))
}
var allErrs field.ErrorList
if oldADC != nil {
allErrs = append(allErrs, r.validateClusterSelector(oldADC)...)
allErrs = append(allErrs, r.validateAVI(oldADC)...)
}
if len(allErrs) == 0 {
return nil
return nil, nil
}
return apierrors.NewInvalid(GroupVersion.WithKind("AKODeploymentConfig").GroupKind(), r.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AKODeploymentConfig").GroupKind(), r.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *AKODeploymentConfig) ValidateDelete() error {
func (r *AKODeploymentConfig) ValidateDelete() (admission.Warnings, error) {
akoDeploymentConfigLog.Info("validate delete", "name", r.Name)
// should not delete the akodeploymentconfig selects management cluster
if r.Name == ManagementClusterAkoDeploymentConfig {
return field.Invalid(field.NewPath("spec", "ClusterSelector"),
return nil, field.Invalid(field.NewPath("spec", "ClusterSelector"),
r.Spec.ClusterSelector,
"can't delete akodeploymentconfig object for management cluster")
}
return nil
return nil, nil
}

// validateClusterSelector checks AKODeploymentConfig object's cluster selector field input is valid or not
Expand Down
6 changes: 3 additions & 3 deletions api/v1alpha1/akodeploymentconfig_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func TestCreateNewAKODeploymentConfig(t *testing.T) {
g.Expect(err).ShouldNot(HaveOccurred())
}

err := tc.adc.ValidateCreate()
_, err := tc.adc.ValidateCreate()
if !tc.expectErr {
g.Expect(err).ShouldNot(HaveOccurred())
} else {
Expand Down Expand Up @@ -518,7 +518,7 @@ func TestUpdateExistingAKODeploymentConfig(t *testing.T) {
err := kclient.Create(context.Background(), tc.certificateSecret)
g.Expect(err).ShouldNot(HaveOccurred())
}
err := tc.new.ValidateUpdate(tc.old)
_, err := tc.new.ValidateUpdate(tc.old)
if !tc.expectErr {
g.Expect(err).ShouldNot(HaveOccurred())
} else {
Expand Down Expand Up @@ -576,7 +576,7 @@ func TestDeleteAKODeploymentConfig(t *testing.T) {
g.Expect(err).ShouldNot(HaveOccurred())
}

err := tc.adc.ValidateDelete()
_, err := tc.adc.ValidateDelete()
if !tc.expectErr {
g.Expect(err).ShouldNot(HaveOccurred())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1"
"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/aviclient"
Expand All @@ -38,11 +37,11 @@ func (r *AKODeploymentConfigReconciler) SetupWithManager(mgr ctrl.Manager) error
return ctrl.NewControllerManagedBy(mgr).
For(&akoov1alpha1.AKODeploymentConfig{}).
Watches(
&source.Kind{Type: &clusterv1.Cluster{}},
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(handlers.AkoDeploymentConfigForCluster(r.Client, r.Log)),
).
Watches(
&source.Kind{Type: &corev1.Secret{}},
&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(r.secretToAKODeploymentConfig(r.Client, r.Log)),
).
Complete(r)
Expand Down Expand Up @@ -161,8 +160,8 @@ func (r *AKODeploymentConfigReconciler) reconcileDelete(
}

func (r *AKODeploymentConfigReconciler) secretToAKODeploymentConfig(c client.Client, log logr.Logger) handler.MapFunc {
return func(o client.Object) []reconcile.Request {
ctx := context.Background()
return func(ctx context.Context, o client.Object) []reconcile.Request {
ctx = context.Background()
secret, ok := o.(*corev1.Secret)
if !ok {
log.Error(errors.New("invalid type"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/go-logr/logr"
"github.com/vmware/alb-sdk/go/models"
"k8s.io/utils/pointer"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -28,7 +29,7 @@ import (
"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/haprovider"

akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1"
akov1alpha1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1alpha1"
akov1beta1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1beta1"

ako_operator "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako-operator"
)
Expand Down Expand Up @@ -292,7 +293,7 @@ func (r *AKODeploymentConfigReconciler) reconcileAviInfraSetting(
}

newAviInfraSetting := r.createAviInfraSetting(adc)
aviInfraSetting := &akov1alpha1.AviInfraSetting{}
aviInfraSetting := &akov1beta1.AviInfraSetting{}

if err := r.Get(ctx, client.ObjectKey{
Name: haprovider.GetAviInfraSettingName(adc),
Expand All @@ -308,39 +309,43 @@ func (r *AKODeploymentConfigReconciler) reconcileAviInfraSetting(
return res, r.Update(ctx, aviInfraSetting)
}

func (r *AKODeploymentConfigReconciler) createAviInfraSetting(adc *akoov1alpha1.AKODeploymentConfig) *akov1alpha1.AviInfraSetting {
func (r *AKODeploymentConfigReconciler) createAviInfraSetting(adc *akoov1alpha1.AKODeploymentConfig) *akov1beta1.AviInfraSetting {
// ShardVSSize describes ingress shared virtual service size, default value is SMALL
shardSize := "SMALL"
if adc.Spec.ExtraConfigs.IngressConfigs.ShardVSSize != "" {
shardSize = adc.Spec.ExtraConfigs.IngressConfigs.ShardVSSize
}

vipNetwork := []akov1alpha1.AviInfraSettingVipNetwork{{
vipNetwork := []akov1beta1.AviInfraSettingVipNetwork{{
NetworkName: adc.Spec.ControlPlaneNetwork.Name,
Cidr: adc.Spec.ControlPlaneNetwork.CIDR,
}}
//Use V6Cidr field if cidr of control plane network is IPv6
if utils.GetIPFamilyFromCidr(adc.Spec.ControlPlaneNetwork.CIDR) == "V6" {
vipNetwork = []akov1alpha1.AviInfraSettingVipNetwork{{
vipNetwork = []akov1beta1.AviInfraSettingVipNetwork{{
NetworkName: adc.Spec.ControlPlaneNetwork.Name,
V6Cidr: adc.Spec.ControlPlaneNetwork.CIDR,
}}
}

return &akov1alpha1.AviInfraSetting{
return &akov1beta1.AviInfraSetting{
ObjectMeta: metav1.ObjectMeta{
Name: haprovider.GetAviInfraSettingName(adc),
},
Spec: akov1alpha1.AviInfraSettingSpec{
SeGroup: akov1alpha1.AviInfraSettingSeGroup{
Spec: akov1beta1.AviInfraSettingSpec{
SeGroup: akov1beta1.AviInfraSettingSeGroup{
Name: adc.Spec.ServiceEngineGroup,
},
Network: akov1alpha1.AviInfraSettingNetwork{
Network: akov1beta1.AviInfraSettingNetwork{
VipNetworks: vipNetwork,
},
L7Settings: akov1alpha1.AviInfraL7Settings{
L7Settings: akov1beta1.AviInfraL7Settings{
ShardSize: shardSize,
},
// Known issue: T1LR value is required when reconciling AKODeploymentConfig
NSXSettings: akov1beta1.AviInfraNSXSettings{
T1LR: pointer.StringPtr(""),
},
},
}
}
Expand All @@ -366,7 +371,7 @@ func (r *AKODeploymentConfigReconciler) reconcileAviInfraSettingDelete(
return res, nil
}

aviInfraSetting := &akov1alpha1.AviInfraSetting{}
aviInfraSetting := &akov1beta1.AviInfraSetting{}
if err := r.Get(ctx, client.ObjectKey{
Name: haprovider.GetAviInfraSettingName(adc),
}, aviInfraSetting); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pkg/errors"
"github.com/vmware/alb-sdk/go/models"
"github.com/vmware/alb-sdk/go/session"
akov1alpha1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1alpha1"
akov1beta1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1beta1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -335,7 +335,7 @@ func intgTestAkoDeploymentConfigController() {
aviInfraSettingName = akoDeploymentConfig.Name + "-ais"
ensureRuntimeObjectMatchExpectation(client.ObjectKey{
Name: aviInfraSettingName,
}, &akov1alpha1.AviInfraSetting{}, true)
}, &akov1beta1.AviInfraSetting{}, true)

service := &corev1.Service{}
ensureRuntimeObjectMatchExpectation(client.ObjectKey{
Expand All @@ -351,7 +351,7 @@ func intgTestAkoDeploymentConfigController() {
aviInfraSettingName = akoDeploymentConfig.Name + "-ais"
ensureRuntimeObjectMatchExpectation(client.ObjectKey{
Name: aviInfraSettingName,
}, &akov1alpha1.AviInfraSetting{}, true)
}, &akov1beta1.AviInfraSetting{}, true)

service := &corev1.Service{}
ensureRuntimeObjectMatchExpectation(client.ObjectKey{
Expand Down
7 changes: 3 additions & 4 deletions controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/go-logr/logr"
"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1"
Expand All @@ -37,7 +36,7 @@ func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Watch Cluster resources.
For(&clusterv1.Cluster{}).
Watches(
&source.Kind{Type: &corev1.Service{}},
&corev1.Service{},
handler.EnqueueRequestsFromMapFunc(r.serviceToCluster(r.Client, r.Log)),
).
Complete(r)
Expand Down Expand Up @@ -127,8 +126,8 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
// serviceToCluster returns a handler map function for mapping Service
// resources to the cluster
func (r *ClusterReconciler) serviceToCluster(c client.Client, log logr.Logger) handler.MapFunc {
return func(o client.Object) []reconcile.Request {
ctx := context.Background()
return func(ctx context.Context, o client.Object) []reconcile.Request {
ctx = context.Background()
service, ok := o.(*corev1.Service)
if !ok {
log.Error(errors.New("invalid type"),
Expand Down
5 changes: 2 additions & 3 deletions controllers/machine/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// SetupWithManager adds this reconciler to a new controller then to the
Expand All @@ -33,7 +32,7 @@ func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Watch Cluster API Machine resources.
For(&clusterv1.Machine{}).
Watches(
&source.Kind{Type: &clusterv1.Cluster{}},
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(handlers.MachinesForCluster(r.Client, r.Log)),
).
Complete(r)
Expand Down Expand Up @@ -76,7 +75,7 @@ func (r *MachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
}()

// Get the name of the cluster to which the current machine belongs
clusterName, exist := obj.Labels[clusterv1.ClusterLabelName]
clusterName, exist := obj.Labels[clusterv1.ClusterNameLabel]
if !exist {
log.Info("machine doesn't have cluster name label, skip reconciling")
return res, nil
Expand Down
Loading

0 comments on commit 1c17e4c

Please sign in to comment.