This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
/
kappcontrollerconfig_handlers.go
71 lines (57 loc) · 2.3 KB
/
kappcontrollerconfig_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2022 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package controllers
import (
"context"
"errors"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterapiutil "sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/vmware-tanzu/tanzu-framework/addons/pkg/constants"
runtanzuv1alpha3 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3"
)
// ClusterToKappControllerConfig returns a list of Requests with KappControllerConfig ObjectKey
func (r *KappControllerConfigReconciler) ClusterToKappControllerConfig(o client.Object) []ctrl.Request {
cluster, ok := o.(*clusterv1beta1.Cluster)
if !ok {
r.Log.Error(errors.New("invalid type"),
"Expected to receive Cluster resource",
"actualType", fmt.Sprintf("%T", o))
return nil
}
log := r.Log.WithValues(constants.ClusterNameLogKey, cluster.Name)
log.V(4).Info("Mapping cluster to KappControllerConfig")
KappControllerConfigList := &runtanzuv1alpha3.KappControllerConfigList{}
if err := r.Client.List(context.Background(), KappControllerConfigList); err != nil {
log.Error(err, "Error listing KappControllerConfig")
return nil
}
var requests []ctrl.Request
for i := range KappControllerConfigList.Items {
config := &KappControllerConfigList.Items[i]
if config.Namespace == cluster.Namespace {
// avoid enqueuing reconcile requests for template KappControllerConfig CRs in event handler of Cluster CR
if _, ok := config.Annotations[constants.TKGAnnotationTemplateConfig]; ok && config.Namespace == r.Config.SystemNamespace {
continue
}
// corresponding kappControllerConfig should have following ownerRef
ownerReference := metav1.OwnerReference{
APIVersion: clusterv1beta1.GroupVersion.String(),
Kind: constants.ClusterKind,
Name: cluster.Name,
UID: cluster.UID,
}
if clusterapiutil.HasOwnerRef(config.OwnerReferences, ownerReference) {
log.V(4).Info("Adding KappControllerConfig for reconciliation",
constants.NamespaceLogKey, config.Namespace, constants.NameLogKey, config.Name)
requests = append(requests, ctrl.Request{
NamespacedName: clusterapiutil.ObjectKey(config),
})
}
}
}
return requests
}