|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + |
| 25 | + v1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + oras "oras.land/oras-go/v2" |
| 28 | + "oras.land/oras-go/v2/content/file" |
| 29 | + "oras.land/oras-go/v2/registry/remote" |
| 30 | + "oras.land/oras-go/v2/registry/remote/auth" |
| 31 | + "oras.land/oras-go/v2/registry/remote/retry" |
| 32 | +) |
| 33 | + |
| 34 | +type publishManifestsOptions struct { |
| 35 | + ociUrl string |
| 36 | + dir string |
| 37 | + files []string |
| 38 | +} |
| 39 | + |
| 40 | +var publishOpts = &publishManifestsOptions{} |
| 41 | + |
| 42 | +var publishCmd = &cobra.Command{ |
| 43 | + Use: "publish", |
| 44 | + GroupID: groupManagement, |
| 45 | + Short: "publish provider manifests to OCI registry", |
| 46 | + Long: LongDesc(` |
| 47 | + Publishes provider manifests to an OCI registry. |
| 48 | + `), |
| 49 | + Example: Examples(` |
| 50 | + # Publish provider manifests to the OCI destination |
| 51 | + capioperator publish -u ttl.sh/${IMAGE_NAME}:5m -d manifests |
| 52 | +
|
| 53 | + # Publish manifests from files to the OCI destination |
| 54 | + capioperator publish -u ttl.sh/${IMAGE_NAME}:5m -f metadata.yaml -f infrastructure-components.yaml |
| 55 | + `), |
| 56 | + Args: cobra.NoArgs, |
| 57 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 58 | + return runPublish() |
| 59 | + }, |
| 60 | +} |
| 61 | + |
| 62 | +func init() { |
| 63 | + publishCmd.PersistentFlags().StringVarP(&publishOpts.dir, "dir", "d", ".", `Directory with provider manifests`) |
| 64 | + publishCmd.PersistentFlags().StringSliceVarP(&publishOpts.files, "file", "f", []string{}, `Provider manifes file`) |
| 65 | + publishCmd.Flags().StringVarP(&publishOpts.ociUrl, "artifact-url", "u", "", |
| 66 | + "The URL of the OCI artifact to collect component manifests from.") |
| 67 | + |
| 68 | + RootCmd.AddCommand(publishCmd) |
| 69 | +} |
| 70 | + |
| 71 | +func runPublish() (err error) { |
| 72 | + // 0. Create a file store |
| 73 | + fs, err := file.New(publishOpts.dir) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + defer func() { |
| 78 | + err = fs.Close() |
| 79 | + }() |
| 80 | + |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + // 1. Add files to the file store |
| 84 | + mediaType := "application/vnd.test.file" |
| 85 | + fileDescriptors := []v1.Descriptor{} |
| 86 | + |
| 87 | + files, err := os.ReadDir(publishOpts.dir) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + for _, file := range files { |
| 93 | + if !file.Type().IsRegular() { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + fileDescriptor, err := fs.Add(ctx, file.Name(), mediaType, "") |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + fileDescriptors = append(fileDescriptors, fileDescriptor) |
| 103 | + |
| 104 | + fmt.Printf("Added file: %s\n", file.Name()) |
| 105 | + } |
| 106 | + |
| 107 | + for _, file := range publishOpts.files { |
| 108 | + fileDescriptor, err := fs.Add(ctx, file, mediaType, "") |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + fileDescriptors = append(fileDescriptors, fileDescriptor) |
| 114 | + |
| 115 | + fmt.Printf("Added custom file: %s\n", file) |
| 116 | + } |
| 117 | + |
| 118 | + // 2. Pack the files and tag the packed manifest |
| 119 | + artifactType := "application/vnd.acme.config" |
| 120 | + opts := oras.PackManifestOptions{ |
| 121 | + Layers: fileDescriptors, |
| 122 | + } |
| 123 | + |
| 124 | + manifestDescriptor, err := oras.PackManifest(ctx, fs, oras.PackManifestVersion1_1, artifactType, opts) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + fmt.Println("Packaged manifests") |
| 130 | + |
| 131 | + parts := strings.Split(publishOpts.ociUrl, ":") |
| 132 | + |
| 133 | + tag := parts[len(parts)-1] |
| 134 | + if err = fs.Tag(ctx, manifestDescriptor, tag); err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + // 3. Connect to a remote repository |
| 139 | + reg := strings.Split(publishOpts.ociUrl, "/")[0] |
| 140 | + |
| 141 | + repo, err := remote.NewRepository(publishOpts.ociUrl) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + if creds := ociAuthentication(); creds != nil { |
| 147 | + repo.Client = &auth.Client{ |
| 148 | + Client: retry.DefaultClient, |
| 149 | + Cache: auth.NewCache(), |
| 150 | + Credential: auth.StaticCredential(reg, *creds), |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // 4. Copy from the file store to the remote repository |
| 155 | + _, err = oras.Copy(ctx, fs, tag, repo, tag, oras.DefaultCopyOptions) |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
0 commit comments