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

Rename sharding labels #2463

Merged
merged 1 commit into from
May 28, 2024
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
4 changes: 2 additions & 2 deletions charts/fleet/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ spec:
metadata:
labels:
app: fleet-controller
shard: "{{ . }}"
fleet.cattle.io/shard-id: "{{ . }}"
{{- if empty . }}
shard-default: "true"
fleet.cattle.io/shard-default: "true"
{{- end }}
spec:
containers:
Expand Down
4 changes: 2 additions & 2 deletions charts/fleet/templates/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ spec:
selector:
app: fleet-controller
{{- if empty . }}
shard-default: "true"
fleet.cattle.io/shard-default: "true"
{{- else }}
shard: "{{ . }}"
fleet.cattle.io/shard-id: "{{ . }}"
{{- end }}
---
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion e2e/assets/gitrepo-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: {{ .Name }}
{{- if ne .Shard "" }}
labels:
fleet.cattle.io/shard: {{ .Shard }}
fleet.cattle.io/shard-ref: {{ .Shard }}
{{- end }}

spec:
Expand Down
2 changes: 1 addition & 1 deletion e2e/assets/gitrepo/gitrepo_sharded.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: fleet.cattle.io/v1alpha1
metadata:
name: {{.Name}}
labels:
fleet.cattle.io/shard: {{.ShardID}}
fleet.cattle.io/shard-ref: {{.ShardID}}
spec:
repo: {{.Repo}}
branch: {{.Branch}}
Expand Down
4 changes: 2 additions & 2 deletions e2e/assets/metrics/fleetcontroller_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ spec:
selector:
app: fleet-controller
{{- if .IsDefaultShard }}
shard-default: "{{ .IsDefaultShard }}"
fleet.cattle.io/shard-default: "{{ .IsDefaultShard }}"
{{ else }}
shard: {{ .Shard }}
fleet.cattle.io/shard-id: {{ .Shard }}
{{- end }}
ports:
- protocol: TCP
Expand Down
7 changes: 5 additions & 2 deletions e2e/metrics/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

dto "github.com/prometheus/client_model/go"

"github.com/rancher/fleet/e2e/testenv"
"github.com/rancher/fleet/pkg/sharding"
)

type MetricsSelector map[string]map[string][]string
Expand Down Expand Up @@ -107,7 +110,7 @@ var _ = Describe("Cluster Metrics", Label("cluster"), func() {
"name": "testing",
}
if shard != "" {
labels["fleet.cattle.io/shard"] = shard
labels[sharding.ShardingRefLabel] = shard
}

err := testenv.CreateCluster(
Expand Down Expand Up @@ -159,7 +162,7 @@ var _ = Describe("Cluster Metrics", Label("cluster"), func() {
"name": "testing",
}
if shard != "" {
labels["fleet.cattle.io/shard"] = shard
labels[sharding.ShardingRefLabel] = shard
}

Expect(testenv.CreateCluster(
Expand Down
4 changes: 3 additions & 1 deletion e2e/metrics/clustergroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/rancher/fleet/e2e/testenv"
"github.com/rancher/fleet/pkg/sharding"
)

var _ = Describe("Cluster Metrics", Label("clustergroup"), func() {
Expand Down Expand Up @@ -47,7 +49,7 @@ var _ = Describe("Cluster Metrics", Label("clustergroup"), func() {
)
labels := make(map[string]string)
if shard != "" {
labels["fleet.cattle.io/shard"] = shard
labels[sharding.ShardingRefLabel] = shard
}
err := testenv.CreateClusterGroup(
k,
Expand Down
6 changes: 3 additions & 3 deletions e2e/single-cluster/imagescan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ var _ = Describe("Image Scan dynamic tests pushing to ttl.sh", Label("infra-setu
})

func getFleetControllerRestarts(k kubectl.Command, index int) int {
out, err := k.Namespace("cattle-fleet-system").Get("pods", "-l", "app=fleet-controller", "-l", "shard=",
out, err := k.Namespace("cattle-fleet-system").Get("pods", "-l", "app=fleet-controller", "-l", "fleet.cattle.io/shard-id=",
"--no-headers",
"-o", fmt.Sprintf("custom-columns=RESTARTS:.status.containerStatuses[%d].restartCount", index))
Expect(err).NotTo(HaveOccurred())
Expand All @@ -200,7 +200,7 @@ func getFleetControllerRestarts(k kubectl.Command, index int) int {
}

func getFleetControllerReady(k kubectl.Command, index int) bool {
out, err := k.Namespace("cattle-fleet-system").Get("pods", "-l", "app=fleet-controller", "-l", "shard=",
out, err := k.Namespace("cattle-fleet-system").Get("pods", "-l", "app=fleet-controller", "-l", "fleet.cattle.io/shard-id=",
"--no-headers",
"-o", fmt.Sprintf("custom-columns=RESTARTS:.status.containerStatuses[%d].ready", index))
Expect(err).NotTo(HaveOccurred())
Expand All @@ -214,7 +214,7 @@ func getFleetControllerContainerIndexInPod(k kubectl.Command, container string)
// the fleet controller pod runs 3 containers.
// we need to know the index of the fleet-controller container inside the pod.
// get all the container names, and return the index of the given container name
out, err := k.Namespace("cattle-fleet-system").Get("pods", "-l", "app=fleet-controller", "-l", "shard=",
out, err := k.Namespace("cattle-fleet-system").Get("pods", "-l", "app=fleet-controller", "-l", "fleet.cattle.io/shard-id=",
"--no-headers", "-o", "custom-columns=RESTARTS:.status.containerStatuses[*].name")
Expect(err).NotTo(HaveOccurred())
out = strings.TrimSuffix(out, "\n")
Expand Down
6 changes: 3 additions & 3 deletions e2e/single-cluster/sharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var _ = Describe("Filtering events by shard", Label("sharding"), Ordered, func()
"-l",
"app=fleet-controller",
"-l",
fmt.Sprintf("shard=%s", shard),
fmt.Sprintf("fleet.cattle.io/shard-id=%s", shard),
"--tail=-1",
)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -82,7 +82,7 @@ var _ = Describe("Filtering events by shard", Label("sharding"), Ordered, func()
"-l",
"app=fleet-controller",
"-l",
fmt.Sprintf("shard=%s", s),
fmt.Sprintf("fleet.cattle.io/shard-id=%s", s),
"--tail=100",
)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -154,7 +154,7 @@ var _ = Describe("Filtering events by shard", Label("sharding"), Ordered, func()
"-l",
"app=fleet-controller",
"-l",
fmt.Sprintf("shard=%s", s),
fmt.Sprintf("fleet.cattle.io/shard-id=%s", s),
"--tail=100",
)
Expect(err).ToNot(HaveOccurred())
Expand Down
12 changes: 10 additions & 2 deletions pkg/sharding/sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

const ShardingLabel string = "fleet.cattle.io/shard"
const (
// ShardingIDLabel is the label key used to identify the shard ID of a controller pod
ShardingIDLabel string = "fleet.cattle.io/shard-id"
// ShardingRefLabel is the label key used by resources to reference the shard ID of a controller
ShardingRefLabel string = "fleet.cattle.io/shard-ref"
// ShardingDefaultLabel is the label key which is set to true on the controller handling unlabeled resources
ShardingDefaultLabel string = "fleet.cattle.io/shard-default"
)

// FilterByShardID returns a predicate function that filters objects by the shard ID they reference
func FilterByShardID(shardID string) predicate.Funcs {
matchesLabel := func(o client.Object) bool {
label, hasLabel := o.GetLabels()[ShardingLabel]
label, hasLabel := o.GetLabels()[ShardingRefLabel]

if shardID == "" {
return !hasLabel
Expand Down
Loading