Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(credential-provider): check empty mirror mapping and add debugging info #8647

Merged
merged 4 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cmd/acr-credential-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/klog/v2"

"sigs.k8s.io/cloud-provider-azure/pkg/credentialprovider"
"sigs.k8s.io/cloud-provider-azure/pkg/version"
)

func main() {
Expand All @@ -37,10 +38,11 @@ func main() {
var RegistryMirrorStr string

command := &cobra.Command{
Use: "acr-credential-provider configFile",
Short: "Acr credential provider for Kubelet",
Long: `The acr credential provider is responsible for providing ACR credentials for kubelet`,
Args: cobra.MinimumNArgs(1),
Use: "acr-credential-provider configFile",
Short: "Acr credential provider for Kubelet",
Long: `The acr credential provider is responsible for providing ACR credentials for kubelet`,
Args: cobra.MinimumNArgs(1),
Version: version.Get().GitVersion,
Run: func(_ *cobra.Command, args []string) {
if len(args) != 1 {
klog.Errorf("Config file is not specified")
Expand All @@ -67,6 +69,7 @@ func main() {
command.Flags().StringVarP(&RegistryMirrorStr, "registry-mirror", "r", "",
"Mirror a source registry host to a target registry host, and image pull credential will be requested to the target registry host when the image is from source registry host")

logs.AddFlags(command.Flags())
if err := command.Execute(); err != nil {
os.Exit(1)
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/credentialprovider/azure_acr_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"unicode"

utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/klog/v2"
)

const (
Expand Down Expand Up @@ -174,6 +175,12 @@ func performTokenExchange(
return "", fmt.Errorf("Www-Authenticate: failed to reach auth url %s", authEndpoint)
}

if exchange.Header != nil {
if correlationID, ok := exchange.Header["X-Ms-Correlation-Request-Id"]; ok {
klog.V(4).Infof("correlationID: %s", correlationID)
}
}

defer exchange.Body.Close()
if exchange.StatusCode != 200 {
return "", fmt.Errorf("Www-Authenticate: auth url %s responded with status code %d", authEndpoint, exchange.StatusCode)
Expand Down
5 changes: 5 additions & 0 deletions pkg/credentialprovider/azure_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ func (a *acrProvider) processImageWithRegistryMirror(image string) (string, stri
func parseRegistryMirror(registryMirrorStr string) map[string]string {
registryMirror := map[string]string{}

registryMirrorStr = strings.TrimSpace(registryMirrorStr)
if len(registryMirrorStr) == 0 {
return registryMirror
}

registryMirrorStr = strings.ReplaceAll(registryMirrorStr, " ", "")
for _, mapping := range strings.Split(registryMirrorStr, ",") {
parts := strings.Split(mapping, ":")
Expand Down
11 changes: 8 additions & 3 deletions pkg/credentialprovider/azure_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,28 @@ func TestProcessMirrorMapping(t *testing.T) {
expected map[string]string
}{
{
"multiple",
"empty registry mirrors",
"",
map[string]string{},
},
{
"multiple registry mirrors",
"aaa:bbb,ccc:ddd",
map[string]string{
"aaa": "bbb",
"ccc": "ddd",
},
},
{
"multiple with some spaces",
"multiple registry mirrors joined with comma and extra spaces",
"aaa: bbb, ccc:ddd",
map[string]string{
"aaa": "bbb",
"ccc": "ddd",
},
},
{
"single",
"single registry mirror",
"aaa:bbb",
map[string]string{
"aaa": "bbb",
Expand Down