Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jottofar committed Jul 28, 2020
1 parent 2927b48 commit d1af4f9
Showing 1 changed file with 73 additions and 52 deletions.
125 changes: 73 additions & 52 deletions pkg/cincinnati/cincinnati.go
Expand Up @@ -10,6 +10,8 @@ import (
"net/url"
"time"

"k8s.io/klog"

"github.com/blang/semver/v4"
"github.com/google/uuid"
)
Expand All @@ -20,7 +22,8 @@ const (
GraphMediaType = "application/json"

// Timeout when calling upstream Cincinnati stack.
getUpdatesTimeout = time.Minute * 60
//getUpdatesTimeout = time.Minute * 60
getUpdatesTimeout = time.Millisecond * 1
)

// Client is a Cincinnati client which can be used to fetch update graphs from
Expand Down Expand Up @@ -57,18 +60,22 @@ func (err *Error) Error() string {
}

// Run HTTP request in a goroutine and pass the response to f.
func clientDo(ctx context.Context, req *http.Request, client http.Client, f func(*http.Response, error) error) error {
c := make(chan error, 1)
//func clientDo(ctx context.Context, req *http.Request, client http.Client, f func(*http.Response, error) error) error {
func clientDo(ctx context.Context, req *http.Request, client http.Client) (*http.Response, error) {
//c := make(chan error, 1)
getUpdatesCtx, cancel := context.WithTimeout(ctx, getUpdatesTimeout)
defer cancel()
go func() { c <- f(client.Do(req.WithContext(getUpdatesCtx))) }()
select {
case <-getUpdatesCtx.Done():
<-c // Wait for f to return.
return getUpdatesCtx.Err()
case err := <-c:
return err
}
return client.Do(req.WithContext(getUpdatesCtx))
/*
go func() { c <- f(client.Do(req.WithContext(getUpdatesCtx))) }()
select {
case <-getUpdatesCtx.Done():
<-c // Wait for f to return.
return getUpdatesCtx.Err()
case err := <-c:
return err
}
*/
}

// GetUpdates fetches the next-applicable update payloads from the specified
Expand All @@ -79,6 +86,11 @@ func clientDo(ctx context.Context, req *http.Request, client http.Client, f func
// the current version and their payloads indicate from where the actual update
// image can be downloaded.
func (c Client) GetUpdates(ctx context.Context, uri *url.URL, arch string, channel string, version semver.Version) ([]Update, error) {
// HACK!!!!!!!!!!!!!!!!!!!!!!!!!!!
version, err := semver.Parse("4.5.0-rc.1")
if err != nil {
return nil, &Error{Reason: "BAD HACK", Message: err.Error(), cause: err}
}
transport := http.Transport{}
// Prepare parametrized cincinnati query.
queryParams := uri.Query()
Expand All @@ -104,57 +116,66 @@ func (c Client) GetUpdates(ctx context.Context, uri *url.URL, arch string, chann

client := http.Client{Transport: &transport}
var updates []Update
err = clientDo(ctx, req, client, func(resp *http.Response, err error) error {
if err != nil {
return &Error{Reason: "RemoteFailed", Message: err.Error(), cause: err}
//err = clientDo(ctx, req, client, func(resp *http.Response, err error) error {
resp, err := clientDo(ctx, req, client)
if err != nil {
klog.Infof("Error=%v!!!!!!!!!!!!!!!!!!!!!!!!", err)
if resp == nil {
klog.Infof("resp is nil!!!!!!!!!!!!!!!!!!!!!!!!")
}
defer resp.Body.Close()
return nil, &Error{Reason: "RemoteFailed", Message: err.Error(), cause: err}
}
if resp == nil {
klog.Infof("resp is nil but err is nil!!!!!!!!!!!!!!!!!!!!!!!!")
return nil, &Error{Reason: "RemoteFailed", Message: "resp is nil but err is nil!!!!!!!!!!!!!!!!!!!!!!!!", cause: err}
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return &Error{Reason: "ResponseFailed", Message: fmt.Sprintf("unexpected HTTP status: %s", resp.Status)}
}
if resp.StatusCode != http.StatusOK {
return nil, &Error{Reason: "ResponseFailed", Message: fmt.Sprintf("unexpected HTTP status: %s", resp.Status)}
}

// Parse the graph.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &Error{Reason: "ResponseFailed", Message: err.Error(), cause: err}
}
// Parse the graph.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, &Error{Reason: "ResponseFailed", Message: err.Error(), cause: err}
}

var graph graph
if err = json.Unmarshal(body, &graph); err != nil {
return &Error{Reason: "ResponseInvalid", Message: err.Error(), cause: err}
}
var graph graph
if err = json.Unmarshal(body, &graph); err != nil {
return nil, &Error{Reason: "ResponseInvalid", Message: err.Error(), cause: err}
}

// Find the current version within the graph.
var currentIdx int
found := false
for i, node := range graph.Nodes {
if version.EQ(node.Version) {
currentIdx = i
found = true
break
}
// Find the current version within the graph.
var currentIdx int
found := false
for i, node := range graph.Nodes {
if version.EQ(node.Version) {
currentIdx = i
found = true
break
}
if !found {
return &Error{
Reason: "VersionNotFound",
Message: fmt.Sprintf("currently installed version %s not found in the %q channel", version, channel),
}
}
if !found {
return nil, &Error{
Reason: "VersionNotFound",
Message: fmt.Sprintf("currently installed version %s not found in the %q channel", version, channel),
}
}

// Find the children of the current version.
var nextIdxs []int
for _, edge := range graph.Edges {
if edge.Origin == currentIdx {
nextIdxs = append(nextIdxs, edge.Destination)
}
// Find the children of the current version.
var nextIdxs []int
for _, edge := range graph.Edges {
if edge.Origin == currentIdx {
nextIdxs = append(nextIdxs, edge.Destination)
}
}

for _, i := range nextIdxs {
updates = append(updates, Update(graph.Nodes[i]))
}
return nil
})
for _, i := range nextIdxs {
updates = append(updates, Update(graph.Nodes[i]))
}
//return nil
//})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d1af4f9

Please sign in to comment.