mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
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]>
195 lines
4.9 KiB
Go
195 lines
4.9 KiB
Go
package cluster
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
|
|
|
"github.com/hanzoai/cd/common"
|
|
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
"github.com/hanzoai/cd/util/clusterauth"
|
|
|
|
clusterpkg "github.com/hanzoai/cd/pkg/apiclient/cluster"
|
|
"github.com/hanzoai/cd/test/e2e/fixture"
|
|
)
|
|
|
|
// this implements the "when" part of given/when/then
|
|
//
|
|
// none of the func implement error checks, and that is complete intended, you should check for errors
|
|
// using the Then()
|
|
type Actions struct {
|
|
context *Context
|
|
lastOutput string
|
|
lastError error
|
|
ignoreErrors bool
|
|
}
|
|
|
|
func (a *Actions) IgnoreErrors() *Actions {
|
|
a.ignoreErrors = true
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) DoNotIgnoreErrors() *Actions {
|
|
a.ignoreErrors = false
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) Create() *Actions {
|
|
_, clusterClient, _ := fixture.ArgoCDClientset.NewClusterClient()
|
|
|
|
_, err := clusterClient.Create(context.Background(), &clusterpkg.ClusterCreateRequest{
|
|
Cluster: &v1alpha1.Cluster{
|
|
Server: a.context.server,
|
|
Name: a.context.GetName(),
|
|
Config: v1alpha1.ClusterConfig{BearerToken: a.context.bearerToken},
|
|
Namespaces: a.context.namespaces,
|
|
RefreshRequestedAt: nil,
|
|
Info: v1alpha1.ClusterInfo{},
|
|
Shard: nil,
|
|
ClusterResources: false,
|
|
Project: a.context.project,
|
|
},
|
|
Upsert: a.context.upsert,
|
|
})
|
|
if err != nil {
|
|
if !a.ignoreErrors {
|
|
log.Fatalf("Failed to upsert cluster %v", err.Error())
|
|
}
|
|
a.lastError = errors.New(err.Error())
|
|
}
|
|
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) CreateWithRBAC() *Actions {
|
|
pathOpts := clientcmd.NewDefaultPathOptions()
|
|
config, err := pathOpts.GetStartingConfig()
|
|
if err != nil {
|
|
a.lastError = err
|
|
return a
|
|
}
|
|
clientConfig := clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{})
|
|
conf, err := clientConfig.ClientConfig()
|
|
if err != nil {
|
|
a.lastError = err
|
|
return a
|
|
}
|
|
client := kubernetes.NewForConfigOrDie(conf)
|
|
|
|
_, err = clusterauth.InstallClusterManagerRBAC(client, "kube-system", []string{}, common.BearerTokenTimeout)
|
|
if err != nil {
|
|
a.lastError = err
|
|
return a
|
|
}
|
|
|
|
// Create a kubeconfig with the current cluster name
|
|
err = a.createKubeconfigForCluster(config, a.context.GetName())
|
|
if err != nil {
|
|
a.lastError = err
|
|
return a
|
|
}
|
|
|
|
return a.Create()
|
|
}
|
|
|
|
// Helper function to create a kubeconfig file with the given cluster name
|
|
func (a *Actions) createKubeconfigForCluster(config *clientcmdapi.Config, newClusterName string) error {
|
|
// Get the current context
|
|
currentContext := config.Contexts[config.CurrentContext]
|
|
if currentContext == nil {
|
|
return errors.New("no current context found")
|
|
}
|
|
|
|
// Get the original cluster
|
|
originalCluster := config.Clusters[currentContext.Cluster]
|
|
if originalCluster == nil {
|
|
return errors.New("cluster not found in config")
|
|
}
|
|
|
|
// Create a new cluster entry with the same config but different name
|
|
newCluster := originalCluster.DeepCopy()
|
|
config.Clusters[newClusterName] = newCluster
|
|
|
|
// Create a new context pointing to the new cluster
|
|
newContext := currentContext.DeepCopy()
|
|
newContext.Cluster = newClusterName
|
|
config.Contexts[newClusterName] = newContext
|
|
|
|
// Set the new context as current
|
|
config.CurrentContext = newClusterName
|
|
|
|
// Write to a temporary kubeconfig file
|
|
tmpFile, err := os.CreateTemp("", "kubeconfig-*.yaml")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tmpFile.Close()
|
|
|
|
// Write the modified config to the temp file
|
|
if err := clientcmd.WriteToFile(*config, tmpFile.Name()); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Set the KUBECONFIG environment variable to use this temp file
|
|
// This will be use by subsequent kubectl/cd commands to connect to the cluster
|
|
a.context.T().Setenv("KUBECONFIG", tmpFile.Name())
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *Actions) List() *Actions {
|
|
a.context.T().Helper()
|
|
a.runCli("cluster", "list")
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) Get() *Actions {
|
|
a.context.T().Helper()
|
|
a.runCli("cluster", "get", a.context.server)
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) GetByName() *Actions {
|
|
a.context.T().Helper()
|
|
a.runCli("cluster", "get", a.context.GetName())
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) SetNamespaces() *Actions {
|
|
a.context.T().Helper()
|
|
a.runCli("cluster", "set", a.context.GetName(), "--namespace", strings.Join(a.context.namespaces, ","))
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) DeleteByName() *Actions {
|
|
a.context.T().Helper()
|
|
|
|
a.runCli("cluster", "rm", a.context.GetName(), "--yes")
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) DeleteByServer() *Actions {
|
|
a.context.T().Helper()
|
|
|
|
a.runCli("cluster", "rm", a.context.server, "--yes")
|
|
return a
|
|
}
|
|
|
|
func (a *Actions) Then() *Consequences {
|
|
a.context.T().Helper()
|
|
time.Sleep(fixture.WhenThenSleepInterval)
|
|
return &Consequences{a.context, a}
|
|
}
|
|
|
|
func (a *Actions) runCli(args ...string) {
|
|
a.context.T().Helper()
|
|
a.lastOutput, a.lastError = fixture.RunCli(args...)
|
|
}
|