events: avoid having to keep individal kind maps

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
pull/3628/head v0.41.0
Hidde Beydals 2 years ago
parent 3f3d68a33a
commit af153ea0cf
No known key found for this signature in database
GPG Key ID: 979F380FC2341744

@ -271,11 +271,6 @@ func getHeaders(showNs bool) []string {
return headers return headers
} }
var fluxKinds = []string{sourcev1.GitRepositoryKind, sourcev1.HelmRepositoryKind, sourcev1.OCIRepositoryKind,
sourcev1.BucketKind, sourcev1.HelmChartKind, kustomizev1.KustomizationKind, helmv2.HelmReleaseKind,
notificationv1.AlertKind, notificationv1.ProviderKind, imagev1.ImageRepositoryKind, imagev1.ImagePolicyKind,
autov1.ImageUpdateAutomationKind}
func getEventRow(e corev1.Event, showNs bool) []string { func getEventRow(e corev1.Event, showNs bool) []string {
var row []string var row []string
if showNs { if showNs {
@ -291,7 +286,7 @@ func getEventRow(e corev1.Event, showNs bool) []string {
// and a string with the format `<kind>/<name>.<namespace>` if it does. // and a string with the format `<kind>/<name>.<namespace>` if it does.
func getObjectRef(ctx context.Context, kubeclient client.Client, selector string, ns string) ([]string, error) { func getObjectRef(ctx context.Context, kubeclient client.Client, selector string, ns string) ([]string, error) {
kind, name := utils.ParseObjectKindName(selector) kind, name := utils.ParseObjectKindName(selector)
ref, err := getGroupVersionAndRef(kind, name, ns) ref, err := fluxKindMap.getRefInfo(kind)
if err != nil { if err != nil {
return nil, fmt.Errorf("error getting groupversion: %w", err) return nil, fmt.Errorf("error getting groupversion: %w", err)
} }
@ -353,69 +348,77 @@ func getObjectRef(ctx context.Context, kubeclient client.Client, selector string
} }
allRefs = append(allRefs, fmt.Sprintf("%s/%s.%s", refKind, refName, refNamespace)) allRefs = append(allRefs, fmt.Sprintf("%s/%s.%s", refKind, refName, refNamespace))
for _, ref := range ref.otherRefs { if ref.otherRefs != nil {
allRefs = append(allRefs, fmt.Sprintf("%s.%s", ref, refNamespace)) for _, otherRef := range ref.otherRefs(ns, name) {
allRefs = append(allRefs, fmt.Sprintf("%s.%s", otherRef, refNamespace))
}
} }
return allRefs, nil return allRefs, nil
} }
type refMap map[string]refInfo
func (r refMap) getRefInfo(kind string) (refInfo, error) {
for key, ref := range r {
if strings.EqualFold(key, kind) {
return ref, nil
}
}
return refInfo{}, fmt.Errorf("'%s' is not a recognized Flux kind", kind)
}
func (r refMap) hasKind(kind string) bool {
_, err := r.getRefInfo(kind)
return err == nil
}
type refInfo struct { type refInfo struct {
gv schema.GroupVersion gv schema.GroupVersion
kind string kind string
crossNamespaced bool crossNamespaced bool
otherRefs []string otherRefs func(namespace, name string) []string
field []string field []string
} }
func getGroupVersionAndRef(kind, name, ns string) (refInfo, error) { var fluxKindMap = refMap{
switch strings.ToLower(kind) { kustomizev1.KustomizationKind: {
case strings.ToLower(kustomizev1.KustomizationKind): gv: kustomizev1.GroupVersion,
return refInfo{ crossNamespaced: true,
gv: kustomizev1.GroupVersion, field: []string{"spec", "sourceRef"},
crossNamespaced: true, },
field: []string{"spec", "sourceRef"}, helmv2.HelmReleaseKind: {
}, nil gv: helmv2.GroupVersion,
case strings.ToLower(helmv2.HelmReleaseKind): crossNamespaced: true,
return refInfo{ otherRefs: func(namespace, name string) []string {
gv: helmv2.GroupVersion, return []string{fmt.Sprintf("%s/%s-%s", sourcev1.HelmChartKind, namespace, name)}
crossNamespaced: true, },
otherRefs: []string{fmt.Sprintf("%s/%s-%s", sourcev1.HelmChartKind, ns, name)}, field: []string{"spec", "chart", "spec", "sourceRef"},
field: []string{"spec", "chart", "spec", "sourceRef"}, },
}, nil notificationv1.AlertKind: {
case strings.ToLower(notificationv1.AlertKind): gv: notificationv1.GroupVersion,
return refInfo{ kind: notificationv1.ProviderKind,
gv: notificationv1.GroupVersion, crossNamespaced: false,
kind: notificationv1.ProviderKind, field: []string{"spec", "providerRef"},
crossNamespaced: false, },
field: []string{"spec", "providerRef"}, notificationv1.ReceiverKind: {gv: notificationv1.GroupVersion},
}, nil notificationv1.ProviderKind: {gv: notificationv1.GroupVersion},
case strings.ToLower(notificationv1.ReceiverKind), imagev1.ImagePolicyKind: {
strings.ToLower(notificationv1.ProviderKind): gv: imagev1.GroupVersion,
return refInfo{ kind: imagev1.ImageRepositoryKind,
gv: notificationv1.GroupVersion, crossNamespaced: true,
}, nil field: []string{"spec", "imageRepositoryRef"},
case strings.ToLower(imagev1.ImagePolicyKind): },
return refInfo{ sourcev1.GitRepositoryKind: {gv: sourcev1.GroupVersion},
gv: imagev1.GroupVersion, sourcev1.OCIRepositoryKind: {gv: sourcev1.GroupVersion},
kind: imagev1.ImageRepositoryKind, sourcev1.BucketKind: {gv: sourcev1.GroupVersion},
crossNamespaced: true, sourcev1.HelmRepositoryKind: {gv: sourcev1.GroupVersion},
field: []string{"spec", "imageRepositoryRef"}, sourcev1.HelmChartKind: {gv: sourcev1.GroupVersion},
}, nil autov1.ImageUpdateAutomationKind: {gv: autov1.GroupVersion},
case strings.ToLower(sourcev1.GitRepositoryKind), strings.ToLower(sourcev1.HelmChartKind), strings.ToLower(sourcev1.BucketKind), imagev1.ImageRepositoryKind: {gv: imagev1.GroupVersion},
strings.ToLower(sourcev1.HelmRepositoryKind), strings.ToLower(sourcev1.OCIRepositoryKind):
return refInfo{gv: sourcev1.GroupVersion}, nil
case strings.ToLower(autov1.ImageUpdateAutomationKind):
return refInfo{gv: autov1.GroupVersion}, nil
case strings.ToLower(imagev1.ImageRepositoryKind):
return refInfo{gv: imagev1.GroupVersion}, nil
default:
return refInfo{}, fmt.Errorf("'%s' is not a recognized Flux kind", kind)
}
} }
func ignoreEvent(e corev1.Event) bool { func ignoreEvent(e corev1.Event) bool {
if !utils.ContainsItemString(fluxKinds, e.InvolvedObject.Kind) { if !fluxKindMap.hasKind(e.InvolvedObject.Kind) {
return true return true
} }

Loading…
Cancel
Save