Skip to content

Commit

Permalink
add standard label for the filtering of cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyu.jiang committed Jul 6, 2022
1 parent 9434155 commit 7b58d6c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 2 deletions.
22 changes: 20 additions & 2 deletions apiserver/pkg/manager/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
rayiov1alpha1 "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/typed/raycluster/v1alpha1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
clientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

Expand Down Expand Up @@ -111,7 +112,14 @@ func (r *ResourceManager) GetCluster(ctx context.Context, clusterName string, na
}

func (r *ResourceManager) ListClusters(ctx context.Context, namespace string) ([]*v1alpha1.RayCluster, error) {
rayClusterList, err := r.getRayClusterClient(namespace).List(ctx, metav1.ListOptions{})
labelSelector := metav1.LabelSelector{
MatchLabels: map[string]string{
util.ManagedByLabelKey: util.DefaultAPIServerName,
},
}
rayClusterList, err := r.getRayClusterClient(namespace).List(ctx, metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
})
if err != nil {
return nil, util.Wrap(err, fmt.Sprintf("List RayCluster failed in %s", namespace))
}
Expand All @@ -132,8 +140,15 @@ func (r *ResourceManager) ListAllClusters(ctx context.Context) ([]*v1alpha1.RayC
}

var result []*v1alpha1.RayCluster
labelSelector := metav1.LabelSelector{
MatchLabels: map[string]string{
util.ManagedByLabelKey: util.DefaultAPIServerName,
},
}
for _, namespace := range namespaces.Items {
rayClusterList, err := r.getRayClusterClient(namespace.Name).List(ctx, metav1.ListOptions{})
rayClusterList, err := r.getRayClusterClient(namespace.Name).List(ctx, metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
})
if err != nil {
return nil, util.Wrap(err, fmt.Sprintf("List RayCluster failed in %s", namespace.Name))
}
Expand Down Expand Up @@ -247,6 +262,9 @@ func getClusterByName(ctx context.Context, client rayiov1alpha1.RayClusterInterf
if err != nil {
return nil, util.Wrap(err, "Get Cluster failed")
}
if managedBy, ok := cluster.Labels[util.ManagedByLabelKey]; !ok || managedBy != util.DefaultAPIServerName {
return nil, fmt.Errorf("RayCluster with name %s not managed by %s", name, util.DefaultAPIServerName)
}

return cluster, nil
}
Expand Down
10 changes: 10 additions & 0 deletions apiserver/pkg/util/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
// The default application name
DefaultApplicationName = "kuberay"

// The default name for apiserver
DefaultAPIServerName = "kuberay-apiserver"
)

type RayCluster struct {
*rayclusterapi.RayCluster
}
Expand Down Expand Up @@ -76,6 +84,8 @@ func buildRayClusterLabels(cluster *api.Cluster) map[string]string {
labels[RayClusterUserLabelKey] = cluster.User
labels[RayClusterVersionLabelKey] = cluster.Version
labels[RayClusterEnvironmentLabelKey] = cluster.Environment.String()
labels[AppNameLabelKey] = DefaultApplicationName
labels[ManagedByLabelKey] = DefaultAPIServerName
return labels
}

Expand Down
2 changes: 2 additions & 0 deletions apiserver/pkg/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
RayClusterUserLabelKey = "ray.io/user"
RayClusterVersionLabelKey = "ray.io/version"
RayClusterEnvironmentLabelKey = "ray.io/environment"
AppNameLabelKey = "app.kubernetes.io/name"
ManagedByLabelKey = "app.kubernetes.io/managed-by"

// Annotation keys
// Role level
Expand Down
9 changes: 9 additions & 0 deletions ray-operator/controllers/ray/common/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const (
RayNodeLabelKey = "ray.io/is-ray-node"
RayIDLabelKey = "ray.io/identifier"

NameLabelKey = "app.kubernetes.io/name"
CreatedByLabelKey = "app.kubernetes.io/created-by"

// Use as separator for pod name, for example, raycluster-small-size-worker-0
DashSymbol = "-"

Expand All @@ -23,6 +26,12 @@ const (
DefaultDashboardName = "dashboard"
DefaultMetricsName = "metrics"

// The default application name
DefaultApplicationName = "kuberay"

// The default name for kuberay operator
DefaultRayOperatorName = "kuberay-operator"

// Check node if ready by checking the path exists or not
PodReadyFilepath = "POD_READY_FILEPATH"

Expand Down
2 changes: 2 additions & 0 deletions ray-operator/controllers/ray/common/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func BuildIngressForHeadService(cluster rayiov1alpha1.RayCluster) (*networkingv1
labels := map[string]string{
RayClusterLabelKey: cluster.Name,
RayIDLabelKey: utils.GenerateIdentifier(cluster.Name, rayiov1alpha1.HeadNode),
NameLabelKey: DefaultApplicationName,
CreatedByLabelKey: DefaultRayOperatorName,
}

// Copy other ingress configuration from cluster annotation
Expand Down
2 changes: 2 additions & 0 deletions ray-operator/controllers/ray/common/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ func labelPod(rayNodeType rayiov1alpha1.RayNodeType, rayClusterName string, grou
RayNodeTypeLabelKey: string(rayNodeType),
RayNodeGroupLabelKey: groupName,
RayIDLabelKey: utils.CheckLabel(utils.GenerateIdentifier(rayClusterName, rayNodeType)),
NameLabelKey: DefaultApplicationName,
CreatedByLabelKey: DefaultRayOperatorName,
}

for k, v := range ret {
Expand Down
6 changes: 6 additions & 0 deletions ray-operator/controllers/ray/common/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func BuildServiceAccount(cluster *v1alpha1.RayCluster) (*v1.ServiceAccount, erro
Namespace: cluster.Namespace,
Labels: map[string]string{
RayClusterLabelKey: cluster.Name,
NameLabelKey: DefaultApplicationName,
CreatedByLabelKey: DefaultRayOperatorName,
},
},
}
Expand All @@ -31,6 +33,8 @@ func BuildRole(cluster *v1alpha1.RayCluster) (*rbacv1.Role, error) {
Namespace: cluster.Namespace,
Labels: map[string]string{
RayClusterLabelKey: cluster.Name,
NameLabelKey: DefaultApplicationName,
CreatedByLabelKey: DefaultRayOperatorName,
},
},
Rules: []rbacv1.PolicyRule{
Expand Down Expand Up @@ -58,6 +62,8 @@ func BuildRoleBinding(cluster *v1alpha1.RayCluster) (*rbacv1.RoleBinding, error)
Namespace: cluster.Namespace,
Labels: map[string]string{
RayClusterLabelKey: cluster.Name,
NameLabelKey: DefaultApplicationName,
CreatedByLabelKey: DefaultRayOperatorName,
},
},
Subjects: []rbacv1.Subject{
Expand Down
2 changes: 2 additions & 0 deletions ray-operator/controllers/ray/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func BuildServiceForHeadPod(cluster rayiov1alpha1.RayCluster) (*corev1.Service,
RayClusterLabelKey: cluster.Name,
RayNodeTypeLabelKey: string(rayiov1alpha1.HeadNode),
RayIDLabelKey: utils.CheckLabel(utils.GenerateIdentifier(cluster.Name, rayiov1alpha1.HeadNode)),
NameLabelKey: DefaultApplicationName,
CreatedByLabelKey: DefaultRayOperatorName,
}

service := &corev1.Service{
Expand Down

0 comments on commit 7b58d6c

Please sign in to comment.