Skip to content

Conversation

@lanmaoxinqing
Copy link
Collaborator

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @lanmaoxinqing, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly extends the system's capabilities by integrating a Function as a Service (FaaS) engine for environment instance management. It introduces a new FaaSClient that allows the API service to provision and control environments using serverless functions, providing an alternative to existing Kubernetes and standard scheduling methods. The changes involve new client implementations, data models for FaaS entities, and adjustments to the API service's startup and routing logic to accommodate this new backend.

Highlights

  • New FaaS Integration: Added support for a FaaS (Function as a Service) engine to manage environment instances, introducing a new scheduling backend option.
  • Dynamic Environment Provisioning: Implemented a new FaaSClient to interact with a FaaS backend for creating, retrieving, listing, and deleting environment instances, including a warmup mechanism.
  • API Route Refactoring: Modified the main API service to conditionally register env-service routes, ensuring they are only active when the 'k8s' schedule type is in use.
  • Deployment Configuration Enhancements: Improved the handling of DeployConfig initialization in the EnvInstanceController and made the setting of TTL conditional on its presence in the request.
  • Environment Resource Extraction: Added utility methods to the Env model to easily retrieve image, memory, and CPU configurations from deployment settings.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for a FaaS (Function as a Service) engine, which is a significant new feature. The changes are extensive, including a new FaaS client, corresponding data models, and a generic HTTP client. My review has focused on ensuring correctness, maintainability, and security. I have identified several high-severity issues, including an unsafe type assertion that could lead to a panic, incorrect timestamp handling, and logging practices that could expose sensitive data. Additionally, I've suggested refactoring the JSON parsing logic to be more robust and maintainable. Please review the detailed comments for specific suggestions.

// use datasource as runtime name
dynamicRuntimeName := ""
if name, ok := req.DeployConfig["dataSource"]; ok {
dynamicRuntimeName = name.(string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The type assertion name.(string) is unsafe and will cause a panic if the value of dataSource in DeployConfig is not a string. This is a critical issue that could crash the service. You should use a two-value type assertion to handle this case gracefully and return an error if the type is incorrect.

        s, ok := name.(string)
		if !ok {
			return nil, fmt.Errorf("value for 'dataSource' in DeployConfig must be a string, but got %T", name)
		}
		dynamicRuntimeName = s

Labels: map[string]string{
faas_model.LabelStatefulFunction: "true",
//faas-api-service receiver uses strconv.Atoi, using int here to prevent overflow
"custom.hcsfaas.hcs.io/idle-timeout": strconv.FormatInt(math.MaxInt, 10),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using math.MaxInt for idle-timeout is architecture-dependent. If this service is compiled on a 64-bit system and the receiving faas-api-service is a 32-bit application that uses strconv.Atoi, it will cause an integer overflow. To ensure cross-platform compatibility, it's safer to use a value that is guaranteed to fit within a 32-bit signed integer, such as math.MaxInt32.

				"custom.hcsfaas.hcs.io/idle-timeout": strconv.FormatInt(math.MaxInt32, 10),

Comment on lines +153 to +164
var result []*models.EnvInstance
for _, inst := range resp.Instances {
result = append(result, &models.EnvInstance{
ID: inst.InstanceID,
IP: inst.IP,
Status: convertStatus(inst.Status),
CreatedAt: time.Now().Format("2006-01-02 15:04:05"), // Could consider constructing from CreateTimestamp
UpdatedAt: time.Now().Format("2006-01-02 15:04:05"),
TTL: "",
Env: nil, // Cannot obtain full Env information from Instance
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In ListEnvInstances, CreatedAt and UpdatedAt are being set to the current time (time.Now()). This is misleading as it doesn't reflect the actual creation or update times of the instances.

Furthermore, the underlying ListInstances function has a bug where it doesn't parse the createTimestamp field from the API response, even though the faas_model.Instance struct defines this field. This should be fixed in ListInstances, and then ListEnvInstances should use that timestamp for CreatedAt. If UpdatedAt is not available from the API, it should probably be set to the same value as CreatedAt rather than time.Now().

A similar issue with UpdatedAt exists in GetEnvInstance on line 126.

Comment on lines +248 to +272
data, ok := funcResp.Data.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid response type for Function")
}

// Convert map to Function struct
function := &faas_model.Function{}
if name, ok := data["name"].(string); ok {
function.Name = name
}
if packageType, ok := data["packageType"].(string); ok {
function.PackageType = packageType
}
if description, ok := data["description"].(string); ok {
function.Description = description
}
if runtime, ok := data["runtime"].(string); ok {
function.Runtime = runtime
}
if memory, ok := data["memory"].(float64); ok {
function.Memory = int64(memory)
}
if timeout, ok := data["timeout"].(float64); ok {
function.Timeout = int64(timeout)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The functions GetFunction, GetRuntime, ListInstances, and GetInstance manually parse the JSON response from a map[string]interface{}. This approach is brittle, error-prone, and hard to maintain. A much safer and cleaner approach is to unmarshal the JSON directly into a typed struct. You can achieve this by passing a pointer to the target struct to the Into() method. This will make the code more robust, easier to read, and less prone to runtime errors from incorrect type assertions or missing keys.

Comment on lines 205 to 235
func (r *HTTPReq) Into(obj interface{}, e ...interface{}) error {
if len(r.errors) > 0 {
return r.errors[0]
}

if r.resp == nil {
return fmt.Errorf("response is not ready")
}

data, err := io.ReadAll(r.resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}

// 如果执行成功,则把 body 的内容解析到对象中;否则返回报错
if r.resp.StatusCode >= http.StatusOK && r.resp.StatusCode < 300 {
if err := json.Unmarshal(data, obj); err != nil {
return fmt.Errorf("failed to unmarshal response data: %s. err: %v", data, err)
}
return nil
} else {
// try to unmarshal the error message into known struct
if len(e) > 0 {
if err := json.Unmarshal(data, e[0]); err != nil {
return fmt.Errorf("http request with non-200 status code: %d, body: %s", r.resp.StatusCode, string(data))
}
}

return fmt.Errorf("http request with non-200 status code: %d, body: %s", r.resp.StatusCode, string(data))
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The Into method logs the full response body in error messages. This can leak sensitive information into logs and also create excessively large log entries, which can be problematic for log storage and analysis. The response body should be truncated before being logged. You could add a helper function for this, similar to the truncateBody function in api-service/service/env_instance.go.

Comment on lines +105 to +107
if req.TTL != "" {
backendEnv.DeployConfig["ttl"] = req.TTL
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While it's good to avoid setting an empty TTL, this change might have an unintended side effect. If a user wants to explicitly remove a TTL by passing an empty string, this change would prevent that. The previous behavior would set the ttl key with an empty value. Please confirm if the intention is to disallow unsetting the TTL via an empty string.

Comment on lines +125 to +134
if envServiceController != nil {
mainRouter.POST("/env-service",
middleware.AuthTokenMiddleware(tokenEnabled, backendClient),
middleware.RateLimit(qps),
envServiceController.CreateEnvService)
mainRouter.GET("/env-service/:id/list", middleware.AuthTokenMiddleware(tokenEnabled, backendClient), envServiceController.ListEnvServices)
mainRouter.GET("/env-service/:id", middleware.AuthTokenMiddleware(tokenEnabled, backendClient), envServiceController.GetEnvService)
mainRouter.DELETE("/env-service/:id", middleware.AuthTokenMiddleware(tokenEnabled, backendClient), envServiceController.DeleteEnvService)
mainRouter.PUT("/env-service/:id", middleware.AuthTokenMiddleware(tokenEnabled, backendClient), envServiceController.UpdateEnvService)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The routes for /env-service are now conditionally registered based on whether envServiceController is initialized. This implies that standard and faas schedule types do not support environment services. While this seems reasonable given the context of FaaS, it's a significant functional change that should be documented or confirmed as intended behavior.

return // Success, don't send error
}

fmt.Printf("Warmup retry: %v\n", lastErr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

fmt.Printf is used here for logging. For consistency with the rest of the application and to enable better log management (e.g., setting log levels, structured output), it's recommended to use a structured logger like logrus, which is already used in other parts of the service.

Comment on lines +469 to +471
default:
return models.EnvInstanceStatusRunning.String()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The default case in convertStatus returns models.EnvInstanceStatusRunning.String(). This could mask new or unexpected statuses from the FaaS API by incorrectly reporting them as "Running". It would be better to handle unknown statuses explicitly, for example by returning an "Unknown" status and logging a warning. This makes the system more robust to changes in the downstream API.

	default:
		return "Unknown"
	}


// Constants for HTTP headers
const (
HttpHeaderInstanceID = "Hcs-Faas-Instance-Id"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to Go's naming conventions, common initialisms like "HTTP" and "ID" should be in all caps. This constant should be renamed to HTTPHeaderInstanceID for consistency with standard Go style and tooling.

Suggested change
HttpHeaderInstanceID = "Hcs-Faas-Instance-Id"
HTTPHeaderInstanceID = "Hcs-Faas-Instance-Id"

Copy link
Collaborator

@JacksonMei JacksonMei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@JacksonMei JacksonMei merged commit 7a00c5a into main Jan 27, 2026
1 check passed
@JacksonMei JacksonMei deleted the sky/faas_support branch January 27, 2026 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants