|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/replicatedhq/replicated/cli/print" |
| 9 | + "github.com/replicatedhq/replicated/pkg/types" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +func (r *runners) InitReleaseImageLS(parent *cobra.Command) { |
| 14 | + imageCmd := &cobra.Command{ |
| 15 | + Use: "image", |
| 16 | + Short: "Manage release images", |
| 17 | + Long: "Manage release images", |
| 18 | + } |
| 19 | + |
| 20 | + lsCmd := &cobra.Command{ |
| 21 | + Use: "ls --channel CHANNEL_NAME_OR_ID [--version SEMVER] [--keep-proxy]", |
| 22 | + Short: "List images in a channel's current or specified release", |
| 23 | + Long: "List all container images in the current release or a specific version of a channel", |
| 24 | + Example: `# List images in current release of a channel by name |
| 25 | +replicated release image ls --channel Stable |
| 26 | +
|
| 27 | +# List images in a specific version of a channel |
| 28 | +replicated release image ls --channel Stable --version 1.2.1 |
| 29 | +
|
| 30 | +# List images in a channel by ID |
| 31 | +replicated release image ls --channel 2abc123 |
| 32 | +
|
| 33 | +# Keep proxy registry domains in the image names |
| 34 | +replicated release image ls --channel Stable --keep-proxy`, |
| 35 | + } |
| 36 | + |
| 37 | + lsCmd.Flags().StringVar(&r.args.releaseImageLSChannel, "channel", "", "The channel name, slug, or ID (required)") |
| 38 | + lsCmd.Flags().StringVar(&r.args.releaseImageLSVersion, "version", "", "The specific semver version to get images for (optional, defaults to current release)") |
| 39 | + lsCmd.Flags().BoolVar(&r.args.releaseImageLSKeepProxy, "keep-proxy", false, "Keep proxy registry domain in image names instead of stripping it") |
| 40 | + lsCmd.MarkFlagRequired("channel") |
| 41 | + |
| 42 | + parent.AddCommand(imageCmd) |
| 43 | + imageCmd.AddCommand(lsCmd) |
| 44 | + lsCmd.RunE = r.releaseImageLS |
| 45 | +} |
| 46 | + |
| 47 | +func (r *runners) releaseImageLS(cmd *cobra.Command, args []string) error { |
| 48 | + if !r.hasApp() { |
| 49 | + return errors.New("no app specified") |
| 50 | + } |
| 51 | + |
| 52 | + if r.args.releaseImageLSChannel == "" { |
| 53 | + return errors.New("channel is required") |
| 54 | + } |
| 55 | + |
| 56 | + // Get the channel to find its current release |
| 57 | + channel, err := r.api.GetChannelByName(r.appID, r.appType, r.args.releaseImageLSChannel) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("failed to get channel: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + var targetRelease *types.ChannelRelease |
| 63 | + var proxyDomain string |
| 64 | + |
| 65 | + if r.args.releaseImageLSVersion != "" { |
| 66 | + // For specific versions, we need to get all releases |
| 67 | + channelReleases, err := r.api.ListChannelReleases(r.appID, r.appType, channel.ID) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to list channel releases: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + targetRelease, err = findTargetRelease(channelReleases, r.args.releaseImageLSVersion) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + // Get proxy domain for version-specific releases |
| 78 | + proxyDomain = targetRelease.ProxyRegistryDomain |
| 79 | + if proxyDomain == "" && r.appType == "kots" { |
| 80 | + customHostnames, err := r.api.GetCustomHostnames(r.appID, r.appType, channel.ID) |
| 81 | + if err == nil && customHostnames.Proxy.Hostname != "" { |
| 82 | + proxyDomain = customHostnames.Proxy.Hostname |
| 83 | + } |
| 84 | + // Check embedded cluster proxy domain if still empty |
| 85 | + if proxyDomain == "" && targetRelease.InstallationTypes.EmbeddedCluster.ProxyRegistryDomain != "" { |
| 86 | + proxyDomain = targetRelease.InstallationTypes.EmbeddedCluster.ProxyRegistryDomain |
| 87 | + } |
| 88 | + // If no explicit proxy domain is configured, fall back to default custom hostname |
| 89 | + if proxyDomain == "" { |
| 90 | + defaultProxy, err := r.api.GetDefaultProxyHostname(r.appID) |
| 91 | + if err == nil && defaultProxy != "" { |
| 92 | + proxyDomain = defaultProxy |
| 93 | + } else { |
| 94 | + // Final fallback to default Replicated proxy |
| 95 | + proxyDomain = "proxy.replicated.com" |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } else { |
| 100 | + // For current release, use optimized method that tries to avoid extra API call |
| 101 | + var err error |
| 102 | + targetRelease, proxyDomain, err = r.api.GetCurrentChannelRelease(r.appID, r.appType, channel.ID) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("failed to get current channel release: %w", err) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Extract and clean up image names |
| 109 | + images := make([]string, 0) |
| 110 | + |
| 111 | + for _, image := range targetRelease.AirgapBundleImages { |
| 112 | + // Remove registry prefixes and clean up image names |
| 113 | + var cleanProxyDomain string |
| 114 | + if r.args.releaseImageLSKeepProxy { |
| 115 | + // Keep proxy domain - don't strip it |
| 116 | + cleanProxyDomain = "" |
| 117 | + } else { |
| 118 | + // Strip proxy domain |
| 119 | + cleanProxyDomain = proxyDomain |
| 120 | + } |
| 121 | + cleanImage := cleanImageName(image, cleanProxyDomain) |
| 122 | + if cleanImage != "" { |
| 123 | + images = append(images, cleanImage) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Print images |
| 128 | + return print.ChannelImages(r.w, images) |
| 129 | +} |
| 130 | + |
| 131 | +func cleanImageName(image string, proxyRegistryDomain string) string { |
| 132 | + cleaned := image |
| 133 | + |
| 134 | + // Remove proxy registry domain if provided and present |
| 135 | + if proxyRegistryDomain != "" { |
| 136 | + // Handle proxy registry patterns like "proxyRegistryDomain/proxy/app-name/" |
| 137 | + proxyPrefix := proxyRegistryDomain + "/proxy/" |
| 138 | + if strings.HasPrefix(cleaned, proxyPrefix) { |
| 139 | + // Remove prefix and find first occurrence after app name |
| 140 | + withoutPrefix := strings.TrimPrefix(cleaned, proxyPrefix) |
| 141 | + parts := strings.SplitN(withoutPrefix, "/", 2) |
| 142 | + if len(parts) == 2 { |
| 143 | + cleaned = parts[1] // Take everything after the app name |
| 144 | + } |
| 145 | + } |
| 146 | + // Also handle anonymous proxy registry domain prefixes |
| 147 | + if strings.HasPrefix(cleaned, proxyRegistryDomain+"/anonymous/") { |
| 148 | + cleaned = strings.TrimPrefix(cleaned, proxyRegistryDomain+"/anonymous/") |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Remove other common registry prefixes |
| 153 | + prefixes := []string{ |
| 154 | + "registry-1.docker.io/library/", |
| 155 | + "registry-1.docker.io/", |
| 156 | + "docker.io/library/", |
| 157 | + "docker.io/", |
| 158 | + "index.docker.io/library/", |
| 159 | + "index.docker.io/", |
| 160 | + "hub.docker.com/library/", |
| 161 | + "hub.docker.com/", |
| 162 | + "registry.hub.docker.com/library/", |
| 163 | + "registry.hub.docker.com/", |
| 164 | + } |
| 165 | + |
| 166 | + for _, prefix := range prefixes { |
| 167 | + if strings.HasPrefix(cleaned, prefix) { |
| 168 | + cleaned = strings.TrimPrefix(cleaned, prefix) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return cleaned |
| 173 | +} |
| 174 | + |
| 175 | +// findTargetRelease finds the target release from a list of releases |
| 176 | +// If requestedVersion is empty, returns the current release (highest channel sequence) |
| 177 | +// If requestedVersion is specified, returns the release with matching semver |
| 178 | +func findTargetRelease(releases []*types.ChannelRelease, requestedVersion string) (*types.ChannelRelease, error) { |
| 179 | + if len(releases) == 0 { |
| 180 | + return nil, errors.New("no releases found in channel") |
| 181 | + } |
| 182 | + |
| 183 | + var targetRelease *types.ChannelRelease |
| 184 | + |
| 185 | + if requestedVersion != "" { |
| 186 | + // Find release by semver |
| 187 | + for _, release := range releases { |
| 188 | + if release.Semver == requestedVersion { |
| 189 | + targetRelease = release |
| 190 | + break |
| 191 | + } |
| 192 | + } |
| 193 | + if targetRelease == nil { |
| 194 | + return nil, fmt.Errorf("no release found with version %q in channel", requestedVersion) |
| 195 | + } |
| 196 | + } else { |
| 197 | + // Find the current release (highest channel sequence) |
| 198 | + for _, release := range releases { |
| 199 | + if targetRelease == nil || release.ChannelSequence > targetRelease.ChannelSequence { |
| 200 | + targetRelease = release |
| 201 | + } |
| 202 | + } |
| 203 | + if targetRelease == nil { |
| 204 | + return nil, errors.New("no current release found") |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return targetRelease, nil |
| 209 | +} |
| 210 | + |
| 211 | + |
0 commit comments