-
Notifications
You must be signed in to change notification settings - Fork 5
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
Automated updates for components image digest #4
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,134 @@ | ||
// Copyright 2024 Stefan Prodan. | ||
// SPDX-License-Identifier: AGPL-3.0 | ||
|
||
package builder | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/fluxcd/pkg/apis/kustomize" | ||
ssautil "github.com/fluxcd/pkg/ssa/utils" | ||
gcname "github.com/google/go-containerregistry/pkg/name" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// ComponentImage represents a container image used by a component. | ||
type ComponentImage struct { | ||
Name string | ||
Repository string | ||
Tag string | ||
Digest string | ||
} | ||
|
||
// ExtractComponentImages reads the source directory and extracts the container images | ||
// from the components manifests. | ||
func ExtractComponentImages(srcDir string, opts Options) ([]ComponentImage, error) { | ||
images := make([]ComponentImage, len(opts.Components)) | ||
for i, component := range opts.Components { | ||
d, err := os.ReadFile(filepath.Join(srcDir, fmt.Sprintf("/%s.yaml", component))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
objects, err := ssautil.ReadObjects(bytes.NewReader(d)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, obj := range objects { | ||
if obj.GetKind() == "Deployment" { | ||
containers, ok, _ := unstructured.NestedSlice(obj.Object, "spec", "template", "spec", "containers") | ||
if !ok { | ||
return nil, fmt.Errorf("containers not found in %s", obj.GetName()) | ||
} | ||
for _, container := range containers { | ||
img := container.(map[string]interface{})["image"].(string) | ||
tag, err := gcname.NewTag(img, gcname.WeakValidation) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
images[i] = ComponentImage{ | ||
Name: component, | ||
Repository: fmt.Sprintf("%s/%s", strings.TrimSuffix(opts.Registry, "/"), component), | ||
Tag: tag.Identifier(), | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return images, nil | ||
} | ||
|
||
// FetchComponentImages fetches the components images from the distribution repository. | ||
func FetchComponentImages(opts Options) (images []ComponentImage, err error) { | ||
registry := strings.TrimSuffix(opts.Registry, "/") | ||
var distro string | ||
|
||
switch registry { | ||
case "fluxcd": | ||
distro = "upstream-alpine" | ||
case "ghcr.io/fluxcd": | ||
distro = "upstream-alpine" | ||
case "ghcr.io/controlplaneio-fluxcd/alpine": | ||
distro = "enterprise-alpine" | ||
case "ghcr.io/controlplaneio-fluxcd/distroless": | ||
distro = "enterprise-distroless" | ||
default: | ||
return nil, fmt.Errorf("unsupported registry: %s", registry) | ||
} | ||
|
||
const ghRepo = "https://raw.githubusercontent.com/controlplaneio-fluxcd/distribution/main/images" | ||
ghURL := fmt.Sprintf("%s/%s/%s.yaml", ghRepo, opts.Version, distro) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ghURL, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("read body: %v", err) | ||
} | ||
|
||
var kc struct { | ||
Images []kustomize.Image `yaml:"images"` | ||
} | ||
err = yaml.Unmarshal(data, &kc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, img := range kc.Images { | ||
component := strings.TrimPrefix(img.Name, registry+"/") | ||
if containsItemString(opts.Components, component) { | ||
images = append(images, ComponentImage{ | ||
Name: component, | ||
Repository: fmt.Sprintf("%s/%s", registry, component), | ||
Tag: img.NewTag, | ||
Digest: img.Digest, | ||
}) | ||
} | ||
} | ||
|
||
if len(images) != len(opts.Components) { | ||
return nil, fmt.Errorf("missing images for components: %v", opts.Components) | ||
} | ||
return images, nil | ||
} |
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.
maybe this should be a parameter? we could then fetch from a test http server in our tests? otherwise tests will fail everytime change the digests upstream.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.
The tests will not fail, as they point to the upstream CNCF Flux images (fixed version v2.3.0). I'm reluctant on exposing the URL in the API, as this function should fetch the digests from the OCI repo directly, instead of using Git like now.