2022-01-17 04:06:44 +00:00
|
|
|
package registryclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-22 00:10:02 -07:00
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/google/go-containerregistry/pkg/authn/github"
|
2022-01-17 04:06:44 +00:00
|
|
|
|
2022-01-28 19:33:27 +00:00
|
|
|
ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
|
|
|
|
"github.com/chrismellard/docker-credential-acr-env/pkg/credhelper"
|
2022-01-17 04:06:44 +00:00
|
|
|
"github.com/google/go-containerregistry/pkg/authn"
|
2022-01-28 19:33:27 +00:00
|
|
|
kauth "github.com/google/go-containerregistry/pkg/authn/kubernetes"
|
|
|
|
"github.com/google/go-containerregistry/pkg/v1/google"
|
2022-01-17 04:06:44 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Secrets []string
|
|
|
|
|
2022-04-22 00:10:02 -07:00
|
|
|
kubeClient kubernetes.Interface
|
|
|
|
namespace string
|
|
|
|
serviceAccount string
|
2022-01-28 19:33:27 +00:00
|
|
|
|
2022-04-22 00:10:02 -07:00
|
|
|
defaultKeychain = authn.NewMultiKeychain(
|
2022-01-28 19:33:27 +00:00
|
|
|
authn.DefaultKeychain,
|
|
|
|
google.Keychain,
|
2022-04-22 00:10:02 -07:00
|
|
|
authn.NewKeychainFromHelper(ecr.NewECRHelper(ecr.WithLogger(ioutil.Discard))),
|
|
|
|
authn.NewKeychainFromHelper(credhelper.NewACRCredentialsHelper()),
|
|
|
|
github.Keychain,
|
2022-01-28 19:33:27 +00:00
|
|
|
)
|
2022-04-22 00:10:02 -07:00
|
|
|
|
|
|
|
DefaultKeychain = defaultKeychain
|
2022-01-17 04:06:44 +00:00
|
|
|
)
|
|
|
|
|
2022-03-16 09:56:47 +05:30
|
|
|
// InitializeLocal loads the docker credentials and initializes the default auth method for container registry API calls
|
|
|
|
func InitializeLocal() {
|
|
|
|
DefaultKeychain = authn.DefaultKeychain
|
|
|
|
}
|
|
|
|
|
2022-01-17 04:06:44 +00:00
|
|
|
// Initialize loads the image pull secrets and initializes the default auth method for container registry API calls
|
2022-04-22 00:10:02 -07:00
|
|
|
func Initialize(client kubernetes.Interface, ns, sa string, imagePullSecrets []string) error {
|
2022-01-17 04:06:44 +00:00
|
|
|
kubeClient = client
|
2022-04-22 00:10:02 -07:00
|
|
|
namespace = ns
|
|
|
|
serviceAccount = sa
|
2022-01-17 04:06:44 +00:00
|
|
|
Secrets = imagePullSecrets
|
|
|
|
|
|
|
|
var kc authn.Keychain
|
2022-01-28 19:33:27 +00:00
|
|
|
kcOpts := kauth.Options{
|
2022-01-17 04:06:44 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
ServiceAccountName: serviceAccount,
|
|
|
|
ImagePullSecrets: imagePullSecrets,
|
|
|
|
}
|
|
|
|
|
2022-01-28 19:33:27 +00:00
|
|
|
kc, err := kauth.New(context.Background(), client, kcOpts)
|
2022-01-17 04:06:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to initialize registry keychain")
|
|
|
|
}
|
|
|
|
|
2022-01-28 19:33:27 +00:00
|
|
|
DefaultKeychain = authn.NewMultiKeychain(
|
|
|
|
defaultKeychain,
|
|
|
|
kc,
|
|
|
|
)
|
|
|
|
|
2022-01-17 04:06:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateKeychain reinitializes the image pull secrets and default auth method for container registry API calls
|
|
|
|
func UpdateKeychain() error {
|
2022-04-22 00:10:02 -07:00
|
|
|
var err = Initialize(kubeClient, namespace, serviceAccount, Secrets)
|
2022-01-17 04:06:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|