Skip to content

Commit

Permalink
Bug 1576881 - Add discovery to openshift adapter (#77)
Browse files Browse the repository at this point in the history
* Bug 1576881 - Add discovery to openshift adapter

* Renamed openshift adapter to partner_rhcc

* Add partner_rhcc to registry.go

* Fix tests
  • Loading branch information
dymurray authored and Shawn Hurley committed May 15, 2018
1 parent 0840159 commit d1bf04c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,75 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/automationbroker/bundle-lib/apb"
log "github.com/sirupsen/logrus"
)

const openShiftName = "openshift"
const openShiftManifestURL = "%v/v2/%v/manifests/%v"
const partnerName = "partner_rhcc"
const partnerManifestURL = "%v/v2/%v/manifests/%v"
const partnerCatalogURL = "%v/v2/_catalog"

// OpenShiftAdapter - Docker Hub Adapter
type OpenShiftAdapter struct {
// PartnerRhccAdapter - Partner RHCC Adapter
type PartnerRhccAdapter struct {
Config Configuration
}

// OpenShiftImage - Image from a OpenShift registry.
type OpenShiftImage struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}

// RegistryName - Retrieve the registry name
func (r OpenShiftAdapter) RegistryName() string {
return openShiftName
func (r PartnerRhccAdapter) RegistryName() string {
return partnerName
}

// GetImageNames - retrieve the images
func (r OpenShiftAdapter) GetImageNames() ([]string, error) {
log.Debug("OpenShiftAdapter::GetImageNames")
log.Debug("BundleSpecLabel: %s", BundleSpecLabel)
func (r PartnerRhccAdapter) GetImageNames() ([]string, error) {
log.Debug("PartnerRhccAdapter::GetImageNames")
log.Debugf("BundleSpecLabel: %s", BundleSpecLabel)

if r.Config.Images != nil {
log.Debugf("Configured to use images: %v", r.Config.Images)
return r.Config.Images, nil
}
log.Debugf("Did not find images in config, attempting to discover from %s/v2/_catalog", r.Config.URL)

req, err := http.NewRequest("GET", fmt.Sprintf(partnerCatalogURL, r.Config.URL), nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorf("Failed to load catalog response at %s - %v", partnerCatalogURL, err)
return nil, err
}
defer resp.Body.Close()

images := r.Config.Images
log.Debug("Configured to use images: %v", images)
if resp.StatusCode != 200 {
log.Errorf("Failed to fetch catalog response. Expected a 200 status and got: %v", resp.Status)
return nil, errors.New(resp.Status)
}
imageResp, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var imageList []string
err = json.Unmarshal(imageResp, &imageList)
if err != nil {
return nil, err
}

return images, nil
return imageList, nil
}

// FetchSpecs - retrieve the spec for the image names.
func (r OpenShiftAdapter) FetchSpecs(imageNames []string) ([]*apb.Spec, error) {
log.Debug("OpenShiftAdapter::FetchSpecs")
func (r PartnerRhccAdapter) FetchSpecs(imageNames []string) ([]*apb.Spec, error) {
log.Debug("PartnerRhccAdapter::FetchSpecs")
specs := []*apb.Spec{}
for _, imageName := range imageNames {
log.Debug("%v", imageName)
log.Debugf("%v", imageName)
spec, err := r.loadSpec(imageName)
if err != nil {
log.Errorf("Failed to retrieve spec data for image %s - %v", imageName, err)
Expand All @@ -74,8 +100,8 @@ func (r OpenShiftAdapter) FetchSpecs(imageNames []string) ([]*apb.Spec, error) {
return specs, nil
}

// getOpenShiftToken - will retrieve the docker hub token.
func (r OpenShiftAdapter) getOpenShiftAuthToken() (string, error) {
// getAuthToken - will retrieve the docker hub token.
func (r PartnerRhccAdapter) getAuthToken() (string, error) {
type TokenResponse struct {
Token string `json:"token"`
}
Expand Down Expand Up @@ -131,19 +157,19 @@ func (r OpenShiftAdapter) getOpenShiftAuthToken() (string, error) {
return tokenResp.Token, nil
}

func (r OpenShiftAdapter) loadSpec(imageName string) (*apb.Spec, error) {
log.Debug("OpenShiftAdapter::LoadSpec")
func (r PartnerRhccAdapter) loadSpec(imageName string) (*apb.Spec, error) {
log.Debug("PartnerRhccAdapter::LoadSpec")
if r.Config.Tag == "" {
r.Config.Tag = "latest"
}
req, err := http.NewRequest("GET", fmt.Sprintf(openShiftManifestURL, r.Config.URL, imageName, r.Config.Tag), nil)
req, err := http.NewRequest("GET", fmt.Sprintf(partnerManifestURL, r.Config.URL, imageName, r.Config.Tag), nil)
if err != nil {
return nil, err
}
token, err := r.getOpenShiftAuthToken()
token, err := r.getAuthToken()
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
return imageToSpec(req, fmt.Sprintf("%s/%s:%s", r.RegistryName(), imageName, r.Config.Tag))
return imageToSpec(req, fmt.Sprintf("%s/%s:%s", r.Config.URL.Hostname(), imageName, r.Config.Tag))
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

var Images = []string{"satoshi/nakamoto", "foo/bar", "paul/atreides"}

const OpenShiftManifestResponse = `
const PartnerManifestResponse = `
{
"schemaVersion": 1,
"name": "%v",
Expand Down Expand Up @@ -89,15 +89,15 @@ const AuthResponse = `
"issued_at": "2018-03-27T19:54:19Z"
}`

func TestOpenShiftName(t *testing.T) {
ocpa := OpenShiftAdapter{}
ft.Equal(t, ocpa.RegistryName(), "openshift", "openshift name does not match `openshift`")
func TestPartnerName(t *testing.T) {
partnera := PartnerRhccAdapter{}
ft.Equal(t, partnera.RegistryName(), "partner_rhcc", "partner_rhcc name does not match `partner_rhcc`")
}

func TestOpenShiftGetImageNames(t *testing.T) {
ocpa := OpenShiftAdapter{}
ocpa.Config.Images = Images
imagesFound, err := ocpa.GetImageNames()
func TestPartnerGetImageNames(t *testing.T) {
partnera := PartnerRhccAdapter{}
partnera.Config.Images = Images
imagesFound, err := partnera.GetImageNames()
if err != nil {
t.Fatal("Error: ", err)
}
Expand All @@ -122,7 +122,7 @@ func TestOpenShiftFetchSpecs(t *testing.T) {
}
if strings.Contains(r.URL.EscapedPath(), "manifests/") {
name := strings.Split(r.URL.EscapedPath(), "manifests/")[1]
fmt.Fprintf(w, fmt.Sprintf(OpenShiftManifestResponse, name))
fmt.Fprintf(w, fmt.Sprintf(PartnerManifestResponse, name))
}

}))
Expand All @@ -131,16 +131,16 @@ func TestOpenShiftFetchSpecs(t *testing.T) {
if err != nil {
t.Fatal("Error: ", err)
}
ocpa := OpenShiftAdapter{
partnera := PartnerRhccAdapter{
Config: Configuration{
URL: url,
User: "satoshi",
Pass: "nakamoto",
Images: Images,
},
}
imageNames, err := ocpa.GetImageNames()
specs, err := ocpa.FetchSpecs(imageNames)
imageNames, err := partnera.GetImageNames()
specs, err := partnera.FetchSpecs(imageNames)
if err != nil {
t.Fatal("Error: ", err)
}
Expand Down
4 changes: 2 additions & 2 deletions registries/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ func NewCustomRegistry(configuration Config, adapter adapters.Adapter, asbNamesp
adapter = &adapters.DockerHubAdapter{Config: c}
case "mock":
adapter = &adapters.MockAdapter{Config: c}
case "openshift":
adapter = &adapters.OpenShiftAdapter{Config: c}
case "partner_rhcc":
adapter = &adapters.PartnerRhccAdapter{Config: c}
case "local_openshift":
adapter = &adapters.LocalOpenShiftAdapter{Config: c}
case "helm":
Expand Down

0 comments on commit d1bf04c

Please sign in to comment.