Modify user agent format with subsystem and is proxy configured information (#3116)
This commit is contained in:
		
							parent
							
								
									b08d533105
								
							
						
					
					
						commit
						202a97ab12
					
				|  | @ -176,6 +176,8 @@ func run(ctx context.Context, rc config.Config, logger logr.Logger, opts runOpti | ||||||
| 		Version:    build.Version, | 		Version:    build.Version, | ||||||
| 		CommitSHA:  build.CommitSHA, | 		CommitSHA:  build.CommitSHA, | ||||||
| 		ScaleSetID: rc.RunnerScaleSetId, | 		ScaleSetID: rc.RunnerScaleSetId, | ||||||
|  | 		HasProxy:   hasProxy(), | ||||||
|  | 		Subsystem:  "githubrunnerscalesetlistener", | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return fmt.Errorf("failed to create an Actions Service client: %w", err) | 		return fmt.Errorf("failed to create an Actions Service client: %w", err) | ||||||
|  | @ -235,3 +237,8 @@ func newActionsClientFromConfig(config config.Config, creds *actions.ActionsAuth | ||||||
| 
 | 
 | ||||||
| 	return actions.NewClient(config.ConfigureUrl, creds, options...) | 	return actions.NewClient(config.ConfigureUrl, creds, options...) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func hasProxy() bool { | ||||||
|  | 	proxyFunc := httpproxy.FromEnvironment().ProxyFunc() | ||||||
|  | 	return proxyFunc != nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -466,6 +466,8 @@ func (r *AutoscalingRunnerSetReconciler) createRunnerScaleSet(ctx context.Contex | ||||||
| 		Version:    build.Version, | 		Version:    build.Version, | ||||||
| 		CommitSHA:  build.CommitSHA, | 		CommitSHA:  build.CommitSHA, | ||||||
| 		ScaleSetID: runnerScaleSet.Id, | 		ScaleSetID: runnerScaleSet.Id, | ||||||
|  | 		HasProxy:   autoscalingRunnerSet.Spec.Proxy != nil, | ||||||
|  | 		Subsystem:  "controller", | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
| 	logger.Info("Created/Reused a runner scale set", "id", runnerScaleSet.Id, "runnerGroupName", runnerScaleSet.RunnerGroupName) | 	logger.Info("Created/Reused a runner scale set", "id", runnerScaleSet.Id, "runnerGroupName", runnerScaleSet.RunnerGroupName) | ||||||
|  |  | ||||||
|  | @ -109,23 +109,31 @@ type ProxyFunc func(req *http.Request) (*url.URL, error) | ||||||
| type ClientOption func(*Client) | type ClientOption func(*Client) | ||||||
| 
 | 
 | ||||||
| type UserAgentInfo struct { | type UserAgentInfo struct { | ||||||
| 	Version    string | 	// Version is the version of the controller
 | ||||||
| 	CommitSHA  string | 	Version string | ||||||
|  | 	// CommitSHA is the git commit SHA of the controller
 | ||||||
|  | 	CommitSHA string | ||||||
|  | 	// ScaleSetID is the ID of the scale set
 | ||||||
| 	ScaleSetID int | 	ScaleSetID int | ||||||
|  | 	// HasProxy is true if the controller is running behind a proxy
 | ||||||
|  | 	HasProxy bool | ||||||
|  | 	// Subsystem is the subsystem such as listener, controller, etc.
 | ||||||
|  | 	// Each system may pick its own subsystem name.
 | ||||||
|  | 	Subsystem string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (u UserAgentInfo) String() string { | func (u UserAgentInfo) String() string { | ||||||
| 	var scaleSetID = "NA" | 	scaleSetID := "NA" | ||||||
| 	if u.ScaleSetID > 0 { | 	if u.ScaleSetID > 0 { | ||||||
| 		scaleSetID = strconv.Itoa(u.ScaleSetID) | 		scaleSetID = strconv.Itoa(u.ScaleSetID) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return fmt.Sprintf( | 	proxy := "Proxy/disabled" | ||||||
| 		"actions-runner-controller/%s CommitSHA/%s ScaleSetID/%s", | 	if u.HasProxy { | ||||||
| 		u.Version, | 		proxy = "Proxy/enabled" | ||||||
| 		u.CommitSHA, | 	} | ||||||
| 		scaleSetID, | 
 | ||||||
| 	) | 	return fmt.Sprintf("actions-runner-controller/%s (%s; %s) ScaleSetID/%s (%s)", u.Version, u.CommitSHA, u.Subsystem, scaleSetID, proxy) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func WithLogger(logger logr.Logger) ClientOption { | func WithLogger(logger logr.Logger) ClientOption { | ||||||
|  |  | ||||||
|  | @ -0,0 +1,24 @@ | ||||||
|  | package actions_test | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"github.com/actions/actions-runner-controller/github/actions" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func TestUserAgentInfoString(t *testing.T) { | ||||||
|  | 	userAgentInfo := actions.UserAgentInfo{ | ||||||
|  | 		Version:    "0.1.0", | ||||||
|  | 		CommitSHA:  "1234567890abcdef", | ||||||
|  | 		ScaleSetID: 10, | ||||||
|  | 		HasProxy:   true, | ||||||
|  | 		Subsystem:  "test", | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	userAgent := userAgentInfo.String() | ||||||
|  | 	expectedProduct := "actions-runner-controller/0.1.0 (1234567890abcdef; test)" | ||||||
|  | 	assert.Contains(t, userAgent, expectedProduct) | ||||||
|  | 	expectedScaleSet := "ScaleSetID/10 (Proxy/enabled)" | ||||||
|  | 	assert.Contains(t, userAgent, expectedScaleSet) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue