Skip to content

Commit

Permalink
address comments, rename
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecutalo committed Jun 26, 2023
1 parent a6c433e commit 486f9f0
Show file tree
Hide file tree
Showing 24 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion backend/service/k8s/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestProcessInformerEvent(t *testing.T) {
Name: "test-pod-1",
Namespace: "testing-namespace",
Labels: map[string]string{
clusterClutchNameLabel: "cluster",
clutchLabelClusterName: "cluster",
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *svc) ListConfigMaps(ctx context.Context, clientset, cluster, namespace
}

func protoForConfigMap(cluster string, k8sconfigMap *v1.ConfigMap) *k8sapiv1.ConfigMap {
clusterName := GetKubeCluster(k8sconfigMap)
clusterName := GetKubeClusterName(k8sconfigMap)
if clusterName == "" {
clusterName = cluster
}
Expand Down
6 changes: 3 additions & 3 deletions backend/service/k8s/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestProtoForConfigMap(t *testing.T) {
configMap: &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"cluster.clutch.sh/name": "production",
clutchLabelClusterName: "production",
},
},
},
Expand All @@ -183,7 +183,7 @@ func TestProtoForConfigMap(t *testing.T) {
configMap: &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"cluster.clutch.sh/name": "staging",
clutchLabelClusterName: "staging",
},
},
},
Expand All @@ -195,7 +195,7 @@ func TestProtoForConfigMap(t *testing.T) {
configMap: &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"cluster.clutch.sh/name": "test",
clutchLabelClusterName: "test",
},
},
Data: map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *svc) DeleteCronJob(ctx context.Context, clientset, cluster, namespace,
}

func ProtoForCronJob(cluster string, k8scronJob *v1beta1.CronJob) *k8sapiv1.CronJob {
clusterName := GetKubeCluster(k8scronJob)
clusterName := GetKubeClusterName(k8scronJob)
if clusterName == "" {
clusterName = cluster
}
Expand Down
4 changes: 2 additions & 2 deletions backend/service/k8s/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestProtoForCron(t *testing.T) {
cron: &v1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
Name: "test1",
},
Expand All @@ -102,7 +102,7 @@ func TestProtoForCron(t *testing.T) {
cron: &v1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "",
clutchLabelClusterName: "",
},
Name: "test2",
},
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *svc) ListDeployments(ctx context.Context, clientset, cluster, namespace
}

func ProtoForDeployment(cluster string, deployment *appsv1.Deployment) *k8sapiv1.Deployment {
clusterName := GetKubeCluster(deployment)
clusterName := GetKubeClusterName(deployment)
if clusterName == "" {
clusterName = cluster
}
Expand Down
4 changes: 2 additions & 2 deletions backend/service/k8s/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestProtoForDeploymentClusterName(t *testing.T) {
deployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
},
Expand All @@ -239,7 +239,7 @@ func TestProtoForDeploymentClusterName(t *testing.T) {
deployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "",
clutchLabelClusterName: "",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *svc) ListEvents(ctx context.Context, clientset, cluster, namespace, obj
}

func ProtoForEvent(cluster string, k8sEvent *corev1.Event) *k8sapiv1.Event {
clusterName := GetKubeCluster(k8sEvent)
clusterName := GetKubeClusterName(k8sEvent)
if clusterName == "" {
clusterName = cluster
}
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *svc) DescribeHPA(ctx context.Context, clientset, cluster, namespace, na
}

func ProtoForHPA(cluster string, autoscaler *autoscalingv1.HorizontalPodAutoscaler) *k8sapiv1.HPA {
clusterName := GetKubeCluster(autoscaler)
clusterName := GetKubeClusterName(autoscaler)
if clusterName == "" {
clusterName = cluster
}
Expand Down
4 changes: 2 additions & 2 deletions backend/service/k8s/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestProtoForHPAClusterName(t *testing.T) {
hpa: &autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
Expand All @@ -151,7 +151,7 @@ func TestProtoForHPAClusterName(t *testing.T) {
hpa: &autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "",
clutchLabelClusterName: "",
},
},
Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *svc) ListJobs(ctx context.Context, clientset, cluster, namespace string
}

func protoForJob(cluster string, k8sJob *v1.Job) *k8sapiv1.Job {
clusterName := GetKubeCluster(k8sJob)
clusterName := GetKubeClusterName(k8sJob)
if clusterName == "" {
clusterName = cluster
}
Expand Down
4 changes: 2 additions & 2 deletions backend/service/k8s/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestProtoForJob(t *testing.T) {
job: &v1.Job{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
},
Expand All @@ -197,7 +197,7 @@ func TestProtoForJob(t *testing.T) {
job: &v1.Job{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "staging",
clutchLabelClusterName: "staging",
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions backend/service/k8s/kube_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Previously we relied on the `clusterName` field on objectmeta
// https://github.com/kubernetes/apimachinery/blob/2456ebdaba229616fab2161a615148884b46644b/pkg/apis/meta/v1/types.go#L266-L270
// This has since be depreacted as of https://github.com/kubernetes/kubernetes/commit/331525670b772eb8956b7f5204078c51c00aaef3
// This has since be deprecated as of https://github.com/kubernetes/kubernetes/commit/331525670b772eb8956b7f5204078c51c00aaef3
// and there was no replacement to this objectmeta field.
//
// To replace this we utilize our own label to denote which cluster the object belongs to,
Expand All @@ -15,17 +15,17 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

const clusterClutchNameLabel = "cluster.clutch.sh/name"
const clutchLabelClusterName = "cluster.clutch.sh/name"

func GetKubeCluster(obj any) string {
func GetKubeClusterName(obj any) string {
objectMeta, err := meta.Accessor(obj)
if err != nil {
// Callers are expected to handle nil cases
return ""
}

labels := objectMeta.GetLabels()
if cluster, ok := labels[clusterClutchNameLabel]; ok {
if cluster, ok := labels[clutchLabelClusterName]; ok {
return cluster
}

Expand All @@ -39,7 +39,7 @@ func ApplyClusterLabels(cluster string, obj runtime.Object) error {
}

objMeta.SetLabels(labels.Merge(objMeta.GetLabels(), labels.Set{
clusterClutchNameLabel: cluster,
clutchLabelClusterName: cluster,
}))

return nil
Expand Down
8 changes: 4 additions & 4 deletions backend/service/k8s/kube_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

func TestGetKubeCluster(t *testing.T) {
func TestGetKubeClusterName(t *testing.T) {
t.Parallel()
tests := []struct {
name string
Expand All @@ -21,7 +21,7 @@ func TestGetKubeCluster(t *testing.T) {
name: "should find cluster",
obj: &metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "meow",
clutchLabelClusterName: "meow",
},
},
expect: "meow",
Expand All @@ -39,7 +39,7 @@ func TestGetKubeCluster(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.expect, GetKubeCluster(tt.obj))
assert.Equal(t, tt.expect, GetKubeClusterName(tt.obj))
})
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestApplyClusterLabels(t *testing.T) {
err := ApplyClusterLabels(tt.cluster, tt.obj)
if !tt.shouldError {
assert.NoError(t, err)
assert.Equal(t, tt.cluster, GetKubeCluster(tt.obj))
assert.Equal(t, tt.cluster, GetKubeClusterName(tt.obj))
} else {
assert.Error(t, err)
}
Expand Down
5 changes: 2 additions & 3 deletions backend/service/k8s/lightweightinformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewLightweightInformer(
}

incomingObjectMeta.SetLabels(labels.Merge(incomingObjectMeta.GetLabels(), labels.Set{
clusterClutchNameLabel: clusterName,
clutchLabelClusterName: clusterName,
}))

switch d.Type {
Expand All @@ -91,8 +91,7 @@ func NewLightweightInformer(
if err := cacheStore.Add(lightweightObj); err != nil {
return err
}
// (mikecutalo) : double check what isInInitialList is doing
h.OnAdd(d.Object, false)
h.OnAdd(d.Object, isInInitialList)
}
case cache.Deleted:
if err := cacheStore.Delete(lightweightObj); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGetPodLogs(t *testing.T) {
Namespace: "testing-namespace",
Labels: map[string]string{
"foo": "bar",
clusterClutchNameLabel: "core-testing",
clutchLabelClusterName: "core-testing",
},
Annotations: map[string]string{"baz": "quuz"},
},
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *svc) DescribeNamespace(ctx context.Context, clientset, cluster, name st
}

func ProtoForNamespace(cluster string, k8snamespace *corev1.Namespace) *k8sapiv1.Namespace {
clusterName := GetKubeCluster(k8snamespace)
clusterName := GetKubeClusterName(k8snamespace)
if clusterName == "" {
clusterName = cluster
}
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *svc) DescribeNode(ctx context.Context, clientset, cluster, name string)
}

func ProtoForNode(cluster string, k8snode *corev1.Node) *k8sapiv1.Node {
clusterName := GetKubeCluster(k8snode)
clusterName := GetKubeClusterName(k8snode)
if clusterName == "" {
clusterName = cluster
}
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func podDescription(k8spod *corev1.Pod, cluster string) *k8sapiv1.Pod {
// if converted, err := ptypes.TimestampProto(k8spod.Status.StartTime.Time); err == nil {
// launch = converted
// }
clusterName := GetKubeCluster(k8spod)
clusterName := GetKubeClusterName(k8spod)
if clusterName == "" {
clusterName = cluster
}
Expand Down
22 changes: 11 additions & 11 deletions backend/service/k8s/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func testPodClientset() *fake.Clientset {
Namespace: "testing-namespace",
Labels: map[string]string{
"foo": "bar",
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
Annotations: map[string]string{"baz": "quuz"},
},
Expand All @@ -56,7 +56,7 @@ func testPodClientset() *fake.Clientset {
Namespace: "testing-namespace",
Labels: map[string]string{
"foo": "bar",
clusterClutchNameLabel: "staging",
clutchLabelClusterName: "staging",
},
Annotations: map[string]string{"baz": "quuz"},
},
Expand Down Expand Up @@ -102,7 +102,7 @@ func testListFakeClientset(numPods int) *fake.Clientset {
Name: fmt.Sprintf("testing-pod-name-%b", i),
Namespace: "testing-namespace",
Labels: map[string]string{
clusterClutchNameLabel: "staging",
clutchLabelClusterName: "staging",
},
},
})
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestPodDescription(t *testing.T) {
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
Status: corev1.PodStatus{
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestPodDescription(t *testing.T) {
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "",
clutchLabelClusterName: "",
},
},
Status: corev1.PodStatus{
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestUpdatePod(t *testing.T) {
Namespace: "testing-namespace",
Labels: map[string]string{
"foo": "bar",
clusterClutchNameLabel: "staging",
clutchLabelClusterName: "staging",
},
Annotations: map[string]string{"baz": "quuz"},
},
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestPodStatus(t *testing.T) {
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
Status: corev1.PodStatus{
Expand All @@ -572,7 +572,7 @@ func TestPodStatus(t *testing.T) {
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
Status: corev1.PodStatus{
Expand All @@ -597,7 +597,7 @@ func TestPodStatus(t *testing.T) {
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
Status: corev1.PodStatus{
Expand Down Expand Up @@ -632,7 +632,7 @@ func TestPodStatus(t *testing.T) {
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
Status: corev1.PodStatus{
Expand All @@ -657,7 +657,7 @@ func TestPodStatus(t *testing.T) {
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
clusterClutchNameLabel: "production",
clutchLabelClusterName: "production",
},
},
Status: corev1.PodStatus{
Expand Down
2 changes: 1 addition & 1 deletion backend/service/k8s/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *svc) DeleteService(ctx context.Context, clientset, cluster, namespace,
}

func ProtoForService(cluster string, k8sservice *corev1.Service) *k8sapiv1.Service {
clusterName := GetKubeCluster(k8sservice)
clusterName := GetKubeClusterName(k8sservice)
if clusterName == "" {
clusterName = cluster
}
Expand Down
Loading

0 comments on commit 486f9f0

Please sign in to comment.