...

Source file src/edge-infra.dev/pkg/edge/webhooks/edge-injector/labels.go

Documentation: edge-infra.dev/pkg/edge/webhooks/edge-injector

     1  package edgeinjector
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"sigs.k8s.io/controller-runtime/pkg/client"
     8  
     9  	"edge-infra.dev/pkg/edge/api/utils"
    10  	"edge-infra.dev/pkg/edge/datasync/couchdb"
    11  	v1 "edge-infra.dev/pkg/sds/ien/k8s/apis/v1"
    12  )
    13  
    14  const (
    15  	InjectorLabel        = "injector.edge.ncr.com"
    16  	InjectorTypeLabel    = "injector.edge.ncr.com/type"
    17  	ContainerFilterLabel = "injector.edge.ncr.com/containers"
    18  )
    19  
    20  type InjectorType string
    21  
    22  const (
    23  	DirectType = InjectorType("direct")
    24  	Reference  = InjectorType("reference")
    25  )
    26  
    27  type SecretType string
    28  
    29  const (
    30  	NodeSecret      = SecretType("node-secret")
    31  	CouchDBSecret   = SecretType("couchdb-secret")
    32  	AVRequestSecret = SecretType("av-request")
    33  )
    34  
    35  type WebhookType string
    36  
    37  const (
    38  	CouchDBUser = WebhookType("couchdb-user")
    39  	Node        = WebhookType("add-node-information")
    40  )
    41  
    42  func LabelValue(obj client.Object, label string) string {
    43  	labels := obj.GetLabels()
    44  	if len(labels) == 0 {
    45  		return ""
    46  	}
    47  	return labels[label]
    48  }
    49  
    50  func AnnotationValue(obj client.Object, label string) string {
    51  	annotations := obj.GetAnnotations()
    52  	if len(annotations) == 0 {
    53  		return ""
    54  	}
    55  	return annotations[label]
    56  }
    57  
    58  func InjectionType(obj client.Object) InjectorType {
    59  	return InjectorType(LabelValue(obj, InjectorTypeLabel))
    60  }
    61  
    62  func SecretLabel(st SecretType) string {
    63  	return InjectorLabel + "/" + string(st)
    64  }
    65  
    66  func SecretLabelValue(obj client.Object, st SecretType) string {
    67  	return LabelValue(obj, SecretLabel(st))
    68  }
    69  
    70  func WebhookLabel(w WebhookType) string {
    71  	return InjectorLabel + "/" + string(w)
    72  }
    73  
    74  func WebhookLabelValue(obj client.Object, w WebhookType) string {
    75  	return LabelValue(obj, InjectorLabel+"/"+string(w))
    76  }
    77  
    78  // CouchDBUserRole trigger by webhook, user read-only-user as default value
    79  func CouchDBUserRole(obj client.Object) string {
    80  	role := LabelValue(obj, WebhookLabel(CouchDBUser))
    81  	if utils.Contains(allowedRoles, role) {
    82  		return role
    83  	}
    84  	return couchdb.ReadOnlyUser
    85  }
    86  
    87  // Containers allows us to specify which container to inject
    88  func Containers(obj client.Object) []string {
    89  	containers := AnnotationValue(obj, ContainerFilterLabel)
    90  	if len(containers) > 0 {
    91  		return strings.Split(containers, ",")
    92  	}
    93  	return nil
    94  }
    95  
    96  func IsCustomLabel(key string) bool {
    97  	return strings.HasPrefix(key, fmt.Sprintf(v1.CustomNodeLabel, ""))
    98  }
    99  

View as plain text