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

Feat: support cluster control plane label #24

Merged
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
28 changes: 21 additions & 7 deletions pkg/apis/cluster/v1alpha1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/selection"
apitypes "k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/utils/strings/slices"
ocmclusterv1 "open-cluster-management.io/api/cluster/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -94,12 +95,8 @@ func (c *clusterClient) Get(ctx context.Context, name string) (*Cluster, error)

func (c *clusterClient) List(ctx context.Context, options ...client.ListOption) (*ClusterList, error) {
opts := apiserver.NewListOptions(options...)
clusters := &ClusterList{}
if opts.LabelSelector == nil || opts.LabelSelector.Empty() {
opts.LabelSelector = labels.NewSelector()
local := NewLocalCluster()
clusters.Items = []Cluster{*local}
}
local := NewLocalCluster()
clusters := &ClusterList{Items: []Cluster{*local}}

secrets := &corev1.SecretList{}
err := c.Client.List(ctx, secrets, clusterSelector{Selector: opts.LabelSelector, RequireCredentialType: true})
Expand All @@ -124,6 +121,17 @@ func (c *clusterClient) List(ctx context.Context, options ...client.ListOption)
}
}
}

// filter clusters
var items []Cluster
for _, cluster := range clusters.Items {
if opts.LabelSelector.Matches(labels.Set(cluster.GetLabels())) {
items = append(items, cluster)
}
}
clusters.Items = items

// sort clusters
sort.Slice(clusters.Items, func(i, j int) bool {
if clusters.Items[i].Name == ClusterLocalName {
return true
Expand All @@ -144,7 +152,13 @@ type clusterSelector struct {

// ApplyToList applies this configuration to the given list options.
func (m clusterSelector) ApplyToList(opts *client.ListOptions) {
opts.LabelSelector = m.Selector
opts.LabelSelector = labels.NewSelector()
requirements, _ := m.Selector.Requirements()
for _, r := range requirements {
if !slices.Contains([]string{LabelClusterControlPlane}, r.Key()) {
opts.LabelSelector = opts.LabelSelector.Add(r)
}
}
if m.RequireCredentialType {
r, _ := labels.NewRequirement(clustergatewaycommon.LabelKeyClusterCredentialType, selection.Exists, nil)
opts.LabelSelector = opts.LabelSelector.Add(*r)
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/cluster/v1alpha1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const (
var (
// AnnotationClusterAlias the annotation key for cluster alias
AnnotationClusterAlias = config.MetaApiGroupName + "/cluster-alias"

// LabelClusterControlPlane identifies whether the cluster is the control plane
LabelClusterControlPlane = config.MetaApiGroupName + "/control-plane"
)

// StorageNamespace refers to the namespace of cluster secret, usually same as the core kubevela system namespace
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/cluster/v1alpha1/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
"context"
"fmt"
"strings"

clustergatewayv1alpha1 "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1"
Expand Down Expand Up @@ -67,6 +68,7 @@ func newCluster(obj client.Object) *Cluster {
}
cluster.Spec.Accepted = true
cluster.Spec.Endpoint = ClusterBlankEndpoint
metav1.SetMetaDataLabel(&cluster.ObjectMeta, LabelClusterControlPlane, fmt.Sprintf("%t", obj == nil))
return cluster
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/cluster/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ var _ = Describe("Test Cluster API", func() {
Expect(clusters.Items[0].Name).To(Equal("ocm-cluster"))
Expect(clusters.Items[1].Name).To(Equal("test-cluster"))

By("Test list clusters that are not control plane")
objs, err = c.List(ctx, &metainternalversion.ListOptions{LabelSelector: labels.SelectorFromSet(map[string]string{
LabelClusterControlPlane: "false",
})})
Ω(err).To(Succeed())
clusters, ok = objs.(*ClusterList)
Ω(ok).To(BeTrue())
Expect(len(clusters.Items)).To(Equal(2))
Expect(clusters.Items[0].Name).To(Equal("ocm-cluster"))
Expect(clusters.Items[1].Name).To(Equal("test-cluster"))

By("Test list clusters that is control plane")
objs, err = c.List(ctx, &metainternalversion.ListOptions{LabelSelector: labels.SelectorFromSet(map[string]string{
LabelClusterControlPlane: "true",
})})
Ω(err).To(Succeed())
clusters, ok = objs.(*ClusterList)
Ω(ok).To(BeTrue())
Expect(len(clusters.Items)).To(Equal(1))
Expect(clusters.Items[0].Name).To(Equal("local"))

By("Test print table")
_, err = c.ConvertToTable(ctx, cluster, nil)
Ω(err).To(Succeed())
Expand Down