-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: alexey.komyakov <[email protected]>
- Loading branch information
Showing
5 changed files
with
80 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
kubeauth "github.com/google/go-containerregistry/pkg/authn/kubernetes" | ||
"github.com/sirupsen/logrus" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn" | ||
) | ||
|
||
type Provider struct { | ||
pullSecretsGetter func(image string) []corev1.Secret | ||
} | ||
|
||
func NewProvider(pullSecretsGetter func(image string) []corev1.Secret) *Provider { | ||
return &Provider{ | ||
pullSecretsGetter: pullSecretsGetter, | ||
} | ||
} | ||
|
||
func (p Provider) GetAuthKeychain(registryStr string) authn.Keychain { | ||
dereferencedPullSecrets := p.pullSecretsGetter(registryStr) | ||
kc, err := kubeauth.NewFromPullSecrets(context.TODO(), dereferencedPullSecrets) | ||
if err != nil { | ||
logrus.Panic(err) | ||
} | ||
return kc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,35 @@ | ||
package providers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
corev1 "k8s.io/api/core/v1" | ||
"strings" | ||
|
||
"github.com/flant/k8s-image-availability-exporter/pkg/providers/amazon" | ||
"github.com/flant/k8s-image-availability-exporter/pkg/providers/k8s" | ||
"github.com/google/go-containerregistry/pkg/authn" | ||
) | ||
|
||
type Provider interface { | ||
GetAuthKeychain(ctx context.Context, registryStr string) (authn.Keychain, error) | ||
GetAuthKeychain(registryStr string) authn.Keychain | ||
} | ||
|
||
func GetProvider(registryStr string) (Provider, error) { | ||
type ProviderRegistry map[string]Provider | ||
|
||
func NewProviderChain(pullSecretsGetter func(image string) []corev1.Secret) ProviderRegistry { | ||
return map[string]Provider{ | ||
"amazon": amazon.NewProvider(), | ||
"k8s": k8s.NewProvider(pullSecretsGetter), | ||
} | ||
} | ||
|
||
type ImagePullSecretsFunc func(image string) []corev1.Secret | ||
|
||
func (p ProviderRegistry) GetAuthKeychain(registryStr string) authn.Keychain { | ||
switch { | ||
case strings.Contains(registryStr, "amazonaws.com"): | ||
return amazon.NewECRProvider(), nil | ||
return p["amazon"].GetAuthKeychain(registryStr) | ||
|
||
default: | ||
return nil, fmt.Errorf("unsupported registry") | ||
return p["k8s"].GetAuthKeychain(registryStr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters