@@ -20,8 +20,11 @@ package main
2020
2121import (
2222 "context"
23+ "errors"
24+ "fmt"
2325 "math/rand"
2426 "os"
27+ "os/signal"
2528 "time"
2629
2730 "github.com/spf13/cobra"
@@ -38,27 +41,31 @@ func main() {
3841 var RegistryMirrorStr string
3942
4043 command := & cobra.Command {
41- Use : "acr-credential-provider configFile" ,
42- Short : "Acr credential provider for Kubelet" ,
43- Long : `The acr credential provider is responsible for providing ACR credentials for kubelet` ,
44- Args : cobra .MinimumNArgs (1 ),
45- Version : version .Get ().GitVersion ,
46- Run : func (_ * cobra.Command , args []string ) {
47- if len (args ) != 1 {
48- klog .Errorf ("Config file is not specified" )
49- os .Exit (1 )
44+ Use : "acr-credential-provider configFile" ,
45+ Short : "Acr credential provider for Kubelet" ,
46+ Long : `The acr credential provider is responsible for providing ACR credentials for kubelet` ,
47+ Args : func (_ * cobra.Command , args []string ) error {
48+ if len (args ) == 0 {
49+ return errors .New ("Config file is not specified" )
5050 }
51-
51+ if len (args ) > 1 {
52+ return fmt .Errorf ("expected exactly one argument (config file); Got arguments: %v" , args )
53+ }
54+ return nil
55+ },
56+ Version : version .Get ().GitVersion ,
57+ RunE : func (cmd * cobra.Command , args []string ) error {
5258 acrProvider , err := credentialprovider .NewAcrProviderFromConfig (args [0 ], RegistryMirrorStr )
5359 if err != nil {
5460 klog .Errorf ("Failed to initialize ACR provider: %v" , err )
55- os . Exit ( 1 )
61+ return err
5662 }
5763
58- if err := NewCredentialProvider (acrProvider ).Run (context . TODO ()); err != nil {
64+ if err := NewCredentialProvider (acrProvider ).Run (cmd . Context ()); err != nil {
5965 klog .Errorf ("Error running acr credential provider: %v" , err )
60- os . Exit ( 1 )
66+ return err
6167 }
68+ return nil
6269 },
6370 }
6471
@@ -70,7 +77,11 @@ func main() {
7077 "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" )
7178
7279 logs .AddFlags (command .Flags ())
73- if err := command .Execute (); err != nil {
80+ if err := func () error {
81+ ctx , cancel := signal .NotifyContext (context .Background (), os .Interrupt )
82+ defer cancel ()
83+ return command .ExecuteContext (ctx )
84+ }(); err != nil {
7485 os .Exit (1 )
7586 }
7687}
0 commit comments