Skip to content

Commit

Permalink
label length limitation (#1092)
Browse files Browse the repository at this point in the history
* label length limitation

Signed-off-by: Allen Li <[email protected]>

* remove string conversion

Signed-off-by: Allen Li <[email protected]>

* update var name

Signed-off-by: Allen Li <[email protected]>

* use annotations instead of label

Signed-off-by: Allen Li <[email protected]>

---------

Signed-off-by: Allen Li <[email protected]>
  • Loading branch information
qpdpQ authored Oct 17, 2024
1 parent 962c1ad commit 8fbfdcd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
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
31 changes: 21 additions & 10 deletions controllers/operator/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,22 +473,24 @@ func (m *ODLMOperator) GetClusterServiceVersionList(ctx context.Context, sub *ol

packageName := sub.Spec.Package
csvNamespace := sub.Namespace
labelKey := packageName + "." + csvNamespace
labelKey = util.GetFirstNCharacter(labelKey, 63)

// Get the ClusterServiceVersion list with label operators.coreos.com/packageName.csvNamespace=''
csvList := &olmv1alpha1.ClusterServiceVersionList{}
opts := []client.ListOption{
client.MatchingLabels{fmt.Sprintf("operators.coreos.com/%s.%s", packageName, csvNamespace): ""},
client.MatchingLabels{fmt.Sprintf("operators.coreos.com/%s", labelKey): ""},
client.InNamespace(csvNamespace),
}

if err := m.Reader.List(ctx, csvList, opts...); err != nil {
if apierrors.IsNotFound(err) || len(csvList.Items) == 0 {
klog.V(3).Infof("No ClusterServiceVersion found with label operators.coreos.com/%s.%s", packageName, csvNamespace)
klog.V(3).Infof("No ClusterServiceVersion found with label operators.coreos.com/%s", labelKey)
return nil, nil
}
return nil, errors.Wrapf(err, "failed to list ClusterServiceVersions with label operators.coreos.com/%s.%s", packageName, csvNamespace)
return nil, errors.Wrapf(err, "failed to list ClusterServiceVersions with label operators.coreos.com/%s", labelKey)
} else if len(csvList.Items) > 1 {
klog.Warningf("Multiple ClusterServiceVersions found with label operators.coreos.com/%s.%s", packageName, csvNamespace)
klog.Warningf("Multiple ClusterServiceVersions found with label operators.coreos.com/%s", labelKey)
}

var csvs []*olmv1alpha1.ClusterServiceVersion
Expand Down Expand Up @@ -836,8 +838,11 @@ func (m *ODLMOperator) GetValueRefFromConfigMap(ctx context.Context, instanceTyp

// Set the Value Reference label for the ConfigMap
util.EnsureLabelsForConfigMap(cm, map[string]string{
constant.ODLMReferenceLabel: instanceType + "." + instanceNs + "." + instanceName,
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 @@ -865,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 @@ -896,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
25 changes: 25 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 Expand Up @@ -525,3 +543,10 @@ func addField(obj map[string]interface{}, fields []string, value interface{}) {
// Add the value to the map
addField(obj[fields[0]].(map[string]interface{}), fields[1:], value)
}

func GetFirstNCharacter(str string, n int) string {
if n >= len(str) {
return str
}
return str[:n]
}

0 comments on commit 8fbfdcd

Please sign in to comment.