-
Notifications
You must be signed in to change notification settings - Fork 22
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 #19 from stacklok/containerimage
Add container image support
- Loading branch information
Showing
8 changed files
with
320 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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 ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// CmdContainerImage represents the containerimage command | ||
func CmdContainerImage() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "containerimage", | ||
Short: "Replace container image references with checksums", | ||
SilenceUsage: true, | ||
} | ||
|
||
cmd.AddCommand(CmdOne()) | ||
|
||
return cmd | ||
} |
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,64 @@ | ||
// 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 ( | ||
"fmt" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/stacklok/frizbee/pkg/containers" | ||
) | ||
|
||
// CmdOne represents the one sub-command | ||
func CmdOne() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "one", | ||
Short: "Replace the tag in container image reference", | ||
Long: `This utility replaces a tag or branch reference in a container image reference | ||
with the digest hash of the referenced tag. | ||
Example: | ||
$ frizbee containerimage one ghcr.io/stacklok/minder/helm/minder:0.20231123.829_ref.26ca90b | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: replaceOne, | ||
SilenceUsage: true, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func replaceOne(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
ref := args[0] | ||
|
||
r, err := name.ParseReference(ref) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse reference: %w", err) | ||
} | ||
|
||
img := r.Context().String() | ||
|
||
sum, err := containers.GetDigest(ctx, ref) | ||
if err != nil { | ||
return fmt.Errorf("failed to get checksum for action '%s': %w", ref, err) | ||
} | ||
|
||
fmt.Fprintf(cmd.OutOrStdout(), "%s@%s\n", img, sum) | ||
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
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,24 @@ | ||
// | ||
// 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 constants provides constants for the frizbee utilities. | ||
package constants | ||
|
||
const ( | ||
// UserAgent is the user agent string used by frizbee. | ||
// | ||
// TODO (jaosorior): Add version information to this. | ||
UserAgent = "frizbee" | ||
) |
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,50 @@ | ||
// | ||
// 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 containers provides functions to replace tags for checksums | ||
package containers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
|
||
"github.com/stacklok/frizbee/pkg/constants" | ||
) | ||
|
||
// GetDigest returns the digest of a container image reference. | ||
func GetDigest(ctx context.Context, refstr string) (string, error) { | ||
ref, err := name.ParseReference(refstr) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse reference: %w", err) | ||
} | ||
|
||
return GetDigestFromRef(ctx, ref) | ||
} | ||
|
||
// GetDigestFromRef returns the digest of a container image reference | ||
// from a name.Reference. | ||
func GetDigestFromRef(ctx context.Context, ref name.Reference) (string, error) { | ||
desc, err := remote.Get(ref, | ||
remote.WithContext(ctx), | ||
remote.WithUserAgent(constants.UserAgent)) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get remote reference: %w", err) | ||
} | ||
|
||
return desc.Digest.String(), nil | ||
} |
Oops, something went wrong.