Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
[[constraint]]
branch = "master"
name = "github.com/golang/glog"

[[constraint]]
name = "github.com/containers/image"
version = "1.5.0"
50 changes: 49 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (
"io/ioutil"
"os"
"strings"
// Enable sha256 in container image references
_ "crypto/sha256"

"github.com/golang/glog"
"github.com/openshift/pivot/types"
"github.com/openshift/pivot/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

imgref "github.com/containers/image/docker/reference"
)

// flag storage
Expand Down Expand Up @@ -70,6 +74,46 @@ func getDefaultDeployment() types.RpmOstreeDeployment {
return rosState.Deployments[0]
}

// getRefDigest parses a Docker/OCI image reference and returns
// its digest, or an error if the string fails to parse as
// a "canonical" image reference with a digest.
func getRefDigest(ref string) (string, error) {
refParsed, err := imgref.ParseNamed(ref)
if err != nil {
return "", fmt.Errorf("parsing reference: %q: %v", ref, err)
}
canon, ok := refParsed.(imgref.Canonical)
if !ok {
return "", fmt.Errorf("not canonical form: %q: %v", ref, err)
}

return canon.Digest().String(), nil
}

// compareOSImageURL determines whether two images are the same, or have
// matching digests.
func compareOSImageURL(current, desired string) (bool, error) {
if current == desired {
return true, nil
}

currentDigest, err := getRefDigest(current)
if err != nil {
return false, fmt.Errorf("parsing current osImageURL: %v", err)
}
desiredDigest, err := getRefDigest(desired)
if err != nil {
return false, fmt.Errorf("parsing desired osImageURL: %v", err)
}

if currentDigest == desiredDigest {
glog.Infof("Current and target osImageURL have matching digest %q", currentDigest)
return true, nil
}

return false, nil
}

// pullAndRebase potentially rebases system if not already rebased.
func pullAndRebase(container string) (imgid string, changed bool) {
defaultDeployment := getDefaultDeployment()
Expand Down Expand Up @@ -98,7 +142,11 @@ func pullAndRebase(container string) (imgid string, changed bool) {
imgid = fmt.Sprintf("%s@%s", imagedata.Name, imagedata.Digest)
glog.Infof("Resolved to: %s", imgid)

if previousPivot == imgid {
targetMatched, err := compareOSImageURL(previousPivot, imgid)
if err != nil {
glog.Fatalf("%v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: we pretty liberally use glog.Fatalf everywhere in this codebase. And since we're not actually adding any context here, I'd say just drop error-handling and Fatalf directly in getRefDigest().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels easier to keep the code looking similar to the MCD code - why change it unnecessarily, plus while I get the policy of using Fatalf where it's convenient, why change working code to stop using "proper" error handling?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change working code to stop using "proper" error handling?

To keep it consistent with the rest of the whole codebase. Not strongly opposed though, just seems out of place. Keeping in sync with the MCD version is a good point though. Maybe add a comment about that?

}
if targetMatched {
changed = false
return
}
Expand Down
32 changes: 32 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"testing"
)

func mustCompareOSImageURL(t *testing.T, refA, refB string) bool {
m, err := compareOSImageURL(refA, refB)
if err != nil {
t.Fatalf("%v", err)
}
return m
}

func TestCompareOSImageURL(t *testing.T) {
refA := "registry.example.com/foo/bar@sha256:0743a3cc3bcf3b4aabb814500c2739f84cb085ff4e7ec7996aef7977c4c19c7f"
refB := "registry.example.com/foo/baz@sha256:0743a3cc3bcf3b4aabb814500c2739f84cb085ff4e7ec7996aef7977c4c19c7f"
refC := "registry.example.com/foo/bar@sha256:2a76681fd15bfc06fa4aa0ff6913ba17527e075417fc92ea29f6bcc2afca24ff"
if !mustCompareOSImageURL(t, refA, refA) {
t.Fatalf("Expected refA ident")
}
if !mustCompareOSImageURL(t, refA, refB) {
t.Fatalf("Expected refA = refB")
}
if mustCompareOSImageURL(t, refA, refC) {
t.Fatalf("Expected refA != refC")
}
m, err := compareOSImageURL(refA, "registry.example.com/foo/bar")
if m || err == nil {
t.Fatalf("Expected err")
}
}
189 changes: 189 additions & 0 deletions vendor/github.com/containers/image/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading