Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
use annotations instead of label
Signed-off-by: Allen Li <[email protected]>
  • Loading branch information
qpdpQ committed Oct 16, 2024
commit 8094035e709f7a729e0f9d7ec88192371be24722
4 changes: 2 additions & 2 deletions controllers/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const (
//InternalOpreqLabel is the label used label the OperandRequest internally created by ODLM
OperandOnlyLabel string = "operator.ibm.com/operand-only"

//ODLMReferenceLabel is the label used to label the resources used for ODLM operand value reference
ODLMReferenceLabel string = "operator.ibm.com/referenced-by-odlm-resource"
//ODLMReferenceAnnotation is the annotation used to annotate the resources used for ODLM operand value reference
ODLMReferenceAnnotation string = "operator.ibm.com/referenced-by-odlm-resource"

//ODLMWatchedLabel is the label used to label the resources watched by ODLM for value reference
ODLMWatchedLabel string = "operator.ibm.com/watched-by-odlm"
Expand Down
6 changes: 3 additions & 3 deletions controllers/operandrequest/operandrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ func (r *Reconciler) getConfigToRequestMapper() handler.MapFunc {
func (r *Reconciler) getReferenceToRequestMapper() handler.MapFunc {
ctx := context.Background()
return func(object client.Object) []ctrl.Request {
labels := object.GetLabels()
if labels == nil {
annotations := object.GetAnnotations()
if annotations == nil {
return []ctrl.Request{}
}
odlmReference, ok := labels[constant.ODLMReferenceLabel]
odlmReference, ok := annotations[constant.ODLMReferenceAnnotation]
if !ok {
return []ctrl.Request{}
}
Expand Down
24 changes: 16 additions & 8 deletions controllers/operator/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,14 @@ func (m *ODLMOperator) GetValueRefFromConfigMap(ctx context.Context, instanceTyp
}
return "", errors.Wrapf(err, "failed to get Configmap %s/%s", cmNs, cmName)
}
labelValue := instanceType + "." + instanceNs + "." + instanceName
labelValue = util.GetFirstNCharacter(labelValue, 63)

// Set the Value Reference label for the ConfigMap
util.EnsureLabelsForConfigMap(cm, map[string]string{
constant.ODLMReferenceLabel: labelValue,
constant.ODLMWatchedLabel: "true",
constant.ODLMWatchedLabel: "true",
})
// Set the Value Reference Annotation for the ConfigMap
util.EnsureAnnotationForConfigMap(cm, map[string]string{
constant.ODLMReferenceAnnotation: instanceType + "." + instanceNs + "." + instanceName,
})
// Update the ConfigMap with the Value Reference label
if err := m.Update(ctx, cm); err != nil {
Expand Down Expand Up @@ -868,8 +870,11 @@ func (m *ODLMOperator) GetValueRefFromSecret(ctx context.Context, instanceType,

// Set the Value Reference label for the Secret
util.EnsureLabelsForSecret(secret, map[string]string{
constant.ODLMReferenceLabel: instanceType + "." + instanceNs + "." + instanceName,
constant.ODLMWatchedLabel: "true",
constant.ODLMWatchedLabel: "true",
})
// Set the Value Reference Annotation for the Secret
util.EnsureAnnotationsForSecret(secret, map[string]string{
constant.ODLMReferenceAnnotation: instanceType + "." + instanceNs + "." + instanceName,
})
// Update the Secret with the Value Reference label
if err := m.Update(ctx, secret); err != nil {
Expand Down Expand Up @@ -899,8 +904,11 @@ func (m *ODLMOperator) GetValueRefFromObject(ctx context.Context, instanceType,

// Set the Value Reference label for the object
m.EnsureLabel(obj, map[string]string{
constant.ODLMReferenceLabel: instanceType + "." + instanceNs + "." + instanceName,
constant.ODLMWatchedLabel: "true",
constant.ODLMWatchedLabel: "true",
})
// Set the Value Reference Annotation for the Secret
m.EnsureAnnotation(obj, map[string]string{
constant.ODLMReferenceAnnotation: instanceType + "." + instanceNs + "." + instanceName,
})
// Update the object with the Value Reference label
if err := m.Update(ctx, &obj); err != nil {
Expand Down
18 changes: 18 additions & 0 deletions controllers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ func EnsureLabelsForSecret(secret *corev1.Secret, labels map[string]string) {
}
}

func EnsureAnnotationsForSecret(secret *corev1.Secret, annotatinos map[string]string) {
if secret.Annotations == nil {
secret.Annotations = make(map[string]string)
}
for k, v := range annotatinos {
secret.Annotations[k] = v
}
}

func EnsureLabelsForConfigMap(cm *corev1.ConfigMap, labels map[string]string) {
if cm.Labels == nil {
cm.Labels = make(map[string]string)
Expand All @@ -311,6 +320,15 @@ func EnsureLabelsForConfigMap(cm *corev1.ConfigMap, labels map[string]string) {
}
}

func EnsureAnnotationForConfigMap(cm *corev1.ConfigMap, annotations map[string]string) {
if cm.Annotations == nil {
cm.Annotations = make(map[string]string)
}
for k, v := range annotations {
cm.Labels[k] = v
}
}

func EnsureLabelsForRoute(r *ocproute.Route, labels map[string]string) {
if r.Labels == nil {
r.Labels = make(map[string]string)
Expand Down