Skip to content

Commit 616c82a

Browse files
dilyevskyclaude
andcommitted
[cmd] recover existing cluster name from namespace annotation on reinstall
Read the apoxy.dev/cluster-name annotation from the target namespace before calling the onboarding API, so reinstalls preserve the original cluster name instead of generating a new random one. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 759b464 commit 616c82a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkg/cmd/k8s.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"k8s.io/apimachinery/pkg/runtime"
2424
runtimejson "k8s.io/apimachinery/pkg/runtime/serializer/json"
2525
"k8s.io/apimachinery/pkg/types"
26+
"k8s.io/client-go/kubernetes"
2627
"k8s.io/client-go/discovery"
2728
"k8s.io/client-go/discovery/cached/memory"
2829
"k8s.io/client-go/dynamic"
@@ -35,6 +36,8 @@ import (
3536
"github.com/apoxy-dev/apoxy/config"
3637
)
3738

39+
const clusterNameAnnotation = "apoxy.dev/cluster-name"
40+
3841
var (
3942
decoder = scheme.Codecs.UniversalDeserializer()
4043
encoder = runtimejson.NewYAMLSerializer(runtimejson.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
@@ -820,6 +823,21 @@ will automatically connect to the Apoxy API and begin managing your in-cluster A
820823
return err
821824
}
822825

826+
// If --cluster-name wasn't explicitly provided, recover it from the
827+
// existing namespace's apoxy.dev/cluster-name annotation so the API
828+
// returns YAML consistent with the current install.
829+
if clusterName == "" {
830+
clientset, err := kubernetes.NewForConfig(kc)
831+
if err == nil {
832+
ns, err := clientset.CoreV1().Namespaces().Get(cmd.Context(), namespace, metav1.GetOptions{})
833+
if err == nil {
834+
if v, ok := ns.Annotations[clusterNameAnnotation]; ok {
835+
clusterName = v
836+
}
837+
}
838+
}
839+
}
840+
823841
yamlz, err := getYAML(clusterName, mirror)
824842
if err != nil {
825843
return fmt.Errorf("failed to get YAML: %w", err)

0 commit comments

Comments
 (0)