github.com/argoproj/argo-cd@v1.8.7/hack/dev-mounter/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path" 9 "path/filepath" 10 "strings" 11 "time" 12 13 "github.com/argoproj/pkg/errors" 14 log "github.com/sirupsen/logrus" 15 "github.com/spf13/cobra" 16 v1 "k8s.io/api/core/v1" 17 "k8s.io/client-go/informers" 18 "k8s.io/client-go/kubernetes" 19 "k8s.io/client-go/tools/cache" 20 "k8s.io/client-go/tools/clientcmd" 21 22 "github.com/argoproj/argo-cd/util/cli" 23 24 // load the gcp plugin (required to authenticate against GKE clusters). 25 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" 26 // load the oidc plugin (required to authenticate with OpenID Connect). 27 _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" 28 // load the azure plugin (required to authenticate with AKS clusters). 29 _ "k8s.io/client-go/plugin/pkg/client/auth/azure" 30 ) 31 32 func newCommand() *cobra.Command { 33 var ( 34 clientConfig clientcmd.ClientConfig 35 configMaps []string 36 ) 37 var command = cobra.Command{ 38 Run: func(cmd *cobra.Command, args []string) { 39 config, err := clientConfig.ClientConfig() 40 errors.CheckError(err) 41 ns, _, err := clientConfig.Namespace() 42 errors.CheckError(err) 43 cmNameToPath := make(map[string]string) 44 for _, cm := range configMaps { 45 parts := strings.Split(cm, "=") 46 if len(parts) != 2 { 47 log.Fatal("--configmap value should be include config map name and the path separated by '='") 48 } 49 log.Infof("Saving %s to %s", parts[0], parts[1]) 50 cmNameToPath[parts[0]] = parts[1] 51 } 52 53 handledConfigMap := func(obj interface{}) { 54 cm, ok := obj.(*v1.ConfigMap) 55 if !ok { 56 return 57 } 58 destPath, ok := cmNameToPath[cm.Name] 59 if !ok { 60 return 61 } 62 err := os.MkdirAll(destPath, os.ModePerm) 63 if err != nil { 64 log.Warnf("Failed to create directory: %v", err) 65 return 66 } 67 // Remove files that do not exist in ConfigMap anymore 68 err = filepath.Walk(destPath, func(path string, info os.FileInfo, err error) error { 69 if info.IsDir() { 70 return nil 71 } 72 if err != nil { 73 log.Warnf("Error walking path %s: %v", path, err) 74 } 75 p := filepath.Base(path) 76 if _, ok := cm.Data[p]; !ok { 77 log.Infof("Removing file '%s'", path) 78 err := os.Remove(path) 79 if err != nil { 80 log.Warnf("Failed to remove file %s: %v", path, err) 81 } 82 } 83 return nil 84 }) 85 if err != nil { 86 log.Fatalf("Error: %v", err) 87 } 88 // Create or update files that are specified in ConfigMap 89 for name, data := range cm.Data { 90 p := path.Join(destPath, name) 91 err := ioutil.WriteFile(p, []byte(data), 0644) 92 if err != nil { 93 log.Warnf("Failed to create file %s: %v", p, err) 94 } 95 } 96 } 97 98 kubeClient := kubernetes.NewForConfigOrDie(config) 99 factory := informers.NewSharedInformerFactoryWithOptions(kubeClient, 1*time.Minute, informers.WithNamespace(ns)) 100 informer := factory.Core().V1().ConfigMaps().Informer() 101 informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ 102 AddFunc: handledConfigMap, 103 UpdateFunc: func(oldObj, newObj interface{}) { 104 handledConfigMap(newObj) 105 }, 106 }) 107 informer.Run(context.Background().Done()) 108 }, 109 } 110 clientConfig = cli.AddKubectlFlagsToCmd(&command) 111 command.Flags().StringArrayVar(&configMaps, "configmap", nil, "Config Map name and corresponding path. E.g. argocd-ssh-known-hosts-cm=/tmp/argocd/ssh") 112 return &command 113 } 114 115 func main() { 116 if err := newCommand().Execute(); err != nil { 117 fmt.Println(err) 118 os.Exit(1) 119 } 120 }