Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Error out when the owner cluster of a VSphereCPIConfig is not found
Browse files Browse the repository at this point in the history
Fix logger

Resolve conmment

Enqueue VSphereCluster intead of Cluster

Fix readme for lint

Rmove log

Fix the cluster type

Skip cpiconfig if it's template
  • Loading branch information
lubronzhan committed Sep 15, 2022
1 parent 894273e commit f7e72b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
16 changes: 8 additions & 8 deletions addons/controllers/cpi/vspherecpiconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ var vsphereCPIProviderServiceAccountAggregatedClusterRole = &rbacv1.ClusterRole{

// Reconcile the VSphereCPIConfig CRD
func (r *VSphereCPIConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Log = r.Log.WithValues("VSphereCPIConfig", req.NamespacedName)
log := r.Log.WithValues("VSphereCPIConfig", req.NamespacedName)

r.Log.Info("Start reconciliation for VSphereCPIConfig")
log.Info("Start reconciliation for VSphereCPIConfig")

// fetch VSphereCPIConfig resource
cpiConfig := &cpiv1alpha1.VSphereCPIConfig{}
if err := r.Client.Get(ctx, req.NamespacedName, cpiConfig); err != nil {
if apierrors.IsNotFound(err) {
r.Log.Info("VSphereCPIConfig resource not found")
log.Info("VSphereCPIConfig resource not found")
return ctrl.Result{}, nil
}
r.Log.Error(err, "Unable to fetch VSphereCPIConfig resource")
log.Error(err, "Unable to fetch VSphereCPIConfig resource")
return ctrl.Result{}, err
}

Expand All @@ -104,11 +104,11 @@ func (r *VSphereCPIConfigReconciler) Reconcile(ctx context.Context, req ctrl.Req
return ctrl.Result{}, err // no need to requeue if cluster is not found
}
if cpiConfig.Spec.VSphereCPI.Mode == nil {
r.Log.Info("VSphere CPI mode is not provided.")
log.Info("VSphere CPI mode is not provided.")
return ctrl.Result{}, nil // no need to requeue if CPI mode is not provided
}
if res, err := r.reconcileVSphereCPIConfig(ctx, cpiConfig, cluster); err != nil {
r.Log.Error(err, "Failed to reconcile VSphereCPIConfig")
log.Error(err, "Failed to reconcile VSphereCPIConfig")
return res, err
}
return ctrl.Result{}, nil
Expand Down Expand Up @@ -247,8 +247,8 @@ func (r *VSphereCPIConfigReconciler) SetupWithManager(_ context.Context, mgr ctr
WithOptions(options).
WithEventFilter(predicates.ConfigOfKindWithoutAnnotation(constants.TKGAnnotationTemplateConfig, constants.VSphereCPIConfigKind, r.Config.SystemNamespace, r.Log)).
Watches(
&source.Kind{Type: &clusterapiv1beta1.Cluster{}},
handler.EnqueueRequestsFromMapFunc(r.ClustersToVSphereCPIConfig),
&source.Kind{Type: &capvvmwarev1beta1.VSphereCluster{}},
handler.EnqueueRequestsFromMapFunc(r.VSphereClusterToVSphereCPIConfig),
).
Complete(r)
}
23 changes: 13 additions & 10 deletions addons/controllers/cpi/vspherecpiconfig_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ import (
pkgtypes "github.com/vmware-tanzu/tanzu-framework/addons/pkg/types"
"github.com/vmware-tanzu/tanzu-framework/addons/pkg/util"
cpiv1alpha1 "github.com/vmware-tanzu/tanzu-framework/apis/addonconfigs/cpi/v1alpha1"
"github.com/vmware-tanzu/tanzu-framework/tkg/log"
)

// ClustersToVSphereCPIConfig returns a list of Requests with VSphereCPIConfig ObjectKey based on Cluster events
func (r *VSphereCPIConfigReconciler) ClustersToVSphereCPIConfig(o client.Object) []ctrl.Request {
r.Log.V(4).Info("Clusters to VSphereCPIConfig handler")

cluster, ok := o.(*clusterapiv1beta1.Cluster)
// VSphereClusterToVSphereCPIConfig returns a list of Requests with VSphereCPIConfig ObjectKey based on Cluster events
func (r *VSphereCPIConfigReconciler) VSphereClusterToVSphereCPIConfig(o client.Object) []ctrl.Request {
cluster, ok := o.(*capvvmwarev1beta1.VSphereCluster)
if !ok {
r.Log.Error(errors.New("invalid type"),
"Expected to receive Cluster resource",
"actualType", fmt.Sprintf("%T", o))
return nil
}

log.V(4).Info("Mapping Cluster to VSphereCPIConfig")
r.Log.V(4).Info("Mapping VSphereCluster to VSphereCPIConfig")

cs := &cpiv1alpha1.VSphereCPIConfigList{}
_ = r.List(context.Background(), cs)
Expand All @@ -52,6 +49,12 @@ func (r *VSphereCPIConfigReconciler) ClustersToVSphereCPIConfig(o client.Object)
for i := 0; i < len(cs.Items); i++ {
config := &cs.Items[i]
if config.Namespace == cluster.Namespace {

// avoid enqueuing reconcile requests for template vSphereCSIConfig CRs in event handler of ConfigMap CR
if _, ok := config.Annotations[constants.TKGAnnotationTemplateConfig]; ok {
continue
}

// corresponding vsphereCPIConfig should have following ownerRef
ownerReference := metav1.OwnerReference{
APIVersion: clusterapiv1beta1.GroupVersion.String(),
Expand All @@ -60,7 +63,7 @@ func (r *VSphereCPIConfigReconciler) ClustersToVSphereCPIConfig(o client.Object)
UID: cluster.UID,
}
if clusterapiutil.HasOwnerRef(config.OwnerReferences, ownerReference) || config.Name == cluster.Name {
log.V(4).Info("Adding VSphereCPIConfig for reconciliation",
r.Log.V(4).Info("Adding VSphereCPIConfig for reconciliation",
constants.NamespaceLogKey, config.Namespace, constants.NameLogKey, config.Name)

requests = append(requests, ctrl.Request{
Expand Down Expand Up @@ -413,8 +416,8 @@ func (r *VSphereCPIConfigReconciler) getOwnerCluster(ctx context.Context, cpiCon
}
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: cpiConfig.Namespace, Name: clusterName}, cluster); err != nil {
if apierrors.IsNotFound(err) {
r.Log.Info(fmt.Sprintf("Cluster resource '%s/%s' not found", cpiConfig.Namespace, clusterName))
return nil, nil
r.Log.Error(err, fmt.Sprintf("Cluster resource '%s/%s' not found", cpiConfig.Namespace, clusterName))
return nil, err
}
r.Log.Error(err, fmt.Sprintf("Unable to fetch cluster '%s/%s'", cpiConfig.Namespace, clusterName))
return nil, err
Expand Down
1 change: 0 additions & 1 deletion pkg/v1/providers/tests/clustergen/README_ccgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ e.g. this picks the first 10 cases using the azure infra
export CASES=$(for i in `grep azure:v1. testdata/*.case | \
cut -d: -f1 | uniq | cut -d/ -f7 | head -10`; do echo -n "$i "; done; echo)


## Details on dry-run generation of ClusterClass-based configuration

Since the clustergen tests are expected to generate configuration entirely
Expand Down

0 comments on commit f7e72b2

Please # to comment.