Skip to content

Commit

Permalink
Implement ListExporters
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCao committed Aug 13, 2024
1 parent 4ee41df commit f2555b6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
6 changes: 3 additions & 3 deletions api/v1alpha1/device_types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v1alpha1

type Device struct {
Uuid string `json:"uuid,omitempty"`
DriverInterface string `json:"driver_interface,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Uuid string `json:"uuid,omitempty"`
ParentUuid *string `json:"parent_uuid,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
1 change: 1 addition & 0 deletions api/v1alpha1/exporter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ExporterStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
Uuid string `json:"uuid,omitempty"`
Devices []Device `json:"devices,omitempty"`
}

Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,18 @@ spec:
devices:
items:
properties:
driver_interface:
type: string
labels:
additionalProperties:
type: string
type: object
parent_uuid:
type: string
uuid:
type: string
type: object
type: array
uuid:
type: string
type: object
type: object
served: true
Expand Down
75 changes: 73 additions & 2 deletions internal/service/controller_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net"
"net/url"
"strings"
"sync"
"time"

Expand All @@ -35,7 +36,9 @@ import (
authv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -188,6 +191,18 @@ func (s *ControllerService) Register(ctx context.Context, req *pb.RegisterReques
return nil, err
}

for k := range exporter.Labels {
if strings.HasPrefix(k, "jumpstarter.dev/") {
delete(exporter.Labels, k)
}
}

for k, v := range req.Labels {
if strings.HasPrefix(k, "jumpstarter.dev/") {
exporter.Labels[k] = v
}
}

exporter.Status.Conditions = []metav1.Condition{{
Type: "Available",
Status: "True",
Expand All @@ -200,12 +215,19 @@ func (s *ControllerService) Register(ctx context.Context, req *pb.RegisterReques
devices := []jumpstarterdevv1alpha1.Device{}
for _, device := range req.Reports {
devices = append(devices, jumpstarterdevv1alpha1.Device{
Uuid: device.GetUuid(),
Labels: device.GetLabels(),
Uuid: device.Uuid,
ParentUuid: device.ParentUuid,
Labels: device.Labels,
})
}
exporter.Status.Uuid = req.Uuid
exporter.Status.Devices = devices

if err := s.Update(ctx, exporter); err != nil {
logger.Error(err, "unable to update exporter", "exporter", exporter)
return nil, status.Errorf(codes.Internal, "unable to update exporter: %s", err)
}

if err := s.Status().Update(ctx, exporter); err != nil {
logger.Error(err, "unable to update exporter status", "exporter", exporter)
return nil, status.Errorf(codes.Internal, "unable to update exporter status: %s", err)
Expand Down Expand Up @@ -247,6 +269,55 @@ func (s *ControllerService) Unregister(
return &pb.UnregisterResponse{}, nil
}

func (s *ControllerService) ListExporters(
ctx context.Context,
req *pb.ListExportersRequest,
) (*pb.ListExportersResponse, error) {
logger := log.FromContext(ctx)

var exporters jumpstarterdevv1alpha1.ExporterList

selector := labels.Everything()

for k, v := range req.GetLabels() {
requirement, err := labels.NewRequirement(k, selection.Equals, []string{v})
if err != nil {
logger.Error(err, "unable to create label requirement")
return nil, status.Errorf(codes.Internal, "unable to create label requirement")
}
selector = selector.Add(*requirement)
}

if err := s.List(ctx, &exporters, &client.ListOptions{
LabelSelector: selector,
}); err != nil {
logger.Error(err, "unable to list exporters")
return nil, status.Errorf(codes.Internal, "unable to list exporters")
}

var results []*pb.GetReportResponse

for _, exporter := range exporters.Items {
reports := []*pb.DriverInstanceReport{}
for _, device := range exporter.Status.Devices {
reports = append(reports, &pb.DriverInstanceReport{
Uuid: device.Uuid,
ParentUuid: device.ParentUuid,
Labels: device.Labels,
})
}
results = append(results, &pb.GetReportResponse{
Uuid: exporter.Status.Uuid,
Labels: exporter.GetLabels(),
Reports: reports,
})
}

return &pb.ListExportersResponse{
Exporters: results,
}, nil
}

func (s *ControllerService) Listen(req *pb.ListenRequest, stream pb.ControllerService_ListenServer) error {
ctx := stream.Context()
logger := log.FromContext(ctx)
Expand Down

0 comments on commit f2555b6

Please sign in to comment.