Skip to content

Commit

Permalink
Stop lowercasing Kubernetes Kind strings
Browse files Browse the repository at this point in the history
If we keep "Pod" as-is, not "pod", this makes things simpler when
mappping to Resource and other things in the Kubiverse.
  • Loading branch information
bboreham committed May 7, 2021
1 parent f45fccc commit ca7ddb2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions controllers/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func objectFromEvent(ctx context.Context, client client.Client, event *corev1.Ev
break
}
ret.actor = ret.object
ret.object = objectReference{Kind: "replicaset", Namespace: lc(ret.object.Namespace), Name: lc(name)}
ret.object = objectReference{Kind: "ReplicaSet", Namespace: lc(ret.object.Namespace), Name: lc(name)}
case event.Source.Component == "replicaset-controller" && event.InvolvedObject.Kind == "ReplicaSet":
// if we have a message like "Created pod: foo-5c5df9754b-4w2hj"; extract the Pod name
name := extractWordAfter(event.Message, "pod: ")
if name == "" {
break
}
ret.actor = ret.object
ret.object = objectReference{Kind: "pod", Namespace: lc(ret.object.Namespace), Name: lc(name)}
ret.object = objectReference{Kind: "Pod", Namespace: lc(ret.object.Namespace), Name: lc(name)}
apiVersion = "v1"
case event.Source.Component == "statefulset-controller" && event.InvolvedObject.Kind == "StatefulSet":
// if we have a message like "create Pod ingester-3 in StatefulSet ingester successful"; extract the Pod name
Expand All @@ -79,7 +79,7 @@ func objectFromEvent(ctx context.Context, client client.Client, event *corev1.Ev
break
}
ret.actor = ret.object
ret.object = objectReference{Kind: "pod", Namespace: lc(ret.object.Namespace), Name: lc(name)}
ret.object = objectReference{Kind: "Pod", Namespace: lc(ret.object.Namespace), Name: lc(name)}
apiVersion = "v1"
}

Expand Down
4 changes: 2 additions & 2 deletions controllers/events/event_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestDeploymentRolloutFromFlux(t *testing.T) {
name: "flux-event-later",
perm: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
wantTraces: []string{
"0: flux deployment.Sync Commit e332e7bac962: Update nginx",
"0: flux Deployment.Sync Commit e332e7bac962: Update nginx",
"1: deployment-controller Deployment.ScalingReplicaSet (0) Scaled up replica set hello-world-f77b4f6c8 to 1",
"2: replicaset-controller ReplicaSet.SuccessfulCreate (1) Created pod: hello-world-f77b4f6c8-6tcj2",
"3: default-scheduler Pod.Scheduled (2) Successfully assigned default/hello-world-f77b4f6c8-6tcj2 to node2",
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestStsRolloutFromFlux(t *testing.T) {
name: "flux-sts",
perm: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
wantTraces: []string{
"0: flux statefulset.Sync Commit fc4e825b46ac: Update ingester to latest, in dev",
"0: flux StatefulSet.Sync Commit fc4e825b46ac: Update ingester to latest, in dev",
"1: statefulset-controller StatefulSet.SuccessfulDelete (0) delete Pod ingester-3 in StatefulSet ingester successful",
"2: kubelet Pod.Killing (1) Stopping container ingester",
"3: statefulset-controller StatefulSet.SuccessfulCreate (0) create Pod ingester-3 in StatefulSet ingester successful",
Expand Down
4 changes: 2 additions & 2 deletions controllers/events/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ var fluxDeploymentUpdateEvents = []string{`
firstTimestamp: "2020-12-03T17:53:52Z"
involvedObject:
apiVersion: apps/v1
kind: deployment
kind: Deployment
name: hello-world
namespace: default
kind: Event
Expand Down Expand Up @@ -3543,7 +3543,7 @@ eventTime: null
firstTimestamp: "2021-02-17T14:20:30Z"
involvedObject:
apiVersion: apps/v1
kind: statefulset
kind: StatefulSet
name: ingester
namespace: cortex
kind: Event
Expand Down
9 changes: 4 additions & 5 deletions controllers/events/mocks_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ func newMockRESTMapper() meta.RESTMapper {
mapper, _ := apiutil.NewDynamicRESTMapper(cfg, apiutil.WithCustomMapper(func() (meta.RESTMapper, error) {
baseMapper := meta.NewDefaultRESTMapper(nil)
// Add the object kinds that we use in fixtures.
baseMapper.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "deployment"}, meta.RESTScopeNamespace)
baseMapper.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, meta.RESTScopeNamespace) // TODO: check why we need this
baseMapper.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "replicaset"}, meta.RESTScopeNamespace)
baseMapper.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "statefulset"}, meta.RESTScopeNamespace)
baseMapper.Add(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "pod"}, meta.RESTScopeNamespace)
baseMapper.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, meta.RESTScopeNamespace)
baseMapper.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "ReplicaSet"}, meta.RESTScopeNamespace)
baseMapper.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}, meta.RESTScopeNamespace)
baseMapper.Add(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}, meta.RESTScopeNamespace)

return baseMapper, nil
}))
Expand Down
9 changes: 5 additions & 4 deletions controllers/events/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,23 @@ func getObject(ctx context.Context, c client.Client, apiVersion, kind, namespace
return obj, errors.Wrap(err, "unable to get object")
}

// canonicalise strings via lowercase - we see "deployment" vs "Deployment"
// Canonicalise strings via lowercase - Kubernetes is case-independent.
// (we don't canonicalise Kind)
func lc(s string) string {
return strings.ToLower(s)
}

func refFromObjRef(oRef corev1.ObjectReference) objectReference {
return objectReference{
Kind: lc(oRef.Kind),
Kind: oRef.Kind,
Namespace: lc(oRef.Namespace),
Name: lc(oRef.Name),
}
}

func refFromOwner(oRef v1.OwnerReference, namespace string) objectReference {
return objectReference{
Kind: lc(oRef.Kind),
Kind: oRef.Kind,
Namespace: lc(namespace),
Name: lc(oRef.Name),
}
Expand All @@ -145,7 +146,7 @@ func refFromOwner(oRef v1.OwnerReference, namespace string) objectReference {
func refFromObject(obj v1.Object) objectReference {
ty := obj.(v1.Type)
return objectReference{
Kind: lc(ty.GetKind()),
Kind: ty.GetKind(),
Namespace: lc(obj.GetNamespace()),
Name: lc(obj.GetName()),
}
Expand Down

0 comments on commit ca7ddb2

Please sign in to comment.