forked from GoogleCloudPlatform/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: resolve CI failures — gofmt and errcheck lint #245
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
Open
ptone
wants to merge
9
commits into
main
Choose a base branch
from
scion/ci-main-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
69f80b4
fix(starter-hub): add project flag to gcloud iam service-accounts create
ptone 76ffa9c
docs(discord): add guide for agent-led installation and setup
ptone 904c5dc
docs(discord): improve agent-led installation with interactive detail…
ptone 790f49c
docs(discord): fix configuration examples and add troubleshooting sec…
ptone bffaef0
docs(discord): refine agent prompt and add configuration checklist to…
ptone 04b14a7
feat: add scion build CLI command for local harness container builds …
ptone 722858d
Integration login endpoint followup pr (#400)
ptone 5d47e7e
feat: wire hub OTel metrics pipeline with GCP Cloud Monitoring export…
ptone eef33c1
fix: resolve CI failures — gofmt formatting and errcheck lint errors
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/GoogleCloudPlatform/scion/pkg/config" | ||
| "github.com/GoogleCloudPlatform/scion/pkg/runtime" | ||
| "github.com/spf13/cobra" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| var ( | ||
| buildTag string | ||
| buildBaseImage string | ||
| buildPush bool | ||
| buildPlatform string | ||
| buildDryRun bool | ||
| ) | ||
|
|
||
| var buildCmd = &cobra.Command{ | ||
| Use: "build <harness-config-name>", | ||
| Short: "Build a container image from a harness-config Dockerfile", | ||
| Long: `Build a container image from a Dockerfile bundled inside a harness-config directory. | ||
|
|
||
| The base image is resolved from the image_registry setting unless --base-image | ||
| is provided. After a successful build the harness-config's config.yaml image | ||
| field is updated to reference the built image.`, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| harnessConfigName := args[0] | ||
|
|
||
| hcDir, err := config.FindHarnessConfigDir(harnessConfigName, projectPath) | ||
| if err != nil { | ||
| return fmt.Errorf("harness-config %q not found: %w", harnessConfigName, err) | ||
| } | ||
| if hcDir.Path == "" { | ||
| return fmt.Errorf("harness-config %q does not have a local directory path", harnessConfigName) | ||
| } | ||
|
|
||
| dockerfilePath := filepath.Join(hcDir.Path, "Dockerfile") | ||
| if _, err := os.Stat(dockerfilePath); err != nil { | ||
| if os.IsNotExist(err) { | ||
| return fmt.Errorf("harness-config %q does not contain a Dockerfile", harnessConfigName) | ||
| } | ||
| return fmt.Errorf("cannot access Dockerfile in harness-config %q: %w", harnessConfigName, err) | ||
| } | ||
|
|
||
| tag := buildTag | ||
|
|
||
| var settings *config.VersionedSettings | ||
| if buildBaseImage == "" || buildPush { | ||
| settings, _, err = config.LoadEffectiveSettings(projectPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load settings: %w", err) | ||
| } | ||
| } | ||
|
|
||
| baseImage := buildBaseImage | ||
| if baseImage == "" { | ||
| imageRegistry := "" | ||
| if settings != nil { | ||
| imageRegistry = settings.ResolveImageRegistry(profile) | ||
| } | ||
| baseImage = "scion-base:" + tag | ||
| if imageRegistry != "" { | ||
| baseImage = imageRegistry + "/scion-base:" + tag | ||
| } | ||
| } | ||
|
|
||
| runtimeBin := runtime.DetectContainerRuntime() | ||
| if runtimeBin == "" { | ||
| return fmt.Errorf("no container runtime found (tried docker, podman)") | ||
| } | ||
|
|
||
| outputImage := harnessConfigName + ":" + tag | ||
| if buildPush { | ||
| imageRegistry := "" | ||
| if settings != nil { | ||
| imageRegistry = settings.ResolveImageRegistry(profile) | ||
| } | ||
| if imageRegistry == "" { | ||
| return fmt.Errorf("--push requires image_registry to be configured") | ||
| } | ||
| outputImage = imageRegistry + "/" + harnessConfigName + ":" + tag | ||
| } | ||
|
|
||
| buildArgs := []string{"build", | ||
| "--build-arg", "BASE_IMAGE=" + baseImage, | ||
| "-t", outputImage, | ||
| } | ||
| if buildPlatform != "" { | ||
| buildArgs = append(buildArgs, "--platform", buildPlatform) | ||
| } | ||
| buildArgs = append(buildArgs, hcDir.Path) | ||
|
|
||
| if buildDryRun { | ||
| fmt.Println(runtimeBin + " " + strings.Join(buildArgs, " ")) | ||
| return nil | ||
| } | ||
|
|
||
| buildExec := exec.CommandContext(cmd.Context(), runtimeBin, buildArgs...) | ||
| buildExec.Stdout = os.Stdout | ||
| buildExec.Stderr = os.Stderr | ||
| if err := buildExec.Run(); err != nil { | ||
| return fmt.Errorf("build failed: %w", err) | ||
| } | ||
|
|
||
| if buildPush { | ||
| pushExec := exec.CommandContext(cmd.Context(), runtimeBin, "push", outputImage) | ||
| pushExec.Stdout = os.Stdout | ||
| pushExec.Stderr = os.Stderr | ||
| if err := pushExec.Run(); err != nil { | ||
| return fmt.Errorf("push failed: %w", err) | ||
| } | ||
| } | ||
|
|
||
| configPath := filepath.Join(hcDir.Path, "config.yaml") | ||
| configData, err := os.ReadFile(configPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read config.yaml for update: %w", err) | ||
| } | ||
| var doc yaml.Node | ||
| if err := yaml.Unmarshal(configData, &doc); err != nil { | ||
| return fmt.Errorf("failed to parse config.yaml: %w", err) | ||
| } | ||
| if len(doc.Content) > 0 && doc.Content[0].Kind == yaml.MappingNode { | ||
| mapping := doc.Content[0] | ||
| found := false | ||
| for i := 0; i < len(mapping.Content)-1; i += 2 { | ||
| if mapping.Content[i].Value == "image" { | ||
| mapping.Content[i+1].Value = outputImage | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| mapping.Content = append(mapping.Content, | ||
| &yaml.Node{Kind: yaml.ScalarNode, Value: "image"}, | ||
| &yaml.Node{Kind: yaml.ScalarNode, Value: outputImage}, | ||
| ) | ||
| } | ||
| } | ||
| updatedData, err := yaml.Marshal(&doc) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal updated config.yaml: %w", err) | ||
| } | ||
| if err := os.WriteFile(configPath, updatedData, 0644); err != nil { | ||
| return fmt.Errorf("failed to write updated config.yaml: %w", err) | ||
| } | ||
| fmt.Printf("Updated %s image to %s\n", configPath, outputImage) | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(buildCmd) | ||
| buildCmd.Flags().StringVar(&buildTag, "tag", "latest", "Image tag") | ||
| buildCmd.Flags().StringVar(&buildBaseImage, "base-image", "", "Override the base image (skips image_registry resolution)") | ||
| buildCmd.Flags().BoolVar(&buildPush, "push", false, "Push built image to image_registry after building") | ||
| buildCmd.Flags().StringVar(&buildPlatform, "platform", "", "Target platform (default: current architecture)") | ||
| buildCmd.Flags().BoolVar(&buildDryRun, "dry-run", false, "Show the docker build command without executing") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Writing directly to config.yaml using os.WriteFile can leave the file truncated or corrupted if the write operation is interrupted or fails (e.g., due to a full disk or process termination).
To ensure robustness and prevent file corruption, perform an atomic write by writing the updated data to a temporary file in the same directory first, and then renaming it to the target path using os.Rename.