Skip to content

Commit

Permalink
Port changes from #383, #413 to stable branch (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaitanyaKulkarni28 authored Sep 5, 2024
1 parent d4d631c commit ec868a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
13 changes: 13 additions & 0 deletions google_guest_agent/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const (
unixConfigPath = `/etc/default/instance_configs.cfg`

defaultConfig = `
[Core]
cloud_logging_enabled = true
[Accounts]
deprovision_remove = false
gpasswd_add_cmd = gpasswd -a {user} {group}
Expand Down Expand Up @@ -108,8 +111,18 @@ command_request_timeout = 10s
`
)

// Core contains the core configuration entries of guest agent, all
// configurations not tied/specific to a subsystem are defined in here.
type Core struct {
// CloudLoggingEnabled config toggle controls Guest Agent cloud logger.
// Disabling it will stop Guest Agent for configuring and logging to Cloud Logging.
CloudLoggingEnabled bool `ini:"cloud_logging_enabled,omitempty"`
}

// Sections encapsulates all the configuration sections.
type Sections struct {
// Core defines the core guest-agent's configuration entries/keys.
Core *Core `ini:"Core,omitempty"`
// AccountManager defines the address management configurations. It takes precedence over instance's
// and project's metadata configuration. The default configuration doesn't define values to it, if the
// user has defined it then we shouldn't even consider metadata values. Users must check if this
Expand Down
5 changes: 5 additions & 0 deletions google_guest_agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func runUpdate(ctx context.Context) {

func runAgent(ctx context.Context) {
opts := logger.LogOpts{LoggerName: programName}

if !cfg.Get().Core.CloudLoggingEnabled {
opts.DisableCloudLogging = true
}

if runtime.GOOS == "windows" {
opts.FormatFunction = logFormatWindows
opts.Writers = []io.Writer{&utils.SerialPort{Port: "COM1"}}
Expand Down
13 changes: 9 additions & 4 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,15 @@ func (c *Client) do(ctx context.Context, cfg requestConfig) (*http.Response, err
return resp, fmt.Errorf("error connecting to metadata server: %+v", err)
}

statusCodeMsg := "error connecting to metadata server, status code: %d"
switch resp.StatusCode {
case 404, 412:
return resp, fmt.Errorf(statusCodeMsg, resp.StatusCode)
if resp == nil {
return nil, fmt.Errorf("got nil response from metadata server")
}

if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
// Ignore read error as we are returning original error and wrapping MDS error code.
r, _ := io.ReadAll(resp.Body)
return resp, fmt.Errorf("invalid response from metadata server, status code: %d, reason: %s", resp.StatusCode, string(r))
}

if cfg.hang {
Expand Down

0 comments on commit ec868a2

Please sign in to comment.