Files
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
go.mod still declared `module github.com/hanzoai/deploy` after the repo was
renamed. A Go module path is not cosmetic — it is the resolvable location. So
`go get github.com/hanzoai/cd` could not work, and anything importing this
module had to spell a name that no longer exists at that address. The build log
of v3.6.0 is where it surfaced: the compiler was still emitting
github.com/hanzoai/deploy/cmd/cd/commands.

3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile
and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to
ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from
the module path, replaced separately so neither could clobber the other.

go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 21:16:24 -07:00

123 lines
3.4 KiB
Go

package main
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"github.com/hanzoai/cd/util/cli"
"github.com/hanzoai/cd/util/errors"
// load the gcp plugin (required to authenticate against GKE clusters).
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// load the oidc plugin (required to authenticate with OpenID Connect).
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
// load the azure plugin (required to authenticate with AKS clusters).
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
)
func newCommand() *cobra.Command {
var (
clientConfig clientcmd.ClientConfig
configMaps []string
)
command := cobra.Command{
Run: func(_ *cobra.Command, _ []string) {
config, err := clientConfig.ClientConfig()
errors.CheckError(err)
ns, _, err := clientConfig.Namespace()
errors.CheckError(err)
cmNameToPath := make(map[string]string)
for _, cm := range configMaps {
parts := strings.Split(cm, "=")
if len(parts) != 2 {
log.Fatal("--configmap value should be include config map name and the path separated by '='")
}
log.Infof("Saving %s to %s", parts[0], parts[1])
cmNameToPath[parts[0]] = parts[1]
}
handledConfigMap := func(obj any) {
cm, ok := obj.(*corev1.ConfigMap)
if !ok {
return
}
destPath, ok := cmNameToPath[cm.Name]
if !ok {
return
}
err := os.MkdirAll(destPath, os.ModePerm)
if err != nil {
log.Warnf("Failed to create directory: %v", err)
return
}
// Remove files that do not exist in ConfigMap anymore
err = filepath.Walk(destPath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if err != nil {
log.Warnf("Error walking path %s: %v", path, err)
}
p := filepath.Base(path)
if _, ok := cm.Data[p]; !ok {
log.Infof("Removing file '%s'", path)
err := os.Remove(path)
if err != nil {
log.Warnf("Failed to remove file %s: %v", path, err)
}
}
return nil
})
if err != nil {
log.Fatalf("Error: %v", err)
}
// Create or update files that are specified in ConfigMap
for name, data := range cm.Data {
p := path.Join(destPath, name)
err := os.WriteFile(p, []byte(data), 0o644)
if err != nil {
log.Warnf("Failed to create file %s: %v", p, err)
}
}
}
kubeClient := kubernetes.NewForConfigOrDie(config)
factory := informers.NewSharedInformerFactoryWithOptions(kubeClient, 1*time.Minute, informers.WithNamespace(ns))
informer := factory.Core().V1().ConfigMaps().Informer()
_, err = informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: handledConfigMap,
UpdateFunc: func(_, newObj any) {
handledConfigMap(newObj)
},
})
if err != nil {
log.Error(err)
}
informer.Run(context.Background().Done())
},
}
clientConfig = cli.AddKubectlFlagsToCmd(&command)
command.Flags().StringArrayVar(&configMaps, "configmap", nil, "Config Map name and corresponding path. E.g. cd-ssh-known-hosts-cm=/tmp/cd/ssh")
return &command
}
func main() {
if err := newCommand().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}