Skip to content

Commit f696a1c

Browse files
authored
configurable k8sclient context
configurable k8sclient context
2 parents 93f2e4d + ce60d99 commit f696a1c

4 files changed

Lines changed: 88 additions & 18 deletions

File tree

cyclops-ctrl/api/v1alpha1/module_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const (
4747
GitOpsWriteRepoAnnotation = "cyclops-ui.com/write-repo"
4848
GitOpsWritePathAnnotation = "cyclops-ui.com/write-path"
4949
GitOpsWriteRevisionAnnotation = "cyclops-ui.com/write-revision"
50+
51+
ModuleManagerAnnotation = "cyclops-ui.com/module-manager"
5052
)
5153

5254
type TemplateRef struct {

cyclops-ctrl/internal/modulecontroller/module_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func (r *ModuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
136136
return ctrl.Result{}, err
137137
}
138138

139+
if module.Annotations[cyclopsv1alpha1.ModuleManagerAnnotation] == "mcp" {
140+
r.telemetryClient.MCPModuleReconciliation()
141+
}
142+
139143
r.logger.Info("upsert module", "namespaced name", req.NamespacedName)
140144

141145
templateVersion := module.Status.TemplateResolvedVersion

cyclops-ctrl/internal/telemetry/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
type Client interface {
99
ModuleCreation()
1010
ModuleReconciliation()
11+
MCPModuleReconciliation()
1112
InstanceStart()
1213
ReleaseUpdate()
1314
ReleaseMigration()
@@ -80,6 +81,14 @@ func (c EnqueueClient) ModuleReconciliation() {
8081
})
8182
}
8283

84+
func (c EnqueueClient) MCPModuleReconciliation() {
85+
_ = c.client.Enqueue(posthog.Capture{
86+
Event: "mcp-module-reconciliation",
87+
DistinctId: c.distinctID,
88+
Properties: c.messageProps(),
89+
})
90+
}
91+
8392
func (c EnqueueClient) ModuleCreation() {
8493
_ = c.client.Enqueue(posthog.Capture{
8594
Event: "module-creation",
@@ -140,6 +149,9 @@ func (c MockClient) InstanceStart() {
140149
func (c MockClient) ModuleReconciliation() {
141150
}
142151

152+
func (c MockClient) MCPModuleReconciliation() {
153+
}
154+
143155
func (c MockClient) ModuleCreation() {
144156
}
145157

cyclops-ctrl/pkg/cluster/k8sclient/client.go

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package k8sclient
22

33
import (
44
"context"
5+
"fmt"
56

67
"k8s.io/client-go/rest"
8+
"k8s.io/client-go/tools/clientcmd"
79
"k8s.io/client-go/tools/remotecommand"
810

911
"github.com/go-logr/logr"
@@ -37,43 +39,93 @@ type KubernetesClient struct {
3739
logger logr.Logger
3840
}
3941

40-
func New(
41-
moduleNamespace string,
42-
helmReleaseNamespace string,
43-
moduleTargetNamespace string,
44-
logger logr.Logger,
45-
) (*KubernetesClient, error) {
46-
config := ctrl.GetConfigOrDie()
47-
clientset, err := kubernetes.NewForConfig(config)
42+
type ClientConfig struct {
43+
KubeconfigPath string
44+
Context string
45+
46+
ModuleNamespace string
47+
HelmReleaseNamespace string
48+
ModuleTargetNamespace string
49+
}
50+
51+
func NewWithConfig(config ClientConfig, logger logr.Logger) (*KubernetesClient, error) {
52+
var k8sConfig *rest.Config
53+
var err error
54+
55+
if config.KubeconfigPath == "" {
56+
k8sConfig, err = rest.InClusterConfig()
57+
if err != nil {
58+
k8sConfig, err = ctrl.GetConfig()
59+
if err != nil {
60+
return nil, err
61+
}
62+
}
63+
} else {
64+
loadingRules := &clientcmd.ClientConfigLoadingRules{
65+
ExplicitPath: config.KubeconfigPath,
66+
}
67+
68+
configOverrides := &clientcmd.ConfigOverrides{}
69+
if config.Context != "" {
70+
configOverrides.CurrentContext = config.Context
71+
}
72+
73+
k8sConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
74+
loadingRules,
75+
configOverrides,
76+
).ClientConfig()
77+
if err != nil {
78+
return nil, fmt.Errorf("failed to load kubeconfig: %w", err)
79+
}
80+
}
81+
82+
clientset, err := kubernetes.NewForConfig(k8sConfig)
4883
if err != nil {
49-
return nil, err
84+
return nil, fmt.Errorf("failed to create clientset: %w", err)
5085
}
5186

52-
moduleSet, err := client.NewForConfig(config)
87+
moduleSet, err := client.NewForConfig(k8sConfig)
5388
if err != nil {
54-
panic(err)
89+
return nil, fmt.Errorf("failed to create module client: %w", err)
5590
}
5691

57-
discovery := discovery.NewDiscoveryClientForConfigOrDie(config)
92+
discovery, err := discovery.NewDiscoveryClientForConfig(k8sConfig)
93+
if err != nil {
94+
return nil, fmt.Errorf("failed to create discovery client: %w", err)
95+
}
5896

59-
dynamic, err := dynamic.NewForConfig(config)
97+
dynamic, err := dynamic.NewForConfig(k8sConfig)
6098
if err != nil {
61-
panic(err.Error())
99+
return nil, fmt.Errorf("failed to create dynamic client: %w", err)
62100
}
63101

64102
return &KubernetesClient{
65-
config: config,
103+
config: k8sConfig,
66104
Dynamic: dynamic,
67105
discovery: discovery,
68106
clientset: clientset,
69107
moduleset: moduleSet,
70-
moduleNamespace: moduleNamespace,
71-
helmReleaseNamespace: helmReleaseNamespace,
72-
moduleTargetNamespace: moduleTargetNamespace,
108+
moduleNamespace: config.ModuleNamespace,
109+
helmReleaseNamespace: config.HelmReleaseNamespace,
110+
moduleTargetNamespace: config.ModuleTargetNamespace,
73111
logger: logger,
74112
}, nil
75113
}
76114

115+
func New(
116+
moduleNamespace string,
117+
helmReleaseNamespace string,
118+
moduleTargetNamespace string,
119+
logger logr.Logger,
120+
) (*KubernetesClient, error) {
121+
config := ClientConfig{
122+
ModuleNamespace: moduleNamespace,
123+
HelmReleaseNamespace: helmReleaseNamespace,
124+
ModuleTargetNamespace: moduleTargetNamespace,
125+
}
126+
return NewWithConfig(config, logger)
127+
}
128+
77129
type IKubernetesClient interface {
78130
GetStreamedPodLogs(ctx context.Context, namespace, container, name string, logCount *int64, logChan chan<- string) error
79131
GetPodLogs(namespace, container, name string, numLogs *int64) ([]string, error)

0 commit comments

Comments
 (0)