mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-11 14:09:21 +00:00
feature: adding env to allow for private repo usage
This commit is contained in:
parent
d6fea4f72c
commit
f8e9196791
2 changed files with 21 additions and 16 deletions
|
|
@ -13,13 +13,13 @@ import (
|
|||
"github.com/yannh/kubeconform/pkg/cache"
|
||||
)
|
||||
|
||||
type httpGetter interface {
|
||||
Get(url string) (resp *http.Response, err error)
|
||||
type httpDoer interface {
|
||||
Do(*http.Request) (resp *http.Response, err error)
|
||||
}
|
||||
|
||||
// SchemaRegistry is a file repository (local or remote) that contains JSON schemas for Kubernetes resources
|
||||
type SchemaRegistry struct {
|
||||
c httpGetter
|
||||
c httpDoer
|
||||
schemaPathTemplate string
|
||||
cache cache.Cache
|
||||
strict bool
|
||||
|
|
@ -72,8 +72,13 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers
|
|||
return url, b.([]byte), nil
|
||||
}
|
||||
}
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
|
||||
resp, err := r.c.Get(url)
|
||||
if token, exist := os.LookupEnv("GITHUB_TOKEN"); exist {
|
||||
req.Header.Add("Authorization", fmt.Sprintf("token %s", token))
|
||||
}
|
||||
|
||||
resp, err := r.c.Do(req)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("failed downloading schema at %s: %s", url, err)
|
||||
if r.debug {
|
||||
|
|
|
|||
|
|
@ -9,23 +9,23 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
type mockHTTPGetter struct {
|
||||
httpGet func(string) (*http.Response, error)
|
||||
type mockHTTPDoer struct {
|
||||
httpDo func(*http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
func newMockHTTPGetter(f func(string) (*http.Response, error)) *mockHTTPGetter {
|
||||
return &mockHTTPGetter{
|
||||
httpGet: f,
|
||||
func newMockHTTPDoer(f func(*http.Request) (*http.Response, error)) *mockHTTPDoer {
|
||||
return &mockHTTPDoer{
|
||||
httpDo: f,
|
||||
}
|
||||
}
|
||||
func (m mockHTTPGetter) Get(url string) (resp *http.Response, err error) {
|
||||
return m.httpGet(url)
|
||||
func (m mockHTTPDoer) Do(req *http.Request) (resp *http.Response, err error) {
|
||||
return m.httpDo(req)
|
||||
}
|
||||
|
||||
func TestDownloadSchema(t *testing.T) {
|
||||
for _, testCase := range []struct {
|
||||
name string
|
||||
c httpGetter
|
||||
c httpDoer
|
||||
schemaPathTemplate string
|
||||
strict bool
|
||||
resourceKind, resourceAPIVersion, k8sversion string
|
||||
|
|
@ -34,7 +34,7 @@ func TestDownloadSchema(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
"error when downloading",
|
||||
newMockHTTPGetter(func(url string) (resp *http.Response, err error) {
|
||||
newMockHTTPDoer(func(req *http.Request) (resp *http.Response, err error) {
|
||||
return nil, fmt.Errorf("failed downloading from registry")
|
||||
}),
|
||||
"http://kubernetesjson.dev",
|
||||
|
|
@ -47,7 +47,7 @@ func TestDownloadSchema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"getting 404",
|
||||
newMockHTTPGetter(func(url string) (resp *http.Response, err error) {
|
||||
newMockHTTPDoer(func(req *http.Request) (resp *http.Response, err error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusNotFound,
|
||||
Body: io.NopCloser(strings.NewReader("http response mock body")),
|
||||
|
|
@ -63,7 +63,7 @@ func TestDownloadSchema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"getting 503",
|
||||
newMockHTTPGetter(func(url string) (resp *http.Response, err error) {
|
||||
newMockHTTPDoer(func(req *http.Request) (resp *http.Response, err error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusServiceUnavailable,
|
||||
Body: io.NopCloser(strings.NewReader("http response mock body")),
|
||||
|
|
@ -79,7 +79,7 @@ func TestDownloadSchema(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"200",
|
||||
newMockHTTPGetter(func(url string) (resp *http.Response, err error) {
|
||||
newMockHTTPDoer(func(req *http.Request) (resp *http.Response, err error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(strings.NewReader("http response mock body")),
|
||||
|
|
|
|||
Loading…
Reference in a new issue