|
| 1 | +package annotations |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/haproxytech/client-native/v2/models" |
| 7 | + |
| 8 | + "github.com/haproxytech/kubernetes-ingress/controller/annotations/common" |
| 9 | + "github.com/haproxytech/kubernetes-ingress/controller/store" |
| 10 | +) |
| 11 | + |
| 12 | +// ModelDefaults takes an annotation holding the path of a defaults cr and returns corresponding Defaults model |
| 13 | +func ModelDefaults(name, defaultNS string, k store.K8s, annotations ...map[string]string) (defaults *models.Defaults, err error) { |
| 14 | + d, modelErr := model(name, defaultNS, 2, k, annotations...) |
| 15 | + if modelErr != nil { |
| 16 | + err = modelErr |
| 17 | + return |
| 18 | + } |
| 19 | + if d != nil { |
| 20 | + defaults = d.(*models.Defaults) |
| 21 | + } |
| 22 | + return |
| 23 | +} |
| 24 | + |
| 25 | +// ModelGlobal takes an annotation holding the path of a global cr and returns corresponding Global model |
| 26 | +func ModelGlobal(name, defaultNS string, k store.K8s, annotations ...map[string]string) (global *models.Global, err error) { |
| 27 | + g, modelErr := model(name, defaultNS, 0, k, annotations...) |
| 28 | + if modelErr != nil { |
| 29 | + err = modelErr |
| 30 | + return |
| 31 | + } |
| 32 | + if g != nil { |
| 33 | + global = g.(*models.Global) |
| 34 | + } |
| 35 | + return |
| 36 | +} |
| 37 | + |
| 38 | +// ModelLog takes an annotation holding the path of a global cr and returns corresponding LogTargerts model |
| 39 | +func ModelLog(name, defaultNS string, k store.K8s, annotations ...map[string]string) (log models.LogTargets, err error) { |
| 40 | + l, modelErr := model(name, defaultNS, 1, k, annotations...) |
| 41 | + if modelErr != nil { |
| 42 | + err = modelErr |
| 43 | + return |
| 44 | + } |
| 45 | + if l != nil { |
| 46 | + log = l.(models.LogTargets) |
| 47 | + } |
| 48 | + return |
| 49 | +} |
| 50 | + |
| 51 | +func model(name, defaultNS string, crType int, k store.K8s, annotations ...map[string]string) (model interface{}, err error) { |
| 52 | + var crNS, crName string |
| 53 | + crNS, crName, err = common.GetK8sPath(name, annotations...) |
| 54 | + if err != nil { |
| 55 | + err = fmt.Errorf("annotation '%s': %w", name, err) |
| 56 | + return |
| 57 | + } |
| 58 | + if crName == "" { |
| 59 | + return |
| 60 | + } |
| 61 | + if crNS == "" { |
| 62 | + crNS = defaultNS |
| 63 | + } |
| 64 | + ns, nsOk := k.Namespaces[crNS] |
| 65 | + if !nsOk { |
| 66 | + return nil, fmt.Errorf("annotation %s: custom resource '%s/%s' doest not exist, namespace not found", name, crNS, crName) |
| 67 | + } |
| 68 | + switch crType { |
| 69 | + case 0: |
| 70 | + global, globalOk := ns.CRs.Global[crName] |
| 71 | + if !globalOk { |
| 72 | + return nil, fmt.Errorf("annotation %s: custom resource '%s/%s' doest not exist", name, crNS, crName) |
| 73 | + } |
| 74 | + return global, nil |
| 75 | + case 1: |
| 76 | + lg, lgOk := ns.CRs.LogTargets[crName] |
| 77 | + if !lgOk { |
| 78 | + return nil, fmt.Errorf("annotation %s: custom resource '%s/%s' doest not exist", name, crNS, crName) |
| 79 | + } |
| 80 | + return lg, nil |
| 81 | + case 2: |
| 82 | + defaults, defaultsOk := ns.CRs.Defaults[crName] |
| 83 | + if !defaultsOk { |
| 84 | + return nil, fmt.Errorf("annotation %s: custom resource '%s/%s' doest not exist", name, crNS, crName) |
| 85 | + } |
| 86 | + return defaults, nil |
| 87 | + } |
| 88 | + return nil, nil |
| 89 | +} |
0 commit comments