-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from stacklok/yaml-container-replace
Add utility to replace image references in YAML files
- Loading branch information
Showing
11 changed files
with
522 additions
and
94 deletions.
There are no files selected for viewing
This file contains 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 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,91 @@ | ||
// Copyright 2023 Stacklok, Inc. | ||
// | ||
// 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 containerimage provides command-line utilities to work with container images. | ||
package containerimage | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/stacklok/frizbee/pkg/config" | ||
cliutils "github.com/stacklok/frizbee/pkg/utils/cli" | ||
) | ||
|
||
// CmdYAML represents the yaml sub-command | ||
func CmdYAML() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "yaml", | ||
Short: "Replace container image references with checksums in YAML files", | ||
Long: `This utility replaces a tag or branch reference in a container image references | ||
with the digest hash of the referenced tag in YAML files. | ||
Example: | ||
$ frizbee containerimage yaml --dir . --dry-run --quiet --error | ||
`, | ||
RunE: replaceYAML, | ||
SilenceUsage: true, | ||
} | ||
|
||
// flags | ||
cmd.Flags().StringP("dir", "d", ".", "workflows directory") | ||
cmd.Flags().StringP("image-regex", "i", "image", "regex to match container image references") | ||
|
||
cliutils.DeclareReplacerFlags(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func replaceYAML(cmd *cobra.Command, _ []string) error { | ||
dir := cmd.Flag("dir").Value.String() | ||
dryRun, err := cmd.Flags().GetBool("dry-run") | ||
if err != nil { | ||
return fmt.Errorf("failed to get dry-run flag: %w", err) | ||
} | ||
errOnModified, err := cmd.Flags().GetBool("error") | ||
if err != nil { | ||
return fmt.Errorf("failed to get error flag: %w", err) | ||
} | ||
quiet, err := cmd.Flags().GetBool("quiet") | ||
if err != nil { | ||
return fmt.Errorf("failed to get quiet flag: %w", err) | ||
} | ||
cfg, err := config.FromContext(cmd.Context()) | ||
if err != nil { | ||
return fmt.Errorf("failed to get config from context: %w", err) | ||
} | ||
ir, err := cmd.Flags().GetString("image-regex") | ||
if err != nil { | ||
return fmt.Errorf("failed to get image-regex flag: %w", err) | ||
} | ||
|
||
dir = cliutils.ProcessDirNameForBillyFS(dir) | ||
|
||
ctx := cmd.Context() | ||
|
||
replacer := &yamlReplacer{ | ||
Replacer: cliutils.Replacer{ | ||
Dir: dir, | ||
DryRun: dryRun, | ||
Quiet: quiet, | ||
ErrOnModified: errOnModified, | ||
Cmd: cmd, | ||
}, | ||
imageRegex: ir, | ||
} | ||
|
||
return replacer.do(ctx, cfg) | ||
} |
This file contains 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,91 @@ | ||
// | ||
// Copyright 2023 Stacklok, Inc. | ||
// | ||
// 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 containerimage | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"path/filepath" | ||
|
||
"github.com/go-git/go-billy/v5/osfs" | ||
|
||
"github.com/stacklok/frizbee/pkg/config" | ||
"github.com/stacklok/frizbee/pkg/containers" | ||
"github.com/stacklok/frizbee/pkg/utils" | ||
cliutils "github.com/stacklok/frizbee/pkg/utils/cli" | ||
) | ||
|
||
type yamlReplacer struct { | ||
cliutils.Replacer | ||
imageRegex string | ||
} | ||
|
||
func (r *yamlReplacer) do(ctx context.Context, _ *config.Config) error { | ||
basedir := filepath.Dir(r.Dir) | ||
base := filepath.Base(r.Dir) | ||
// NOTE: For some reason using boundfs causes a panic when trying to open a file. | ||
// I instead falled back to chroot which is the default. | ||
bfs := osfs.New(basedir) | ||
|
||
outfiles := map[string]string{} | ||
modified := false | ||
|
||
err := utils.Traverse(bfs, base, func(path string, info fs.FileInfo) error { | ||
if !utils.IsYAMLFile(info) { | ||
return nil | ||
} | ||
|
||
f, err := bfs.Open(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to open file %s: %w", path, err) | ||
} | ||
|
||
// nolint:errcheck // ignore error | ||
defer f.Close() | ||
|
||
r.Logf("Processing %s\n", path) | ||
|
||
buf := bytes.Buffer{} | ||
m, err := containers.ReplaceReferenceFromYAML(ctx, r.imageRegex, f, &buf) | ||
if err != nil { | ||
return fmt.Errorf("failed to process YAML file %s: %w", path, err) | ||
} | ||
|
||
modified = modified || m | ||
|
||
if m { | ||
r.Logf("Modified %s\n", path) | ||
outfiles[path] = buf.String() | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := r.ProcessOutput(bfs, outfiles); err != nil { | ||
return err | ||
} | ||
|
||
if r.ErrOnModified && modified { | ||
return fmt.Errorf("modified files") | ||
} | ||
|
||
return nil | ||
} |
This file contains 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 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.