-
Notifications
You must be signed in to change notification settings - Fork 0
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(server): implement syncer service APIs #336
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,12 @@ import ( | |
"github.com/go-logr/logr" | ||
v1 "github.com/llmariner/job-manager/api/v1" | ||
"github.com/llmariner/job-manager/server/internal/k8s" | ||
"github.com/llmariner/rbac-manager/pkg/auth" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/reflection" | ||
"google.golang.org/grpc/status" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
// NewSyncerServiceServer creates a new syncer service server. | ||
|
@@ -55,3 +59,95 @@ func (ss *SS) Run(ctx context.Context, port int) error { | |
} | ||
return nil | ||
} | ||
|
||
// PatchKubernetesObject applies a kubernetes object. | ||
func (ss *SS) PatchKubernetesObject(ctx context.Context, req *v1.PatchKubernetesObjectRequest) (*v1.PatchKubernetesObjectResponse, error) { | ||
if req.Name == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "name is required") | ||
} | ||
if req.Version == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "version is required") | ||
} | ||
if req.Resource == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "resource is required") | ||
} | ||
if len(req.Data) == 0 { | ||
return nil, status.Errorf(codes.InvalidArgument, "data is required") | ||
} | ||
|
||
userInfo, ok := auth.ExtractUserInfoFromContext(ctx) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to extract user info from context") | ||
} | ||
apikey, err := auth.ExtractTokenFromContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO(aya): Schedule to the cluster where it was created If the resource is not newly created. | ||
sresult, err := ss.scheduler.Schedule(userInfo) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Is this correct that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I have only tested the creation path currently. Eventually, to support creation and updating in this same function, I plan to store cluster info as a resource annotation in the tenant cluster and use it for updating. I'll leave a TODO comment here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! |
||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "schedule: %s", err) | ||
} | ||
clusterID := sresult.ClusterID | ||
|
||
if sresult.Namespace != req.Namespace { | ||
// TODO(aya): rethink the namespace handling | ||
return nil, status.Errorf(codes.NotFound, "not found the namespace") | ||
} | ||
|
||
client, err := ss.k8sClientFactory.NewDynamicClient(clusterID, apikey) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "create k8s client: %s", err) | ||
} | ||
|
||
gvr := schema.GroupVersionResource{ | ||
Group: req.Group, | ||
Version: req.Version, | ||
Resource: req.Resource, | ||
} | ||
uobj, err := client.PatchResource(ctx, req.Name, req.Namespace, gvr, req.Data) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "patch k8s object: %s", err) | ||
} | ||
return &v1.PatchKubernetesObjectResponse{ | ||
ClusterId: clusterID, | ||
Uid: string(uobj.GetUID()), | ||
}, nil | ||
} | ||
|
||
// DeleteKubernetesObject deletes a kubernetes object. | ||
func (ss *SS) DeleteKubernetesObject(ctx context.Context, req *v1.DeleteKubernetesObjectRequest) (*v1.DeleteKubernetesObjectResponse, error) { | ||
if req.ClusterId == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "cluster ID is required") | ||
} | ||
if req.Name == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "name is required") | ||
} | ||
if req.Version == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "version is required") | ||
} | ||
if req.Resource == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "resource is required") | ||
} | ||
|
||
apikey, err := auth.ExtractTokenFromContext(ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about adding the request validation (e.g., return an error if |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client, err := ss.k8sClientFactory.NewDynamicClient(req.ClusterId, apikey) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "create k8s client: %s", err) | ||
} | ||
|
||
gvr := schema.GroupVersionResource{ | ||
Group: req.Group, | ||
Version: req.Version, | ||
Resource: req.Resource, | ||
} | ||
if err := client.DeleteResource(ctx, req.Name, req.Namespace, gvr); err != nil { | ||
return nil, status.Errorf(codes.Internal, "delete k8s object: %s", err) | ||
} | ||
return &v1.DeleteKubernetesObjectResponse{}, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about adding the request validation?