From 43483d76cbf5f0bcc04e043779bd7b0143575334 Mon Sep 17 00:00:00 2001 From: Russell Teague Date: Mon, 15 Jul 2019 11:53:24 -0400 Subject: [PATCH 1/4] Add repo details for disconnected install/upgrade When performing a release mirror, provide details of repos where images have been mirrored from to be used for disconnected install or upgrade. --- .../oc/pkg/cli/admin/release/mirror.go | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go b/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go index 51fe6f5af495..0418084cd8dc 100644 --- a/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go +++ b/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go @@ -4,12 +4,14 @@ import ( "bytes" "encoding/json" "fmt" + "io" "os" "sort" "strings" "sync" "time" + "github.com/ghodss/yaml" digest "github.com/opencontainers/go-digest" "github.com/spf13/cobra" "k8s.io/klog" @@ -269,6 +271,10 @@ func (o *MirrorOptions) Run() error { } } + var currentRepo string + exists := struct{}{} + repoMap := map[string]struct{}{} + // build the mapping list for mirroring and rewrite if necessary for i := range is.Spec.Tags { tag := &is.Spec.Tags[i] @@ -283,6 +289,10 @@ func (o *MirrorOptions) Run() error { return fmt.Errorf("image-references should only contain pointers to images by digest: %s", tag.From.Name) } + // Create a unique map of repos as keys + currentRepo = from.AsRepository().String() + repoMap[currentRepo] = exists + dstMirrorRef := targetFn(tag.Name) mappings = append(mappings, mirror.Mapping{ Source: from, @@ -442,9 +452,57 @@ func (o *MirrorOptions) Run() error { } else { fmt.Fprintf(o.Out, "\nSuccess\nUpdate image: %s\nMirrored to: %s\n", to, o.To) } + + printInstallInstructions(o.Out, o.From, o.To, repoMap) return nil } +// printInstallInstructions provides detail to the user for using the new mirror for installs. +// The instructions include yaml formatted output which should be added to the install-config.yaml. +// +// Exmaple output: +// +// +// imageContentSources: +// - sources: +// - mycompany.com/myrepository/repo # destination (mirror) listed first +// - registry.svc.ci.openshift.org/ocp/release # source listed second +// - quay.io/openshift-release-dev/ocp-v4.0-art-dev # repos from image-references +// +func printInstallInstructions(out io.Writer, from, to string, repos map[string]struct{}) { + type installConfigSubsectionImageContentSources struct { + Sources []string `json:"sources"` + } + type installConfigSubsection struct { + ImageContentSources []installConfigSubsectionImageContentSources `json:"imageContentSources"` + } + + var repoList []string + + // Append destination (mirror) repo first + mirrorRef, _ := imagereference.Parse(to) + mirrorRepo := mirrorRef.AsRepository().String() + repoList = append(repoList, mirrorRepo) + + // Append source repo second + sourceRef, _ := imagereference.Parse(from) + sourceRepo := sourceRef.AsRepository().String() + repoList = append(repoList, sourceRepo) + + // Append unordered repos from image-references in release-image + for repo := range repos { + repoList = append(repoList, repo) + } + sources := installConfigSubsectionImageContentSources{ + Sources: repoList} + imageContentSource := installConfigSubsection{ + ImageContentSources: []installConfigSubsectionImageContentSources{sources}} + result, _ := yaml.Marshal(imageContentSource) + + fmt.Fprintf(out, "\nTo use the new mirrored release-image to install, provide the following list of release-image sources to the installer using the install-config.yaml:\n") + fmt.Fprintf(out, string(result)) +} + func sourceImageRef(is *imagev1.ImageStream, name string) (string, bool) { for _, tag := range is.Spec.Tags { if tag.Name != name { From 3a023936d2704691e2298ca2b08f20e9670d3762 Mon Sep 17 00:00:00 2001 From: Russell Teague Date: Tue, 16 Jul 2019 16:54:59 -0400 Subject: [PATCH 2/4] Add error handling --- .../oc/pkg/cli/admin/release/mirror.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go b/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go index 0418084cd8dc..d360959c2d9f 100644 --- a/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go +++ b/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go @@ -469,7 +469,7 @@ func (o *MirrorOptions) Run() error { // - registry.svc.ci.openshift.org/ocp/release # source listed second // - quay.io/openshift-release-dev/ocp-v4.0-art-dev # repos from image-references // -func printInstallInstructions(out io.Writer, from, to string, repos map[string]struct{}) { +func printInstallInstructions(out io.Writer, from, to string, repos map[string]struct{}) error { type installConfigSubsectionImageContentSources struct { Sources []string `json:"sources"` } @@ -480,12 +480,18 @@ func printInstallInstructions(out io.Writer, from, to string, repos map[string]s var repoList []string // Append destination (mirror) repo first - mirrorRef, _ := imagereference.Parse(to) + mirrorRef, err := imagereference.Parse(to) + if err != nil { + return fmt.Errorf("Unable to parse '--to' image reference: %v", err) + } mirrorRepo := mirrorRef.AsRepository().String() repoList = append(repoList, mirrorRepo) // Append source repo second - sourceRef, _ := imagereference.Parse(from) + sourceRef, err := imagereference.Parse(from) + if err != nil { + return fmt.Errorf("Unable to parse '--from' image reference: %v", err) + } sourceRepo := sourceRef.AsRepository().String() repoList = append(repoList, sourceRepo) @@ -497,10 +503,15 @@ func printInstallInstructions(out io.Writer, from, to string, repos map[string]s Sources: repoList} imageContentSource := installConfigSubsection{ ImageContentSources: []installConfigSubsectionImageContentSources{sources}} - result, _ := yaml.Marshal(imageContentSource) + result, err := yaml.Marshal(imageContentSource) + if err != nil { + return fmt.Errorf("Unable to marshal imageContentSource yaml: %v", err) + } fmt.Fprintf(out, "\nTo use the new mirrored release-image to install, provide the following list of release-image sources to the installer using the install-config.yaml:\n") fmt.Fprintf(out, string(result)) + + return nil } func sourceImageRef(is *imagev1.ImageStream, name string) (string, bool) { From 2b1f78e4119307e18a65e9f545645c073d212d55 Mon Sep 17 00:00:00 2001 From: Russell Teague Date: Tue, 16 Jul 2019 16:57:39 -0400 Subject: [PATCH 3/4] Remove unused function: sourceImageRef --- .../openshift/oc/pkg/cli/admin/release/mirror.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go b/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go index d360959c2d9f..b98bb1b6bcc5 100644 --- a/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go +++ b/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go @@ -513,16 +513,3 @@ func printInstallInstructions(out io.Writer, from, to string, repos map[string]s return nil } - -func sourceImageRef(is *imagev1.ImageStream, name string) (string, bool) { - for _, tag := range is.Spec.Tags { - if tag.Name != name { - continue - } - if tag.From == nil || tag.From.Kind != "DockerImage" { - return "", false - } - return tag.From.Name, true - } - return "", false -} From 09b90f017cc0341fb32902121eda13070c265dd4 Mon Sep 17 00:00:00 2001 From: Russell Teague Date: Wed, 17 Jul 2019 15:02:02 -0400 Subject: [PATCH 4/4] Fixup: Address comments --- .../openshift/oc/pkg/cli/admin/release/mirror.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go b/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go index b98bb1b6bcc5..429522b3f3ed 100644 --- a/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go +++ b/staging/src/github.com/openshift/oc/pkg/cli/admin/release/mirror.go @@ -11,7 +11,6 @@ import ( "sync" "time" - "github.com/ghodss/yaml" digest "github.com/opencontainers/go-digest" "github.com/spf13/cobra" "k8s.io/klog" @@ -23,6 +22,7 @@ import ( "k8s.io/client-go/util/retry" kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/util/templates" + "sigs.k8s.io/yaml" imagev1 "github.com/openshift/api/image/v1" imageclient "github.com/openshift/client-go/image/clientset/versioned" @@ -271,9 +271,7 @@ func (o *MirrorOptions) Run() error { } } - var currentRepo string - exists := struct{}{} - repoMap := map[string]struct{}{} + repositories := make(map[string]struct{}) // build the mapping list for mirroring and rewrite if necessary for i := range is.Spec.Tags { @@ -290,8 +288,8 @@ func (o *MirrorOptions) Run() error { } // Create a unique map of repos as keys - currentRepo = from.AsRepository().String() - repoMap[currentRepo] = exists + currentRepo := from.AsRepository().String() + repositories[currentRepo] = struct{}{} dstMirrorRef := targetFn(tag.Name) mappings = append(mappings, mirror.Mapping{ @@ -453,7 +451,7 @@ func (o *MirrorOptions) Run() error { fmt.Fprintf(o.Out, "\nSuccess\nUpdate image: %s\nMirrored to: %s\n", to, o.To) } - printInstallInstructions(o.Out, o.From, o.To, repoMap) + printInstallInstructions(o.Out, o.From, o.To, repositories) return nil }