Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s: replace usage of clusterName to for k8s upgrade #2704

Merged
merged 4 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ require (
google.golang.org/protobuf v1.30.0
gopkg.in/square/go-jose.v2 v2.6.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.23.6
k8s.io/apimachinery v0.23.6
k8s.io/client-go v0.23.6
k8s.io/metrics v0.23.6
k8s.io/api v0.27.3
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.3
k8s.io/metrics v0.27.3
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5
)

Expand Down Expand Up @@ -89,37 +89,44 @@ require (
github.com/cloudflare/circl v1.1.0 // indirect
github.com/cncf/xds/go v0.0.0-20230105202645-06c439db220b // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lyft/protoc-gen-star v0.6.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/pjbgf/sha1cd v0.2.4-0.20230130103033-0066bacacbf6 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down Expand Up @@ -153,9 +160,9 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
140 changes: 39 additions & 101 deletions backend/go.sum

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions backend/service/k8s/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
// Setting a large channel buffer mostly for first boot and the resync timer,
// this really should be sized according to the size of your k8s deployment.
// However this should be a large enough buffer for the datastore to keep up with.
const topologyObjectChanBufferSize = 5000
const topologyInformerLockId = 1
const (
topologyObjectChanBufferSize = 5000
topologyInformerLockId = 1
)

func (s *svc) CacheEnabled() bool {
return true
Expand Down Expand Up @@ -133,7 +135,7 @@ func (s *svc) cacheFullRelist(ctx context.Context, cluster string, lwPods, lwDep
podItems := pods.(*corev1.PodList).Items
for i := range podItems {
pod := &podItems[i]
if err := ApplyClusterMetadata(cluster, pod); err != nil {
if err := ApplyClusterLabels(cluster, pod); err != nil {
s.log.Error("Unable to apply cluster name to pod object", zap.Error(err))
}
s.processInformerEvent(pod, topologyv1.UpdateCacheRequest_CREATE_OR_UPDATE)
Expand All @@ -147,7 +149,7 @@ func (s *svc) cacheFullRelist(ctx context.Context, cluster string, lwPods, lwDep
deploymentItems := deployments.(*appsv1.DeploymentList).Items
for i := range deploymentItems {
deployment := &deploymentItems[i]
if err := ApplyClusterMetadata(cluster, deployment); err != nil {
if err := ApplyClusterLabels(cluster, deployment); err != nil {
s.log.Error("Unable to apply cluster name to deployment object", zap.Error(err))
}
s.processInformerEvent(deployment, topologyv1.UpdateCacheRequest_CREATE_OR_UPDATE)
Expand All @@ -161,7 +163,7 @@ func (s *svc) cacheFullRelist(ctx context.Context, cluster string, lwPods, lwDep
hpaItems := hpas.(*autoscalingv1.HorizontalPodAutoscalerList).Items
for i := range hpaItems {
hpa := &hpaItems[i]
if err := ApplyClusterMetadata(cluster, hpa); err != nil {
if err := ApplyClusterLabels(cluster, hpa); err != nil {
s.log.Error("Unable to apply cluster name to deployment object", zap.Error(err))
}
s.processInformerEvent(hpa, topologyv1.UpdateCacheRequest_CREATE_OR_UPDATE)
Expand All @@ -175,7 +177,7 @@ func (s *svc) cacheFullRelist(ctx context.Context, cluster string, lwPods, lwDep
nodeItems := nodes.(*corev1.NodeList).Items
for i := range nodeItems {
node := &nodeItems[i]
if err := ApplyClusterMetadata(cluster, node); err != nil {
if err := ApplyClusterLabels(cluster, node); err != nil {
s.log.Error("Unable to apply cluster name to deployment object", zap.Error(err))
}
s.processInformerEvent(node, topologyv1.UpdateCacheRequest_CREATE_OR_UPDATE)
Expand Down
8 changes: 5 additions & 3 deletions backend/service/k8s/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ func TestProcessInformerEvent(t *testing.T) {

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "cluster",
Name: "test-pod-1",
Namespace: "testing-namespace",
Name: "test-pod-1",
Namespace: "testing-namespace",
Labels: map[string]string{
clutchLabelClusterName: "cluster",
},
},
}

Expand Down
3 changes: 1 addition & 2 deletions backend/service/k8s/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func (s *svc) DescribeConfigMap(ctx context.Context, clientset, cluster, namespa
configMapList, err := cs.CoreV1().ConfigMaps(cs.Namespace()).List(ctx, metav1.ListOptions{
FieldSelector: "metadata.name=" + name,
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -70,7 +69,7 @@ func (s *svc) ListConfigMaps(ctx context.Context, clientset, cluster, namespace
}

func protoForConfigMap(cluster string, k8sconfigMap *v1.ConfigMap) *k8sapiv1.ConfigMap {
clusterName := k8sconfigMap.ClusterName
clusterName := GetKubeClusterName(k8sconfigMap)
if clusterName == "" {
clusterName = cluster
}
Expand Down
14 changes: 10 additions & 4 deletions backend/service/k8s/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestDeleteConfigMap(t *testing.T) {
func TestProtoForConfigMap(t *testing.T) {
t.Parallel()

var configMapTestCases = []struct {
configMapTestCases := []struct {
id string
inputClusterName string
expectedClusterName string
Expand All @@ -170,7 +170,9 @@ func TestProtoForConfigMap(t *testing.T) {
expectedClusterName: "production",
configMap: &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "production",
Labels: map[string]string{
clutchLabelClusterName: "production",
},
},
},
},
Expand All @@ -180,7 +182,9 @@ func TestProtoForConfigMap(t *testing.T) {
expectedClusterName: "staging",
configMap: &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "",
Labels: map[string]string{
clutchLabelClusterName: "staging",
},
},
},
},
Expand All @@ -190,7 +194,9 @@ func TestProtoForConfigMap(t *testing.T) {
expectedClusterName: "test",
configMap: &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "",
Labels: map[string]string{
clutchLabelClusterName: "test",
},
},
Data: map[string]string{
"key1": "value1",
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 := k8scronJob.ClusterName
clusterName := GetKubeClusterName(k8scronJob)
if clusterName == "" {
clusterName = cluster
}
Expand Down
14 changes: 9 additions & 5 deletions backend/service/k8s/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestDeleteCron(t *testing.T) {
func TestProtoForCron(t *testing.T) {
t.Parallel()

var cronTestCases = []struct {
cronTestCases := []struct {
id string
inputClusterName string
expectedClusterName string
Expand All @@ -87,8 +87,10 @@ func TestProtoForCron(t *testing.T) {
expectedName: "test1",
cron: &v1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "production",
Name: "test1",
Labels: map[string]string{
clutchLabelClusterName: "production",
},
Name: "test1",
},
},
},
Expand All @@ -99,8 +101,10 @@ func TestProtoForCron(t *testing.T) {
expectedName: "test2",
cron: &v1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "",
Name: "test2",
Labels: map[string]string{
clutchLabelClusterName: "",
},
Name: "test2",
},
Spec: v1beta1.CronJobSpec{
ConcurrencyPolicy: v1beta1.AllowConcurrent,
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 := deployment.ClusterName
clusterName := GetKubeClusterName(deployment)
if clusterName == "" {
clusterName = cluster
}
Expand Down
20 changes: 11 additions & 9 deletions backend/service/k8s/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import (
k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1"
)

var (
t1 = time.Date(2021, time.May, 4, 0, 0, 0, 0, time.UTC)
)
var t1 = time.Date(2021, time.May, 4, 0, 0, 0, 0, time.UTC)

func testDeploymentClientset() k8s.Interface {
deployment := &appsv1.Deployment{
Expand Down Expand Up @@ -125,7 +123,7 @@ func TestDeleteDeployment(t *testing.T) {
func TestMergeDeploymentLabelsAndAnnotations(t *testing.T) {
t.Parallel()

var mergeDeploymentLabelAnnotationsTestCases = []struct {
mergeDeploymentLabelAnnotationsTestCases := []struct {
id string
fields *k8sapiv1.UpdateDeploymentRequest_Fields
expect *appsv1.Deployment
Expand Down Expand Up @@ -216,7 +214,7 @@ func TestMergeDeploymentLabelsAndAnnotations(t *testing.T) {
func TestProtoForDeploymentClusterName(t *testing.T) {
t.Parallel()

var deploymentTestCases = []struct {
deploymentTestCases := []struct {
id string
inputClusterName string
expectedClusterName string
Expand All @@ -228,7 +226,9 @@ func TestProtoForDeploymentClusterName(t *testing.T) {
expectedClusterName: "production",
deployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "production",
Labels: map[string]string{
clutchLabelClusterName: "production",
},
},
},
},
Expand All @@ -238,7 +238,9 @@ func TestProtoForDeploymentClusterName(t *testing.T) {
expectedClusterName: "staging",
deployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "",
Labels: map[string]string{
clutchLabelClusterName: "",
},
},
},
},
Expand All @@ -258,7 +260,7 @@ func TestProtoForDeploymentClusterName(t *testing.T) {
func TestProtoForDeploymentSpec(t *testing.T) {
t.Parallel()

var deploymentTestCases = []struct {
deploymentTestCases := []struct {
id string
deployment *appsv1.Deployment
}{
Expand Down Expand Up @@ -306,7 +308,7 @@ func TestProtoForDeploymentSpec(t *testing.T) {
func TestProtoForDeploymentStatus(t *testing.T) {
t.Parallel()

var deploymentTestCases = []struct {
deploymentTestCases := []struct {
id string
deployment *appsv1.Deployment
}{
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 := k8sEvent.ClusterName
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 := autoscaler.ClusterName
clusterName := GetKubeClusterName(autoscaler)
if clusterName == "" {
clusterName = cluster
}
Expand Down
16 changes: 9 additions & 7 deletions backend/service/k8s/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1"
)

var (
newInt32 = func(n int32) *int32 { return &n }
)
var newInt32 = func(n int32) *int32 { return &n }

func testHPAClientset() k8s.Interface {
hpa := &autoscalingv1.HorizontalPodAutoscaler{
Expand All @@ -33,7 +31,7 @@ func testHPAClientset() k8s.Interface {
func TestNormalizeChanges(t *testing.T) {
t.Parallel()

var applyScalingTestCases = []struct {
applyScalingTestCases := []struct {
id string
sizing *k8sapiv1.ResizeHPARequest_Sizing
resultSpec autoscalingv1.HorizontalPodAutoscalerSpec
Expand Down Expand Up @@ -124,7 +122,7 @@ func TestResizeHPA(t *testing.T) {
func TestProtoForHPAClusterName(t *testing.T) {
t.Parallel()

var hpaTestCases = []struct {
hpaTestCases := []struct {
id string
inputClusterName string
expectedClusterName string
Expand All @@ -136,7 +134,9 @@ func TestProtoForHPAClusterName(t *testing.T) {
expectedClusterName: "production",
hpa: &autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "production",
Labels: map[string]string{
clutchLabelClusterName: "production",
},
},
Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
MinReplicas: newInt32(1),
Expand All @@ -150,7 +150,9 @@ func TestProtoForHPAClusterName(t *testing.T) {
expectedClusterName: "staging",
hpa: &autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
ClusterName: "",
Labels: map[string]string{
clutchLabelClusterName: "",
},
},
Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
MinReplicas: newInt32(1),
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 := k8sJob.ClusterName
clusterName := GetKubeClusterName(k8sJob)
if clusterName == "" {
clusterName = cluster
}
Expand Down
Loading