generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 235
kro cli package command #617
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
DhairyaMajmudar
wants to merge
16
commits into
kubernetes-sigs:main
Choose a base branch
from
DhairyaMajmudar:cli-package
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 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
65e5b0e
Add google/go-containerregistry package
DhairyaMajmudar 3ecac9c
Add package cli command
DhairyaMajmudar adbf534
fix lint errors
DhairyaMajmudar 16b482c
Rename config to generateConfig
DhairyaMajmudar 4971081
Add kro license header
DhairyaMajmudar 39793d8
Merge branch 'main' into cli-package
DhairyaMajmudar c9b7bfd
fix file structure
DhairyaMajmudar 9a8c9c7
Merge branch 'main' into cli-package
DhairyaMajmudar 4a7bf79
Replace config with generateConfig
DhairyaMajmudar da8f114
Made output flag optional
DhairyaMajmudar 1809aea
Add flag for tag
DhairyaMajmudar f4f3442
Remove outputFile flag
DhairyaMajmudar 5db8280
Migrate package command from gcr to oras
DhairyaMajmudar 8f4d479
Update ATTRIBUTION.md and import order
DhairyaMajmudar 48257dc
Merge branch 'main' into cli-package
DhairyaMajmudar 31ffa68
Change package name to pkg
DhairyaMajmudar 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
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,130 @@ | ||
// Copyright 2025 The Kube Resource Orchestrator Authors | ||
// | ||
// 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 commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/google/go-containerregistry/pkg/v1/static" | ||
"github.com/google/go-containerregistry/pkg/v1/tarball" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/yaml" | ||
|
||
"github.com/kro-run/kro/api/v1alpha1" | ||
) | ||
|
||
type PackageConfig struct { | ||
resourceGraphDefinitionFile string | ||
outputFile string | ||
} | ||
|
||
var packageConfig = &PackageConfig{} | ||
|
||
func init() { | ||
packageRGDCmd.PersistentFlags().StringVarP(&packageConfig.resourceGraphDefinitionFile, "file", "f", "", | ||
"Path to the ResourceGraphDefinition file") | ||
packageRGDCmd.PersistentFlags().StringVarP(&packageConfig.outputFile, "output", "o", "", | ||
"Output file name for the OCI image tarball") | ||
} | ||
|
||
var packageRGDCmd = &cobra.Command{ | ||
Use: "package", | ||
Short: "Package ResourceGraphDefinition file into an OCI image", | ||
DhairyaMajmudar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Long: "Package command packages the ResourceGraphDefinition" + | ||
DhairyaMajmudar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"file into an OCI image, which can be used for distribution and deployment.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if packageConfig.resourceGraphDefinitionFile == "" { | ||
return fmt.Errorf("ResourceGraphDefinition file is required") | ||
} | ||
|
||
if packageConfig.outputFile == "" { | ||
return fmt.Errorf("output file name is required") | ||
} | ||
|
||
data, err := os.ReadFile(packageConfig.resourceGraphDefinitionFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to read ResourceGraphDefinition file: %w", err) | ||
} | ||
|
||
var rgd v1alpha1.ResourceGraphDefinition | ||
if err := yaml.Unmarshal(data, &rgd); err != nil { | ||
return fmt.Errorf("failed to unmarshal ResourceGraphDefinition: %w", err) | ||
} | ||
|
||
fmt.Fprintf(os.Stderr, "Packaging ResourceGraphDefinition into %s...\n", | ||
packageConfig.outputFile) | ||
|
||
if err = packageRGD(data, &rgd); err != nil { | ||
return fmt.Errorf("failed to package ResourceGraphDefinition: %w", err) | ||
} | ||
|
||
fmt.Fprintf(os.Stderr, "Successfully packaged ResourceGraphDefinition to %s\n", | ||
packageConfig.outputFile) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func packageRGD(data []byte, rgd *v1alpha1.ResourceGraphDefinition) error { | ||
layer := static.NewLayer(data, types.MediaType("application/vnd.kro.resourcegraphdefinition.v1alpha1+yaml")) | ||
|
||
img := empty.Image | ||
|
||
img, err := mutate.AppendLayers(img, layer) | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to append layer: %w", err) | ||
} | ||
|
||
configFile, err := img.ConfigFile() | ||
if err != nil { | ||
return fmt.Errorf("failed to get config file: %w", err) | ||
} | ||
|
||
now := time.Now() | ||
configFile.Created = v1.Time{Time: now} | ||
|
||
configFile.Config.Labels = map[string]string{ | ||
"kro.run/type": "resourcegraphdefinition", | ||
"kro.run/name": rgd.Name, | ||
} | ||
|
||
img, err = mutate.ConfigFile(img, configFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to update image config: %w", err) | ||
} | ||
|
||
ref, err := name.ParseReference(fmt.Sprintf("kro.run/rgd/%s:latest", rgd.Name)) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse image reference: %w", err) | ||
} | ||
|
||
if err := tarball.WriteToFile(packageConfig.outputFile, ref, img); err != nil { | ||
return fmt.Errorf("failed to write image to file: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func AddPackageCommand(rootCmd *cobra.Command) { | ||
rootCmd.AddCommand(packageRGDCmd) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.